|
| 1 | +import fs from 'node:fs' |
| 2 | +import path from 'node:path' |
| 3 | +import type * as actionsCore from '@actions/core' |
| 4 | +import type { context as actionsContext } from '@actions/github' |
| 5 | +import type { GitHub } from '@actions/github/lib/utils' |
| 6 | +import { getCommitDetailsForPR } from './get-pr-commit-details.js' |
| 7 | +import { dismissReviews, postReview } from './reviews.js' |
| 8 | +import { classify } from './supportedBranches.js' |
| 9 | + |
| 10 | +/** |
| 11 | + * Reminders to post as a non-blocking review when a pull request touches |
| 12 | + * certain matching paths. |
| 13 | + */ |
| 14 | +export const reminders: { key: string; paths: RegExp[] }[] = [ |
| 15 | + { |
| 16 | + key: 'docs-styleguide', |
| 17 | + paths: [/^doc\//, /^nixos\/doc\//, /^nixos\/modules\/.*\.md$/], |
| 18 | + }, |
| 19 | +] |
| 20 | + |
| 21 | +export function matchesAny(changedPaths: string[], patterns: RegExp[]) { |
| 22 | + return changedPaths.some((changedPath) => |
| 23 | + patterns.some((pattern) => pattern.test(changedPath)), |
| 24 | + ) |
| 25 | +} |
| 26 | + |
| 27 | +function reminderBody(key: string) { |
| 28 | + return fs |
| 29 | + .readFileSync(path.join(import.meta.dirname, 'reminders', `${key}.md`), { |
| 30 | + encoding: 'utf8', |
| 31 | + }) |
| 32 | + .trim() |
| 33 | +} |
| 34 | + |
| 35 | +export default async function postReminders({ |
| 36 | + github, |
| 37 | + context, |
| 38 | + core, |
| 39 | + repoPath, |
| 40 | + dry, |
| 41 | +}: { |
| 42 | + github: InstanceType<typeof GitHub> |
| 43 | + context: typeof actionsContext |
| 44 | + core: typeof actionsCore |
| 45 | + repoPath?: string |
| 46 | + dry: boolean |
| 47 | +}) { |
| 48 | + const pull_number = context.payload.pull_request?.number |
| 49 | + if (!pull_number) { |
| 50 | + core.info('This is not a pull request. Skipping reminders.') |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + const pr = ( |
| 55 | + await github.rest.pulls.get({ |
| 56 | + ...context.repo, |
| 57 | + pull_number, |
| 58 | + }) |
| 59 | + ).data |
| 60 | + |
| 61 | + if (pr.user.login.endsWith('[bot]')) { |
| 62 | + core.info('This is a bot, so reminders do not apply.') |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + const baseBranchType = classify( |
| 67 | + pr.base.ref.replace(/^refs\/heads\//, ''), |
| 68 | + ).type |
| 69 | + const headBranchType = classify( |
| 70 | + pr.head.ref.replace(/^refs\/heads\//, ''), |
| 71 | + ).type |
| 72 | + |
| 73 | + if ( |
| 74 | + baseBranchType.includes('development') && |
| 75 | + headBranchType.includes('development') && |
| 76 | + pr.base.repo.id === pr.head.repo?.id |
| 77 | + ) { |
| 78 | + // This matches, for example, PRs from NixOS:staging-next to NixOS:master, or vice versa. |
| 79 | + // We ignore them, we should only care about PRs introducing new commits. |
| 80 | + // We still want to run on PRs from, e.g., Someone:master to NixOS:master though. |
| 81 | + core.info( |
| 82 | + 'This PR is from one development branch to another. Skipping reminders.', |
| 83 | + ) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + const details = await getCommitDetailsForPR({ core, pr, repoPath }) |
| 88 | + const changedPaths = details.flatMap(({ changedPaths }) => changedPaths) |
| 89 | + |
| 90 | + for (const { key, paths } of reminders) { |
| 91 | + if (matchesAny(changedPaths, paths)) { |
| 92 | + await postReview({ |
| 93 | + github, |
| 94 | + context, |
| 95 | + core, |
| 96 | + dry, |
| 97 | + event: 'COMMENT', |
| 98 | + body: reminderBody(key), |
| 99 | + reviewKey: key, |
| 100 | + }) |
| 101 | + } else { |
| 102 | + await dismissReviews({ |
| 103 | + github, |
| 104 | + context, |
| 105 | + core, |
| 106 | + dry, |
| 107 | + reviewKey: key, |
| 108 | + }) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments