Reusable GitHub Actions workflows for running Danger and posting a PR comment from a JSON report.
Setup Danger in your repository.
For example, the following Gemfile and Dangerfile install danger with the danger-changelog plugin.
group :development, :test do
gem 'danger'
gem 'danger-changelog'
gem 'danger-pr-comment', require: false
end# frozen_string_literal: true
danger.import_dangerfile(gem: 'danger-pr-comment')
changelog.check!
Run bundle install and bundle exec danger to make sure it works.
bundle exec danger
Could not find the type of CI for Danger to run on.From your repository root:
curl -fsSL https://raw.githubusercontent.com/danger/danger-pr-comment/main/scripts/install-workflows.sh | bashUse --force to overwrite existing workflow files .github/workflows/danger.yml and .github/workflows/danger-comment.yml:
curl -fsSL https://raw.githubusercontent.com/danger/danger-pr-comment/main/scripts/install-workflows.sh | bash -s -- --forceTo target a specific directory:
curl -fsSL https://raw.githubusercontent.com/danger/danger-pr-comment/main/scripts/install-workflows.sh | bash -s -- --root /path/to/repoCreate .github/workflows/danger.yml in your repository:
name: Danger
on:
pull_request:
types: [opened, reopened, edited, synchronize]
jobs:
danger:
uses: danger/danger-pr-comment/.github/workflows/danger-run.yml@v0.2.0
secrets: inherit
with:
ruby-version: '3.4'
bundler-cache: trueCreate .github/workflows/danger-comment.yml in your repository:
name: Danger Comment
on:
workflow_run:
workflows: [Danger]
types: [completed]
permissions:
actions: read # download artifacts
issues: write # list + create/update comments
pull-requests: write # PR comment access
jobs:
comment:
uses: danger/danger-pr-comment/.github/workflows/danger-comment.yml@v0.2.0
secrets: inheritUsing 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.
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.
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. GitHub concurrency does not guarantee execution order; this is not a newest-report selection policy.
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. An empty report has no errors, warnings, messages, or markdown; this is independent of whether the Danger command succeeded. Existing report comments are still updated to "No issues found" when findings clear, so previous results do not remain stale.
Your Dangerfile must write a JSON report to ENV['DANGER_REPORT_PATH'].
# Import danger-pr-comment for automatic danger report export to JSON
danger.import_dangerfile(gem: 'danger-pr-comment')See Dangerfile for implementation details.
# Dangerfile
require 'json'
require 'English'
dangerfile_instance = self if defined?(Danger::Dangerfile) && is_a?(Danger::Dangerfile)
at_exit do
next if $ERROR_INFO && !$ERROR_INFO.is_a?(SystemExit)
next unless dangerfile_instance
report_path = ENV.fetch('DANGER_REPORT_PATH', nil)
event_path = ENV.fetch('GITHUB_EVENT_PATH', nil)
next unless report_path && event_path && File.exist?(event_path)
event = JSON.parse(File.read(event_path))
pr_number = event.dig('pull_request', 'number')
next unless pr_number
to_messages = lambda do |items|
Array(items).map { |item| item.respond_to?(:message) ? item.message : item.to_s }
end
report = {
pr_number: pr_number,
errors: to_messages.call(dangerfile_instance.status_report[:errors]),
warnings: to_messages.call(dangerfile_instance.status_report[:warnings]),
messages: to_messages.call(dangerfile_instance.status_report[:messages]),
markdowns: to_messages.call(dangerfile_instance.status_report[:markdowns])
}
File.write(report_path, JSON.pretty_generate(report))
endThe 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).
ruby-version: Ruby version forruby/setup-ruby. Leave empty to use.ruby-version/.tool-versions.gemfile: Gemfile path for Bundler (defaultGemfile), for examplegemfiles/danger.gemfile.bundler-cache: Enable Bundler caching (defaulttrue).danger-args: Arguments passed tobundle exec danger(defaultdry_run).report-artifact-name: Artifact name for the report (defaultdanger-report).report-file: Report filename (defaultdanger-report.json).
report-artifact-name: Artifact name to download (defaultdanger-report).report-file: Report filename inside the artifact (defaultdanger-report.json).comment-title: Heading for the PR comment (defaultDanger Report).comment-marker: Marker string used to update the comment (default<!-- danger-report -->).comment-empty: Create comments for empty reports (boolean, defaulttrue). Set tofalseto skip creating empty-report comments; existing report comments are always updated.
MIT License. See LICENSE for details.