-
Notifications
You must be signed in to change notification settings - Fork 18.9k
184 lines (165 loc) · 5.89 KB
/
Copy pathadvanced-fast-track.yml
File metadata and controls
184 lines (165 loc) · 5.89 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
name: Advanced Fast Track
on:
create:
delete:
push:
branches:
- my-first-branch
pull_request:
branches:
- main
types:
- opened
- edited
- synchronize
- reopened
- closed
workflow_dispatch:
permissions:
contents: read
issues: write
pull-requests: read
concurrency:
group: advanced-fast-track
cancel-in-progress: false
jobs:
find_exercise:
name: Find Exercise Issue
uses: skills/exercise-toolkit/.github/workflows/find-exercise-issue.yml@v0.7.0
evaluate:
name: Evaluate exercise progress
needs: find_exercise
if: ${{ !github.event.repository.is_template }}
runs-on: ubuntu-latest
env:
ISSUE_NUMBER: ${{ needs.find_exercise.outputs.issue-number }}
steps:
- name: Checkout task list
uses: actions/checkout@v4
- name: Evaluate tasks and update progress
uses: actions/github-script@v7
with:
script: |
const issueNumber = Number(process.env.ISSUE_NUMBER);
const owner = context.repo.owner;
const repo = context.repo.repo;
const stateMarker = '<!-- advanced-fast-track-state -->';
const taskFile = '.github/steps/advanced-task-list.md';
const tasks = require('fs')
.readFileSync(taskFile, 'utf8')
.split('\n')
.filter((line) => line.startsWith('- '))
.map((line) => line.slice(2));
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number: issueNumber,
per_page: 100,
});
const stateComment = comments.find((comment) => comment.body?.includes(stateMarker));
let frontier = 0;
if (stateComment) {
const match = stateComment.body.match(/frontier:\s*(\d+)/);
frontier = match ? Number(match[1]) : 0;
}
async function branchExists(branch) {
try {
await github.rest.repos.getBranch({ owner, repo, branch });
return true;
} catch (error) {
if (error.status === 404) return false;
throw error;
}
}
async function profileContentIsCorrect() {
try {
const response = await github.rest.repos.getContent({
owner,
repo,
path: 'PROFILE.md',
ref: 'my-first-branch',
});
return response.data.type === 'file' &&
Buffer.from(response.data.content, 'base64').toString('utf8').trim() ===
'Welcome to my GitHub profile!';
} catch (error) {
if (error.status === 404) return false;
throw error;
}
}
async function profileCommitExists() {
if (!(await branchExists('my-first-branch'))) return false;
const commits = await github.paginate(github.rest.repos.listCommits, {
owner,
repo,
sha: 'my-first-branch',
path: 'PROFILE.md',
per_page: 100,
});
return commits.some((commit) => commit.commit.message.trim() === 'Add PROFILE.md');
}
async function findPullRequest() {
const response = await github.rest.pulls.list({
owner,
repo,
state: 'all',
base: 'main',
head: `${owner}:my-first-branch`,
per_page: 100,
});
return response.data[0];
}
async function taskIsComplete(taskNumber) {
if (taskNumber === 1) return branchExists('my-first-branch');
if (taskNumber === 2) return profileContentIsCorrect();
if (taskNumber === 3) return profileContentIsCorrect();
if (taskNumber === 4) return profileCommitExists();
const pullRequest = await findPullRequest();
if (taskNumber === 5) return Boolean(pullRequest);
if (taskNumber === 6) {
return Boolean(pullRequest?.title.toLowerCase().includes('add my first file'));
}
if (taskNumber === 7) return Boolean(pullRequest?.body?.trim());
if (taskNumber === 8) return Boolean(pullRequest?.merged_at);
if (taskNumber === 9) return Boolean(pullRequest?.merged_at) &&
!(await branchExists('my-first-branch'));
return false;
}
while (frontier < tasks.length && await taskIsComplete(frontier + 1)) {
frontier += 1;
}
const progress = tasks.map((task, index) =>
`${index < frontier ? '- [x]' : '- [ ]'} ${task}`
).join('\n');
const body = [
stateMarker,
`frontier: ${frontier}`,
'',
'### Advanced Fast Track progress',
'',
progress,
].join('\n');
if (stateComment) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: stateComment.id,
body,
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body,
});
}
if (frontier === tasks.length) {
await github.rest.issues.update({
owner,
repo,
issue_number: issueNumber,
state: 'closed',
});
}
core.info(`Advanced Fast Track frontier: ${frontier}/${tasks.length}`);