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
|
export interface ScaffoldResult {
readmeCommitted: boolean;
workflowCommitted: boolean;
workflowsSynced: number;
runsEnqueued: number;
filesIndexed: number;
errors: string[];
}
function ciWorkflow(repoName: string): string {
return `name: CI
on:
- push
jobs:
verify:
runs-on: ubuntu-latest
steps:
- name: Say hello
run: echo "CI is running for ${repoName}"
- name: Check the workspace exists
run: ls -a
`;
}
function readme(owner: string, repoName: string): string {
return `# ${repoName}
Created on Gluecron. This repository was set up for you — nothing here needed
configuring.
## What is already switched on
- **Branch protection** on the default branch
- **Secret + security scanning** on every push
- **AI review** on every pull request
- **CI** — see \`.gluecron/workflows/ci.yml\`, which ran when this commit landed
## Next
Push some code:
\`\`\`bash
git clone https://gluecron.com/${owner}/${repoName}.git
cd ${repoName}
# ...
git push
\`\`\`
Every push is scanned, reviewed and gated automatically.
`;
}
export interface ScaffoldDeps {
createOrUpdateFileOnBranch: (input: {
owner: string;
name: string;
branch: string;
filePath: string;
bytes: Uint8Array;
message: string;
authorName: string;
authorEmail: string;
}) => Promise<
{ commitSha: string; blobSha: string; parentSha: string | null } | { error: string }
>;
syncAndEnqueuePushWorkflows: (opts: {
owner: string;
repo: string;
repositoryId: string;
branch: string;
commitSha: string;
triggeredBy?: string | null;
}) => Promise<{ synced: number; enqueued: number; errors: string[] }>;
}
async function realDeps(): Promise<ScaffoldDeps> {
const { createOrUpdateFileOnBranch } = await import("../git/repository");
const { syncAndEnqueuePushWorkflows } = await import("./push-workflow-sync");
return {
createOrUpdateFileOnBranch: createOrUpdateFileOnBranch as ScaffoldDeps["createOrUpdateFileOnBranch"],
syncAndEnqueuePushWorkflows,
};
}
export async function scaffoldFirstRepo(
opts: {
owner: string;
repoName: string;
repositoryId: string;
defaultBranch: string;
authorName: string;
authorEmail: string;
userId: string;
},
injected?: ScaffoldDeps
): Promise<ScaffoldResult> {
const result: ScaffoldResult = {
readmeCommitted: false,
workflowCommitted: false,
workflowsSynced: 0,
runsEnqueued: 0,
filesIndexed: 0,
errors: [],
};
const deps = injected ?? (await realDeps());
const { createOrUpdateFileOnBranch } = deps;
const enc = new TextEncoder();
let headSha: string | null = null;
const write = async (filePath: string, body: string, message: string) => {
const res = await createOrUpdateFileOnBranch({
owner: opts.owner,
name: opts.repoName,
branch: opts.defaultBranch,
filePath,
bytes: enc.encode(body),
message,
authorName: opts.authorName,
authorEmail: opts.authorEmail,
});
if ("error" in res) throw new Error(`${filePath}: ${res.error}`);
return res.commitSha;
};
try {
headSha = await write(
"README.md",
readme(opts.owner, opts.repoName),
"Add README"
);
result.readmeCommitted = true;
} catch (err) {
result.errors.push(
`readme: ${err instanceof Error ? err.message : String(err)}`
);
}
try {
headSha = await write(
".gluecron/workflows/ci.yml",
ciWorkflow(opts.repoName),
"Add CI workflow"
);
result.workflowCommitted = true;
} catch (err) {
result.errors.push(
`workflow: ${err instanceof Error ? err.message : String(err)}`
);
}
if (result.workflowCommitted && headSha) {
try {
const sync = await deps.syncAndEnqueuePushWorkflows({
owner: opts.owner,
repo: opts.repoName,
repositoryId: opts.repositoryId,
branch: opts.defaultBranch,
commitSha: headSha,
triggeredBy: opts.userId,
});
result.workflowsSynced = sync.synced ?? 0;
result.runsEnqueued = sync.enqueued ?? 0;
} catch (err) {
result.errors.push(
`sync: ${err instanceof Error ? err.message : String(err)}`
);
}
}
if (headSha) {
try {
const { indexExistingRepo } = await import("./index-existing-repo");
const idx = await indexExistingRepo({
repositoryId: opts.repositoryId,
owner: opts.owner,
repoName: opts.repoName,
ref: opts.defaultBranch,
commitSha: headSha,
});
result.filesIndexed = idx.indexed;
if (idx.error) result.errors.push(`index: ${idx.error}`);
} catch (err) {
result.errors.push(
`index: ${err instanceof Error ? err.message : String(err)}`
);
}
}
return result;
}
|