Skip to content

Commit 088d51b

Browse files
committed
Move PR diff generation to our separate "Periodic" workflow
1 parent dc9cd28 commit 088d51b

File tree

2 files changed

+76
-51
lines changed

2 files changed

+76
-51
lines changed

.github/workflows/periodic.yml

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# 🤬 https://github.com/actions/labeler/issues/12 / https://github.saobby.my.eu.orgmunity/t5/GitHub-Actions/GitHub-actions-are-severely-limited-on-PRs/m-p/54669/highlight/true#M9249
22
# (this workflow shouldn't exist)
33

4+
# For future reference (in case we're someday able to revert this):
5+
# - https://github.com/docker-library/official-images/commit/a40bfb3617fb765ccabf9794e55f7cece6281696 (initial labelling)
6+
# - TODO (initial diffing)
7+
48
name: Periodic Actions
59

610
on:
@@ -18,7 +22,8 @@ jobs:
1822
runs-on: ubuntu-latest
1923
timeout-minutes: 15 # given that this runs every 15 minutes, it needs to take less than that to do whatever it is going to do
2024
steps:
21-
- name: Apply Labels
25+
- id: labels
26+
name: Apply Labels (and gather "Diff PR" list)
2227
uses: actions/github-script@0.9.0
2328
with:
2429
script: |
@@ -30,8 +35,9 @@ jobs:
3035
direction: 'desc',
3136
per_page: 100,
3237
});
38+
let pullRequestsThatNeedDiffs = []; // this will hold a list of PR numbers we need to check for up-to-date diffs on
3339
for (let i = 0; i < pulls.length; ++i) {
34-
let pull = pulls[i];
40+
const pull = pulls[i];
3541
const { data: files } = await github.pulls.listFiles({
3642
owner: context.repo.owner,
3743
repo: context.repo.repo,
@@ -56,4 +62,72 @@ jobs:
5662
labels: newLabels,
5763
});
5864
}
65+
66+
if (currentLabels.concat(newLabels).find((label) => { return label.startsWith('library/') })) {
67+
const commentText = 'Diff for ' + pull.head.sha + ':';
68+
const { data: comments } = await github.issues.listComments({
69+
owner: context.repo.owner,
70+
repo: context.repo.repo,
71+
issue_number: pull.number,
72+
sort: 'created',
73+
direction: 'desc',
74+
per_page: 100,
75+
});
76+
let needNewComment = true;
77+
for (let j = 0; j < comments.length; ++j) {
78+
const comment = comments[j];
79+
if (comment.user.login === 'github-actions[bot]') {
80+
if (comment.body.includes(commentText)) {
81+
needNewComment = false;
82+
} else {
83+
await github.issues.deleteComment({
84+
owner: context.repo.owner,
85+
repo: context.repo.repo,
86+
comment_id: comment.id,
87+
});
88+
}
89+
}
90+
}
91+
if (needNewComment) {
92+
pullRequestsThatNeedDiffs = pullRequestsThatNeedDiffs.concat([{
93+
number: pull.number,
94+
text: commentText,
95+
}]);
96+
}
97+
}
5998
}
99+
core.setOutput('diffPulls', JSON.stringify({ pulls: pullRequestsThatNeedDiffs, count: pullRequestsThatNeedDiffs.length }));
100+
- name: Checkout
101+
uses: actions/checkout@v2
102+
if: fromJSON(steps.labels.outputs.diffPulls).count > 0
103+
- id: diffs
104+
name: Generate Diffs
105+
run: |
106+
pulls="$(
107+
jq -c '.pulls[]' <<EOF
108+
${{ steps.labels.outputs.diffPulls }}
109+
EOF
110+
)"
111+
git clone --depth 1 https://github.com/docker-library/bashbrew.git -b master ~/bashbrew
112+
~/bashbrew/bashbrew.sh --version > /dev/null
113+
export PATH="$HOME/bashbrew/bin:$PATH"
114+
bashbrew --version
115+
IFS=$'\n'
116+
for pull in $pulls; do
117+
number="$(jq -r '.number' <<<"$pull")"
118+
text="$(jq -r '.text' <<<"$pull")"
119+
diff="$(./diff-pr.sh "$number" 2>&1 || :)"
120+
# "Body is too long (maximum is 65536 characters)" (so we'll check for some fudge room and pre-filter the diff)
121+
# TODO consider instead creating a Gist (although that requires a separate type of token, so much less interesting)
122+
jq -Rcs --arg text "$text" 'rtrimstr("\n") | {
123+
body: (
124+
"<details>\n<summary>" + $text + "</summary>\n\n```diff\n"
125+
+ if length < 65000 then . else
126+
"TODO diff too large for GitHub comment!\n"
127+
+ "See: http://github.com/" + env.GITHUB_REPOSITORY + "/actions/runs/" + env.GITHUB_RUN_ID
128+
end
129+
+ "\n```\n\n</details>"
130+
),
131+
}' <<<"$diff" | curl -fL --header 'Authorization: token ${{ secrets.GITHUB_TOKEN }}' --header 'Accept: application/vnd.github.v3+json' --data '@-' --request POST "https://api.github.com/repos/$GITHUB_REPOSITORY/issues/$number/comments"
132+
done
133+
if: fromJSON(steps.labels.outputs.diffPulls).count > 0

