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.

Instance Attribute Summary collapse

Attributes inherited from CI

#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.



50
51
52
53
54
# File 'lib/danger/ci_source/gitlab_ci.rb', line 50

def initialize(env)
  @env = env
  @repo_slug = env["CI_MERGE_REQUEST_PROJECT_PATH"] || env["CI_PROJECT_PATH"]
  @project_url = env["CI_MERGE_REQUEST_PROJECT_URL"] || env["CI_PROJECT_URL"]
end

Instance Attribute Details

#project_urlObject (readonly)

Returns the value of attribute project_url.



20
21
22
# File 'lib/danger/ci_source/gitlab_ci.rb', line 20

def project_url
  @project_url
end

Class Method Details

.determine_merge_request_id(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/danger/ci_source/gitlab_ci.rb', line 34

def self.determine_merge_request_id(env)
  return env["CI_MERGE_REQUEST_IID"] if env["CI_MERGE_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

  merge_requests = client.merge_requests(project_path, state: :opened)
  merge_request = merge_requests.auto_paginate.find do |mr|
    mr.sha == base_commit
  end

  merge_request.nil? ? 0 : merge_request.iid
end

.validates_as_ci?(env) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/danger/ci_source/gitlab_ci.rb', line 22

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

.validates_as_pr?(env) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/danger/ci_source/gitlab_ci.rb', line 26

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

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

Instance Method Details

#pull_request_idObject



60
61
62
# File 'lib/danger/ci_source/gitlab_ci.rb', line 60

def pull_request_id
  @pull_request_id ||= self.class.determine_merge_request_id(@env)
end

#supported_request_sourcesObject



56
57
58
# File 'lib/danger/ci_source/gitlab_ci.rb', line 56

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