|
1 | 1 | # 🤬 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 |
2 | 2 | # (this workflow shouldn't exist) |
3 | 3 |
|
| 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 | + |
4 | 8 | name: Periodic Actions |
5 | 9 |
|
6 | 10 | on: |
|
18 | 22 | runs-on: ubuntu-latest |
19 | 23 | 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 |
20 | 24 | steps: |
21 | | - - name: Apply Labels |
| 25 | + - id: labels |
| 26 | + name: Apply Labels (and gather "Diff PR" list) |
22 | 27 | uses: actions/github-script@0.9.0 |
23 | 28 | with: |
24 | 29 | script: | |
|
30 | 35 | direction: 'desc', |
31 | 36 | per_page: 100, |
32 | 37 | }); |
| 38 | + let pullRequestsThatNeedDiffs = []; // this will hold a list of PR numbers we need to check for up-to-date diffs on |
33 | 39 | for (let i = 0; i < pulls.length; ++i) { |
34 | | - let pull = pulls[i]; |
| 40 | + const pull = pulls[i]; |
35 | 41 | const { data: files } = await github.pulls.listFiles({ |
36 | 42 | owner: context.repo.owner, |
37 | 43 | repo: context.repo.repo, |
|
56 | 62 | labels: newLabels, |
57 | 63 | }); |
58 | 64 | } |
| 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 | + } |
59 | 98 | } |
| 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 |
0 commit comments