-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepos.ts
More file actions
555 lines (490 loc) · 14.4 KB
/
Copy pathrepos.ts
File metadata and controls
555 lines (490 loc) · 14.4 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import * as core from '@actions/core'
import type { ExecOptions } from '@actions/exec'
import * as exec from '@actions/exec'
import { Octokit } from '@octokit/rest'
import fs from 'fs'
import path from 'path'
import { Common } from '../enums.js'
import type { Classroom } from '../types.js'
import { generateTeamName } from './teams.js'
/**
* Generates the repository name for this class and user.
*
* @param classroom Classroom
* @param handle GitHub Handle
*/
export function generateRepoName(classroom: Classroom, handle: string): string {
return `gh-int-${classroom.customerAbbr.toLowerCase()}-${handle}`
}
/**
* Creates a repository for an attendee.
*
* @param octokit Octokit
* @param classroom Classroom
* @param handle GitHub Handle
* @returns Repository Name
*/
export async function create(
octokit: InstanceType<typeof Octokit>,
classroom: Classroom,
handle: string
): Promise<string> {
core.info(`Creating Repository: ${generateRepoName(classroom, handle)}`)
const response = await octokit.rest.repos.createUsingTemplate({
template_owner: Common.TEMPLATE_OWNER,
template_repo: Common.TEMPLATE_REPO,
owner: classroom.organization,
name: generateRepoName(classroom, handle),
description: `GitHub Intermediate - ${classroom.customerName}`,
include_all_branches: true,
private: true
})
// Grant the team access to the repository.
await octokit.rest.teams.addOrUpdateRepoPermissionsInOrg({
org: classroom.organization,
team_slug: generateTeamName(classroom),
owner: classroom.organization,
repo: response.data.name,
permission: 'admin'
})
return response.data.name
}
/**
* Checks if the repository exists.
*
* @param octokit Octokit
* @param classroom Classroom
* @param handle GitHub Handle
* @returns True if the Repository Exists
*/
export async function exists(
octokit: InstanceType<typeof Octokit>,
classroom: Classroom,
handle: string
): Promise<boolean> {
try {
await octokit.rest.repos.get({
owner: classroom.organization,
repo: generateRepoName(classroom, handle)
})
} catch (error: any) {
if (error.status === 404) return false
}
return true
}
/**
* Configures an attendee repository.
*
* @param octokit Octokit
* @param classroom Classroom
* @param repo Repository Name
*/
export async function configure(
octokit: InstanceType<typeof Octokit>,
classroom: Classroom,
repo: string
): Promise<void> {
core.info(`Configuring Repository: ${repo}`)
// Create the deployments environment.
await octokit.rest.repos.createOrUpdateEnvironment({
owner: classroom.organization,
repo,
environment_name: 'deployments'
})
// Configure GitHub Pages.
const response = await octokit.rest.repos.createPagesSite({
owner: classroom.organization,
repo,
build_type: 'workflow'
})
// Update the About page of the repo to include the URL.
await octokit.rest.repos.update({
owner: classroom.organization,
repo,
homepage: response.data.html_url
})
// Configure the exec options.
const options: exec.ExecOptions = {
cwd: process.cwd(),
listeners: {
stdout: (data: Buffer) => {},
stderr: (data: Buffer) => {
/* istanbul ignore next */
if (
!data.toString().startsWith('Cloning into') &&
!data.toString().startsWith('To https://') &&
!data.toString().startsWith('Switched to') &&
!data.toString().startsWith('Already on') &&
!data.toString().startsWith('remote:')
)
console.error(`\t${data.toString()}`)
}
},
silent: true
}
// Clone the repository to the local workspace.
await exec.exec(
'git',
[
'clone',
`https://x-access-token:${process.env.GITHUB_TOKEN!}@${classroom.githubServer}/${classroom.organization}/${repo}.git`
],
options
)
// Update the working directory to the checked out repository.
options.cwd = path.resolve(process.cwd(), repo)
// Update the remote URL to use the token.
await exec.exec(
'git',
[
'remote',
'set-url',
'origin',
`https://x-access-token:${process.env.GITHUB_TOKEN!}@${classroom.githubServer}/${classroom.organization}/${repo}.git`
],
options
)
// Configure the labs
await configureLab1(options, octokit, classroom)
await configureLab2(options, octokit, classroom)
await configureLab3(options, octokit, classroom)
await configureLab4(options, octokit, classroom)
await configureLab5(options, octokit, classroom)
await configureLab6(options, octokit, classroom)
await configureLab7(options, octokit, classroom)
await configureLab8(options, octokit, classroom)
await configureLab9(options, octokit, classroom)
await configureLab10(options, octokit, classroom)
await configureLab11(options, octokit, classroom)
}
/**
* Deletes all class repositories.
*
* @param octokit Octokit
* @param classroom Classroom
*/
export async function deleteRepositories(
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<void> {
core.info(`Deleting Repositories: ${classroom.customerAbbr}`)
// Get the repositories for this request.
const prefix = `gh-int-${classroom.customerAbbr.toLowerCase()}-`
const response = await octokit.rest.search.repos({
q: `org:${classroom.organization} ${prefix}`
})
// Delete the repositories for each member.
for (const repo of response.data.items) {
core.info(`\tDeleting Repository: ${repo.name}`)
await octokit.rest.repos.delete({
owner: classroom.organization,
repo: repo.name
})
}
}
/**
* Configure Lab 1: Add a Feature
*
* @param options Exec Options
* @param octokit Octokit Client
* @param classroom Classroom
*/
export async function configureLab1(
options: ExecOptions,
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<void> {
core.info('\tConfiguring Lab 1: Add a Feature')
// Nothing needs to be done...
}
/**
* Configure Lab 2: Add Tags
*
* @param options Exec Options
* @param octokit Octokit Client
* @param classroom Classroom
*/
export async function configureLab2(
options: ExecOptions,
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<void> {
core.info('\tConfiguring Lab 2: Add Tags')
// Nothing needs to be done...
}
/**
* Configure Lab 3: Git Bisect
*
* This setup step creates a number of commits in the lab repository. One commit
* will contain the specific code that the student will need to bisect to find.
* The remainder are just filler commits to make the history more complex.
*
* @param options Exec Options
* @param octokit Octokit Client
* @param classroom Classroom
*/
export async function configureLab3(
options: ExecOptions,
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<void> {
core.info('\tConfiguring Lab 3: Git Bisect')
for (let i = 1; i < 10; i++) {
// Get the file contents.
const filename = `keyboard_input_manager.test.${i}`
const contents = fs.readFileSync(
`${process.cwd()}/lab-files/3-git-bisect/${filename}`,
'utf8'
)
// Remove the old file if it exists.
await exec.exec('rm', ['__tests__/keyboard_input_manager.test.ts'], options)
// Write the new file.
fs.writeFileSync(
`${options.cwd}/__tests__/keyboard_input_manager.test.ts`,
contents,
'utf8'
)
// Commit the changes.
await exec.exec('git', ['add', '.'], options)
await exec.exec('git', ['commit', '-m', `Adding unit tests ${i}`], options)
}
await exec.exec('git', ['push'], options)
await exec.exec('git', ['checkout', 'main'], options)
}
/**
* Configure Lab 4: Interactive Rebase
*
* This setup step creates a feature branch off an earlier commit on main, so
* that students can rebase the feature branch onto the latest commit on main.
*
* @param options Exec Options
* @param octokit Octokit Client
* @param classroom Classroom
*/
export async function configureLab4(
options: ExecOptions,
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<void> {
core.info('\tConfiguring Lab 4: Interactive Rebase')
// Get the file contents.
const contents = fs.readFileSync(
`${process.cwd()}/lab-files/4-interactive-rebase/html_actuator.1`,
'utf8'
)
// After the lab 3 configuration runs, there should be ~10 commits on main.
// Get the fifth previous commit SHA.
const response = await exec.getExecOutput(
'git',
['rev-parse', 'HEAD~5'],
options
)
const sha = response.stdout?.trim()
// Checkout a feature branch at the SHA.
await exec.exec(
'git',
['checkout', '-b', 'feature/animate-score', sha],
options
)
// Remove the old file if it exists.
await exec.exec('rm', ['src/html_actuator.ts'], options)
// Write the new file.
fs.writeFileSync(`${options.cwd}/src/html_actuator.ts`, contents, 'utf8')
// Commit the changes.
await exec.exec('git', ['add', '.'], options)
await exec.exec('git', ['commit', '-m', 'Animate score update'], options)
await exec.exec(
'git',
['push', '--set-upstream', 'origin', 'feature/animate-score'],
options
)
await exec.exec('git', ['checkout', 'main'], options)
}
/**
* Configure Lab 5: Cherry-Pick
*
* @param options Exec Options
* @param octokit Octokit Client
* @param classroom Classroom
*/
export async function configureLab5(
options: ExecOptions,
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<void> {
core.info('\tConfiguring Lab 5: Cherry-Pick')
// Nothing needs to be done...
}
/**
* Configure Lab 6: Protect Main
*
* @param options Exec Options
* @param octokit Octokit Client
* @param classroom Classroom
*/
export async function configureLab6(
options: ExecOptions,
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<void> {
core.info('\tConfiguring Lab 6: Protect Main')
// Nothing needs to be done...
}
/**
* Configure Lab 7: GitHub Flow
*
* @param options Exec Options
* @param octokit Octokit Client
* @param classroom Classroom
*/
export async function configureLab7(
options: ExecOptions,
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<void> {
core.info('\tConfiguring Lab 7: GitHub Flow')
// Nothing needs to be done...
}
/**
* Configure Lab 8: Merge Conflicts
*
* @param options Exec Options
* @param octokit Octokit Client
* @param classroom Classroom
*/
export async function configureLab8(
options: ExecOptions,
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<void> {
core.info('\tConfiguring Lab 8: Merge Conflicts')
// Create the PRs for the first merge conflict to resolve.
for (let i = 1; i < 3; i++) {
// Get the file contents.
const filename = `game_manager.${i}`
const contents = fs.readFileSync(
`${process.cwd()}/lab-files/8-merge-conflicts/${filename}`,
'utf8'
)
// Checkout a feature branch.
await exec.exec(
'git',
['checkout', '-b', `feature/tile-value-${i}`],
options
)
// Remove the old file if it exists.
await exec.exec('rm', ['src/game_manager.ts'], options)
// Write the new file.
fs.writeFileSync(`${options.cwd}/src/game_manager.ts`, contents, 'utf8')
// Commit the changes.
await exec.exec('git', ['add', '.'], options)
await exec.exec(
'git',
['commit', '-m', `Increase rate of tiles with value 4`],
options
)
// Push the changes.
await exec.exec(
'git',
['push', '--set-upstream', 'origin', `feature/tile-value-${i}`],
options
)
await exec.exec('git', ['checkout', 'main'], options)
// Create the pull request.
await octokit.rest.pulls.create({
owner: classroom.organization,
repo: (options.cwd as string).split('/').pop() as string,
head: `feature/tile-value-${i}`,
base: 'main',
title: 'Increase rate of tiles with value 4',
body: 'This PR increases the rate at which random tiles are created with a value of 4, making the game easier.'
})
}
// Create the PRs for the second merge conflict to resolve.
for (let i = 3; i < 5; i++) {
// Get the file contents.
const filename = `game_manager.${i}`
const contents = fs.readFileSync(
`${process.cwd()}/lab-files/8-merge-conflicts/${filename}`,
'utf8'
)
// Checkout a feature branch.
await exec.exec(
'git',
['checkout', '-b', `feature/start-tiles-${i}`],
options
)
// Remove the old file if it exists.
await exec.exec('rm', ['src/game_manager.ts'], options)
// Write the new file.
fs.writeFileSync(`${options.cwd}/src/game_manager.ts`, contents, 'utf8')
// Commit the changes.
await exec.exec('git', ['add', '.'], options)
await exec.exec(
'git',
['commit', '-m', `Increase the number of starting tiles`],
options
)
// Push the changes.
await exec.exec(
'git',
['push', '--set-upstream', 'origin', `feature/start-tiles-${i}`],
options
)
await exec.exec('git', ['checkout', 'main'], options)
// Create the pull request.
await octokit.rest.pulls.create({
owner: classroom.organization,
repo: (options.cwd as string).split('/').pop() as string,
head: `feature/start-tiles-${i}`,
base: 'main',
title: 'Increase the number of starting tiles',
body: 'This PR increases the number of starting tiles in new games, so that players can get started quickly.'
})
}
}
/**
* Configure Lab 9: Run a Workflow
*
* @param options Exec Options
* @param octokit Octokit Client
* @param classroom Classroom
*/
export async function configureLab9(
options: ExecOptions,
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<void> {
core.info('\tConfiguring Lab 9: Run a Workflow')
// Nothing needs to be done...
}
/**
* Configure Lab 10: Create a Release
*
* @param options Exec Options
* @param octokit Octokit Client
* @param classroom Classroom
*/
export async function configureLab10(
options: ExecOptions,
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<void> {
core.info('\tConfiguring Lab 10: Create a Release')
// Nothing needs to be done...
}
/**
* Configure Lab 11: Deploy to an Environment
*
* @param options Exec Options
* @param octokit Octokit Client
* @param classroom Classroom
*/
export async function configureLab11(
options: ExecOptions,
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<void> {
core.info('\tConfiguring Lab 11: Deploy to an Environment')
// Nothing needs to be done...
}