Skip to content

Commit acb1640

Browse files
committed
Remove API token requirement from Danger workflow
1 parent baab208 commit acb1640

File tree

3 files changed

+138
-17
lines changed

3 files changed

+138
-17
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const fs = require('fs');
2+
const core = require('@actions/core');
3+
4+
module.exports = async ({ github, context }) => {
5+
const hasItems = (arr) => Array.isArray(arr) && arr.length > 0;
6+
7+
let report;
8+
try {
9+
report = JSON.parse(fs.readFileSync('danger_report.json', 'utf8'));
10+
} catch (e) {
11+
console.log('No danger report found, skipping comment');
12+
return;
13+
}
14+
15+
if (!report.pr_number) {
16+
console.log('No PR number found in report, skipping comment');
17+
return;
18+
}
19+
20+
let body = '## Danger Report\n\n';
21+
22+
if (hasItems(report.errors)) {
23+
body += '### ❌ Errors\n';
24+
report.errors.forEach(e => body += `- ${e}\n`);
25+
body += '\n';
26+
}
27+
28+
if (hasItems(report.warnings)) {
29+
body += '### ⚠️ Warnings\n';
30+
report.warnings.forEach(w => body += `- ${w}\n`);
31+
body += '\n';
32+
}
33+
34+
if (hasItems(report.messages)) {
35+
body += '### ℹ️ Messages\n';
36+
report.messages.forEach(m => body += `- ${m}\n`);
37+
body += '\n';
38+
}
39+
40+
if (hasItems(report.markdowns)) {
41+
report.markdowns.forEach(md => body += `${md}\n\n`);
42+
}
43+
44+
if (!hasItems(report.errors) &&
45+
!hasItems(report.warnings) &&
46+
!hasItems(report.messages) &&
47+
!hasItems(report.markdowns)) {
48+
body += '✅ All checks passed!';
49+
}
50+
51+
const { data: comments } = await github.rest.issues.listComments({
52+
owner: context.repo.owner,
53+
repo: context.repo.repo,
54+
issue_number: report.pr_number
55+
});
56+
57+
const botComment = comments.find(c =>
58+
c.user.login === 'github-actions[bot]' &&
59+
c.body.includes('## Danger Report')
60+
);
61+
62+
if (botComment) {
63+
await github.rest.issues.updateComment({
64+
owner: context.repo.owner,
65+
repo: context.repo.repo,
66+
comment_id: botComment.id,
67+
body: body
68+
});
69+
} else {
70+
await github.rest.issues.createComment({
71+
owner: context.repo.owner,
72+
repo: context.repo.repo,
73+
issue_number: report.pr_number,
74+
body: body
75+
});
76+
}
77+
78+
// Fail if there are errors
79+
if (report.errors && report.errors.length > 0) {
80+
core.setFailed('Danger found errors');
81+
}
82+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Danger Comment
2+
on:
3+
workflow_run:
4+
workflows: [Danger]
5+
types: [completed]
6+
7+
permissions:
8+
actions: read
9+
contents: read
10+
issues: write
11+
pull-requests: write
12+
13+
jobs:
14+
comment:
15+
runs-on: ubuntu-latest
16+
if: github.event.workflow_run.event == 'pull_request'
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 1
22+
- name: Download Danger Report
23+
uses: actions/download-artifact@v4
24+
with:
25+
name: danger-report
26+
run-id: ${{ github.event.workflow_run.id }}
27+
- name: Post or Update PR Comment
28+
uses: actions/github-script@v7
29+
with:
30+
script-file: .github/scripts/post-danger-comment.js

.github/workflows/danger.yml

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1-
---
2-
name: danger
3-
on: pull_request
4-
1+
name: Danger
2+
on:
3+
pull_request:
4+
types: [ opened, reopened, edited, synchronize ]
55
jobs:
66
danger:
7+
name: Danger
78
runs-on: ubuntu-latest
89
steps:
9-
- uses: actions/checkout@v4
10-
with:
11-
fetch-depth: 100
12-
- name: Set up Ruby
13-
uses: ruby/setup-ruby@v1
14-
with:
15-
ruby-version: 3.4
16-
bundler-cache: true
17-
- name: Run Danger
18-
run: |
19-
# the token is public, has public_repo scope and belongs to the grape-bot user owned by @dblock, this is ok
20-
TOKEN=$(echo -n Z2hwX2lYb0dPNXNyejYzOFJyaTV3QUxUdkNiS1dtblFwZTFuRXpmMwo= | base64 --decode)
21-
DANGER_GITHUB_API_TOKEN=$TOKEN bundle exec danger --verbose
10+
- name: Checkout
11+
uses: actions/checkout@v3
12+
with:
13+
fetch-depth: 0
14+
- name: Set up Ruby
15+
uses: ruby/setup-ruby@v1
16+
with:
17+
ruby-version: 2.7
18+
bundler-cache: true
19+
- name: Run Danger
20+
run: bundle exec danger dry_run --verbose
21+
env:
22+
DANGER_REPORT_PATH: danger_report.json
23+
- name: Upload Danger Report
24+
if: always()
25+
uses: actions/upload-artifact@v4
26+
with:
27+
name: danger-report
28+
path: danger_report.json
29+
retention-days: 1
30+
if-no-files-found: ignore

0 commit comments

Comments
 (0)