Class: Danger::DangerGitlabReviewbot

Inherits:
Plugin
  • Object
show all
Defined in:
lib/gitlab_reviewbot/plugin.rb

Overview

This is your plugin class. Any attributes or methods you expose here will be available from within your Dangerfile.

To be published on the Danger plugins site, you will need to have the public interface documented. Danger uses [YARD](yardoc.org/) for generating documentation from your plugin source, and you can verify by running ‘danger plugins lint` or `bundle exec rake spec`.

You should replace these comments with a public description of your library.

Examples:

Ensure people are well warned about merging on Mondays


my_plugin.warn_on_mondays

See Also:

  • Gallonetto/danger-gitlab_reviewbot

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#assignees_amountObject

Define the amount of reviewers to add to the merge requests. Default is 1. NOTE: The plugin won’t remove existing assigned reviewers

Returns:

  • Int



35
36
37
# File 'lib/gitlab_reviewbot/plugin.rb', line 35

def assignees_amount
  @assignees_amount
end

#gitlab_groupObject

Define the group to take the reviewers from. NOTE: This is the group full path as in ‘tech/iOS’ instead of just the group name

Returns:

  • String



28
29
30
# File 'lib/gitlab_reviewbot/plugin.rb', line 28

def gitlab_group
  @gitlab_group
end

#strategyObject

Define the strategy for chosing reviewers. Valid values are:

  • Danger::AssignStrategies::RandomStrategy - assigns N reviewers at random from the group (excluding the author).

  • Danger::AssignStrategies::LeastBusyStrategy - assign the N users with the least amount of open MRs to review



47
48
49
# File 'lib/gitlab_reviewbot/plugin.rb', line 47

def strategy
  @strategy
end

#verboseObject

Add verbosity to the logs

Returns:

  • Bool



54
55
56
# File 'lib/gitlab_reviewbot/plugin.rb', line 54

def verbose
  @verbose
end

Instance Method Details

#assign!Object

Call this method from the Dangerfile to assign reviewers to your merge requests

Returns:

  • The usernames list of assigned reviewes [Array<String>]



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gitlab_reviewbot/plugin.rb', line 59

def assign!
  project_id = ENV['CI_PROJECT_ID']
  mr_iid = ENV['CI_MERGE_REQUEST_IID']
  if mr_iid.nil?
    raise "Env variable CI_MERGE_REQUEST_IID doesn't point to a valid merge request iid"
  end

  if project_id.nil?
    raise "Env variable CI_PROJECT_ID doesn't point to a valid project id"
  end

  current_assignees = (ENV['CI_MERGE_REQUEST_ASSIGNEES'] || '').split(',')
  required_assignees_count = [assignees_amount - current_assignees.length, 0].max

  puts "Project ID: #{project_id}" if @verbose
  puts "MR IID: #{mr_iid}" if @verbose
  puts "Currently assigned: #{current_assignees}" if @verbose
  puts "Required: #{required_assignees_count}" if @verbose
  if required_assignees_count == 0
    puts "Nothing to do" if @verbose
    return
  end

  strategy_class = strategy.new(client: gitlab.api, project: project_id, mr: mr_iid, group: gitlab_group)

  assignees = strategy_class.assign! required_assignees_count

  puts "Assigning: #{assignees}" if @verbose
  return assignees
end