Class: Danger::DangerfileGitLabPlugin

Inherits:
Plugin
  • Object
show all
Defined in:
lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb

Overview

Handles interacting with GitLab inside a Dangerfile. Provides a few functions which wrap ‘mr_json` and also through a few standard functions to simplify your code.

Examples:

Warn when an MR is classed as work in progress.


warn "MR is classed as Work in Progress" if gitlab.mr_title.include? "[WIP]"

Declare a MR to be simple to avoid specific Danger rules.


declared_trivial = (gitlab.mr_title + gitlab.mr_body).include?("#trivial")

Ensure that labels have been applied to the MR.


failure "Please add labels to this MR" if gitlab.mr_labels.empty?

Ensure that all MRs have an assignee.


warn "This MR does not have any assignees yet." unless gitlab.mr_json["assignee"]

Ensure there is a summary for a MR.


failure "Please provide a summary in the Merge Request description" if gitlab.mr_body.length < 5

Only accept MRs to the develop branch.


failure "Please re-submit this MR to develop, we may have already fixed your issue." if gitlab.branch_for_merge != "develop"

Note when MRs don’t reference a milestone, make the warning stick around on subsequent runs


has_milestone = gitlab.mr_json["milestone"] != nil
warn("This MR does not refer to an existing milestone", sticky: true) unless has_milestone

Note when a MR cannot be manually merged


can_merge = gitlab.mr_json["mergeable"]
warn("This MR cannot be merged yet.") unless can_merge

Highlight when a celebrity makes a merge request.


message "Welcome, Danger." if gitlab.mr_author == "dangermcshane"

Send a message with links to a collection of specific files.


if git.modified_files.include? "config/*.js"
  config_files = git.modified_files.select { |path| path.include? "config/" }
  message "This MR changes #{ gitlab.html_link(config_files) }"
end

Highlight with a clickable link if a Package.json is changed.


warn "#{gitlab.html_link("Package.json")} was edited." if git.modified_files.include? "Package.json"

Select a random group member as assignee if no assignee is selected


if gitlab.mr_json["assignee"].nil?
  reviewer = gitlab.api.group_members(gitlab.api.merge_request_approvals(project_id, mr_id).to_hash["approver_groups"].first["group"]["id"]).sample
  if gitlab.api.group_members(gitlab.api.merge_request_approvals(project_id, mr_id).to_hash["approver_groups"].first["group"]["id"]).length > 1
    while reviewer.to_hash["id"] == gitlab.mr_json["author"]["id"] do
      reviewer = gitlab.api.group_members(gitlab.api.merge_request_approvals(project_id, mr_id).to_hash["approver_groups"].first["group"]["id"]).sample
    end
  end
  message "Reviewer roulete rolled for: #{reviewer.to_hash['name']} (@#{reviewer.to_hash['username']})"
  gitlab.api.update_merge_request(project_id, mr_id, { assignee_id: reviewer.to_hash["id"] })
end

See Also:

  • danger/danger

MR Metadata collapse

MR Content collapse

MR Commit Metadata collapse

GitLab Misc collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

all_plugins, clear_external_plugins, inherited, #method_missing

Constructor Details

#initialize(dangerfile) ⇒ DangerfileGitLabPlugin

Returns a new instance of DangerfileGitLabPlugin.



87
88
89
90
91
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 87

def initialize(dangerfile)
  super(dangerfile)

  @gitlab = dangerfile.env.request_source
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Danger::Plugin

Class Method Details

.instance_nameString

The instance name used in the Dangerfile

Returns:



83
84
85
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 83

def self.instance_name
  "gitlab"
end

.new(dangerfile) ⇒ Object

So that this init can fail.



75
76
77
78
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 75

def self.new(dangerfile)
  return nil if dangerfile.env.request_source.class != Danger::RequestSources::GitLab
  super
end

Instance Method Details

#apiGitLab::Client

Provides access to the GitLab API client used inside Danger. Making it easy to use the GitLab API inside a Dangerfile. See the gitlab gem’s [documentation](www.rubydoc.info/gems/gitlab/Gitlab/Client) for accessible methods.

Returns:

  • (GitLab::Client)


191
192
193
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 191

def api
  @gitlab.client
end

#base_commitString

The base commit to which the MR is going to be merged as a parent

Returns:



163
164
165
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 163

def base_commit
  @gitlab.mr_json.diff_refs.base_sha
end

#branch_for_baseString

The branch to which the MR is going to be merged into.

Returns:



147
148
149
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 147

def branch_for_base
  @gitlab.mr_json.target_branch
end

#branch_for_headString

The branch to which the MR is going to be merged from.

Returns:



155
156
157
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 155

def branch_for_head
  @gitlab.mr_json.source_branch
end

#branch_for_mergeString

Deprecated.

Please use #branch_for_base instead

The branch to which the MR is going to be merged into

Returns:



139
140
141
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 139

def branch_for_merge
  branch_for_base
end

#head_commitString

The head commit to which the MR is requesting to be merged from

Returns:



171
172
173
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 171

def head_commit
  @gitlab.mr_json.diff_refs.head_sha
end

Returns a list of HTML anchors for a file, or files in the head repository. An example would be: ‘<a href=’gitlab.com/artsy/eigen/blob/561827e46167077b5e53515b4b7349b8ae04610b/file.txt’>file.txt</a>‘. It returns a string of multiple anchors if passed an array.

Parameters:

  • paths (String or Array<String>)

    A list of strings to convert to gitlab anchors

  • full_path (Bool) (defaults to: true)

    Shows the full path as the link’s text, defaults to ‘true`.

Returns:



205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 205

def html_link(paths, full_path: true)
  paths = [paths] unless paths.kind_of?(Array)
  commit = head_commit

  paths = paths.map do |path|
    url_path = path.start_with?("/") ? path : "/#{path}"
    text = full_path ? path : File.basename(path)
    create_link("#{env.ci_source.project_url}/blob/#{commit}#{url_path}", text)
  end

  return paths.first if paths.count < 2
  paths.first(paths.count - 1).join(", ") + " & " + paths.last
end

#mr_authorString

The username of the author of the Merge Request

Returns:



113
114
115
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 113

def mr_author
  @gitlab.mr_json.author.username.to_s
end

#mr_bodyString

The body text of the Merge Request

Returns:



105
106
107
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 105

def mr_body
  @gitlab.mr_json.description.to_s
end

#mr_diffString

The unified diff produced by GitLab for this MR see [Unified diff](en.wikipedia.org/wiki/Diff_utility#Unified_format)

Returns:



130
131
132
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 130

def mr_diff
  @gitlab.mr_diff
end

#mr_jsonHash

The hash that represents the MR’s JSON. See documentation for the structure [here](docs.gitlab.com/ce/api/merge_requests.html#get-single-mr)

Returns:

  • (Hash)


180
181
182
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 180

def mr_json
  @gitlab.mr_json.to_hash
end

#mr_labelsString

The labels assigned to the Merge Request

Returns:



121
122
123
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 121

def mr_labels
  @gitlab.mr_json.labels
end

#mr_titleString

The title of the Merge Request

Returns:



97
98
99
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 97

def mr_title
  @gitlab.mr_json.title.to_s
end