summaryrefslogtreecommitdiffstats
path: root/ci/github-script/check-target-branch.ts
blob: b12c2aa9d68bc934c64dbda6d4bce5139a7fc534 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { readFile } from 'node:fs/promises'
import type * as actionsCore from '@actions/core'
import type { context as actionsContext } from '@actions/github'
import type { GitHub } from '@actions/github/lib/utils'
import {
  evaluateTargetBranchPolicy,
  getTargetBranchPolicy,
} from './check-target-branch-policy.ts'
import { dismissReviews, postReview } from './reviews.js'
import { split } from './supportedBranches.js'

// TODO: should this be combined with the branch checks in prepare.js?
// They do seem quite similar, but this needs to run after eval,
// and prepare.js obviously doesn't.

const reviewKey = 'check-target-branch'

type ChangedPaths = {
  attrdiff: {
    added: string[]
    changed: string[]
    removed: string[]
  }
  attrdiffByKernel: Record<
    string,
    {
      added: string[]
      changed: string[]
      removed: string[]
    }
  >
  attrdiffByPlatform: Record<
    string,
    {
      added: string[]
      changed: string[]
      removed: string[]
    }
  >
  labels: Record<string, boolean>
  rebuildCountByKernel: Record<string, number>
  rebuildsByKernel: Record<string, string[]>
  rebuildsByPlatform: Record<string, string[]>
}

type TargetBranchReviewFacts = {
  github: InstanceType<typeof GitHub>
  context: typeof actionsContext
  core: typeof actionsCore
  dry: boolean
  base: string
  maxRebuildCount: number
}

function getStagingBranch(base: string) {
  const version = split(base).version
  return version ? `staging-${version}` : 'staging'
}

async function postMassRebuildReview(facts: TargetBranchReviewFacts) {
  const { github, context, core, dry, base, maxRebuildCount } = facts
  const desiredBranch = getStagingBranch(base)
  const body = [
    `The PR's base branch is set to \`${base}\`, but this PR causes ${maxRebuildCount} rebuilds.`,
    'It is therefore considered a mass rebuild.',
    `Please [change the base branch](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-base-branch-of-a-pull-request) to [the right base branch for your changes](https://github.com/NixOS/nixpkgs/blob/master/CONTRIBUTING.md#branch-conventions) (probably \`${desiredBranch}\`).`,
  ].join('\n')

  await postReview({
    github,
    context,
    core,
    dry,
    body,
    event: 'REQUEST_CHANGES',
    reviewKey,
  })
}

async function postNixosRebuildReview(facts: TargetBranchReviewFacts) {
  const { github, context, core, dry, base, maxRebuildCount } = facts
  let branchText: string
  if (base === 'master' && maxRebuildCount >= 500) {
    branchText = '(probably either `staging-nixos` or `staging`)'
  } else if (base === 'master') {
    branchText = '(probably `staging-nixos`)'
  } else if (maxRebuildCount >= 500) {
    branchText = `(probably either \`staging-nixos-${split(base).version}\` or \`staging-${split(base).version}\`)`
  } else {
    branchText = `(probably \`staging-nixos-${split(base).version}\`)`
  }
  const body = [
    `The PR's base branch is set to \`${base}\`, but this PR rebuilds all NixOS tests.`,
    base === 'master' && maxRebuildCount >= 500
      ? `Since this PR also causes ${maxRebuildCount} rebuilds, it may also be considered a mass rebuild.`
      : '',
    `Please [change the base branch](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-base-branch-of-a-pull-request) to [the right base branch for your changes](https://github.com/NixOS/nixpkgs/blob/master/CONTRIBUTING.md#branch-conventions) ${branchText}.`,
  ].join('\n')

  await postReview({
    github,
    context,
    core,
    dry,
    body,
    event: 'REQUEST_CHANGES',
    reviewKey,
  })
}

