Skip to content

Commit 48ad606

Browse files
committed
Address comments and cleanup
1 parent eb47ff7 commit 48ad606

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

.github/workflows/notify_test_workflow.yml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,34 @@ jobs:
3838
with:
3939
github-token: ${{ secrets.GITHUB_TOKEN }}
4040
script: |
41-
const endpoint = "GET /repos/:owner/:repo/actions/workflows/:id/runs?&branch=:branch"
41+
const endpoint = 'GET /repos/:owner/:repo/actions/workflows/:id/runs?&branch=:branch'
4242
4343
// TODO: Should use pull_request.user and pull_request.user.repos_url?
4444
// If a different person creates a commit to another forked repo,
4545
// it wouldn't be able to detect.
4646
const params = {
4747
owner: context.payload.pull_request.head.repo.owner.login,
4848
repo: context.payload.pull_request.head.repo.name,
49-
id: "build_and_test.yml",
49+
id: 'build_and_test.yml',
5050
branch: context.payload.pull_request.head.ref,
5151
}
5252
53+
console.log('SHA: ' + context.payload.pull_request.head.ref)
54+
55+
// Wait 3 seconds to make sure the fork repository triggered a workflow.
56+
await new Promise(r => setTimeout(r, 3000))
57+
5358
const runs = await github.request(endpoint, params)
5459
const runID = runs.data.workflow_runs[0].id
55-
const runUrl = "https://github.com/"
60+
// TODO: If no workflows were found, it's likely GitHub Actions was not enabled
61+
62+
if (runs.data.workflow_runs[0].head_sha != context.payload.pull_request.head.sha) {
63+
throw new Error('There was a new unsynced commit pushed. Please retrigger the workflow.');
64+
}
65+
66+
const runUrl = 'https://github.com/'
5667
+ context.payload.pull_request.head.repo.full_name
57-
+ "/actions/runs/"
68+
+ '/actions/runs/'
5869
+ runID
5970
6071
const name = 'Build and test'

.github/workflows/update_build_status.yml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,41 @@ jobs:
3333
with:
3434
github-token: ${{ secrets.GITHUB_TOKEN }}
3535
script: |
36-
const endpoint = "GET /repos/:owner/:repo/pulls?state=:state"
36+
const endpoint = 'GET /repos/:owner/:repo/pulls?state=:state'
3737
const params = {
3838
owner: context.repo.owner,
3939
repo: context.repo.repo,
4040
state: 'open'
4141
}
4242
43-
const maybeReady = ["clean", "has_hooks", "unknown", "unstable"];
44-
const notReady = ["dirty", "draft"];
43+
// See https://docs.github.com/en/graphql/reference/enums#mergestatestatus
44+
const maybeReady = ['behind', 'clean', 'draft', 'has_hooks', 'unknown', 'unstable'];
4545
46+
// Iterate open PRs
4647
for await (const prs of github.paginate.iterator(endpoint,params)) {
48+
// Each page
4749
for await (const pr of prs.data) {
50+
console.log('SHA: ' + pr.head.sha)
51+
console.log(' Mergeable status: ' + pr.mergeable_state)
4852
if (pr.mergeable_state == null || maybeReady.includes(pr.mergeable_state)) {
4953
const checkRuns = await github.request('GET /repos/{owner}/{repo}/commits/{ref}/check-runs', {
5054
owner: context.repo.owner,
5155
repo: context.repo.repo,
5256
ref: pr.head.sha
5357
})
5458
59+
// Iterator GitHub Checks in the PR
5560
for await (const cr of checkRuns.data.check_runs) {
56-
if (cr.name == "Build and test") {
57-
const params = JSON.parse(cr.output.text) // text contains parameters to make request in JSON
61+
if (cr.name == 'Build and test') {
62+
// text contains parameters to make request in JSON.
63+
const params = JSON.parse(cr.output.text)
64+
65+
// Get the workflow run in the forked repository
5866
const run = await github.request('GET /repos/{owner}/{repo}/actions/runs/{run_id}', params)
5967
6068
// Keep syncing the status of the checks
61-
if (run.data.status == "completed") {
69+
if (run.data.status == 'completed') {
70+
console.log(' Run ' + cr.id + ': set status (' + run.data.status + ') and conclusion (' + run.data.conclusion + ')')
6271
const response = await github.request('PATCH /repos/{owner}/{repo}/check-runs/{check_run_id}', {
6372
owner: context.repo.owner,
6473
repo: context.repo.repo,
@@ -68,6 +77,7 @@ jobs:
6877
conclusion: run.data.conclusion
6978
})
7079
} else {
80+
console.log(' Run ' + cr.id + ': set status (' + run.data.status + ')')
7181
const response = await github.request('PATCH /repos/{owner}/{repo}/check-runs/{check_run_id}', {
7282
owner: context.repo.owner,
7383
repo: context.repo.repo,

0 commit comments

Comments
 (0)