Repository navigation
Expand file tree
/
Copy pathgithub_actions_resource_commands.ts
More file actions
218 lines (209 loc) · 11.8 KB
/
Copy pathgithub_actions_resource_commands.ts
File metadata and controls
218 lines (209 loc) · 11.8 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
import type { SetupCredentialValue, SetupResourceTarget, SetupVariable, SetupVariablesWriteResult, SetupVariableWriteFailure } from '../../../domain/setup';
import type { GithubClientPort } from '../../../infrastructure/github/ports/github_client_provider_port';
import type { GithubRepositoryVariablesClient } from '../../../infrastructure/github/ports/github_repository_variables_protocol';
import { encryptSecret } from '../../../infrastructure/github_secret_encryption';
import { listCollection } from './github_actions_resource_collection';
import { getGithubErrorStatus, isGithubRateLimited } from './github_error_policy';
/** GitHub Actions Secret encryption and scope-preserving resource writes. */
export class GithubActionsResourceCommands {
constructor(private readonly githubClient: GithubClientPort<GithubRepositoryVariablesClient>) {}
async upsertSecrets(
owner: string,
repository: string,
token: string,
credentials: readonly SetupCredentialValue[],
): Promise<{ created: number; updated: number; skipped: number; errors: string[] }> {
const client = this.githubClient.getClient(token);
const actions = client.rest.actions;
if (!actions.listRepoSecrets || !actions.getRepoPublicKey || !actions.createOrUpdateRepoSecret) {
throw new Error('GitHub repository Secret API is unavailable.');
}
const existing = new Set((await listCollection(client, actions.listRepoSecrets,
{ owner, repo: repository, per_page: 100 }, 'secrets')).map(secret => secret.name));
const publicKey = await actions.getRepoPublicKey({ owner, repo: repository });
let created = 0;
let updated = 0;
const skipped = 0;
const errors: string[] = [];
for (const credential of credentials) {
try {
await actions.createOrUpdateRepoSecret({
owner,
repo: repository,
secret_name: credential.name,
encrypted_value: await encryptSecret(credential.value, publicKey.data.key),
key_id: publicKey.data.key_id,
});
if (existing.has(credential.name)) updated += 1;
else created += 1;
} catch {
errors.push(`Unable to configure repository Secret ${credential.name}.`);
}
}
return { created, updated, skipped, errors };
}
async upsertScopedSecrets(
owner: string,
repository: string,
token: string,
target: SetupResourceTarget,
credentials: readonly SetupCredentialValue[],
): Promise<{ created: number; updated: number; skipped: number; errors: string[] }> {
if (target.scope === 'repository') return this.upsertSecrets(owner, repository, token, credentials);
const client = this.githubClient.getClient(token);
const secrets = client.rest.actions;
if (!secrets?.getOrgPublicKey || !secrets.createOrUpdateOrgSecret || !secrets.listOrgSecrets) {
throw new Error('GitHub organization Secret API is unavailable or the setup PAT lacks organization Secret permissions.');
}
if (target.organizationVisibility === 'selected' && target.repositoryId === undefined) {
throw new Error('The repository ID is required for selected organization Secret access.');
}
const existing = new Map((await listCollection(client, secrets.listOrgSecrets, { org: owner, per_page: 30 }, 'secrets'))
.map(secret => [secret.name, secret]));
const publicKey = await secrets.getOrgPublicKey({ org: owner });
let created = 0;
let updated = 0;
const errors: string[] = [];
for (const credential of credentials) {
try {
const current = existing.get(credential.name);
const visibility = current?.visibility ?? target.organizationVisibility;
if (visibility === 'selected' && (target.repositoryId === undefined || !secrets.addSelectedRepoToOrgSecret)) {
throw new Error('Selected organization Secret access cannot be granted to this repository.');
}
await secrets.createOrUpdateOrgSecret({
org: owner,
secret_name: credential.name,
encrypted_value: await encryptSecret(credential.value, publicKey.data.key),
key_id: publicKey.data.key_id,
visibility,
...(visibility === 'selected' && !current
? { selected_repository_ids: [target.repositoryId] }
: {}),
});
if (visibility === 'selected') {
await secrets.addSelectedRepoToOrgSecret!({ org: owner, secret_name: credential.name, repository_id: target.repositoryId });
}
if (current) updated += 1;
else created += 1;
} catch {
errors.push(`Unable to configure organization Secret ${credential.name}.`);
}
}
return { created, updated, skipped: 0, errors };
}
async upsert(
owner: string,
repository: string,
token: string,
variables: readonly { name: string; value: string }[],
): Promise<SetupVariablesWriteResult> {
return this.writeRepositoryVariables(owner, repository, token, variables, true);
}
async createMissing(owner: string, repository: string, token: string, variables: readonly SetupVariable[]): Promise<SetupVariablesWriteResult> {
return this.writeRepositoryVariables(owner, repository, token, variables, false);
}
private async writeRepositoryVariables(owner: string, repository: string, token: string, variables: readonly SetupVariable[], allowUpdate: boolean): Promise<SetupVariablesWriteResult> {
const client = this.githubClient.getClient(token);
const existingVariables = await listCollection(client, client.rest.actions.listRepoVariables, { owner, repo: repository, per_page: 100 }, 'variables');
const existingValues = new Map(existingVariables.map(variable => [variable.name, variable.value]));
let created = 0;
let updated = 0;
const errors: string[] = [];
const failures: SetupVariableWriteFailure[] = [];
for (const variable of variables) {
try {
if (existingValues.has(variable.name)) {
if (existingValues.get(variable.name) === variable.value) continue;
if (!allowUpdate) {
errors.push(`Repository Variable ${variable.name} already exists with a different value. Review a separate manual update, then prepare setup again.`);
failures.push({ name: variable.name, scope: 'repository', phase: 'update', reason: 'conflict' });
continue;
}
await client.rest.actions.updateRepoVariable({ owner, repo: repository, name: variable.name, value: variable.value });
updated += 1;
} else {
await client.rest.actions.createRepoVariable({ owner, repo: repository, name: variable.name, value: variable.value });
created += 1;
}
} catch (error) {
failures.push(variableWriteFailure(variable.name, 'repository', existingValues.has(variable.name) ? 'update' : 'create', error));
errors.push(`Unable to configure repository Variable ${variable.name}.`);
}
}
return { created, updated, errors, ...(failures.length ? { failures, unclassifiedErrors: [] } : {}) };
}
async upsertScopedVariables(
owner: string,
repository: string,
token: string,
target: SetupResourceTarget,
variables: readonly SetupVariable[],
): Promise<SetupVariablesWriteResult> {
if (target.scope === 'repository') return this.upsert(owner, repository, token, variables);
const client = this.githubClient.getClient(token);
const actions = client.rest.actions;
if (!actions.listOrgVariables || !actions.createOrgVariable || !actions.updateOrgVariable) {
throw new Error('GitHub organization Variable API is unavailable or the setup PAT lacks organization Variable permissions.');
}
if (target.organizationVisibility === 'selected' && target.repositoryId === undefined) {
throw new Error('The repository ID is required for selected organization Variable access.');
}
const existing = new Map((await listCollection(client, actions.listOrgVariables, { org: owner, per_page: 30 }, 'variables'))
.map(variable => [variable.name, variable]));
const accessible = new Set<string>();
if (actions.listRepoOrganizationVariables && variables.some(variable => existing.get(variable.name)?.visibility === 'selected')) {
try {
const inherited = await listCollection(client, actions.listRepoOrganizationVariables,
{ owner, repo: repository, per_page: 30 }, 'variables');
inherited.forEach(variable => accessible.add(variable.name));
} catch { /* Unknown access still requires the grant operation below. */ }
}
let created = 0;
let updated = 0;
const errors: string[] = [];
const failures: SetupVariableWriteFailure[] = [];
for (const variable of variables) {
let phase: SetupVariableWriteFailure['phase'] = existing.has(variable.name) ? 'update' : 'create';
try {
const current = existing.get(variable.name);
const visibility = current?.visibility ?? target.organizationVisibility;
const needsGrant = visibility === 'selected' && current !== undefined && !accessible.has(variable.name);
if (visibility === 'selected' && (target.repositoryId === undefined || (needsGrant && !actions.addSelectedRepoToOrgVariable))) {
throw new Error('Selected organization Variable access cannot be granted to this repository.');
}
const write = current ? actions.updateOrgVariable : actions.createOrgVariable;
await write({
org: owner,
name: variable.name,
value: variable.value,
...(!current ? { visibility } : {}),
...(visibility === 'selected' && !current
? { selected_repository_ids: [target.repositoryId] }
: {}),
});
if (needsGrant) {
phase = 'repository-access';
await actions.addSelectedRepoToOrgVariable!({ org: owner, name: variable.name, repository_id: target.repositoryId });
}
if (current) updated += 1;
else created += 1;
} catch (error) {
failures.push(variableWriteFailure(variable.name, 'organization', phase, error));
errors.push(`Unable to configure organization Variable ${variable.name}.`);
}
}
return { created, updated, errors, ...(failures.length ? { failures, unclassifiedErrors: [] } : {}) };
}
}
function variableWriteFailure(
name: string, scope: SetupVariableWriteFailure['scope'], phase: SetupVariableWriteFailure['phase'], error: unknown,
): SetupVariableWriteFailure {
const status = getGithubErrorStatus(error);
const reason = isGithubRateLimited(error) ? 'rate-limited'
: status === 401 || status === 403 ? 'authorization'
: status === 400 || status === 422 ? 'invalid-input'
: status === 409 ? 'conflict'
: 'unavailable';
return { name, scope, phase, reason };
}