Преглед изворни кода

Move PR diff generation to our separate "Periodic" workflow

Tianon Gravi пре 5 година
родитељ
комит
088d51bfde
2 измењених фајлова са 76 додато и 51 уклоњено
  1. 76 2
      .github/workflows/periodic.yml
  2. 0 49
      .github/workflows/test-pr.yml

+ 76 - 2
.github/workflows/periodic.yml

@@ -1,6 +1,10 @@
 # 🤬 https://github.com/actions/labeler/issues/12 / https://github.community/t5/GitHub-Actions/GitHub-actions-are-severely-limited-on-PRs/m-p/54669/highlight/true#M9249
 # (this workflow shouldn't exist)
 
+# For future reference (in case we're someday able to revert this):
+#   - https://github.com/docker-library/official-images/commit/a40bfb3617fb765ccabf9794e55f7cece6281696 (initial labelling)
+#   - TODO (initial diffing)
+
 name: Periodic Actions
 
 on:
@@ -18,7 +22,8 @@ jobs:
     runs-on: ubuntu-latest
     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
     steps:
-      - name: Apply Labels
+      - id: labels
+        name: Apply Labels (and gather "Diff PR" list)
         uses: actions/[email protected]
         with:
           script: |
@@ -30,8 +35,9 @@ jobs:
               direction: 'desc',
               per_page: 100,
             });
+            let pullRequestsThatNeedDiffs = []; // this will hold a list of PR numbers we need to check for up-to-date diffs on
             for (let i = 0; i < pulls.length; ++i) {
-              let pull = pulls[i];
+              const pull = pulls[i];
               const { data: files } = await github.pulls.listFiles({
                 owner: context.repo.owner,
                 repo: context.repo.repo,
@@ -56,4 +62,72 @@ jobs:
                   labels: newLabels,
                 });
               }
+
+              if (currentLabels.concat(newLabels).find((label) => { return label.startsWith('library/') })) {
+                const commentText = 'Diff for ' + pull.head.sha + ':';
+                const { data: comments } = await github.issues.listComments({
+                  owner: context.repo.owner,
+                  repo: context.repo.repo,
+                  issue_number: pull.number,
+                  sort: 'created',
+                  direction: 'desc',
+                  per_page: 100,
+                });
+                let needNewComment = true;
+                for (let j = 0; j < comments.length; ++j) {
+                  const comment = comments[j];
+                  if (comment.user.login === 'github-actions[bot]') {
+                    if (comment.body.includes(commentText)) {
+                      needNewComment = false;
+                    } else {
+                      await github.issues.deleteComment({
+                        owner: context.repo.owner,
+                        repo: context.repo.repo,
+                        comment_id: comment.id,
+                      });
+                    }
+                  }
+                }
+                if (needNewComment) {
+                  pullRequestsThatNeedDiffs = pullRequestsThatNeedDiffs.concat([{
+                    number: pull.number,
+                    text: commentText,
+                  }]);
+                }
+              }
             }
+            core.setOutput('diffPulls', JSON.stringify({ pulls: pullRequestsThatNeedDiffs, count: pullRequestsThatNeedDiffs.length }));
+      - name: Checkout
+        uses: actions/checkout@v2
+        if: fromJSON(steps.labels.outputs.diffPulls).count > 0
+      - id: diffs
+        name: Generate Diffs
+        run: |
+          pulls="$(
+          jq -c '.pulls[]' <<EOF
+          ${{ steps.labels.outputs.diffPulls }}
+          EOF
+          )"
+          git clone --depth 1 https://github.com/docker-library/bashbrew.git -b master ~/bashbrew
+          ~/bashbrew/bashbrew.sh --version > /dev/null
+          export PATH="$HOME/bashbrew/bin:$PATH"
+          bashbrew --version
+          IFS=$'\n'
+          for pull in $pulls; do
+            number="$(jq -r '.number' <<<"$pull")"
+            text="$(jq -r '.text' <<<"$pull")"
+            diff="$(./diff-pr.sh "$number" 2>&1 || :)"
+            # "Body is too long (maximum is 65536 characters)" (so we'll check for some fudge room and pre-filter the diff)
+            # TODO consider instead creating a Gist (although that requires a separate type of token, so much less interesting)
+            jq -Rcs --arg text "$text" 'rtrimstr("\n") | {
+              body: (
+                "<details>\n<summary>" + $text + "</summary>\n\n```diff\n"
+                + if length < 65000 then . else
+                  "TODO diff too large for GitHub comment!\n"
+                  + "See: http://github.com/" + env.GITHUB_REPOSITORY + "/actions/runs/" + env.GITHUB_RUN_ID
+                end
+                + "\n```\n\n</details>"
+              ),
+            }' <<<"$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"
+          done
+        if: fromJSON(steps.labels.outputs.diffPulls).count > 0

+ 0 - 49
.github/workflows/test-pr.yml

@@ -22,55 +22,6 @@ jobs:
           bashbrew --version
           .github/workflows/naughty.sh
 
-  diff:
-    name: Diff
-    runs-on: ubuntu-latest
-    steps:
-      - uses: actions/checkout@v2
-      - id: diff
-        name: Run ./diff-pr.sh
-        run: |
-          git clone --depth 1 https://github.com/docker-library/bashbrew.git -b master ~/bashbrew
-          ~/bashbrew/bashbrew.sh --version > /dev/null
-          export PATH="$HOME/bashbrew/bin:$PATH"
-          bashbrew --version
-          diff="$(./diff-pr.sh 0)"
-          # "Body is too long (maximum is 65536 characters)" (so we'll check for some fudge room and pre-filter the diff)
-          # TODO consider instead creating a Gist (although that requires a separate type of token, so much less interesting)
-          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")"
-          echo "::set-output name=diff::$diff"
-      - name: Delete Old Comments
-        uses: actions/[email protected]
-        with:
-          script: |
-            const { data: comments } = await github.issues.listComments({
-              owner: context.repo.owner,
-              repo: context.repo.repo,
-              issue_number: context.payload.pull_request.number,
-            })
-            comments.forEach((comment) => {
-              if (comment.user.login === 'github-actions[bot]') {
-                github.issues.deleteComment({
-                  owner: context.repo.owner,
-                  repo: context.repo.repo,
-                  comment_id: comment.id,
-                })
-              }
-            })
-      - name: Create Diff Comment
-        uses: actions/[email protected]
-        with:
-          script: |
-            const data = ${{ steps.diff.outputs.diff }}
-            const body = "<details>\n<summary>Diff for " + context.payload.pull_request.head.sha + ":</summary>\n\n```diff\n" + data.diff + "\n```\n\n</details>"
-            github.issues.createComment({
-              owner: context.repo.owner,
-              repo: context.repo.repo,
-              issue_number: context.payload.pull_request.number,
-              body: body,
-            })
-        if: fromJSON(steps.diff.outputs.diff).length > 0
-
   generate-jobs:
     name: Generate Jobs
     runs-on: ubuntu-latest