Skip to content

Commit

Permalink
Merge pull request #134 from DroidKaigi/takahirom/add-detekt-comment-…
Browse files Browse the repository at this point in the history
…workflow/2024-07-15

Use artifact to pass PR number for detekt comment
  • Loading branch information
takahirom authored Jul 15, 2024
2 parents cdbc315 + d6fdc60 commit cc3defe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
30 changes: 22 additions & 8 deletions .github/workflows/detekt-comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ on:
- completed

permissions:
actions: read
pull-requests: write

jobs:
Expand All @@ -18,27 +17,42 @@ jobs:
pull-requests: write

steps:
- name: Get PR number
- name: Download PR event artifact
uses: actions/github-script@v6
id: get-pr
with:
script: |
const run = await github.rest.actions.getWorkflowRun({
const fs = require('fs')
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{ github.event.workflow_run.id }}
});
return run.data.pull_requests[0].number;
result-encoding: string
})
const matchArtifact = artifacts.data.artifacts.find(artifact => artifact.name === "pr")
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip'
})
fs.writeFileSync('pr.zip', Buffer.from(download.data))
- run: unzip pr.zip

- name: Get PR number
id: pr
run: |
PR_NUMBER=$(jq -r '.number' event.json)
echo "number=$PR_NUMBER" >> $GITHUB_OUTPUT
- name: Comment PR
uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.rest.issues.createComment({
issue_number: ${{ steps.get-pr.outputs.result }},
issue_number: ${{ steps.pr.outputs.number }},
owner: context.repo.owner,
repo: context.repo.repo,
body: "Detekt check failed. Please run `./gradlew detekt --auto-correct` to fix the issues."
})
12 changes: 12 additions & 0 deletions .github/workflows/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ jobs:
- name: Run detekt
id: detekt
run: ./gradlew detekt --stacktrace

- name: Save PR event
if: failure()
run: |
mkdir -p ./pr
echo '${{ toJson(github.event) }}' > ./pr/event.json
- uses: actions/upload-artifact@v3
if: failure()
with:
name: pr
path: pr/

0 comments on commit cc3defe

Please sign in to comment.