class Danger::DangerfileGitLabPlugin

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.

@example Warn when an MR is classed as work in progress.

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

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

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

@example Ensure that labels have been applied to the MR.

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

@example Ensure that all MRs have an assignee.

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

@example Ensure there is a summary for a MR.

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

@example 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"

@example 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

@example Note when a MR cannot be manually merged

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

@example Highlight when a celebrity makes a merge request.

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

@example 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

@example 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"

@example 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 danger/danger @tags core, gitlab