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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
import { and, eq } from "drizzle-orm";
import { db } from "../db";
import { workflowSecrets } from "../db/schema";
import { encryptSecret } from "./workflow-secrets-crypto";
export type GithubSecretName = {
name: string;
createdAt: string;
updatedAt: string;
};
type GithubSecretListResponse = {
total_count: number;
secrets: Array<{
name: string;
created_at: string;
updated_at: string;
}>;
};
const GITHUB_API_BASE = "https://api.github.com";
const PER_PAGE = 30;
const MAX_PAGES = 10;
export async function listGithubSecretNames(args: {
owner: string;
repo: string;
githubToken: string;
fetchImpl?: typeof fetch;
}): Promise<GithubSecretName[]> {
const { owner, repo, githubToken } = args;
const doFetch = args.fetchImpl ?? fetch;
if (
typeof owner !== "string" ||
!owner ||
typeof repo !== "string" ||
!repo ||
typeof githubToken !== "string" ||
!githubToken
) {
return [];
}
const headers: Record<string, string> = {
Accept: "application/vnd.github.v3+json",
Authorization: `Bearer ${githubToken}`,
"User-Agent": "gluecron/1.0",
"X-GitHub-Api-Version": "2022-11-28",
};
const out: GithubSecretName[] = [];
try {
let page = 1;
let total = Infinity;
while (page <= MAX_PAGES && out.length < total) {
const url =
`${GITHUB_API_BASE}/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}` +
`/actions/secrets?per_page=${PER_PAGE}&page=${page}`;
const res = await doFetch(url, { headers });
if (!res.ok) {
return out;
}
let body: GithubSecretListResponse;
try {
body = (await res.json()) as GithubSecretListResponse;
} catch {
return out;
}
if (
!body ||
typeof body.total_count !== "number" ||
!Array.isArray(body.secrets)
) {
return out;
}
total = body.total_count;
for (const s of body.secrets) {
if (s && typeof s.name === "string" && s.name) {
out.push({
name: s.name,
createdAt: typeof s.created_at === "string" ? s.created_at : "",
updatedAt: typeof s.updated_at === "string" ? s.updated_at : "",
});
}
}
if (body.secrets.length < PER_PAGE) break;
page++;
}
} catch {
return out;
}
return out;
}
export async function createPlaceholderSecrets(args: {
repositoryId: string;
names: string[];
createdByUserId: string;
}): Promise<{ created: number; skippedExisting: number }> {
const { repositoryId, names, createdByUserId } = args;
if (typeof repositoryId !== "string" || !repositoryId) {
return { created: 0, skippedExisting: 0 };
}
if (typeof createdByUserId !== "string" || !createdByUserId) {
return { created: 0, skippedExisting: 0 };
}
if (!Array.isArray(names) || names.length === 0) {
return { created: 0, skippedExisting: 0 };
}
const enc = encryptSecret("");
if (!enc.ok) {
return { created: 0, skippedExisting: 0 };
}
let created = 0;
let skippedExisting = 0;
for (const rawName of names) {
if (typeof rawName !== "string") continue;
const name = rawName.trim();
if (!name) continue;
try {
const [existing] = await db
.select({ id: workflowSecrets.id })
.from(workflowSecrets)
.where(
and(
eq(workflowSecrets.repositoryId, repositoryId),
eq(workflowSecrets.name, name)
)
)
.limit(1);
if (existing) {
skippedExisting++;
continue;
}
await db.insert(workflowSecrets).values({
repositoryId,
name,
encryptedValue: enc.ciphertext,
createdBy: createdByUserId,
});
created++;
} catch (err) {
console.error("[github-secrets-import] insert failed:", err instanceof Error ? err.message : String(err));
}
}
return { created, skippedExisting };
}
export type ImportSecretsResult = {
imported: { name: string; status: "placeholder_created" | "already_exists" }[];
errors: string[];
};
export async function importSecretsForRepo(args: {
githubOwner: string;
githubRepo: string;
githubToken: string;
gluecronRepositoryId: string;
importedByUserId: string;
fetchImpl?: typeof fetch;
}): Promise<ImportSecretsResult> {
const errors: string[] = [];
if (!args.githubToken) {
errors.push("no_github_token");
return { imported: [], errors };
}
let names: GithubSecretName[];
try {
names = await listGithubSecretNames({
owner: args.githubOwner,
repo: args.githubRepo,
githubToken: args.githubToken,
fetchImpl: args.fetchImpl,
});
} catch {
errors.push("github_api_failed");
return { imported: [], errors };
}
if (names.length === 0) {
return { imported: [], errors };
}
let existingNames = new Set<string>();
try {
const existing = await db
.select({ name: workflowSecrets.name })
.from(workflowSecrets)
.where(eq(workflowSecrets.repositoryId, args.gluecronRepositoryId));
existingNames = new Set(existing.map((r) => r.name));
} catch {
errors.push("db_lookup_failed");
}
await createPlaceholderSecrets({
repositoryId: args.gluecronRepositoryId,
names: names.map((n) => n.name),
createdByUserId: args.importedByUserId,
});
const imported = names.map((n) => ({
name: n.name,
status: existingNames.has(n.name)
? ("already_exists" as const)
: ("placeholder_created" as const),
}));
return { imported, errors };
}
|