Class: Danger::GithubUtils

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

Overview

Provides common GitHub utility methods related to Pull Requests in a Danger context.

Examples:

Checking if the branch is a release or hotfix branch:

github_utils.release_branch? #=> true or false

Checking if there are active reviewers on the PR:

github_utils.active_reviewers? #=> true or false

Checking if there are requested teams or reviewers on the PR:

github_utils.requested_reviewers? #=> true or false

Checking if the branch is a main branch (trunk, main, master, or develop):

github_utils.main_branch? #=> true or false

Checking if the PR is a work-in-progress (WIP) based on labels or title:

github_utils.wip_feature? #=> true or false

See Also:

  • Automattic/dangermattic

Instance Method Summary collapse

Instance Method Details

#active_reviewers?Boolean

Checks if there are active reviewers providing feedback and potentially changing the state of the PR (e.g., approved, changes requested).

Returns:

  • (Boolean)

    True if there are active reviewers, otherwise false.



29
30
31
32
33
34
# File 'lib/dangermattic/plugins/common/github_utils.rb', line 29

def active_reviewers?
  repo_name = github.pr_json['base']['repo']['full_name']
  pr_number = github.pr_json['number']

  !github.api.pull_request_reviews(repo_name, pr_number).empty?
end

#main_branch?Boolean

Checks if the current branch is a main branch (trunk, main, master, or develop).

Returns:

  • (Boolean)

    True if the branch is a main branch, otherwise false.



47
48
49
# File 'lib/dangermattic/plugins/common/github_utils.rb', line 47

def main_branch?
  %w[trunk main master develop].include?(github.branch_for_base)
end

#release_branch?Boolean

Checks if the current branch is a release or hotfix branch.

Returns:

  • (Boolean)

    True if the branch is a release or hotfix branch, otherwise false.



54
55
56
# File 'lib/dangermattic/plugins/common/github_utils.rb', line 54

def release_branch?
  github.branch_for_base.start_with?('release/') || github.branch_for_base.start_with?('hotfix/')
end

#requested_reviewers?Boolean

Checks if there are requested teams or reviewers who haven't reacted yet.

Returns:

  • (Boolean)

    True if there are requested teams or reviewers, otherwise false.



39
40
41
42
# File 'lib/dangermattic/plugins/common/github_utils.rb', line 39

def requested_reviewers?
  has_requested_reviews = !github.pr_json['requested_teams'].to_a.empty? || !github.pr_json['requested_reviewers'].to_a.empty?
  has_requested_reviews || active_reviewers?
end

#wip_feature?Boolean

Checks if the PR is a work-in-progress (WIP) based on labels or title.

Returns:

  • (Boolean)

    True if the PR is a work-in-progress, otherwise false.



61
62
63
64
65
66
# File 'lib/dangermattic/plugins/common/github_utils.rb', line 61

def wip_feature?
  has_wip_label = github.pr_labels.any? { |label| label.include?('WIP') }
  has_wip_title = github.pr_title.include?('WIP')

  has_wip_label || has_wip_title
end