Class: Danger::GitLabCI

Inherits:
CI
  • Object
show all
Defined in:
lib/danger/ci_source/gitlab_ci.rb

Overview

### CI Setup

Install dependencies and add a danger step to your .gitlab-ci.yml:

“‘yml before_script:

- bundle install

danger:

script:
 - bundle exec danger

“‘

### Token Setup

Add the ‘DANGER_GITLAB_API_TOKEN` to your pipeline env variables if you are hosting your code on GitLab. If you are using GitLab as a mirror for the purpose of CI/CD, while hosting your repo on GitHub, set the `DANGER_GITHUB_API_TOKEN` as well as the project repo URL to `DANGER_PROJECT_REPO_URL`.

Instance Attribute Summary

Attributes inherited from CI

#pull_request_id, #repo_slug, #repo_url

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CI

available_ci_sources, inherited, #supports?

Constructor Details

#initialize(env) ⇒ GitLabCI

Returns a new instance of GitLabCI.



77
78
79
80
# File 'lib/danger/ci_source/gitlab_ci.rb', line 77

def initialize(env)
  self.repo_slug = self.class.slug_from(env)
  self.pull_request_id = self.class.determine_pull_or_merge_request_id(env)
end

Class Method Details

.determine_pull_or_merge_request_id(env) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/danger/ci_source/gitlab_ci.rb', line 40

def self.determine_pull_or_merge_request_id(env)
  return env["CI_MERGE_REQUEST_IID"] if env["CI_MERGE_REQUEST_IID"]
  return env["CI_EXTERNAL_PULL_REQUEST_IID"] if env["CI_EXTERNAL_PULL_REQUEST_IID"]
  return 0 unless env["CI_COMMIT_SHA"]

  project_path = env["CI_MERGE_REQUEST_PROJECT_PATH"] || env["CI_PROJECT_PATH"]
  base_commit = env["CI_COMMIT_SHA"]
  client = RequestSources::GitLab.new(nil, env).client

  client_version = Gem::Version.new(client.version.version)
  if client_version >= Gem::Version.new("10.7")
    # Use the 'list merge requests associated with a commit' API, for speed
    # (GET /projects/:id/repository/commits/:sha/merge_requests) available for GitLab >= 10.7
    merge_request = client.commit_merge_requests(project_path, base_commit, state: :opened).first
    if client_version >= Gem::Version.new("13.8")
      # Gitlab 13.8.0 started returning merge requests for merge commits and squashed commits
      # By checking for merge_request.state, we can ensure danger only comments on MRs which are open
      return 0 if merge_request.nil?
      return 0 unless merge_request.state == "opened"
    end
  else
    merge_requests = client.merge_requests(project_path, state: :opened)
    merge_request = merge_requests.auto_paginate.find do |mr|
      mr.sha == base_commit
    end
  end
  merge_request.nil? ? 0 : merge_request.iid
end

.slug_from(env) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/danger/ci_source/gitlab_ci.rb', line 69

def self.slug_from(env)
  if env["DANGER_PROJECT_REPO_URL"]
    env["DANGER_PROJECT_REPO_URL"].split("/").last(2).join("/")
  else
    env["CI_MERGE_REQUEST_PROJECT_PATH"] || env["CI_PROJECT_PATH"]
  end
end

.validates_as_ci?(env) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/danger/ci_source/gitlab_ci.rb', line 28

def self.validates_as_ci?(env)
  env.key? "GITLAB_CI"
end

.validates_as_pr?(env) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# File 'lib/danger/ci_source/gitlab_ci.rb', line 32

def self.validates_as_pr?(env)
  exists = [
    "GITLAB_CI", "CI_PROJECT_PATH"
  ].all? { |x| env[x] }

  exists && determine_pull_or_merge_request_id(env).to_i > 0
end

Instance Method Details

#supported_request_sourcesObject



82
83
84
85
86
87
# File 'lib/danger/ci_source/gitlab_ci.rb', line 82

def supported_request_sources
  @supported_request_sources ||= [
    Danger::RequestSources::GitHub,
    Danger::RequestSources::GitLab
  ]
end