瀏覽代碼

Label PRs with failing workflows (#6294)

Albert Johnston 4 年之前
父節點
當前提交
c89e2683e3
共有 2 個文件被更改,包括 57 次插入0 次删除
  1. 11 0
      .github/workflows/build.yml
  2. 46 0
      .github/workflows/label-failing-pr.yml

+ 11 - 0
.github/workflows/build.yml

@@ -4,6 +4,17 @@ jobs:
   setup:
     runs-on: ubuntu-20.04
     steps:
+      # Required for workflow triggers like the auto-label for failing PRs
+      - name: Save PR number
+        if: github.event_name == 'pull_request'
+        run: |
+          mkdir -p ./pr
+          echo ${{ github.event.number }} > ./pr/NR
+      - uses: actions/upload-artifact@v2
+        if: github.event_name == 'pull_request'
+        with:
+          name: pr
+          path: pr/
       # Commit branch/name extraction from:
       # https://github.community/t/accessing-commit-message-in-pull-request-event/17158/8
       #

+ 46 - 0
.github/workflows/label-failing-pr.yml

@@ -0,0 +1,46 @@
+name: Label PR if failed
+on:
+  workflow_run:
+    workflows: [ "build" ]
+    types:
+      - completed
+jobs:
+  apply_label:
+    if: ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'failure' }}
+    runs-on: ubuntu-20.04
+    steps:
+      - name: 'Download artifact'
+        uses: actions/[email protected]
+        with:
+          # scripts lightly modified from https://securitylab.github.com/research/github-actions-preventing-pwn-requests
+          script: |
+            var artifacts = await github.actions.listWorkflowRunArtifacts({
+               owner: context.repo.owner,
+               repo: context.repo.repo,
+               run_id: ${{github.event.workflow_run.id }},
+            });
+            var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
+              return artifact.name == "pr"
+            })[0];
+            var download = await github.actions.downloadArtifact({
+               owner: context.repo.owner,
+               repo: context.repo.repo,
+               artifact_id: matchArtifact.id,
+               archive_format: 'zip',
+            });
+            var fs = require('fs');
+            fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data));
+      - run: unzip pr.zip
+      - name: Label PR
+        uses: actions/github-script@v3
+        with:
+          github-token: ${{ secrets.GITHUB_TOKEN }}
+          script: |
+            var fs = require('fs');
+            var issue_number = Number(fs.readFileSync('./NR'));
+            await github.issues.addLabels({
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              issue_number: issue_number,
+              labels: ['PR: Please Update']
+            });