|
| 1 | +const removeHtmlComments = (input) => { |
| 2 | + let visible = ""; |
| 3 | + let cursor = 0; |
| 4 | + |
| 5 | + while (cursor < input.length) { |
| 6 | + const commentStart = input.indexOf("<!--", cursor); |
| 7 | + if (commentStart === -1) { |
| 8 | + visible += input.slice(cursor); |
| 9 | + break; |
| 10 | + } |
| 11 | + |
| 12 | + visible += input.slice(cursor, commentStart); |
| 13 | + const commentEnd = input.indexOf("-->", commentStart + 4); |
| 14 | + if (commentEnd === -1) { |
| 15 | + // GitHub hides an unterminated comment through the end of the body. |
| 16 | + break; |
| 17 | + } |
| 18 | + cursor = commentEnd + 3; |
| 19 | + } |
| 20 | + |
| 21 | + return visible; |
| 22 | +}; |
| 23 | + |
| 24 | +const removeFencedCodeBlocks = (input) => { |
| 25 | + const lines = input.split("\n"); |
| 26 | + const visibleLines = []; |
| 27 | + let fence = null; |
| 28 | + |
| 29 | + for (const line of lines) { |
| 30 | + const content = line.replace(/^ {0,3}/, ""); |
| 31 | + |
| 32 | + if (!fence) { |
| 33 | + const opener = content.match(/^(`{3,}|~{3,})/); |
| 34 | + if (opener) { |
| 35 | + fence = { marker: opener[1][0], length: opener[1].length }; |
| 36 | + visibleLines.push(""); |
| 37 | + } else { |
| 38 | + visibleLines.push(line); |
| 39 | + } |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + let markerLength = 0; |
| 44 | + while (content[markerLength] === fence.marker) { |
| 45 | + markerLength += 1; |
| 46 | + } |
| 47 | + if ( |
| 48 | + markerLength >= fence.length && |
| 49 | + content.slice(markerLength).trim() === "" |
| 50 | + ) { |
| 51 | + fence = null; |
| 52 | + } |
| 53 | + visibleLines.push(""); |
| 54 | + } |
| 55 | + |
| 56 | + return visibleLines.join("\n"); |
| 57 | +}; |
| 58 | + |
| 59 | +module.exports = async ({ github, context, core }) => { |
| 60 | + const body = context.payload.pull_request.body || ""; |
| 61 | + const visibleBody = removeFencedCodeBlocks(removeHtmlComments(body)); |
| 62 | + |
| 63 | + const problems = []; |
| 64 | + const sectionMatch = visibleBody.match( |
| 65 | + /^##\s*AI assistance\s*\n([\s\S]*?)(?=\n##\s|$(?![\s\S]))/im, |
| 66 | + ); |
| 67 | + |
| 68 | + if (!sectionMatch) { |
| 69 | + problems.push("the `## AI assistance` section is missing"); |
| 70 | + } else { |
| 71 | + const section = sectionMatch[1]; |
| 72 | + |
| 73 | + const usageLine = section.match(/^-[ \t]*AI usage:[ \t]*(.*)$/im); |
| 74 | + const usage = usageLine?.[1].trim(); |
| 75 | + if (!usage) { |
| 76 | + problems.push( |
| 77 | + "`AI usage:` is not filled in (write `None` if no AI was used)", |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + const humanLine = section.match(/^-[ \t]*Responsible human:[ \t]*(.*)$/im); |
| 82 | + const human = humanLine?.[1].trim(); |
| 83 | + const usernameMatch = human?.match(/^@([A-Za-z0-9-]+)$/); |
| 84 | + const username = usernameMatch?.[1]; |
| 85 | + const usernameIsValid = |
| 86 | + username && |
| 87 | + username.length <= 39 && |
| 88 | + !username.startsWith("-") && |
| 89 | + !username.endsWith("-") && |
| 90 | + !username.includes("--") && |
| 91 | + username.toLowerCase() !== "your-github-id"; |
| 92 | + |
| 93 | + if (!usernameIsValid) { |
| 94 | + problems.push( |
| 95 | + "`Responsible human:` must name a real GitHub user, e.g. `@octocat`", |
| 96 | + ); |
| 97 | + } else { |
| 98 | + const prAuthor = context.payload.pull_request.user; |
| 99 | + const authorIsBot = |
| 100 | + prAuthor.type === "Bot" || prAuthor.login.toLowerCase().endsWith("[bot]"); |
| 101 | + |
| 102 | + if ( |
| 103 | + !authorIsBot && |
| 104 | + username.toLowerCase() !== prAuthor.login.toLowerCase() |
| 105 | + ) { |
| 106 | + problems.push( |
| 107 | + `Responsible human must match the PR author \`@${prAuthor.login}\``, |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + try { |
| 112 | + const { data: account } = await github.rest.users.getByUsername({ |
| 113 | + username, |
| 114 | + }); |
| 115 | + if (account.type !== "User") { |
| 116 | + problems.push("`Responsible human:` must refer to a human account"); |
| 117 | + } |
| 118 | + } catch (error) { |
| 119 | + if (error.status === 404) { |
| 120 | + problems.push(`Responsible human \`@${username}\` does not exist`); |
| 121 | + } else { |
| 122 | + throw error; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + const readBox = section.match( |
| 128 | + /^-\s*\[x\]\s+The responsible human has read every line/im, |
| 129 | + ); |
| 130 | + if (!readBox) { |
| 131 | + problems.push( |
| 132 | + 'the checkbox "The responsible human has read every line of this diff" is not checked', |
| 133 | + ); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if (problems.length > 0) { |
| 138 | + core.setOutput("ai", "invalid"); |
| 139 | + core.setOutput("problems", problems.map((p) => `- ${p}`).join("\n")); |
| 140 | + core.setFailed(`AI assistance section incomplete: ${problems.join("; ")}`); |
| 141 | + } else { |
| 142 | + core.setOutput("ai", "valid"); |
| 143 | + } |
| 144 | +}; |
0 commit comments