This repository was archived by the owner on Dec 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
122 lines (107 loc) · 3.74 KB
/
Copy pathdanger-comment.yml
File metadata and controls
122 lines (107 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
name: Danger Comment
on:
workflow_run:
workflows: [Danger]
types: [completed]
workflow_call:
permissions:
actions: read
contents: read
issues: write
pull-requests: write
jobs:
comment:
runs-on: ubuntu-latest
if: |
(github.event_name == 'workflow_run' && github.event.workflow_run.event == 'pull_request')
|| github.event_name == 'workflow_call'
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Download Danger Report (workflow_run)
if: github.event_name == 'workflow_run'
uses: actions/download-artifact@v4
continue-on-error: true
with:
name: danger-report
run-id: ${{ github.event.workflow_run.id }}
repository: ${{ github.event.workflow_run.repository.full_name }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Download Danger Report (reusable call)
if: github.event_name == 'workflow_call'
uses: actions/download-artifact@v4
continue-on-error: true
with:
name: danger-report
- name: Post or Update PR Comment
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const hasItems = (arr) => Array.isArray(arr) && arr.length > 0;
let report;
try {
report = JSON.parse(fs.readFileSync('danger_report.json', 'utf8'));
} catch (e) {
console.log('No danger report found, skipping comment');
return;
}
if (!report.pr_number) {
console.log('No PR number found in report, skipping comment');
return;
}
let body = '## Danger Report\n\n';
if (hasItems(report.errors)) {
body += '### ❌ Errors\n';
report.errors.forEach(e => body += `- ${e}\n`);
body += '\n';
}
if (hasItems(report.warnings)) {
body += '### ⚠️ Warnings\n';
report.warnings.forEach(w => body += `- ${w}\n`);
body += '\n';
}
if (hasItems(report.messages)) {
body += '### ℹ️ Messages\n';
report.messages.forEach(m => body += `- ${m}\n`);
body += '\n';
}
if (hasItems(report.markdowns)) {
report.markdowns.forEach(md => body += `${md}\n\n`);
}
if (!hasItems(report.errors) &&
!hasItems(report.warnings) &&
!hasItems(report.messages) &&
!hasItems(report.markdowns)) {
body += '✅ All checks passed!';
}
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: report.pr_number
});
const botComment = comments.find(c =>
c.user.login === 'github-actions[bot]' &&
c.body.includes('## Danger Report')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: body
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: report.pr_number,
body: body
});
}
// Fail if there are errors
if (hasItems(report.errors)) {
core.setFailed('Danger found errors');
}