-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
prebuilt-status-maintainer.ts
230 lines (207 loc) · 10 KB
/
prebuilt-status-maintainer.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
/**
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
* Licensed under the Gitpod Enterprise Source Code License,
* See License.enterprise.txt in the project root folder.
*/
import { ProbotOctokit } from 'probot';
import { injectable, inject } from 'inversify';
import { WorkspaceDB, TracedWorkspaceDB, DBWithTracing } from '@gitpod/gitpod-db/lib';
import { v4 as uuidv4 } from 'uuid';
import { HeadlessWorkspaceEvent } from '@gitpod/gitpod-protocol/lib/headless-workspace-log';
import { log } from '@gitpod/gitpod-protocol/lib/util/logging';
import { PrebuiltWorkspaceUpdatable, PrebuiltWorkspace, Disposable, DisposableCollection, WorkspaceConfig } from '@gitpod/gitpod-protocol';
import { TraceContext } from '@gitpod/gitpod-protocol/lib/util/tracing';
import { LocalMessageBroker } from '../../../src/messaging/local-message-broker';
import { repeat } from "@gitpod/gitpod-protocol/lib/util/repeat";
export interface CheckRunInfo {
owner: string;
repo: string;
head_sha: string;
details_url: string;
}
// 6 hours
const MAX_UPDATABLE_AGE = 6 * 60 * 60 * 1000;
const DEFAULT_STATUS_DESCRIPTION = "Open a prebuilt online workspace in Gitpod";
const NON_PREBUILT_STATUS_DESCRIPTION = "Open an online workspace in Gitpod";
export type AuthenticatedGithubProvider = (installationId: number) => Promise<InstanceType<typeof ProbotOctokit> | undefined>;
@injectable()
export class PrebuildStatusMaintainer implements Disposable {
@inject(TracedWorkspaceDB) protected readonly workspaceDB: DBWithTracing<WorkspaceDB>;
@inject(LocalMessageBroker) protected readonly localMessageBroker: LocalMessageBroker;
protected githubApiProvider: AuthenticatedGithubProvider;
protected readonly disposables = new DisposableCollection();
start(githubApiProvider: AuthenticatedGithubProvider): void {
// set github before registering the msgbus listener - otherwise an incoming message and the github set might race
this.githubApiProvider = githubApiProvider;
this.disposables.push(
this.localMessageBroker.listenForPrebuildUpdatableEvents((ctx, msg) => this.handlePrebuildFinished(ctx, msg))
);
this.disposables.push(
repeat(this.periodicUpdatableCheck.bind(this), 60 * 1000)
);
log.debug("prebuild updatatable status maintainer started");
}
public async registerCheckRun(ctx: TraceContext, installationId: number, pws: PrebuiltWorkspace, cri: CheckRunInfo, config?: WorkspaceConfig) {
const span = TraceContext.startSpan("registerCheckRun", ctx);
span.setTag("pws-state", pws.state);
try {
const githubApi = await this.getGitHubApi(installationId);
if (!githubApi) {
throw new Error("unable to authenticate GitHub app");
}
if (pws.state == 'queued' || pws.state == "building") {
await this.workspaceDB.trace({span}).attachUpdatableToPrebuild(pws.id, {
id: uuidv4(),
owner: cri.owner,
repo: cri.repo,
isResolved: false,
installationId: installationId.toString(),
contextUrl: cri.details_url,
prebuiltWorkspaceId: pws.id,
});
await githubApi.repos.createCommitStatus({
repo: cri.repo,
owner: cri.owner,
sha: cri.head_sha,
target_url: cri.details_url,
context: "Gitpod",
description: "prebuilding an online workspace for this PR",
state: "pending",
});
} else {
// prebuild isn't running - mark with check
const conclusion = this.getConclusionFromPrebuildState(pws);
await githubApi.repos.createCommitStatus({
repo: cri.repo,
owner: cri.owner,
sha: cri.head_sha,
target_url: cri.details_url,
context: "Gitpod",
description: conclusion == 'success' ? DEFAULT_STATUS_DESCRIPTION : NON_PREBUILT_STATUS_DESCRIPTION,
state: (config?.github?.prebuilds?.addCheck === 'prevent-merge-on-error' ? conclusion : 'success')
});
}
} catch (err) {
TraceContext.setError({span}, err);
throw err;
} finally {
span.finish();
}
}
protected getConclusionFromPrebuildState(pws: PrebuiltWorkspace): "error" | "failure" | "pending" | "success" {
if (pws.state === "aborted") {
return "error";
} else if (pws.state === "queued") {
return "pending";
} else if (pws.state === "timeout") {
return "error";
} else if (pws.state === "available" && !pws.error) {
return "success";
} else if (pws.state === "available" && !!pws.error) {
return "failure";
} else if (pws.state === "building") {
return "pending";
} else {
log.warn("Should have updated prebuilt workspace updatable, but don't know how. Resorting to error conclusion.", { pws });
return "error";
}
}
protected async handlePrebuildFinished(ctx: TraceContext, msg: HeadlessWorkspaceEvent) {
const span = TraceContext.startSpan("PrebuildStatusMaintainer.handlePrebuildFinished", ctx)
try {
// this code assumes that the prebuild is updated in the database before the msgbus msg is received
const prebuild = await this.workspaceDB.trace({span}).findPrebuildByWorkspaceID(msg.workspaceID);
if (!prebuild) {
log.warn("received headless log message without associated prebuild", msg);
return;
}
const updatatables = await this.workspaceDB.trace({span}).findUpdatablesForPrebuild(prebuild.id);
await Promise.all(updatatables.filter(u => !u.isResolved).map(u => this.doUpdate({span}, u, prebuild)));
} catch (err) {
TraceContext.setError({span}, err);
throw err;
} finally {
span.finish();
}
}
protected async doUpdate(ctx: TraceContext, updatatable: PrebuiltWorkspaceUpdatable, pws: PrebuiltWorkspace): Promise<void> {
const span = TraceContext.startSpan("doUpdate", ctx);
try {
const githubApi = await this.getGitHubApi(Number.parseInt(updatatable.installationId));
if (!githubApi) {
log.error("unable to authenticate GitHub app - this leaves user-facing checks dangling.");
return;
}
const workspace = await this.workspaceDB.trace({span}).findById(pws.buildWorkspaceId);
if (!!updatatable.contextUrl && !!workspace) {
const conclusion = this.getConclusionFromPrebuildState(pws);
if (conclusion === 'pending') {
log.info(`Prebuild is still running.`, { prebuiltWorkspaceId: updatatable.prebuiltWorkspaceId });
return;
}
let found = true;
try {
await githubApi.repos.createCommitStatus({
owner: updatatable.owner,
repo: updatatable.repo,
context: "Gitpod",
sha: pws.commit,
target_url: updatatable.contextUrl,
description: conclusion === 'success' ? DEFAULT_STATUS_DESCRIPTION : NON_PREBUILT_STATUS_DESCRIPTION,
state: (workspace?.config?.github?.prebuilds?.addCheck === 'prevent-merge-on-error' ? conclusion : 'success')
});
} catch (err) {
if (err.message == "Not Found") {
log.info("Did not find repository while updating updatable. Probably we lost the GitHub permission for the repo.", {owner: updatatable.owner, repo: updatatable.repo});
found = true;
} else {
throw err;
}
}
TraceContext.addNestedTags({ span }, {
doUpdate: {
update: 'done',
found,
},
});
await this.workspaceDB.trace({span}).markUpdatableResolved(updatatable.id);
log.info(`Resolved updatable. Marked check on ${updatatable.contextUrl} as ${conclusion}`);
} else if (!!updatatable.issue) {
// this updatatable updates a label
log.debug("Update label on a PR - we're not using this yet");
}
} catch (err) {
TraceContext.setError({span}, err);
throw err;
} finally {
span.finish();
}
}
protected async getGitHubApi(installationId: number): Promise<InstanceType<typeof ProbotOctokit> | undefined> {
const api = await this.githubApiProvider(installationId);
if (!api) {
return undefined
}
return (api as InstanceType<typeof ProbotOctokit>);
}
protected async periodicUpdatableCheck() {
const ctx = TraceContext.childContext("periodicUpdatableCheck", {});
try {
const unresolvedUpdatables = await this.workspaceDB.trace(ctx).getUnresolvedUpdatables();
for (const updatable of unresolvedUpdatables) {
if ((Date.now() - Date.parse(updatable.workspace.creationTime)) > MAX_UPDATABLE_AGE) {
log.info("found unresolved updatable that's older than MAX_UPDATABLE_AGE and is inconclusive. Resolving.", updatable);
await this.doUpdate(ctx, updatable, updatable.prebuild);
}
}
} catch (err) {
TraceContext.setError(ctx, err);
throw err;
} finally {
ctx.span?.finish();
}
}
dispose(): void {
this.disposables.dispose();
}
}