Skip to content

Commit

Permalink
[new feature] auto-close - fixes #17, #19 (#23)
Browse files Browse the repository at this point in the history
* [auto-close] fixes #17, #19

* no message
  • Loading branch information
seferov authored Nov 23, 2019
1 parent ce0a49b commit 674f011
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ It is a linter of pull requests for [Github Actions](https://github.com/features

## Why?

To enforce pull request titles in the same format will be useful like generating standard changelog messages .
To enforce pull request titles in the same format will be useful like generating standard changelog messages.
Besides it can be used to parse titles and link with issue tracking systems such as JIRA.

## Example:
Expand All @@ -27,3 +27,36 @@ jobs:
```
In this example, for every pull request the title is expected to match `^\[PROJECT-\d*\]\ ` regex with a global flag `g`. For instance, `[PROJECT-123] lorem ipsum` or `[PROJECT-2345] dolor sit amet` are valid titles for this example. You can customize the title regex for your needs. The regex flags configuration is optional.

## Auto-close

It can be configured to close pull request automatically if the title does not match the pattern provided. To do so, `github-token` and `auto-close-message` options must be configured.
In the message, `%pattern%` is replaced with the actual the pattern provided.

Example:

```diff
name: PR lint
on:
pull_request:
types: ['opened', 'edited', 'reopened', 'synchronize']
jobs:
pr-lint:
runs-on: ubuntu-latest
steps:
- uses: seferov/pr-lint-action@master
with:
title-regex: '^\[PROJECT-\d*\]\ '
+ github-token: ${{ secrets.GITHUB_TOKEN }}
+ auto-close-message: 'Closing this pull request since the title does not match %pattern% pattern. Please fix the title and re-open the pull request.'
```

## Known Issues

After a failing job **just** correcting title results in both failed and successful statutes.
This is a limitation on Github since just editing pull request title does not count as a *change*, thus doesn't overwrite failing status.
To overcome this, after correcting title empty commit can be sent

`git commit --allow-empty -m "title corrected"`
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ inputs:
description: 'Title regex to match'
required: true
default: '^\[PROJECT-\d*\]\ '
github-token:
description: '${{ secrets.GITHUB_TOKEN }}'
required: false
auto-close-message:
description: 'If set, on a failing job automatically closes pull request and comments on it with the given value'
required: false
branding:
icon: 'edit-3'
color: 'blue'
27 changes: 27 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,33 @@ async function run() {

if (!title.match(new RegExp(titleRegex, titleRegexFlags))) {
core.setFailed(`Please fix your PR title to match "${titleRegex}" with "${titleRegexFlags}" flags`);

const autoCloseMessage = core.getInput('auto-close-message'),
githubToken = core.getInput('github-token');
if (autoCloseMessage) {
if (!githubToken) {
core.setFailed('To use auto-close feature you must provide github-token. See: https://github.com/seferov/pr-lint-action#auto-close');

return;
}

const client: github.GitHub = new github.GitHub(githubToken),
pr = github.context.issue;

client.pulls.createReview({
owner: pr.owner,
repo: pr.repo,
pull_number: pr.number,
body: autoCloseMessage.replace('%pattern%', titleRegex),
event: 'COMMENT'
});
client.pulls.update({
owner: pr.owner,
repo: pr.repo,
pull_number: pr.number,
state: 'closed'
});
}
}
} catch (error) {
core.setFailed(error.message);
Expand Down

0 comments on commit 674f011

Please sign in to comment.