Class: Danger::Jenkins

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

Overview

### CI Setup Ah Jenkins, so many memories. So, if you’re using Jenkins, you’re hosting your own environment.

#### GitHub You will want to be using the [GitHub pull request builder plugin](wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin) in order to ensure that you have the build environment set up for PR integration.

With that set up, you can edit your job to add ‘bundle exec danger` at the build action.

##### Pipeline If your’re using [pipelines](jenkins.io/solutions/pipeline/) you should be using the [GitHub branch source plugin](wiki.jenkins-ci.org/display/JENKINS/GitHub+Branch+Source+Plugin) for easy setup and handling of PRs.

After you’ve set up the plugin, add a ‘sh ’bundle exec danger’‘ line in your pipeline script and make sure that build PRs is enabled.

#### GitLab You will want to be using the [GitLab Plugin](github.com/jenkinsci/gitlab-plugin) in order to ensure that you have the build environment set up for MR integration.

With that set up, you can edit your job to add ‘bundle exec danger` at the build action.

#### General

People occasionally see issues with Danger not classing your CI runs as a PR, to give you visibilty the Jenkins side of Danger expects to see one of these env vars:

  • ghprbPullId

  • CHANGE_ID

  • gitlabMergeRequestIid

  • gitlabMergeRequestId

### Token Setup

#### GitHub As you own the machine, it’s up to you to add the environment variable for the ‘DANGER_GITHUB_API_TOKEN`.

#### GitLab As you own the machine, it’s up to you to add the environment variable for the ‘DANGER_GITLAB_API_TOKEN`.

Defined Under Namespace

Classes: EnvNotFound

Instance Attribute Summary collapse

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) ⇒ Jenkins

Returns a new instance of Jenkins.

Raises:



77
78
79
80
81
82
83
84
# File 'lib/danger/ci_source/jenkins.rb', line 77

def initialize(env)
  raise EnvNotFound.new if env.nil? || env.empty?

  self.repo_url = self.class.repo_url(env)
  self.pull_request_id = self.class.pull_request_id(env)
  self.repo_slug = self.class.repo_slug(self.repo_url)
  self.project_url = env["CI_MERGE_REQUEST_PROJECT_URL"] || env["CI_PROJECT_URL"]
end

Instance Attribute Details

#project_urlObject

Returns the value of attribute project_url.



50
51
52
# File 'lib/danger/ci_source/jenkins.rb', line 50

def project_url
  @project_url
end

Class Method Details

.pull_request_id(env) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/danger/ci_source/jenkins.rb', line 114

def self.pull_request_id(env)
  if env["ghprbPullId"]
    env["ghprbPullId"]
  elsif env["CHANGE_ID"]
    env["CHANGE_ID"]
  elsif env["gitlabMergeRequestIid"]
    env["gitlabMergeRequestIid"]
  else
    env["gitlabMergeRequestId"]
  end
end

.repo_slug(repo_url) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/danger/ci_source/jenkins.rb', line 86

def self.repo_slug(repo_url)
  slug = self.slug_ssh(repo_url)
  slug = self.slug_http(repo_url) unless slug
  slug = self.slug_bitbucket(repo_url) unless slug
  slug = self.slug_fallback(repo_url) unless slug
  return slug.gsub(/\.git$/, "") unless slug.nil?
end

.repo_url(env) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/danger/ci_source/jenkins.rb', line 126

def self.repo_url(env)
  if env["GIT_URL_1"]
    env["GIT_URL_1"]
  elsif env["CHANGE_URL"]
    change_url = env["CHANGE_URL"]
    case change_url
    when %r{\/pull\/} # GitHub
      matches = change_url.match(%r{(.+)\/pull\/[0-9]+})
      matches[1] unless matches.nil?
    when %r{\/merge_requests\/} # GitLab
      matches = change_url.match(%r{(.+)\/merge_requests\/[0-9]+})
      matches[1] unless matches.nil?
    when %r{\/pull-requests\/} # Bitbucket
      matches = change_url.match(%r{(.+)\/pull-requests\/[0-9]+})
      matches[1] unless matches.nil?
    else
      change_url
    end
  else
    env["GIT_URL"]
  end
end

.slug_bitbucket(repo_url) ⇒ Object



94
95
96
97
# File 'lib/danger/ci_source/jenkins.rb', line 94

def self.slug_bitbucket(repo_url)
  repo_matches = repo_url.match(%r{(?:[\/:])projects\/([^\/.]+)\/repos\/([^\/.]+)})
  return "#{repo_matches[1]}/#{repo_matches[2]}" if repo_matches
end

.slug_fallback(repo_url) ⇒ Object



109
110
111
112
# File 'lib/danger/ci_source/jenkins.rb', line 109

def self.slug_fallback(repo_url)
  repo_matches = repo_url.match(%r{([\/:])([^\/]+\/[^\/]+)$})
  return repo_matches[2]
end

.slug_http(repo_url) ⇒ Object



104
105
106
107
# File 'lib/danger/ci_source/jenkins.rb', line 104

def self.slug_http(repo_url)
  repo_matches = repo_url.match(%r{^https?.+(?>\.\w*\d*\/)(.+.git$)})
  return repo_matches[1] if repo_matches
end

.slug_ssh(repo_url) ⇒ Object



99
100
101
102
# File 'lib/danger/ci_source/jenkins.rb', line 99

def self.slug_ssh(repo_url)
  repo_matches = repo_url.match(%r{^git@.+:(.+)})
  return repo_matches[1] if repo_matches
end

.validates_as_ci?(env) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/danger/ci_source/jenkins.rb', line 57

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

.validates_as_pr?(env) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/danger/ci_source/jenkins.rb', line 61

def self.validates_as_pr?(env)
  id = pull_request_id(env)
  !id.nil? && !id.empty? && !!id.match(%r{^\d+$})
end

Instance Method Details

#supported_request_sourcesObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/danger/ci_source/jenkins.rb', line 66

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