lib/danger/ci_source/jenkins.rb in danger-9.1.0 vs lib/danger/ci_source/jenkins.rb in danger-9.2.0

- old
+ new

@@ -46,10 +46,11 @@ # #### GitLab # As you own the machine, it's up to you to add the environment variable for the `DANGER_GITLAB_API_TOKEN`. # class Jenkins < CI attr_accessor :project_url + class EnvNotFound < StandardError def initialize super("ENV not found, please check your Jenkins. Related: https://stackoverflow.com/search?q=jenkins+env+null") end end @@ -58,22 +59,20 @@ env.key? "JENKINS_URL" end def self.validates_as_pr?(env) id = pull_request_id(env) - !id.nil? && !id.empty? && !!id.match(%r{^\d+$}) + !id.nil? && !id.empty? && !!id.match(/^\d+$/) end def supported_request_sources - @supported_request_sources ||= begin - [ - Danger::RequestSources::GitHub, - Danger::RequestSources::GitLab, - Danger::RequestSources::BitbucketServer, - Danger::RequestSources::BitbucketCloud - ] - end + @supported_request_sources ||= [ + Danger::RequestSources::GitHub, + Danger::RequestSources::GitLab, + Danger::RequestSources::BitbucketServer, + Danger::RequestSources::BitbucketCloud + ] end def initialize(env) raise EnvNotFound.new if env.nil? || env.empty? @@ -83,33 +82,33 @@ self.project_url = env["CI_MERGE_REQUEST_PROJECT_URL"] || env["CI_PROJECT_URL"] end 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 + slug ||= self.slug_http(repo_url) + slug ||= self.slug_bitbucket(repo_url) + slug ||= self.slug_fallback(repo_url) return slug.gsub(/\.git$/, "") unless slug.nil? end def self.slug_bitbucket(repo_url) - repo_matches = repo_url.match(%r{(?:[\/:])projects\/([^\/.]+)\/repos\/([^\/.]+)}) + repo_matches = repo_url.match(%r{(?:[/:])projects/([^/.]+)/repos/([^/.]+)}) return "#{repo_matches[1]}/#{repo_matches[2]}" if repo_matches end def self.slug_ssh(repo_url) - repo_matches = repo_url.match(%r{^git@.+:(.+)}) + repo_matches = repo_url.match(/^git@.+:(.+)/) return repo_matches[1] if repo_matches end def self.slug_http(repo_url) - repo_matches = repo_url.match(%r{^https?.+(?>\.\w*\d*\/)(.+.git$)}) + repo_matches = repo_url.match(%r{^https?.+(?>\.\w*\d*/)(.+.git$)}) return repo_matches[1] if repo_matches end def self.slug_fallback(repo_url) - repo_matches = repo_url.match(%r{([\/:])([^\/]+\/[^\/]+)$}) + repo_matches = repo_url.match(%r{([/:])([^/]+/[^/]+)$}) return repo_matches[2] end def self.pull_request_id(env) if env["ghprbPullId"] @@ -127,17 +126,17 @@ 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]+}) + 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]+}) + 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]+}) + when %r{/pull-requests/} # Bitbucket + matches = change_url.match(%r{(.+)/pull-requests/[0-9]+}) matches[1] unless matches.nil? else change_url end else