Skip to content

Commit

Permalink
feat!: Add opt-in for WIP (#73)
Browse files Browse the repository at this point in the history
* Add opt-in for WIP

* Cleanup
  • Loading branch information
amannn authored Jan 8, 2021
1 parent e92d269 commit fb077fa
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ Note that since PR titles only have a single line, you have to use the `!` synta

See [Conventional Commits](https://www.conventionalcommits.org/) for more examples.

### Work in progress

Github has support for [draft pull requests](https://github.blog/2019-02-14-introducing-draft-pull-requests/), which will disable the merge button until the PR is marked as ready for merge.

However, [this feature might be disabled for your repository](https://git.luolix.topmunity/t/draft-pull-requests-not-available/1753/7). In this case you can use the special `[WIP] ` prefix to indicate that a pull request is work in progress and isn't ready to be merged. This will avoid the validation of the PR title and the pull request checks remain pending.

## Example config

```yml
Expand Down Expand Up @@ -54,11 +48,18 @@ jobs:
ui
# Configure that a scope must always be provided.
requireScope: true
# For work-in-progress PRs you can typically use draft pull requests
# from Github. However, private repositories on the free plan don't have
# this option and therefore this action allows you to opt-in to using the
# special "[WIP]" prefix to indicate this state. This will avoid the
# validation of the PR title and the pull request checks remain pending.
# Note that a second check will be reported if this is enabled.
wip: true
```
## Event triggers
There are two events that can be used as triggers for this action, each with different characteristics:
1. [`pull_request_target`](https://github.blog/2020-08-03-github-actions-improvements-for-fork-and-pull-request-workflows/): This allows the action to be used in a fork-based workflow, where e.g. you want to accept pull requests in a public repository. In this case, the configuration from the main branch of your repository will be used for the check. This means that you need to have this configuration in the main branch for the action to run at all (e.g. it won't run within a PR that adds the action initially). Also if you change configuration in a PR, the changes will not be reflected for the current PR – only subsequent ones after the changes are in the main branch.
2. `pull_request`: This configuration uses the latest configuration that is available in the current branch. It will only work if the branch is based in the repository itself. If this configuration is used and a pull request from a fork is opened, you'll encounter an error as the Github token environment parameter is not available.
1. [`pull_request_target`](https://github.blog/2020-08-03-github-actions-improvements-for-fork-and-pull-request-workflows/): This allows the action to be used in a fork-based workflow, where e.g. you want to accept pull requests in a public repository. In this case, the configuration from the main branch of your repository will be used for the check. This means that you need to have this configuration in the main branch for the action to run at all (e.g. it won't run within a PR that adds the action initially). Also if you change the configuration in a PR, the changes will not be reflected for the current PR – only subsequent ones after the changes are in the main branch.
2. `pull_request`: This configuration uses the latest configuration that is available in the current branch. It will only work if the branch is based in the repository itself. If this configuration is used and a pull request from a fork is opened, you'll encounter an error as the Github token environment parameter is not available. This option is viable if all contributors have write access to the repository.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ inputs:
requireScope:
description: "Configure that a scope must always be provided."
required: false
wip:
description: "For work-in-progress PRs you can typically use draft pull requests from Github. However, private repositories on the free plan don't have this option and therefore this action allows you to opt-in to using the special '[WIP]' prefix to indicate this state. This will avoid the validation of the PR title and the pull request checks remain pending. Note that a second check will be reported if this is enabled."
required: false
43 changes: 23 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const validatePrTitle = require('./validatePrTitle');
module.exports = async function run() {
try {
const client = github.getOctokit(process.env.GITHUB_TOKEN);
const {types, scopes, requireScope} = parseConfig();
const {types, scopes, requireScope, wip} = parseConfig();

const contextPullRequest = github.context.payload.pull_request;
if (!contextPullRequest) {
Expand All @@ -29,7 +29,7 @@ module.exports = async function run() {
});

// Pull requests that start with "[WIP] " are excluded from the check.
const isWip = /^\[WIP\]\s/.test(pullRequest.title);
const isWip = wip && /^\[WIP\]\s/.test(pullRequest.title);

let validationError;
if (!isWip) {
Expand All @@ -40,25 +40,28 @@ module.exports = async function run() {
}
}

const newStatus = isWip || validationError != null ? 'pending' : 'success';
if (wip) {
const newStatus =
isWip || validationError != null ? 'pending' : 'success';

// When setting the status to "pending", the checks don't
// complete. This can be used for WIP PRs in repositories
// which don't support draft pull requests.
// https://developer.github.com/v3/repos/statuses/#create-a-status
await client.request('POST /repos/:owner/:repo/statuses/:sha', {
owner,
repo,
sha: pullRequest.head.sha,
state: newStatus,
target_url: 'https://github.com/amannn/action-semantic-pull-request',
description: isWip
? 'This PR is marked with "[WIP]".'
: validationError
? 'PR title validation failed'
: 'Ready for review & merge.',
context: 'action-semantic-pull-request'
});
// When setting the status to "pending", the checks don't
// complete. This can be used for WIP PRs in repositories
// which don't support draft pull requests.
// https://developer.github.com/v3/repos/statuses/#create-a-status
await client.request('POST /repos/:owner/:repo/statuses/:sha', {
owner,
repo,
sha: pullRequest.head.sha,
state: newStatus,
target_url: 'https://github.com/amannn/action-semantic-pull-request',
description: isWip
? 'This PR is marked with "[WIP]".'
: validationError
? 'PR title validation failed'
: 'Ready for review & merge.',
context: 'action-semantic-pull-request'
});
}

if (!isWip && validationError) {
throw validationError;
Expand Down
7 changes: 6 additions & 1 deletion src/parseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ module.exports = function parseConfig() {
requireScope = ConfigParser.parseBoolean(process.env.INPUT_REQUIRESCOPE);
}

return {types, scopes, requireScope};
let wip;
if (process.env.INPUT_WIP) {
wip = ConfigParser.parseBoolean(process.env.INPUT_WIP);
}

return {types, scopes, requireScope, wip};
};

0 comments on commit fb077fa

Please sign in to comment.