/
job.js
270 lines (245 loc) · 8.22 KB
/
job.js
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
261
262
263
264
265
266
267
268
269
270
import { slugid } from 'taskcluster-client-web';
import groupBy from 'lodash/groupBy';
import { createQueryParams, getApiUrl } from '../helpers/url';
import { formatTaskclusterError } from '../helpers/errorMessage';
import { addAggregateFields } from '../helpers/job';
import { getProjectUrl } from '../helpers/location';
import { getData } from '../helpers/http';
import { getAction } from '../helpers/taskcluster';
import PushModel from './push';
import TaskclusterModel from './taskcluster';
const uri = '/jobs/';
// JobModel is the js counterpart of job
export default class JobModel {
static async getList(options, config = {}) {
// The `uri` config allows to fetch a list of jobs from an arbitrary
// endpoint e.g. the similar jobs endpoint. It defaults to the job
// list endpoint.
const { fetchAll, uri: configUri } = config;
const jobUri = configUri || getApiUrl(uri);
const { data, failureStatus } = await getData(
`${jobUri}${options ? createQueryParams(options) : ''}`,
);
if (!failureStatus) {
const {
results,
job_property_names: jobPropertyNames,
next: nextUrl,
} = data;
let itemList;
let nextPagesJobs = [];
if (fetchAll && nextUrl) {
const page = new URLSearchParams(nextUrl.split('?')[1]).get('page');
const newOptions = { ...options, page };
const {
data: nextData,
failureStatus: nextFailureStatus,
} = await JobModel.getList(newOptions, config);
if (!nextFailureStatus) {
nextPagesJobs = nextData;
}
}
if (jobPropertyNames) {
// the results came as list of fields
// we need to convert them to objects
itemList = results.map(elem =>
addAggregateFields(
jobPropertyNames.reduce(
(prev, prop, i) => ({ ...prev, [prop]: elem[i] }),
{},
),
),
);
} else {
itemList = results.map(jobObj => addAggregateFields(jobObj));
}
return { data: [...itemList, ...nextPagesJobs], failureStatus: null };
}
return { data, failureStatus };
}
static get(repoName, pk, signal) {
// a static method to retrieve a single instance of JobModel
return fetch(`${getProjectUrl(uri, repoName)}${pk}/`, { signal }).then(
async response => {
if (response.ok) {
const job = await response.json();
return addAggregateFields(job);
}
const text = await response.text();
throw Error(`Loading job with id ${pk} : ${text}`);
},
);
}
static getSimilarJobs(pk, options, config) {
config = config || {};
// The similar jobs endpoints returns the same type of objects as
// the job list endpoint, so let's reuse the getList method logic.
config.uri = `${getProjectUrl(uri)}${pk}/similar_jobs/`;
return JobModel.getList(options, config);
}
static async retrigger(
jobs,
currentRepo,
notify,
times = 1,
decisionTaskIdMap = null,
testMode = false,
) {
const jobTerm = jobs.length > 1 ? 'jobs' : 'job';
try {
notify(`Attempting to retrigger/add ${jobTerm} via actions.json`, 'info');
const pushIds = [...new Set(jobs.map(job => job.push_id))];
const taskIdMap =
decisionTaskIdMap ||
(await PushModel.getDecisionTaskMap(pushIds, notify));
const uniquePerPushJobs = groupBy(jobs, job => job.push_id);
for (const [key, value] of Object.entries(uniquePerPushJobs)) {
const decisionTaskId = taskIdMap[key].id;
TaskclusterModel.load(decisionTaskId, null, currentRepo, testMode)
.then(async results => {
const actionTaskId = slugid();
const taskLabels = value.map(job => job.job_type_name);
let retriggerAction = results.actions.find(
action => action.name === 'retrigger-multiple',
);
let actionInput = {
requests: [{ tasks: taskLabels, times }],
};
if (!retriggerAction) {
// The `retrigger-multiple` action as introduced in Bug 1521032, to all the action
// to control whether new task are created, or existing ones re-run. We fall back
// to `add-new-jobs` to support pushing old revision to try, where the duplicating
// the release tasks impacted is unlikely to cause problems.
retriggerAction = getAction(results.actions, 'add-new-jobs');
actionInput = {
tasks: taskLabels,
};
}
await TaskclusterModel.submit({
action: retriggerAction,
actionTaskId,
decisionTaskId,
taskId: null,
task: null,
input: actionInput,
staticActionVariables: results.staticActionVariables,
currentRepo,
testMode,
})
.then(() =>
notify(
`Request sent to retrigger/add new jobs via actions.json (${actionTaskId})`,
),
)
.catch(error => {
notify(
`Retrigger failed with Decision task: ${decisionTaskId}: ${error}`,
'danger',
{ sticky: true },
);
});
})
.catch(error => notify(error.message, 'danger', { sticky: true }));
}
} catch (e) {
notify(
`Unable to retrigger/add ${jobTerm}. ${formatTaskclusterError(e)}`,
'danger',
{ sticky: true },
);
}
}
static async cancelAll(
pushId,
currentRepo,
notify,
decisionTask,
testMode = false,
) {
const { id: decisionTaskId } =
decisionTask || (await PushModel.getDecisionTaskId(pushId, notify));
let results;
try {
results = await TaskclusterModel.load(
decisionTaskId,
null,
currentRepo,
testMode,
);
} catch (e) {
notify(e.message, 'danger', { sticky: true });
}
try {
const cancelAllTask = getAction(results.actions, 'cancel-all');
await TaskclusterModel.submit({
action: cancelAllTask,
decisionTaskId,
input: {},
staticActionVariables: results.staticActionVariables,
currentRepo,
testMode,
});
} catch (e) {
// The full message is too large to fit in a Treeherder
// notification box.
notify(formatTaskclusterError(e), 'danger', { sticky: true });
}
notify('Request sent to cancel all jobs via action.json', 'success');
}
static async cancel(
jobs,
currentRepo,
notify,
decisionTaskIdMap = null,
testMode = false,
) {
const jobTerm = jobs.length > 1 ? 'jobs' : 'job';
const taskIdMap =
decisionTaskIdMap ||
(await PushModel.getDecisionTaskMap(
[...new Set(jobs.map(job => job.push_id))],
notify,
));
try {
notify(
`Attempting to cancel selected ${jobTerm} via actions.json`,
'info',
);
/* eslint-disable no-await-in-loop */
for (const job of jobs) {
const decisionTaskId = taskIdMap[job.push_id].id;
let results;
try {
results = await TaskclusterModel.load(
decisionTaskId,
job,
currentRepo,
testMode,
);
} catch (e) {
notify(e.message, 'danger', { sticky: true });
}
try {
const cancelTask = getAction(results.actions, 'cancel');
await TaskclusterModel.submit({
action: cancelTask,
decisionTaskId,
taskId: results.originalTaskId,
input: {},
staticActionVariables: results.staticActionVariables,
currentRepo,
testMode,
});
} catch (e) {
// The full message is too large to fit in a Treeherder
// notification box.
notify(formatTaskclusterError(e), 'danger', { sticky: true });
}
}
/* eslint-enable no-await-in-loop */
notify(`Request sent to cancel ${jobTerm} via action.json`, 'success');
} catch (e) {
notify(`Unable to cancel ${jobTerm}`, 'danger', { sticky: true });
}
}
}