danger-pr-comment 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Dangerfile +2 -1
- data/README.md +53 -30
- data/bin/danger-pr-comment +6 -0
- data/lib/danger-pr-comment/cli.rb +33 -0
- data/lib/danger-pr-comment/renderer.rb +36 -0
- data/lib/danger-pr-comment/reporter.rb +11 -2
- data/lib/danger-pr-comment/version.rb +1 -1
- data/lib/danger-pr-comment.rb +1 -0
- metadata +8 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6fed8e666ce46c9b4f3fdc44a433efca46ba2dcc4112a3ea529fa8a76083abaa
|
|
4
|
+
data.tar.gz: 1557f054587f2864e752a9a2c604d0a49a0e8f85c913828c568a7adae1a7565f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9e0522910d60ef0a804d0faea57feeb5d8c3ed8a9c2c528cdec5791e7fb4e783d7112d5c59686abcc805a3687c6bdbbdefd6393efec78eb72de353a4f6ee206d
|
|
7
|
+
data.tar.gz: 45d1f8319763a2752510251c59f07b7d1ac8c24dc4fb3356b82a59639d08ab8d884f4ec010809e99e1330d47776be596a203830627442e8f7865a30fe2ab7f21
|
data/Dangerfile
CHANGED
|
@@ -13,6 +13,7 @@ at_exit do
|
|
|
13
13
|
reporter = DangerPrComment::Reporter.new(dangerfile_instance.status_report)
|
|
14
14
|
reporter.export_json(
|
|
15
15
|
ENV.fetch('DANGER_REPORT_PATH', nil),
|
|
16
|
-
ENV.fetch('GITHUB_EVENT_PATH', nil)
|
|
16
|
+
ENV.fetch('GITHUB_EVENT_PATH', nil),
|
|
17
|
+
summary_path: ENV.fetch('GITHUB_STEP_SUMMARY', nil)
|
|
17
18
|
)
|
|
18
19
|
end
|
data/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Danger PR Comment
|
|
2
2
|
|
|
3
|
-
[](https://github.com/danger/danger-pr-comment/actions/workflows/test.yml)
|
|
4
|
+
[](https://coveralls.io/github/danger/danger-pr-comment?branch=main)
|
|
5
5
|
|
|
6
6
|
Reusable GitHub Actions workflows for running Danger and posting a PR comment from a JSON report.
|
|
7
7
|
|
|
@@ -11,14 +11,17 @@ Reusable GitHub Actions workflows for running Danger and posting a PR comment fr
|
|
|
11
11
|
- [Prerequisites](#prerequisites)
|
|
12
12
|
- [Quick Install](#quick-install)
|
|
13
13
|
- [Manual Setup](#manual-setup)
|
|
14
|
+
- [Local Preview](#local-preview)
|
|
14
15
|
- [Implementation Details](#implementation-details)
|
|
15
16
|
- [JSON Report Output](#json-report-output)
|
|
16
17
|
- [Shared Dangerfile](#shared-dangerfile)
|
|
17
18
|
- [Custom at_exit Hook](#custom-at_exit-hook)
|
|
19
|
+
- [Actions Summaries](#actions-summaries)
|
|
18
20
|
- [Permissions](#permissions)
|
|
19
21
|
- [Inputs](#inputs)
|
|
20
22
|
- [danger-run.yml](#danger-runyml)
|
|
21
23
|
- [danger-comment.yml](#danger-commentyml)
|
|
24
|
+
- [Development](#development)
|
|
22
25
|
- [License](#license)
|
|
23
26
|
|
|
24
27
|
## Usage
|
|
@@ -58,19 +61,19 @@ Could not find the type of CI for Danger to run on.
|
|
|
58
61
|
From your repository root:
|
|
59
62
|
|
|
60
63
|
```bash
|
|
61
|
-
curl -fsSL https://raw.githubusercontent.com/
|
|
64
|
+
curl -fsSL https://raw.githubusercontent.com/danger/danger-pr-comment/main/scripts/install-workflows.sh | bash
|
|
62
65
|
```
|
|
63
66
|
|
|
64
67
|
Use `--force` to overwrite existing workflow files `.github/workflows/danger.yml` and `.github/workflows/danger-comment.yml`:
|
|
65
68
|
|
|
66
69
|
```bash
|
|
67
|
-
curl -fsSL https://raw.githubusercontent.com/
|
|
70
|
+
curl -fsSL https://raw.githubusercontent.com/danger/danger-pr-comment/main/scripts/install-workflows.sh | bash -s -- --force
|
|
68
71
|
```
|
|
69
72
|
|
|
70
73
|
To target a specific directory:
|
|
71
74
|
|
|
72
75
|
```bash
|
|
73
|
-
curl -fsSL https://raw.githubusercontent.com/
|
|
76
|
+
curl -fsSL https://raw.githubusercontent.com/danger/danger-pr-comment/main/scripts/install-workflows.sh | bash -s -- --root /path/to/repo
|
|
74
77
|
```
|
|
75
78
|
|
|
76
79
|
### Manual Setup
|
|
@@ -85,7 +88,7 @@ on:
|
|
|
85
88
|
|
|
86
89
|
jobs:
|
|
87
90
|
danger:
|
|
88
|
-
uses:
|
|
91
|
+
uses: danger/danger-pr-comment/.github/workflows/danger-run.yml@v0.3.0
|
|
89
92
|
secrets: inherit
|
|
90
93
|
with:
|
|
91
94
|
ruby-version: '3.4'
|
|
@@ -108,17 +111,39 @@ permissions:
|
|
|
108
111
|
|
|
109
112
|
jobs:
|
|
110
113
|
comment:
|
|
111
|
-
uses:
|
|
114
|
+
uses: danger/danger-pr-comment/.github/workflows/danger-comment.yml@v0.3.0
|
|
112
115
|
secrets: inherit
|
|
113
116
|
```
|
|
114
117
|
|
|
118
|
+
### Local Preview
|
|
119
|
+
|
|
120
|
+
Render an existing JSON report as Markdown without running Danger or posting a comment:
|
|
121
|
+
|
|
122
|
+
```sh
|
|
123
|
+
bundle exec danger-pr-comment render danger-report.json
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Redirect stdout to save the preview, for example `bundle exec danger-pr-comment render danger-report.json > danger-report.md`. The renderer exits successfully even if the report contains findings; unreadable files, invalid JSON, and invalid report roots exit nonzero with a diagnostic on stderr.
|
|
127
|
+
|
|
128
|
+
The gem packages the executable from [`bin/danger-pr-comment`](bin/danger-pr-comment).
|
|
129
|
+
|
|
130
|
+
The shared Ruby renderer formats errors, warnings, messages, and raw Markdown. Reports exported by the gem include this content in `rendered_markdown`, so local previews, Actions summaries, and PR comments use the same report body. When `rendered_markdown` is a string, it overrides the legacy arrays for both the comment body and the empty-report decision. The commenter still supplies its own marker, title, and separately spaced workflow-run link, and retains legacy rendering for reports without a string `rendered_markdown`. It never executes the report or checks out pull request code.
|
|
131
|
+
|
|
115
132
|
## Implementation Details
|
|
116
133
|
|
|
117
134
|
Using danger-pr-comment solves the problem of needing special permissions to post a PR comment from contributions from forks. This is implemented by producing a JSON report during the PR, and reading the report in a separate workflow.
|
|
118
135
|
|
|
136
|
+
Before commenting, the workflow looks for one open PR matching the run's source repository, branch, and commit, then checks its current target. This reduces misdirected comments but does not prove which PR originally triggered the run. See [Security](SECURITY.md) for the worst-case risks, detailed checks, and stacked-PR setup.
|
|
137
|
+
|
|
138
|
+
Comments are sticky: the commenter searches every page of PR comments and updates the first comment authored by `github-actions[bot]` whose body starts with the configured marker on its own line. Comments from people or other bots, quoted markers, and other report markers are left untouched. If no matching comment exists, it creates one.
|
|
139
|
+
|
|
140
|
+
Writers sharing the caller repository, source repository ID, head branch, and comment marker are serialized without cancelling an in-progress writer. This prevents simultaneous runs for the same PR source from both creating a comment, including for fork contributions. Different markers remain independent.
|
|
141
|
+
|
|
142
|
+
By default, the commenter posts even when the report is empty. Set `comment-empty: false` in the commenter job's `with` inputs to avoid creating comments for empty reports. The effective report body is empty when it is blank/whitespace or, after trimming whitespace, exactly the renderer's `No issues found.` message; this is independent of whether the Danger command succeeded. For legacy reports, the body comes from the errors, warnings, messages, and markdown arrays. Existing report comments are still updated to "No issues found" when findings clear, including when a Markdown override is blank, so previous results do not remain stale.
|
|
143
|
+
|
|
119
144
|
### JSON Report Output
|
|
120
145
|
|
|
121
|
-
Your Dangerfile must write a JSON report to `ENV['DANGER_REPORT_PATH']`.
|
|
146
|
+
Your Dangerfile must write a JSON report to `ENV['DANGER_REPORT_PATH']`. The shared exporter preserves the existing `pr_number`, `errors`, `warnings`, `messages`, and `markdowns` fields and adds `rendered_markdown`.
|
|
122
147
|
|
|
123
148
|
#### Shared Dangerfile
|
|
124
149
|
|
|
@@ -133,7 +158,7 @@ See [Dangerfile](Dangerfile) for implementation details.
|
|
|
133
158
|
|
|
134
159
|
```ruby
|
|
135
160
|
# Dangerfile
|
|
136
|
-
require '
|
|
161
|
+
require 'danger-pr-comment'
|
|
137
162
|
require 'English'
|
|
138
163
|
|
|
139
164
|
dangerfile_instance = self if defined?(Danger::Dangerfile) && is_a?(Danger::Dangerfile)
|
|
@@ -141,30 +166,18 @@ at_exit do
|
|
|
141
166
|
next if $ERROR_INFO && !$ERROR_INFO.is_a?(SystemExit)
|
|
142
167
|
next unless dangerfile_instance
|
|
143
168
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
pr_number = event.dig('pull_request', 'number')
|
|
150
|
-
next unless pr_number
|
|
151
|
-
|
|
152
|
-
to_messages = lambda do |items|
|
|
153
|
-
Array(items).map { |item| item.respond_to?(:message) ? item.message : item.to_s }
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
report = {
|
|
157
|
-
pr_number: pr_number,
|
|
158
|
-
errors: to_messages.call(dangerfile_instance.status_report[:errors]),
|
|
159
|
-
warnings: to_messages.call(dangerfile_instance.status_report[:warnings]),
|
|
160
|
-
messages: to_messages.call(dangerfile_instance.status_report[:messages]),
|
|
161
|
-
markdowns: to_messages.call(dangerfile_instance.status_report[:markdowns])
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
File.write(report_path, JSON.pretty_generate(report))
|
|
169
|
+
DangerPrComment::Reporter.new(dangerfile_instance.status_report).export_json(
|
|
170
|
+
ENV.fetch('DANGER_REPORT_PATH', nil),
|
|
171
|
+
ENV.fetch('GITHUB_EVENT_PATH', nil),
|
|
172
|
+
summary_path: ENV.fetch('GITHUB_STEP_SUMMARY', nil)
|
|
173
|
+
)
|
|
165
174
|
end
|
|
166
175
|
```
|
|
167
176
|
|
|
177
|
+
### Actions Summaries
|
|
178
|
+
|
|
179
|
+
When GitHub provides `GITHUB_STEP_SUMMARY`, the shared Dangerfile appends the rendered report under a "Danger Report" heading on the workflow run's Summary page. Earlier summary content is preserved, and empty reports show "No issues found." This runs in the report-producing job, including when Danger exits with reported errors, and does not depend on the commenting workflow or require additional write permissions. Outside Actions, the summary is simply omitted unless a summary path is explicitly supplied.
|
|
180
|
+
|
|
168
181
|
### Permissions
|
|
169
182
|
|
|
170
183
|
The `Danger Comment` workflow requires explicit permissions. Reusable workflows cannot grant permissions to their callers. Required: `actions: read` (download artifacts from the Danger run), `issues: write` and `pull-requests: write` (create/update PR comments).
|
|
@@ -186,6 +199,16 @@ The `Danger Comment` workflow requires explicit permissions. Reusable workflows
|
|
|
186
199
|
- `report-file`: Report filename inside the artifact (default `danger-report.json`).
|
|
187
200
|
- `comment-title`: Heading for the PR comment (default `Danger Report`).
|
|
188
201
|
- `comment-marker`: Marker string used to update the comment (default `<!-- danger-report -->`).
|
|
202
|
+
- `comment-empty`: Create comments for empty reports (boolean, default `true`). Set to `false` to skip creating empty-report comments; existing report comments are always updated.
|
|
203
|
+
- `base-branch`: Allowed current PR target (default: repository default branch). A literal value is a hard restriction; `"*"` explicitly allows changing stacked targets within the caller repository. Configure this in the trusted caller, not from report data. See [Security](SECURITY.md#allowed-target-branches) for details.
|
|
204
|
+
|
|
205
|
+
## Development
|
|
206
|
+
|
|
207
|
+
Run `bundle exec rake` for the tests and Ruby lint.
|
|
208
|
+
|
|
209
|
+
Installed-gem consumer scenarios live in [`spec/fixtures/consumer`](spec/fixtures/consumer). Each folder contains its own `Dangerfile`, `event.json`, `expectations.yml`, and any supporting assets. Expectations describe the Danger exit status, exported JSON, preview output, summary, and comment operation/body. Optional inputs cover missing summaries, existing comments, and legacy or pre-rendered reports. Add a folder to extend coverage without changing the Ruby test runner.
|
|
210
|
+
|
|
211
|
+
The integration tests build and install the real gem, resolve a separate consumer bundle offline, and run the actual Danger and preview executables. Dependency gem bindirs are added explicitly so vendored CI bundles do not depend on globally installed executables. Comment writes are stubbed.
|
|
189
212
|
|
|
190
213
|
## License
|
|
191
214
|
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'danger-pr-comment/renderer'
|
|
5
|
+
|
|
6
|
+
module DangerPrComment
|
|
7
|
+
class CLI
|
|
8
|
+
USAGE = 'Usage: danger-pr-comment render REPORT.json'
|
|
9
|
+
|
|
10
|
+
def self.run(argv, out: $stdout, err: $stderr)
|
|
11
|
+
return usage(out) if [%w[-h], %w[--help]].include?(argv)
|
|
12
|
+
|
|
13
|
+
render(argv, out, err)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.render(argv, out, err)
|
|
17
|
+
return usage(err, 1) unless argv.length == 2 && argv.first == 'render'
|
|
18
|
+
|
|
19
|
+
out.write(Renderer.new(JSON.parse(File.read(argv.last))).render)
|
|
20
|
+
0
|
|
21
|
+
rescue JSON::ParserError, ArgumentError, SystemCallError => e
|
|
22
|
+
err.puts("danger-pr-comment: #{e.message}")
|
|
23
|
+
1
|
|
24
|
+
end
|
|
25
|
+
private_class_method :render
|
|
26
|
+
|
|
27
|
+
def self.usage(out, status = 0)
|
|
28
|
+
out.puts(USAGE)
|
|
29
|
+
status
|
|
30
|
+
end
|
|
31
|
+
private_class_method :usage
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DangerPrComment
|
|
4
|
+
class Renderer
|
|
5
|
+
LIST_SECTIONS = { 'errors' => 'Errors', 'warnings' => 'Warnings', 'messages' => 'Messages' }.freeze
|
|
6
|
+
|
|
7
|
+
def initialize(report)
|
|
8
|
+
raise ArgumentError, 'Danger report must be a JSON object' unless report.is_a?(Hash)
|
|
9
|
+
|
|
10
|
+
@report = report.transform_keys(&:to_s)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def render
|
|
14
|
+
sections = list_sections
|
|
15
|
+
markdowns = items('markdowns')
|
|
16
|
+
sections << "### Markdowns\n#{markdowns.join("\n\n")}" unless markdowns.empty?
|
|
17
|
+
"#{sections.empty? ? 'No issues found.' : sections.join("\n\n")}\n"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def list_sections
|
|
23
|
+
LIST_SECTIONS.filter_map do |key, title|
|
|
24
|
+
messages = items(key)
|
|
25
|
+
next if messages.empty?
|
|
26
|
+
|
|
27
|
+
"### #{title}\n#{messages.map { |message| "- #{message}" }.join("\n")}"
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def items(key)
|
|
32
|
+
value = @report[key]
|
|
33
|
+
value.is_a?(Array) ? value : []
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'json'
|
|
4
|
+
require 'danger-pr-comment/renderer'
|
|
4
5
|
|
|
5
6
|
module DangerPrComment
|
|
6
7
|
class Reporter
|
|
@@ -8,7 +9,7 @@ module DangerPrComment
|
|
|
8
9
|
@status_report = status_report
|
|
9
10
|
end
|
|
10
11
|
|
|
11
|
-
def export_json(report_path, event_path)
|
|
12
|
+
def export_json(report_path, event_path, summary_path: nil)
|
|
12
13
|
return unless report_path && event_path && File.exist?(event_path)
|
|
13
14
|
|
|
14
15
|
event = JSON.parse(File.read(event_path))
|
|
@@ -17,18 +18,26 @@ module DangerPrComment
|
|
|
17
18
|
|
|
18
19
|
report = build_report(pr_number)
|
|
19
20
|
File.write(report_path, JSON.pretty_generate(report))
|
|
21
|
+
export_summary(summary_path, report[:rendered_markdown])
|
|
20
22
|
end
|
|
21
23
|
|
|
22
24
|
private
|
|
23
25
|
|
|
24
26
|
def build_report(pr_number)
|
|
25
|
-
{
|
|
27
|
+
report = {
|
|
26
28
|
pr_number: pr_number,
|
|
27
29
|
errors: to_messages(@status_report[:errors]),
|
|
28
30
|
warnings: to_messages(@status_report[:warnings]),
|
|
29
31
|
messages: to_messages(@status_report[:messages]),
|
|
30
32
|
markdowns: to_messages(@status_report[:markdowns])
|
|
31
33
|
}
|
|
34
|
+
report.merge(rendered_markdown: Renderer.new(report).render)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def export_summary(path, markdown)
|
|
38
|
+
return unless path
|
|
39
|
+
|
|
40
|
+
File.open(path, 'a') { |file| file.write("## Danger Report\n\n#{markdown}\n") }
|
|
32
41
|
end
|
|
33
42
|
|
|
34
43
|
def to_messages(items)
|
data/lib/danger-pr-comment.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: danger-pr-comment
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andrei Subbota
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-18 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: danger
|
|
@@ -28,17 +28,21 @@ description: Shared Dangerfile that exports a JSON report for posting Danger res
|
|
|
28
28
|
as a PR comment.
|
|
29
29
|
email:
|
|
30
30
|
- numbata@users.noreply.github.com
|
|
31
|
-
executables:
|
|
31
|
+
executables:
|
|
32
|
+
- danger-pr-comment
|
|
32
33
|
extensions: []
|
|
33
34
|
extra_rdoc_files: []
|
|
34
35
|
files:
|
|
35
36
|
- Dangerfile
|
|
36
37
|
- LICENSE.txt
|
|
37
38
|
- README.md
|
|
39
|
+
- bin/danger-pr-comment
|
|
38
40
|
- lib/danger-pr-comment.rb
|
|
41
|
+
- lib/danger-pr-comment/cli.rb
|
|
42
|
+
- lib/danger-pr-comment/renderer.rb
|
|
39
43
|
- lib/danger-pr-comment/reporter.rb
|
|
40
44
|
- lib/danger-pr-comment/version.rb
|
|
41
|
-
homepage: https://github.com/
|
|
45
|
+
homepage: https://github.com/danger/danger-pr-comment
|
|
42
46
|
licenses:
|
|
43
47
|
- MIT
|
|
44
48
|
metadata:
|