|
| 1 | +/** |
| 2 | + * Fails the run when a pull request body does not carry the sections that its |
| 3 | + * template marks as required. |
| 4 | + */ |
| 5 | + |
| 6 | +const fs = require('fs'); |
| 7 | +const path = require('path'); |
| 8 | + |
| 9 | +const { isBot } = require('./utils'); |
| 10 | +const { PR_TEMPLATE_PATHS, PR_TEMPLATE_TARGET } = require('./constants'); |
| 11 | + |
| 12 | +const ORG_TEMPLATE_PATH = path.join(__dirname, '..', PR_TEMPLATE_TARGET); |
| 13 | + |
| 14 | +const HEADING = /^## [^#]/; |
| 15 | +const REQUIRED_MARKER = /^\s*<!--\s*REQUIRED\s*-->\s*$/; |
| 16 | +const HTML_COMMENT = /<!--[\s\S]*?-->/g; |
| 17 | +const PLACEHOLDER = '…'; |
| 18 | + |
| 19 | +function sections(markdown) { |
| 20 | + const out = []; |
| 21 | + for (const line of (markdown || '').split(/\r?\n/)) { |
| 22 | + if (HEADING.test(line)) out.push({ heading: line.trim(), lines: [] }); |
| 23 | + else if (out.length) out[out.length - 1].lines.push(line); |
| 24 | + } |
| 25 | + return out; |
| 26 | +} |
| 27 | + |
| 28 | +function isMarked(section) { |
| 29 | + const first = section.lines.find(line => line.trim() !== ''); |
| 30 | + return first !== undefined && REQUIRED_MARKER.test(first); |
| 31 | +} |
| 32 | + |
| 33 | +// A template that marks nothing requires every section, so a repo whose template |
| 34 | +// has no markers is strict rather than unchecked. |
| 35 | +function requiredSections(template) { |
| 36 | + const all = sections(template); |
| 37 | + const marked = all.filter(isMarked); |
| 38 | + return marked.length ? marked : all; |
| 39 | +} |
| 40 | + |
| 41 | +function content(section) { |
| 42 | + return section.lines.join('\n').replace(HTML_COMMENT, '').trim(); |
| 43 | +} |
| 44 | + |
| 45 | +function failures(body, required) { |
| 46 | + const found = new Map(sections(body).map(section => [section.heading.toLowerCase(), section])); |
| 47 | + const problems = []; |
| 48 | + for (const { heading, lines } of required) { |
| 49 | + const section = found.get(heading.toLowerCase()); |
| 50 | + if (!section) { |
| 51 | + problems.push(`\`${heading}\` is missing.`); |
| 52 | + continue; |
| 53 | + } |
| 54 | + const text = content(section); |
| 55 | + if (text === '') { |
| 56 | + problems.push(`\`${heading}\` is empty.`); |
| 57 | + } else if (text.split('\n').some(line => line.trim() === PLACEHOLDER)) { |
| 58 | + // The template puts the placeholder on a line of its own, so an ellipsis |
| 59 | + // inside a sentence is prose rather than an unfilled section. |
| 60 | + problems.push(`\`${heading}\` still holds the \`${PLACEHOLDER}\` placeholder.`); |
| 61 | + } else if (text === content({ lines })) { |
| 62 | + problems.push(`\`${heading}\` still holds the text from the template.`); |
| 63 | + } |
| 64 | + } |
| 65 | + return problems; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * The repo's own template, `null` if none exists, or `{ unreadable }` if a |
| 70 | + * candidate path returns anything other than a 404. An unreadable path may |
| 71 | + * contain a repo-owned template, so validating against the shared template |
| 72 | + * could reject a body that follows the template the author was given. |
| 73 | + */ |
| 74 | +async function readRepoTemplate({ github, context, core }, ref) { |
| 75 | + for (const templatePath of PR_TEMPLATE_PATHS) { |
| 76 | + try { |
| 77 | + const response = await github.rest.repos.getContent({ |
| 78 | + owner: context.repo.owner, |
| 79 | + repo: context.repo.repo, |
| 80 | + path: templatePath, |
| 81 | + ref, |
| 82 | + }); |
| 83 | + if (response.data?.content) { |
| 84 | + core.info(`Checking against ${templatePath} on ${ref}.`); |
| 85 | + return { template: Buffer.from(response.data.content, 'base64').toString('utf8') }; |
| 86 | + } |
| 87 | + } catch (error) { |
| 88 | + if (error.status !== 404) { |
| 89 | + return { unreadable: `${templatePath}: ${error.message}` }; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + return { template: null }; |
| 94 | +} |
| 95 | + |
| 96 | +module.exports = async ({ github, context, core }) => { |
| 97 | + try { |
| 98 | + const pullRequest = context.payload.pull_request; |
| 99 | + const author = pullRequest.user.login; |
| 100 | + |
| 101 | + if (await isBot(author, { core })) { |
| 102 | + core.info(`${author} is a bot, so the template is not checked.`); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const found = await readRepoTemplate({ github, context, core }, pullRequest.base.ref); |
| 107 | + if (found.unreadable) { |
| 108 | + core.warning(`Could not read ${found.unreadable}, so the template is not checked.`); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + const template = found.template ?? fs.readFileSync(ORG_TEMPLATE_PATH, 'utf8'); |
| 113 | + if (!found.template) { |
| 114 | + core.info('No template in this repo, so the shared template applies.'); |
| 115 | + } |
| 116 | + |
| 117 | + const required = requiredSections(template); |
| 118 | + if (!required.length) { |
| 119 | + core.warning('The template holds no sections, so there is nothing to check.'); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + const problems = failures(pullRequest.body, required); |
| 124 | + if (!problems.length) { |
| 125 | + core.info(`The body carries all ${required.length} required sections.`); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + for (const problem of problems) { |
| 130 | + core.error(problem); |
| 131 | + } |
| 132 | + core.setFailed( |
| 133 | + `The description does not follow the pull request template:\n` + |
| 134 | + problems.map(problem => `- ${problem}`).join('\n') + |
| 135 | + `\n\nRequired: ${required.map(section => section.heading).join(', ')}. ` + |
| 136 | + `Edit the description and the check runs again.`, |
| 137 | + ); |
| 138 | + } catch (error) { |
| 139 | + core.setFailed(`Action failed with error: ${error.message}`); |
| 140 | + } |
| 141 | +}; |
0 commit comments