async function postPossibleMassRebuildReview(facts: TargetBranchReviewFacts) {
  const { github, context, core, dry, base, maxRebuildCount } = facts
  const stagingBranch = getStagingBranch(base)
  const body = [
    `The PR's base branch is set to \`${base}\`, and this PR causes ${maxRebuildCount} rebuilds.`,
    `Please consider whether this PR causes a mass rebuild according to [our conventions](https://github.com/NixOS/nixpkgs/blob/master/CONTRIBUTING.md#branch-conventions).`,
    `If it does cause a mass rebuild, please [change the base branch](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-base-branch-of-a-pull-request) to [the right base branch for your changes](https://github.com/NixOS/nixpkgs/blob/master/CONTRIBUTING.md#branch-conventions) (probably \`${stagingBranch}\`).`,
    `If it does not cause a mass rebuild, this message can be ignored.`,
  ].join('\n')

  await postReview({
    github,
    context,
    core,
    dry,
    body,
    event: 'REQUEST_CHANGES',
    reviewKey,
  })
}

export async function checkTargetBranch({
  github,
  context,
  core,
  dry,
}: {
  github: InstanceType<typeof GitHub>
  context: typeof actionsContext
  core: typeof actionsCore
  dry: boolean
}) {
  const changed: ChangedPaths = JSON.parse(
    await readFile('comparison/changed-paths.json', 'utf-8'),
  )
  const pull_number = context.payload.pull_request?.number
  if (!pull_number) {
    core.warning(
      'Skipping checkTargetBranch: no pull_request number (is this being run as part of a merge group?)',
    )
    return
  }
  const prInfo = (
    await github.rest.pulls.get({
      ...context.repo,
      pull_number,
    })
  ).data
  const base = prInfo.base.ref
  const head = prInfo.head.ref
  const { shouldCheckMassRebuild } = getTargetBranchPolicy({ base, head })

  const maxRebuildCount = Math.max(
    ...Object.values(changed.rebuildCountByKernel),
  )
  const rebuildsAllTests =
    changed.attrdiff.changed.includes('nixosTests.simple-container') ||
    changed.attrdiff.changed.includes('nixosTests.simple-vm')

  let onlyChangedFile: string | null = null
  if (shouldCheckMassRebuild && prInfo.changed_files === 1) {
    const changedFiles = (
      await github.rest.pulls.listFiles({
        ...context.repo,
        pull_number,
      })
    ).data
    onlyChangedFile =
      changedFiles.length === 1 ? changedFiles[0].filename : null
  }

  const {
    decision,
    details: { isExemptKernelUpdate, isExemptHomeAssistantUpdate },
  } = evaluateTargetBranchPolicy({
    base,
    head,
    maxRebuildCount,
    rebuildsAllTests,
    onlyChangedFile,
  })

  core.info(
    [
      `checkTargetBranch: this PR:`,
      `  * causes ${maxRebuildCount} rebuilds`,
      `  * ${rebuildsAllTests ? 'rebuilds' : 'does not rebuild'} all NixOS tests`,
      `  * ${isExemptKernelUpdate ? 'is' : 'is not'} an exempt kernel update`,
      `  * ${isExemptHomeAssistantUpdate ? 'is' : 'is not'} an exempt home-assistant update`,
    ].join('\n'),
  )

  const reviewFacts: TargetBranchReviewFacts = {
    github,
    context,
    core,
    dry,
    base,
    maxRebuildCount,
  }

  if (decision === 'mass-rebuild') {
    await postMassRebuildReview(reviewFacts)
    return
  }

  if (decision === 'nixos-rebuild') {
    await postNixosRebuildReview(reviewFacts)
    return
  }

  if (decision === 'possible-mass-rebuild') {
    await postPossibleMassRebuildReview(reviewFacts)
    return
  }

  if (decision === 'skip-development-merge') {
    core.info(
      `Skipping checkTargetBranch: PR merges the development branch ${head} into ${base}`,
    )
  } else {
    core.info('checkTargetBranch: this PR is against an appropriate branch.')
  }

  await dismissReviews({
    github,
    context,
    core,
    dry,
    reviewKey,
  })
}