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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Once a strategy is in place, adopt the conf methods



64
65
66
67
68
69
70
71
# File 'lib/gitlab_reviewbot/plugin.rb', line 64

def method_missing(method, *args)
  super unless method.to_s.start_with? 'strategy_'
  if strategy.respond_to? method.to_s.delete_prefix('strategy_')
    strategy.send(method.to_s.delete_prefix('strategy_'), *args)
  else
    super
  end
end

Instance Attribute Details

#assignees_amountObject



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

def assignees_amount
  @assignees_amount || 1
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

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>]



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gitlab_reviewbot/plugin.rb', line 76

def assign!
  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(',') #buggy?
  already_assigned_count = current_assignees.length
  required_assignees_count = [assignees_amount - already_assigned_count, 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.group_name = gitlab_group

  assignees = @strategy.assign! assignees_amount

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

#mr_iidObject



59
60
61
# File 'lib/gitlab_reviewbot/plugin.rb', line 59

def mr_iid
  ENV['CI_MERGE_REQUEST_IID']
end

#project_idObject



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

def project_id
  ENV['CI_PROJECT_ID']
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 #|| Danger::AssignStrategies::RandomStrategy.new(client: gitlab.api, project: project_id, mr: mr_iid)
end

#strategy=(klass) ⇒ Object



51
52
53
# File 'lib/gitlab_reviewbot/plugin.rb', line 51

def strategy=(klass)
  @strategy = klass.new(client: gitlab.api, project: project_id, mr: mr_iid)
end