|
| 1 | +const core = require('@actions/core'); |
| 2 | +const github = require('@actions/github'); |
| 3 | + |
| 4 | +function raiseError(message) { |
| 5 | + throw new Error(message); |
| 6 | +} |
| 7 | + |
| 8 | +async function getPullRequest() { |
| 9 | + const client = github.getOctokit(process.env.GITHUB_TOKEN); |
| 10 | + |
| 11 | + const pr = github.context.payload.pull_request; |
| 12 | + if (!pr) { |
| 13 | + throw new Error( |
| 14 | + "This action can only be invoked in `pull_request_target` or `pull_request` events. Otherwise the pull request can't be inferred.", |
| 15 | + ); |
| 16 | + } |
| 17 | + |
| 18 | + const owner = pr.base.user.login; |
| 19 | + const repo = pr.base.repo.name; |
| 20 | + |
| 21 | + const { data } = await client.rest.pulls.get({ |
| 22 | + owner, |
| 23 | + repo, |
| 24 | + pull_number: pr.number, |
| 25 | + }); |
| 26 | + |
| 27 | + return data; |
| 28 | +} |
| 29 | + |
| 30 | +function checkTitle(title) { |
| 31 | + if (/!/.test(title)) { |
| 32 | + raiseError(`Do not use exclamation mark ('!') in your PR Title.`); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function checkDescription(body, labels) { |
| 37 | + if (!labels.some(label => label.name === 'breaking change')) { |
| 38 | + return; |
| 39 | + } |
| 40 | + const [firstLine, secondLine] = body.split(/\r?\n/); |
| 41 | + |
| 42 | + if (!firstLine || !/^BREAKING CHANGE:/.test(firstLine)) { |
| 43 | + raiseError(`Breaking change PR body should starts with "BREAKING CHANGE:"`); |
| 44 | + } |
| 45 | + if (!secondLine) { |
| 46 | + raiseError(`The description of breaking change is missing.`); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +async function run() { |
| 51 | + const pullRequest = await getPullRequest(); |
| 52 | + try { |
| 53 | + checkTitle(pullRequest.title); |
| 54 | + checkDescription(pullRequest.body, pullRequest.labels); |
| 55 | + } catch (e) { |
| 56 | + core.setFailed(e.message); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +run(); |
0 commit comments