forked from electron/trop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackport-to-location.ts
124 lines (114 loc) · 3.21 KB
/
backport-to-location.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
import { CHECK_PREFIX } from '../constants';
import { PRStatus, BackportPurpose, LogLevel } from '../enums';
import { getCheckRun } from '../utils/checks-util';
import * as labelUtils from '../utils/label-utils';
import { log } from '../utils/log-util';
import { backportImpl } from '../utils';
import { Probot } from 'probot';
import { SimpleWebHookRepoContext, WebHookPR } from '../types';
const createOrUpdateCheckRun = async (
context: SimpleWebHookRepoContext,
pr: WebHookPR,
targetBranch: string,
) => {
const check = await getCheckRun(context, pr, targetBranch);
if (check) {
if (check.conclusion === 'neutral') {
await context.octokit.checks.update(
context.repo({
name: check.name,
check_run_id: check.id,
status: 'queued' as 'queued',
}),
);
}
} else {
await context.octokit.checks.create(
context.repo({
name: `${CHECK_PREFIX}${targetBranch}`,
head_sha: pr.head.sha,
status: 'queued' as 'queued',
details_url: 'https://github.com/electron/trop',
}),
);
}
};
/**
* Performs a backport to a specified label representing a branch.
*
* @param {Probot} robot - an instance of Probot
* @param {WebHookRepoContext} context - the context of the event that was triggered
* @param {PullsGetResponseLabelsItem} label - the label representing the target branch for backporting
*/
export const backportToLabel = async (
robot: Probot,
context: SimpleWebHookRepoContext,
pr: WebHookPR,
label: { name: string },
) => {
log(
'backportToLabel',
LogLevel.INFO,
`Executing backport to branch from label ${label.name}`,
);
if (!label.name.startsWith(PRStatus.TARGET)) {
log(
'backportToLabel',
LogLevel.ERROR,
`Label '${label.name}' does not begin with '${PRStatus.TARGET}'`,
);
return;
}
const targetBranch = labelUtils.labelToTargetBranch(label, PRStatus.TARGET);
if (!targetBranch) {
log(
'backportToLabel',
LogLevel.WARN,
'No target branch specified - aborting backport process',
);
return;
}
await createOrUpdateCheckRun(context, pr, targetBranch);
const labelToRemove = label.name;
const labelToAdd = label.name.replace(PRStatus.TARGET, PRStatus.IN_FLIGHT);
await backportImpl(
robot,
context,
pr,
targetBranch,
BackportPurpose.ExecuteBackport,
labelToRemove,
labelToAdd,
);
};
/**
* Performs a backport to a specified target branch.
*
* @param {Probot} robot - an instance of Probot
* @param {WebHookRepoContext} context - the context of the event that was triggered
* @param {string} targetBranch - the branch to which the backport will be performed
*/
export const backportToBranch = async (
robot: Probot,
context: SimpleWebHookRepoContext,
pr: WebHookPR,
targetBranch: string,
) => {
log(
'backportToLabel',
LogLevel.INFO,
`Executing backport to branch '${targetBranch}'`,
);
await createOrUpdateCheckRun(context, pr, targetBranch);
const labelToRemove = undefined;
const labelToAdd = PRStatus.IN_FLIGHT + targetBranch;
await backportImpl(
robot,
context,
pr,
targetBranch,
BackportPurpose.ExecuteBackport,
labelToRemove,
labelToAdd,
);
};