-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
executable file
·250 lines (227 loc) · 7 KB
/
Copy pathsetup.js
File metadata and controls
executable file
·250 lines (227 loc) · 7 KB
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
#!/usr/bin/env node
// @ts-check
"use strict";
const { execFileSync } = require("node:child_process");
const { appendFileSync, readFileSync } = require("node:fs");
const path = require("node:path");
/** Presence marks an integration as ours; value records the installed release. */
const VERSION_VARIABLE = "UPSUN_GITHUB_ENV_SYNC_VERSION";
const EVENTS = [
"environment.push",
"environment.activate",
"environment.redeploy",
"environment.domain.create",
"environment.domain.delete",
"environment.deactivate",
"environment.delete",
];
/**
* @typedef {{ id: string, type: string }} Integration
* @typedef {{ id: string, name: string, value?: string, is_sensitive?: boolean }} Variable
*
* @typedef {object} Client
* @property {() => Integration[]} listIntegrations
* @property {(integrationId: string) => Variable[]} listVariables
* @property {() => string} createIntegration Returns the new integration ID.
* @property {(integrationId: string) => void} updateIntegration
* @property {(integrationId: string, variable: Omit<Variable, "id">) => void} createVariable
* @property {(integrationId: string, variableId: string, variable: Omit<Variable, "id" | "name">) => void} patchVariable
*/
/**
* Only integrations carrying the version variable are ours. Others are never touched.
*
* @param {Client} client
* @returns {string | null}
*/
function findManagedIntegration(client) {
const managed = client
.listIntegrations()
.filter((integration) => integration.type === "script")
.map((integration) => String(integration.id))
.filter((integrationId) =>
client
.listVariables(integrationId)
.some((variable) => variable.name === VERSION_VARIABLE),
);
if (managed.length > 1) {
throw new Error(
`Found ${managed.length} script integrations with a ${VERSION_VARIABLE} variable. Delete all but one.`,
);
}
return managed[0] ?? null;
}
/**
* @param {Client} client
* @param {string} integrationId
* @param {Omit<Variable, "id">} variable
*/
function upsertVariable(client, integrationId, variable) {
const existing = client
.listVariables(integrationId)
.find((candidate) => candidate.name === variable.name);
if (existing) {
client.patchVariable(integrationId, existing.id, {
value: variable.value,
is_sensitive: variable.is_sensitive,
});
} else {
client.createVariable(integrationId, variable);
}
}
/**
* @param {object} options
* @param {Client} options.client
* @param {string} options.version
* @param {string} options.githubRepository
* @param {string} options.githubDeployToken
* @param {(message: string) => void} [options.log]
* @returns {string}
*/
function sync({
client,
version,
githubRepository,
githubDeployToken,
log = () => {},
}) {
const versionVariable = {
name: VERSION_VARIABLE,
value: version,
is_sensitive: false,
};
const existingId = findManagedIntegration(client);
let integrationId = existingId;
if (integrationId) {
log(`Updating script integration ${integrationId}`);
client.updateIntegration(integrationId);
} else {
log("Creating script integration");
integrationId = client.createIntegration();
// Mark first so a failure below leaves an integration the next run recognizes.
upsertVariable(client, integrationId, versionVariable);
}
upsertVariable(client, integrationId, {
name: "GH_TOKEN",
value: githubDeployToken,
is_sensitive: true,
});
upsertVariable(client, integrationId, {
name: "GH_REPO",
value: githubRepository,
is_sensitive: false,
});
if (existingId) {
// Record the version last so it only changes once the update completed.
upsertVariable(client, integrationId, versionVariable);
}
return integrationId;
}
/**
* Talks to the Upsun API through `upsun api:curl`, which handles authentication.
*
* @param {object} options
* @param {string} options.projectId
* @param {string} options.scriptFile
* @returns {Client}
*/
function createCliClient({ projectId, scriptFile }) {
/**
* @param {string} apiPath
* @param {"GET" | "POST" | "PATCH"} method
* @param {object} [payload]
* @returns {any}
*/
const api = (apiPath, method, payload) => {
const args = ["api:curl", apiPath, "--request", method, "--yes"];
if (payload) {
args.push("--json", JSON.stringify(payload));
}
let output;
try {
output = execFileSync("upsun", args, {
encoding: "utf8",
stdio: ["ignore", "pipe", "inherit"],
});
} catch (error) {
// execFileSync's own message repeats the command line, payload and secrets included.
const body = /** @type {{ stdout?: string }} */ (error).stdout?.trim();
throw new Error(`${method} ${apiPath} failed${body ? `: ${body}` : ""}`);
}
return JSON.parse(output || "null");
};
const integrationsPath = `/api/projects/${projectId}/integrations`;
const integrationFields = () => ({
script: readFileSync(scriptFile, "utf8"),
events: EVENTS,
states: ["*"],
environments: ["*"],
});
return {
listIntegrations: () => api(integrationsPath, "GET") ?? [],
listVariables: (integrationId) =>
api(`${integrationsPath}/${integrationId}/variables`, "GET") ?? [],
createIntegration: () => {
const response = api(integrationsPath, "POST", {
type: "script",
...integrationFields(),
});
const id = response?._embedded?.entity?.id;
if (!id) {
throw new Error(
`Create response has no integration ID: ${JSON.stringify(response)}`,
);
}
return String(id);
},
updateIntegration: (integrationId) => {
api(`${integrationsPath}/${integrationId}`, "PATCH", integrationFields());
},
createVariable: (integrationId, variable) => {
api(`${integrationsPath}/${integrationId}/variables`, "POST", variable);
},
patchVariable: (integrationId, variableId, variable) => {
api(
`${integrationsPath}/${integrationId}/variables/${variableId}`,
"PATCH",
variable,
);
},
};
}
/**
* @param {string} name
* @returns {string}
*/
function requireEnv(name) {
const value = process.env[name];
if (!value) {
throw new Error(`Missing required environment variable: ${name}`);
}
return value;
}
function main() {
const actionPath = requireEnv("ACTION_PATH");
const githubOutput = requireEnv("GITHUB_OUTPUT");
const integrationId = sync({
client: createCliClient({
projectId: requireEnv("UPSUN_PROJECT_ID"),
scriptFile: path.join(actionPath, "activity-script.js"),
}),
version: require(path.resolve(actionPath, "package.json")).version,
githubRepository: requireEnv("GITHUB_REPOSITORY"),
githubDeployToken: requireEnv("GITHUB_DEPLOY_TOKEN"),
log: console.log,
});
appendFileSync(githubOutput, `integration_id=${integrationId}\n`);
console.log(`Synchronized integration ${integrationId}`);
}
if (require.main === module) {
main();
}
module.exports = {
VERSION_VARIABLE,
EVENTS,
findManagedIntegration,
upsertVariable,
sync,
};