|
| 1 | +/** |
| 2 | + * @module checklist |
| 3 | + * @description Automatically check PRs against a checklist of conditions. |
| 4 | + * This is useful if you want to ensure that PRs meet certain criteria before they can be merged. |
| 5 | + * @param {string} Input - A blank string (no input variable is required) |
| 6 | + * @param {object} branch - The branch context variable. |
| 7 | + * @param {object} files - The files context variable. |
| 8 | + * @param {object} pr - The pr context variable. |
| 9 | + * @param {object} repo - The repo context variable. |
| 10 | + * @param {object} env - The env context variable. |
| 11 | + * @param {object} source - The source context variable. |
| 12 | + * @returns {string} Returns a formatted GitHub comment with a checklist of conditions that the PR meets. |
| 13 | + * @example |
| 14 | + * - action: add-comment@v1 |
| 15 | + args: |
| 16 | + comment: {{ "" | checklist(branch, files, pr, repo, env, source) }} |
| 17 | + * @license MIT |
| 18 | +**/ |
| 19 | + |
| 20 | +const checklistFilter = async (empty, branch, files, pr, repo, env, source, callback) => { // made sync temporarily |
| 21 | + |
| 22 | + const checks = [ |
| 23 | + { |
| 24 | + title: "low-risk", |
| 25 | + label: "The PR is a low-risk change", |
| 26 | + // our sample definition of a low-risk change is a docs-only PR from designated docs writers |
| 27 | + condition: files.every(file => /docs\//.test(file)) && pr.author_teams.includes("tech-writers") |
| 28 | + }, |
| 29 | + { |
| 30 | + title: "has-jira", |
| 31 | + label: "The PR has a Jira reference in the title", |
| 32 | + condition: /\b[A-Za-z]+-\d+\b/.test(pr.title) |
| 33 | + }, |
| 34 | + { |
| 35 | + title: "updates-tests", |
| 36 | + label: "The PR includes updates to tests", |
| 37 | + condition: files.some(file => /[^a-zA-Z0-9](spec|test|tests)[^a-zA-Z0-9]/.test(file)) |
| 38 | + }, |
| 39 | + { |
| 40 | + title: "includes-docs", |
| 41 | + label: "The PR includes changes to the documentation", |
| 42 | + condition: files.some(file => /docs\//.test(file)) |
| 43 | + }, |
| 44 | + { |
| 45 | + title: "first-time", |
| 46 | + label: "The PR author is a first-time contributor", |
| 47 | + condition: repo.author_age < 1 && repo.age > 0 // if the PR author made their first contirbution on the current day |
| 48 | + }, |
| 49 | + { |
| 50 | + title: "requires-opsec", |
| 51 | + label: "The PR doesn't expose any secrets", |
| 52 | + condition: source.diff.files |
| 53 | + .map(file => file.new_content) |
| 54 | + .every(file_content => |
| 55 | + [ |
| 56 | + "MY_SECRET_ENVIRONMENT_VARIABLE" |
| 57 | + ].every(env_var => !file_content.includes(env_var)) |
| 58 | + // nothing added to any file during this comment contains any of the secret environment variables in this array |
| 59 | + ) |
| 60 | + } |
| 61 | + ]; |
| 62 | + |
| 63 | + const comment = await Promise.resolve(checks |
| 64 | + .map(check => `- [${check.condition ? "x" : " "}] ${check.label}`) |
| 65 | + .join("\n")); |
| 66 | + |
| 67 | + return callback( |
| 68 | + null, |
| 69 | + JSON.stringify(comment) |
| 70 | + ); |
| 71 | +}; |
| 72 | + |
| 73 | +module.exports = { |
| 74 | + async: true, |
| 75 | + filter: checklistFilter |
| 76 | +} |
0 commit comments