| 128 | } |
| 129 | |
| 130 | async function run({ github, context, core }) { |
| 131 | if (context.eventName !== "pull_request_target" || |
| 132 | !Number.isInteger(context.issue.number) || context.issue.number < 1) { |
| 133 | throw new Error("PR Commit Policy requires a pull_request_target event."); |
| 134 | } |
| 135 | const pullRequest = await readPullRequest(github, { |
| 136 | owner: context.repo.owner, |
| 137 | repo: context.repo.repo, |
| 138 | number: context.issue.number, |
| 139 | }); |
| 140 | if (pullRequest.head !== context.payload.pull_request?.head?.sha) { |
| 141 | throw new Error("PR head does not match the triggering event. Re-run PR Commit Policy for the latest PR update."); |
| 142 | } |
| 143 | if (pullRequest.title !== context.payload.pull_request.title) { |
| 144 | throw new Error("PR title does not match the triggering event. Re-run PR Commit Policy for the latest PR update."); |
| 145 | } |
| 146 | const violations = []; |
| 147 | const titleDirectives = findSkipDirectives(pullRequest.title); |
| 148 | if (titleDirectives.length) { |
| 149 | violations.push(`PR title contains ${titleDirectives.join(", ")}.`); |
| 150 | } |
| 151 | for (const commit of pullRequest.commits) { |
| 152 | const directives = findSkipDirectives(commit.message); |
| 153 | if (directives.length) { |
| 154 | violations.push(`Commit ${commit.oid} contains ${directives.join(", ")}.`); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (violations.length) { |
| 159 | for (const violation of violations.slice(0, 20)) core.error(violation); |
| 160 | core.setFailed( |
| 161 | `Found CI skip directives in ${violations.length} PR title/commit message(s). ` + |
| 162 | "Remove them from the title and rewrite affected commit messages; " + |
| 163 | "adding a clean commit does not remove earlier violations. " + |
| 164 | "See CONTRIBUTING.md#commit-messages. At most 20 violations are shown." |
| 165 | ); |
| 166 | } else { |
| 167 | core.info(`PR title and all ${pullRequest.commits.length} commit messages contain no CI skip directives.`); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | module.exports = { findSkipDirectives, readPullRequest, run }; |