Skip to content

Commit

Permalink
feat: Add [WIP] support. (#3)
Browse files Browse the repository at this point in the history
* Attempt at adding WIP support.

* Fix call.

* Run checks on push as well.

* Attemt to fix.

* Attemt to fix.

* Attemt to fix.

* Attemt to fix.

* Attemt to fix.

* Attempt to run on synchronize.

* Log response of status update.

* Fix syntax error.

* Pass along owner and repo.

* Update wording and remove logging.

* Update wording.

* Update README.

* Update wording again.

* Move script inside "src".

* Fix import.
  • Loading branch information
amannn authored Nov 20, 2019
1 parent 7383c0d commit 2b4d25e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ on:
types:
- opened
- edited
- synchronize

jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: amannn/action-semantic-pull-request@fix/get-current-title
- uses: amannn/action-semantic-pull-request@feature/WIP-support
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ on:
types:
- opened
- edited
- synchronize

jobs:
main:
Expand All @@ -22,3 +23,14 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
Examples for valid PR titles:
- fix: Correct typo.
- feat: Add support for Node 12.
- refactor!: Drop support for Node 6.
Note that since PR titles only have a single line, you have to use the `!` syntax for breaking changes.

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

Additionally, the special `[WIP] ` prefix is supported, to indicate that a pull request is work in progress and isn't ready to be merged. In this case the PR title isn't validated and the pull request checks remain pending.
25 changes: 1 addition & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
const core = require('@actions/core');
const github = require('@actions/github');
const validatePrTitle = require('./src/validatePrTitle');

async function run() {
try {
const client = new github.GitHub(process.env.GITHUB_TOKEN);

// The pull request info on the context isn't up to date. When
// the user updates the title and re-runs the workflow, it would
// be outdated. Therefore fetch the pull request via the REST API
// to ensure we use the current title.
const contextPullRequest = github.context.payload.pull_request;
const {data: pullRequest} = await client.pulls.get({
owner: contextPullRequest.base.user.login,
repo: contextPullRequest.base.repo.name,
pull_number: contextPullRequest.number
});

await validatePrTitle(pullRequest.title);
} catch (error) {
core.setFailed(error.message);
}
}
const run = require('./src');

run();
56 changes: 56 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const core = require('@actions/core');
const github = require('@actions/github');
const validatePrTitle = require('./validatePrTitle');

module.exports = async function run() {
try {
const client = new github.GitHub(process.env.GITHUB_TOKEN);

const contextPullRequest = github.context.payload.pull_request;
if (!contextPullRequest) {
throw new Error(
"This action can only be invoked in `pull_request` events. Otherwise the pull request can't be inferred."
);
}

const owner = contextPullRequest.base.user.login;
const repo = contextPullRequest.base.repo.name;

// The pull request info on the context isn't up to date. When
// the user updates the title and re-runs the workflow, it would
// be outdated. Therefore fetch the pull request via the REST API
// to ensure we use the current title.
const {data: pullRequest} = await client.pulls.get({
owner,
repo,
pull_number: contextPullRequest.number
});

// Pull requests that start with "[WIP] " are excluded from the check.
const isWip = /^\[WIP\]\s/.test(pullRequest.title);
const newStatus = isWip ? 'pending' : 'success';

// When setting the status to "pending", the checks don't complete.
// https://developer.github.com/v3/repos/statuses/#create-a-status
const response = 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]".'
: 'Ready for review & merge.',
context: 'action-semantic-pull-request'
}
);

if (!isWip) {
await validatePrTitle(pullRequest.title);
}
} catch (error) {
core.setFailed(error.message);
}
};

0 comments on commit 2b4d25e

Please sign in to comment.