Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use artifact to pass PR number for detekt comment #134

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/
Loading