.github/workflows/test-pr.yml

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,55 +22,6 @@ jobs:
2222
bashbrew --version
2323
.github/workflows/naughty.sh
2424
25-
diff:
26-
name: Diff
27-
runs-on: ubuntu-latest
28-
steps:
29-
- uses: actions/checkout@v2
30-
- id: diff
31-
name: Run ./diff-pr.sh
32-
run: |
33-
git clone --depth 1 https://github.com/docker-library/bashbrew.git -b master ~/bashbrew
34-
~/bashbrew/bashbrew.sh --version > /dev/null
35-
export PATH="$HOME/bashbrew/bin:$PATH"
36-
bashbrew --version
37-
diff="$(./diff-pr.sh 0)"
38-
# "Body is too long (maximum is 65536 characters)" (so we'll check for some fudge room and pre-filter the diff)
39-
# TODO consider instead creating a Gist (although that requires a separate type of token, so much less interesting)
40-
diff="$(jq -Rcs 'rtrimstr("\n") | { diff: (if length < 65000 then . else "TODO diff too large for GitHub comment!\nSee: http://github.com/" + env.GITHUB_REPOSITORY + "/actions/runs/" + env.GITHUB_RUN_ID end), length: length }' <<<"$diff")"
41-
echo "::set-output name=diff::$diff"
42-
- name: Delete Old Comments
43-
uses: actions/github-script@0.9.0
44-
with:
45-
script: |
46-
const { data: comments } = await github.issues.listComments({
47-
owner: context.repo.owner,
48-
repo: context.repo.repo,
49-
issue_number: context.payload.pull_request.number,
50-
})
51-
comments.forEach((comment) => {
52-
if (comment.user.login === 'github-actions[bot]') {
53-
github.issues.deleteComment({
54-
owner: context.repo.owner,
55-
repo: context.repo.repo,
56-
comment_id: comment.id,
57-
})
58-
}
59-
})
60-
- name: Create Diff Comment
61-
uses: actions/github-script@0.9.0
62-
with:
63-
script: |
64-
const data = ${{ steps.diff.outputs.diff }}
65-
const body = "<details>\n<summary>Diff for " + context.payload.pull_request.head.sha + ":</summary>\n\n```diff\n" + data.diff + "\n```\n\n</details>"
66-
github.issues.createComment({
67-
owner: context.repo.owner,
68-
repo: context.repo.repo,
69-
issue_number: context.payload.pull_request.number,
70-
body: body,
71-
})
72-
if: fromJSON(steps.diff.outputs.diff).length > 0
73-
7425
generate-jobs:
7526
name: Generate Jobs
7627
runs-on: ubuntu-latest

0 commit comments

Comments
 (0)