diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 37232a7f9..7268ed3d0 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -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 }} diff --git a/README.md b/README.md index 531dbf21f..971a6aa59 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ on: types: - opened - edited + - synchronize jobs: main: @@ -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. diff --git a/index.js b/index.js index ebb751496..f28ab7a52 100644 --- a/index.js +++ b/index.js @@ -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(); diff --git a/src/index.js b/src/index.js new file mode 100644 index 000000000..4c6373fb0 --- /dev/null +++ b/src/index.js @@ -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); + } +};