generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 9
/
merger.ts
260 lines (231 loc) · 7.07 KB
/
merger.ts
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import * as github from '@actions/github'
import * as core from '@actions/core'
import {PullsGetResponseData} from '@octokit/types'
import Retry from './retry'
import {inspect} from 'util'
export type labelStrategies = 'all' | 'atLeastOne'
export interface Inputs {
checkStatus: boolean
comment: string
dryRun: boolean
ignoreLabels: string[]
ignoreLabelsStrategy: labelStrategies
failStep: boolean
intervalSeconds: number
labels: string[]
labelsStrategy: labelStrategies
repo: string
owner: string
pullRequestNumber: number
sha: string
strategy: Strategy
token: string
timeoutSeconds: number
}
export type Strategy = 'merge' | 'squash' | 'rebase'
interface ValidationResult {
failed: boolean
message: string
}
export class Merger {
private retry: Retry
constructor(private cfg: Inputs) {
this.retry = new Retry()
.timeout(this.cfg.timeoutSeconds)
.interval(this.cfg.intervalSeconds)
.failStep(this.cfg.failStep)
}
private isAllLabelsValid(
pr: PullsGetResponseData,
labels: string[],
type: 'labels' | 'ignoreLabels'
): ValidationResult {
const hasLabels = pr.labels
.filter(prLabel => {
return labels.includes(prLabel.name)
})
.map(label => label.name)
let failed = true
if (type === 'labels' && hasLabels.length === labels.length) {
failed = false
}
if (type === 'ignoreLabels' && !hasLabels.length) {
failed = false
}
core.debug(
`Checking all labels for type:${type} and prLabels:${inspect(
pr.labels.map(l => l.name)
)}, hasLabels:${inspect(hasLabels)}, labels:${inspect(
labels
)} and failed: ${failed}`
)
return {
failed,
message: `PR ${pr.id} ${
type === 'labels' ? '' : "does't"
} contains all ${inspect(labels)} for PR labels ${inspect(
pr.labels.map(l => l.name)
)} and and failed: ${failed}`
}
}
private isAtLeastOneLabelsValid(
pr: PullsGetResponseData,
labels: string[],
type: 'labels' | 'ignoreLabels'
): ValidationResult {
const hasLabels = pr.labels
.filter(prLabel => {
return labels.includes(prLabel.name)
})
.map(label => label.name)
let failed = true
if (type === 'labels' && hasLabels.length) {
failed = false
}
if (type === 'ignoreLabels' && !hasLabels.length) {
failed = false
}
core.debug(
`Checking atLeastOne labels for type:${type} and prLabels:${inspect(
pr.labels.map(l => l.name)
)}, hasLabels:${inspect(hasLabels)}, labels:${inspect(
labels
)} and failed: ${failed}`
)
return {
failed,
message: `PR ${pr.id} ${
type === 'labels' ? '' : "does't"
} contains ${inspect(labels)} for PR labels ${inspect(
pr.labels.map(l => l.name)
)}`
}
}
private isLabelsValid(
pr: PullsGetResponseData,
labels: string[],
strategy: labelStrategies,
type: 'labels' | 'ignoreLabels'
): ValidationResult {
switch (strategy) {
case 'atLeastOne':
return this.isAtLeastOneLabelsValid(pr, labels, type)
case 'all':
default:
return this.isAllLabelsValid(pr, labels, type)
}
}
async merge(): Promise<void> {
const client = github.getOctokit(this.cfg.token)
const {owner, repo} = this.cfg
try {
await this.retry.exec(
async (count): Promise<void> => {
try {
const {data: pr} = await client.pulls.get({
owner,
repo,
pull_number: this.cfg.pullRequestNumber
})
if (this.cfg.labels.length) {
const labelResult = this.isLabelsValid(
pr,
this.cfg.labels,
this.cfg.labelsStrategy,
'labels'
)
if (labelResult.failed) {
throw new Error(`Checked labels failed: ${labelResult.message}`)
}
core.debug(
`Checked labels and passed with message:${labelResult.message} with ${this.cfg.labelsStrategy}`
)
core.info(
`Checked labels and passed with labels:${inspect(
this.cfg.labels
)}`
)
}
if (this.cfg.ignoreLabels.length) {
const ignoreLabelResult = this.isLabelsValid(
pr,
this.cfg.ignoreLabels,
this.cfg.ignoreLabelsStrategy,
'ignoreLabels'
)
if (ignoreLabelResult.failed) {
throw new Error(
`Checked ignore labels failed: ${ignoreLabelResult.message}`
)
}
core.debug(
`Checked ignore labels and passed with message:${ignoreLabelResult.message} with ${this.cfg.ignoreLabelsStrategy} strategy`
)
core.info(
`Checked ignore labels and passed with ignoreLabels:${inspect(
this.cfg.ignoreLabels
)}`
)
}
if (this.cfg.checkStatus) {
const {data: checks} = await client.checks.listForRef({
owner: this.cfg.owner,
repo: this.cfg.repo,
ref: this.cfg.sha
})
const totalStatus = checks.total_count
const totalSuccessStatuses = checks.check_runs.filter(
check =>
check.conclusion === 'success' ||
check.conclusion === 'skipped'
).length
if (totalStatus - 1 !== totalSuccessStatuses) {
throw new Error(
`Not all status success, ${totalSuccessStatuses} out of ${
totalStatus - 1
} (ignored this check) success`
)
}
core.debug(`All ${totalStatus} status success`)
core.debug(`Merge PR ${pr.number}`)
}
} catch (err) {
core.debug(`failed retry count:${count} with error ${inspect(err)}`)
throw err
}
}
)
if (this.cfg.comment) {
const {data: resp} = await client.issues.createComment({
owner: this.cfg.owner,
repo: this.cfg.repo,
issue_number: this.cfg.pullRequestNumber,
body: this.cfg.comment
})
core.debug(`Post comment ${inspect(this.cfg.comment)}`)
core.setOutput(`commentID`, resp.id)
}
if (!this.cfg.dryRun) {
await client.pulls.merge({
owner,
repo,
pull_number: this.cfg.pullRequestNumber,
merge_method: this.cfg.strategy
})
core.setOutput('merged', true)
} else {
core.info(`dry run merge action`)
core.setOutput('merged', false)
}
} catch (err) {
core.debug(`Error on retry error:${inspect(err)}`)
if (this.cfg.failStep) {
throw err
}
core.debug('timeout but passing because "failStep" is configure to false')
}
}
}
export default {
Merger
}