lib/danger/ci_source/jenkins.rb in danger-5.10.1 vs lib/danger/ci_source/jenkins.rb in danger-5.10.2

- old
+ new

@@ -76,17 +76,38 @@ 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) + end - repo_matches = self.repo_url.match(%r{(?:[\/:])projects\/([^\/.]+)\/repos\/([^\/.]+)}) # Bitbucket Server - if repo_matches - self.repo_slug = "#{repo_matches[1]}/#{repo_matches[2]}" - else - repo_matches = self.repo_url.match(%r{([\/:])([^\/]+\/[^\/]+)$}) - self.repo_slug = repo_matches[2].gsub(/\.git$/, "") unless repo_matches.nil? - 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 + return slug.gsub(/\.git$/, "") unless slug.nil? + end + + 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 + + def self.slug_ssh(repo_url) + repo_matches = repo_url.match(%r{^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$)}) + return repo_matches[1] if repo_matches + end + + def self.slug_fallback(repo_url) + repo_matches = repo_url.match(%r{([\/:])([^\/]+\/[^\/]+)$}) + return repo_matches[2] end def self.pull_request_id(env) if env["ghprbPullId"] env["ghprbPullId"]