-
Notifications
You must be signed in to change notification settings - Fork 355
/
taskcluster.js
136 lines (118 loc) · 4.09 KB
/
taskcluster.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
import defaults from 'lodash/defaults';
import jsone from 'json-e';
import { Auth, Hooks } from 'taskcluster-client-web';
import { satisfiesExpression } from 'taskcluster-lib-scopes';
import taskcluster from '../helpers/taskcluster';
import { tcRootUrl } from '../helpers/url';
export default class TaskclusterModel {
static taskInContext(tagSetList, taskTags) {
return tagSetList.some(tagSet =>
Object.keys(tagSet).every(
tag => taskTags[tag] && taskTags[tag] === tagSet[tag],
),
);
}
static async submit({
action,
actionTaskId,
decisionTaskId,
taskId,
task,
input,
staticActionVariables,
}) {
const context = defaults(
{},
{
taskGroupId: decisionTaskId,
taskId: taskId || null,
input,
},
staticActionVariables,
);
const queue = taskcluster.getQueue();
if (action.kind === 'task') {
context.task = task;
context.ownTaskId = actionTaskId;
const actionTask = jsone(action.task, context);
const decisionTask = await queue.task(decisionTaskId);
const submitQueue = queue.use({ authorizedScopes: decisionTask.scopes });
await submitQueue.createTask(actionTaskId, actionTask);
return actionTaskId;
}
if (action.kind === 'hook') {
const hookPayload = jsone(action.hookPayload, context);
const { hookId, hookGroupId } = action;
const auth = new Auth({ rootUrl: tcRootUrl });
const hooks = new Hooks({
credentialAgent: taskcluster.getAgent(),
rootUrl: tcRootUrl,
});
const decisionTask = await queue.task(decisionTaskId);
const expansion = await auth.expandScopes({
scopes: decisionTask.scopes,
});
const expression = `in-tree:hook-action:${hookGroupId}/${hookId}`;
if (!satisfiesExpression(expansion.scopes, expression)) {
throw new Error(
`Action is misconfigured: decision task's scopes do not satisfy ${expression}`,
);
}
const result = await hooks.triggerHook(hookGroupId, hookId, hookPayload);
return result.status.taskId;
}
}
static async load(decisionTaskID, job) {
if (!decisionTaskID) {
throw Error("No decision task, can't find taskcluster actions");
}
const queue = taskcluster.getQueue();
const actionsUrl = queue.buildUrl(
queue.getLatestArtifact,
decisionTaskID,
'public/actions.json',
);
const knownKinds = ['task', 'hook'];
let originalTaskId;
let originalTaskPromise = Promise.resolve(null);
if (job && job.taskcluster_metadata) {
originalTaskId = job.taskcluster_metadata.task_id;
originalTaskPromise = fetch(
`https://queue.taskcluster.net/v1/task/${originalTaskId}`,
).then(async response => response.json());
}
return Promise.all([fetch(actionsUrl), originalTaskPromise]).then(
async ([response, originalTask]) => {
const jsonData = await response.json();
if (!jsonData) {
throw Error('Unable to load actions.json');
}
if (jsonData.version !== 1) {
throw Error('Wrong version of actions.json, unable to continue');
}
// The filter in the value of the actions key is an implementation
// of the specification for action context in
// https://docs.taskcluster.net/manual/using/actions/spec#action-context
// It decides if the specific action is applicable for this task.
return {
originalTask,
originalTaskId,
staticActionVariables: jsonData.variables,
actions: jsonData.actions.reduce((actions, action) => {
if (
knownKinds.includes(action.kind) &&
!actions.some(({ name }) => name === action.name) &&
((!action.context.length && !originalTask) ||
(originalTask &&
originalTask.tags &&
this.taskInContext(action.context, originalTask.tags)))
) {
return actions.concat(action);
}
return actions;
}, []),
};
},
);
}
}