danger-pr-comment 0.1.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ac924e732e1c8df10638ac0e603fd82deacd4f0a3f190c40147908bd07fcb9ab
4
- data.tar.gz: 8fe5fdadc336e663248022ea2a5ad150bb7101ed4d9b5a5f24ef8fbed0278509
3
+ metadata.gz: 6fed8e666ce46c9b4f3fdc44a433efca46ba2dcc4112a3ea529fa8a76083abaa
4
+ data.tar.gz: 1557f054587f2864e752a9a2c604d0a49a0e8f85c913828c568a7adae1a7565f
5
5
  SHA512:
6
- metadata.gz: 7a4a9505d2a5659cc08cc7ec60d8a6d5383b539915b041057199c299bbe17bf43a052d83300ebeb88499b72789ec934d36519974a103cedf3b23e6c9e177a029
7
- data.tar.gz: 4c7615bebc46bb91024bd78b628bade8a919247817462ba4ff1a207260fe45f335cfe34fac5a58626ef0c0a70c10a77ffaf377751045fe5a7e49481c61e5c7d3
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,6 +1,7 @@
1
1
  # Danger PR Comment
2
2
 
3
- [![Test](https://github.com/numbata/danger-pr-comment/actions/workflows/test.yml/badge.svg)](https://github.com/numbata/danger-pr-comment/actions/workflows/test.yml)
3
+ [![Test](https://github.com/danger/danger-pr-comment/actions/workflows/test.yml/badge.svg)](https://github.com/danger/danger-pr-comment/actions/workflows/test.yml)
4
+ [![Coverage Status](https://coveralls.io/repos/github/danger/danger-pr-comment/badge.svg?branch=main)](https://coveralls.io/github/danger/danger-pr-comment?branch=main)
4
5
 
5
6
  Reusable GitHub Actions workflows for running Danger and posting a PR comment from a JSON report.
6
7
 
@@ -10,14 +11,17 @@ Reusable GitHub Actions workflows for running Danger and posting a PR comment fr
10
11
  - [Prerequisites](#prerequisites)
11
12
  - [Quick Install](#quick-install)
12
13
  - [Manual Setup](#manual-setup)
14
+ - [Local Preview](#local-preview)
13
15
  - [Implementation Details](#implementation-details)
14
16
  - [JSON Report Output](#json-report-output)
15
17
  - [Shared Dangerfile](#shared-dangerfile)
16
18
  - [Custom at_exit Hook](#custom-at_exit-hook)
19
+ - [Actions Summaries](#actions-summaries)
17
20
  - [Permissions](#permissions)
18
21
  - [Inputs](#inputs)
19
22
  - [danger-run.yml](#danger-runyml)
20
23
  - [danger-comment.yml](#danger-commentyml)
24
+ - [Development](#development)
21
25
  - [License](#license)
22
26
 
23
27
  ## Usage
@@ -57,19 +61,19 @@ Could not find the type of CI for Danger to run on.
57
61
  From your repository root:
58
62
 
59
63
  ```bash
60
- curl -fsSL https://raw.githubusercontent.com/numbata/danger-pr-comment/main/scripts/install-workflows.sh | bash
64
+ curl -fsSL https://raw.githubusercontent.com/danger/danger-pr-comment/main/scripts/install-workflows.sh | bash
61
65
  ```
62
66
 
63
67
  Use `--force` to overwrite existing workflow files `.github/workflows/danger.yml` and `.github/workflows/danger-comment.yml`:
64
68
 
65
69
  ```bash
66
- curl -fsSL https://raw.githubusercontent.com/numbata/danger-pr-comment/main/scripts/install-workflows.sh | bash -s -- --force
70
+ curl -fsSL https://raw.githubusercontent.com/danger/danger-pr-comment/main/scripts/install-workflows.sh | bash -s -- --force
67
71
  ```
68
72
 
69
73
  To target a specific directory:
70
74
 
71
75
  ```bash
72
- curl -fsSL https://raw.githubusercontent.com/numbata/danger-pr-comment/main/scripts/install-workflows.sh | bash -s -- --root /path/to/repo
76
+ curl -fsSL https://raw.githubusercontent.com/danger/danger-pr-comment/main/scripts/install-workflows.sh | bash -s -- --root /path/to/repo
73
77
  ```
74
78
 
75
79
  ### Manual Setup
@@ -84,7 +88,7 @@ on:
84
88
 
85
89
  jobs:
86
90
  danger:
87
- uses: numbata/danger-pr-comment/.github/workflows/danger-run.yml@v0.1.1
91
+ uses: danger/danger-pr-comment/.github/workflows/danger-run.yml@v0.3.0
88
92
  secrets: inherit
89
93
  with:
90
94
  ruby-version: '3.4'
@@ -107,17 +111,39 @@ permissions:
107
111
 
108
112
  jobs:
109
113
  comment:
110
- uses: numbata/danger-pr-comment/.github/workflows/danger-comment.yml@v0.1.1
114
+ uses: danger/danger-pr-comment/.github/workflows/danger-comment.yml@v0.3.0
111
115
  secrets: inherit
112
116
  ```
113
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
+
114
132
  ## Implementation Details
115
133
 
116
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.
117
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
+
118
144
  ### JSON Report Output
119
145
 
120
- 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`.
121
147
 
122
148
  #### Shared Dangerfile
123
149
 
@@ -132,7 +158,7 @@ See [Dangerfile](Dangerfile) for implementation details.
132
158
 
133
159
  ```ruby
134
160
  # Dangerfile
135
- require 'json'
161
+ require 'danger-pr-comment'
136
162
  require 'English'
137
163
 
138
164
  dangerfile_instance = self if defined?(Danger::Dangerfile) && is_a?(Danger::Dangerfile)
@@ -140,30 +166,18 @@ at_exit do
140
166
  next if $ERROR_INFO && !$ERROR_INFO.is_a?(SystemExit)
141
167
  next unless dangerfile_instance
142
168
 
143
- report_path = ENV.fetch('DANGER_REPORT_PATH', nil)
144
- event_path = ENV.fetch('GITHUB_EVENT_PATH', nil)
145
- next unless report_path && event_path && File.exist?(event_path)
146
-
147
- event = JSON.parse(File.read(event_path))
148
- pr_number = event.dig('pull_request', 'number')
149
- next unless pr_number
150
-
151
- to_messages = lambda do |items|
152
- Array(items).map { |item| item.respond_to?(:message) ? item.message : item.to_s }
153
- end
154
-
155
- report = {
156
- pr_number: pr_number,
157
- errors: to_messages.call(dangerfile_instance.status_report[:errors]),
158
- warnings: to_messages.call(dangerfile_instance.status_report[:warnings]),
159
- messages: to_messages.call(dangerfile_instance.status_report[:messages]),
160
- markdowns: to_messages.call(dangerfile_instance.status_report[:markdowns])
161
- }
162
-
163
- 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
+ )
164
174
  end
165
175
  ```
166
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
+
167
181
  ### Permissions
168
182
 
169
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).
@@ -173,6 +187,7 @@ The `Danger Comment` workflow requires explicit permissions. Reusable workflows
173
187
  #### `danger-run.yml`
174
188
 
175
189
  - `ruby-version`: Ruby version for `ruby/setup-ruby`. Leave empty to use `.ruby-version`/`.tool-versions`.
190
+ - `gemfile`: Gemfile path for Bundler (default `Gemfile`), for example `gemfiles/danger.gemfile`.
176
191
  - `bundler-cache`: Enable Bundler caching (default `true`).
177
192
  - `danger-args`: Arguments passed to `bundle exec danger` (default `dry_run`).
178
193
  - `report-artifact-name`: Artifact name for the report (default `danger-report`).
@@ -184,6 +199,16 @@ The `Danger Comment` workflow requires explicit permissions. Reusable workflows
184
199
  - `report-file`: Report filename inside the artifact (default `danger-report.json`).
185
200
  - `comment-title`: Heading for the PR comment (default `Danger Report`).
186
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.
187
212
 
188
213
  ## License
189
214
 
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'danger-pr-comment/cli'
5
+
6
+ exit DangerPrComment::CLI.run(ARGV)
@@ -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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DangerPrComment
4
- VERSION = '0.1.1'
4
+ VERSION = '0.3.0'
5
5
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'danger-pr-comment/renderer'
3
4
  require 'danger-pr-comment/reporter'
4
5
  require 'danger-pr-comment/version'
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.1.1
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-10-21 00:00:00.000000000 Z
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/numbata/danger-pr-comment
45
+ homepage: https://github.com/danger/danger-pr-comment
42
46
  licenses:
43
47
  - MIT
44
48
  metadata: