Class: Danger::LabelsChecker

Inherits:
Plugin
  • Object
show all
Defined in:
lib/dangermattic/plugins/labels_checker.rb

Overview

Plugin for checking labels associated with a pull request.

Examples:

Checking for specific labels and generating warnings/errors:

labels_checker.check(
  do_not_merge_labels: ['Do Not Merge'],
  required_labels: ['Bug', 'Enhancement'],
  required_labels_error: 'Please ensure the PR has labels "Bug" or "Enhancement".',
  recommended_labels: ['Documentation'],
  recommended_labels_warning: 'Consider adding the "Documentation" label for better tracking.'
)

See Also:

  • Automattic/dangermattic

Instance Method Summary collapse

Instance Method Details

#check(do_not_merge_labels: [], required_labels: [], required_labels_error: nil, recommended_labels: [], recommended_labels_warning: nil) ⇒ void

Note:

Tip: if you want to require or recommend "at least one label", you can use an array of a single empty regex [//] to match "a label with any name".

This method returns an undefined value.

Checks if a PR is missing labels or is marked with labels for not merging. If recommended labels are missing, the plugin will emit a warning. If a required label is missing, or the PR has a label indicating that the PR should not be merged, an error will be emitted, preventing the final merge.

with a warning if it doesn't (e.g. [/^feature:/,/^type:/]orbug|bugfix-exemption`). Defaults to an empty array if not provided.

Parameters:

  • do_not_merge_labels (Array<String>) (defaults to: [])

    The possible labels indicating that a merge should not be allowed.

  • required_labels (Array<Regexp>) (defaults to: [])

    The list of Regular Expressions describing all the type of labels that are required on PR (e.g. [/^feature:/,/^type:/]orbug|bugfix-exemption`). Defaults to an empty array if not provided.

  • required_labels_error (String) (defaults to: nil)

    The error message displayed if the required labels are not present. Defaults to a generic message that includes the missing label's regexes.

  • recommended_labels (Array<Regexp>) (defaults to: [])

    The list of Regular Expressions describing all the type of labels that we want a PR to have,

  • recommended_labels_warning (String) (defaults to: nil)

    The warning message displayed if the recommended labels are not present. Defaults to a generic message that includes the missing label's regexes.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dangermattic/plugins/labels_checker.rb', line 38

def check(do_not_merge_labels: [], required_labels: [], required_labels_error: nil, recommended_labels: [], recommended_labels_warning: nil)
  github_labels = danger.github.pr_labels

  # A PR shouldn't be merged with the 'DO NOT MERGE' label
  found_do_not_merge_labels = github_labels.select do |github_label|
    do_not_merge_labels&.any? { |label| github_label.casecmp?(label) }
  end

  failure("This PR is tagged with #{markdown_list_string(found_do_not_merge_labels)} label(s).") unless found_do_not_merge_labels.empty?

  # fail if a PR is missing any of the required labels
  check_missing_labels(labels: github_labels, expected_labels: required_labels, report_on_missing: :error, custom_message: required_labels_error)

  # warn if a PR is missing any of the recommended labels
  check_missing_labels(labels: github_labels, expected_labels: recommended_labels, report_on_missing: :warning, custom_message: recommended_labels_warning)
end