Skip to content

Commit

Permalink
feat: Add task that adds GitHub Actions workflow to automerge Dependa…
Browse files Browse the repository at this point in the history
…bot pull requests
  • Loading branch information
sapegin committed Sep 5, 2020
1 parent ce8d6e1 commit 072b443
Show file tree
Hide file tree
Showing 12 changed files with 678 additions and 553 deletions.
198 changes: 132 additions & 66 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/mrm-preset-default/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ See [Mrm docs](https://mrm.js.org/docs/getting-started) for more details.

- [codecov](https://github.com/sapegin/mrm/tree/master/packages/mrm-task-codecov)
- [contributing](https://github.com/sapegin/mrm/tree/master/packages/mrm-task-contributing)
- [dependabot](https://github.com/sapegin/mrm/tree/master/packages/mrm-task-dependabot)
- [editorconfig](https://github.com/sapegin/mrm/tree/master/packages/mrm-task-editorconfig)
- [eslint](https://github.com/sapegin/mrm/tree/master/packages/mrm-task-eslint)
- [gitignore](https://github.com/sapegin/mrm/tree/master/packages/mrm-task-gitignore)
Expand Down
1 change: 1 addition & 0 deletions packages/mrm-preset-default/dependabot/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('mrm-task-dependabot');
6 changes: 6 additions & 0 deletions packages/mrm-preset-default/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ it('should have contributing task', () => {
expect(task.description).toMatch('contributing');
});

it('should have dependabot task', () => {
const task = require('./dependabot/index.js');
expect(task).toEqual(expect.any(Function));
expect(task.description).toMatch('Dependabot');
});

it('should have editorconfig task', () => {
const task = require('./editorconfig/index.js');
expect(task).toEqual(expect.any(Function));
Expand Down
9 changes: 9 additions & 0 deletions packages/mrm-task-dependabot/License.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The MIT License

Copyright 2020 Artem Sapegin (http://sapegin.me), contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 changes: 38 additions & 0 deletions packages/mrm-task-dependabot/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!-- Dependabot -->

# mrm-task-dependabot

[Mrm](https://github.com/sapegin/mrm) task that adds GitHub Actions workflow to automerge [Dependabot](https://dependabot.com/) pull requests.

## What it does

- Creates a workflow file

## Usage

```
npm install -g mrm mrm-task-dependabot
mrm dependabot
```

## Options

See [Mrm docs](../../docs/Getting_started.md) for more details.

### `workflowFile` (default: `.github/workflows/dependabot.yml`)

Location of GitHub Actions workflow file

## Changelog

The changelog can be found in [CHANGELOG.md](CHANGELOG.md).

## Contributing

Everyone is welcome to contribute. Please take a moment to review the [contributing guidelines](../../Contributing.md).

## Authors and license

[Artem Sapegin](https://sapegin.me) and [contributors](https://github.com/sapegin/mrm/graphs/contributors).

MIT License, see the included [License.md](License.md) file.
29 changes: 29 additions & 0 deletions packages/mrm-task-dependabot/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should add a workflow file 1`] = `
Object {
"/.github/workflows/dependabot.yml": "name: Dependabot Automerge
'on': pull_request
jobs:
worker:
runs-on: ubuntu-latest
if: 'github.actor == ''dependabot[bot]'''
steps:
- uses: actions/github-script@v3
with:
script: |-
github.pulls.createReview({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
pull_number: context.payload.pull_request.number,
event: 'APPROVE'
})
github.pulls.merge({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
pull_number: context.payload.pull_request.number,
merge_method: 'squash'
})
",
}
`;
49 changes: 49 additions & 0 deletions packages/mrm-task-dependabot/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { yaml } = require('mrm-core');

module.exports = function task({ workflowFile }) {
// Create workflow file (no update)
const workflowYml = yaml(workflowFile);
if (!workflowYml.exists()) {
workflowYml
.merge({
name: 'Dependabot Automerge',
on: 'pull_request',
jobs: {
worker: {
'runs-on': 'ubuntu-latest',
if: "github.actor == 'dependabot[bot]'",
steps: [
{
uses: 'actions/github-script@v3',
with: {
script: `github.pulls.createReview({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
pull_number: context.payload.pull_request.number,
event: 'APPROVE'
})
github.pulls.merge({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
pull_number: context.payload.pull_request.number,
merge_method: 'squash'
})`,
},
},
],
},
},
})
.save();
}
};

module.exports.description =
'Adds GitHub Actions workflow to automerge Dependabot pull requests';
module.exports.parameters = {
workflowFile: {
type: 'input',
message: 'Enter location of GitHub Actions workflow file',
default: '.github/workflows/dependabot.yml',
},
};
17 changes: 17 additions & 0 deletions packages/mrm-task-dependabot/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
jest.mock('fs');
jest.mock('git-username');
jest.mock('mrm-core/src/util/log', () => ({
added: jest.fn(),
}));

const { getTaskOptions } = require('mrm');
const vol = require('memfs').vol;
const task = require('./index');

afterEach(() => vol.reset());

it('should add a workflow file', async () => {
task(await getTaskOptions(task));

expect(vol.toJSON()).toMatchSnapshot();
});
Loading

0 comments on commit 072b443

Please sign in to comment.