From 8360ab83e71a5f22be7f78e9bb7208746cb6e21b Mon Sep 17 00:00:00 2001 From: orta Date: Fri, 10 Feb 2017 15:27:03 -0500 Subject: [PATCH] Add support for gitlab --- Gemfile | 1 + Gemfile.lock | 1 + README.md | 2 +- lib/danger_plugin.rb | 20 ++++++++++++++++++-- lib/version.rb | 2 +- spec/danger_plugin_spec.rb | 31 ++++++++++++++++++++++++++++--- 6 files changed, 50 insertions(+), 7 deletions(-) diff --git a/Gemfile b/Gemfile index 5a1e64a..131d1e1 100644 --- a/Gemfile +++ b/Gemfile @@ -3,3 +3,4 @@ source 'https://rubygems.org' gemspec gem 'rspec', '~> 3.4' +gem 'pry' diff --git a/Gemfile.lock b/Gemfile.lock index 617d281..05168ed 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -120,6 +120,7 @@ DEPENDENCIES danger-mention! guard (~> 2.14) guard-rspec (~> 4.7) + pry rake (~> 10.0) rspec (~> 3.4) rubocop (~> 0.41) diff --git a/README.md b/README.md index a2a3008..ad1d9a5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Danger Mention -A [Danger](https://github.com/danger/danger) plugin to automatically mention potential reviewers on pull requests. +A [Danger](https://github.com/danger/danger) plugin to automatically mention potential reviewers on pull requests on GitHub and GitLab. ## Installation diff --git a/lib/danger_plugin.rb b/lib/danger_plugin.rb index 57e1a79..9993947 100755 --- a/lib/danger_plugin.rb +++ b/lib/danger_plugin.rb @@ -75,7 +75,18 @@ def select_files(file_blacklist) def compose_urls(files) host = 'https://' + env.request_source.host repo_slug = env.ci_source.repo_slug - path = host + '/' + repo_slug + '/' + 'blame' + '/' + github.branch_for_base + + path = "" + if defined? @dangerfile.gitlab + # https://gitlab.com/danger-systems/danger.systems/blame/danger_update/.gitlab-ci.yml + path = host + '/' + repo_slug + '/' + 'blame' + '/' + gitlab.branch_for_base + + elsif defined? @dangerfile.github + # https://github.com/artsy/emission/blame/master/dangerfile.js + path = host + '/' + repo_slug + '/' + 'blame' + '/' + github.branch_for_base + else + raise "This plugin does not yet support bitbucket, would love PRs: https://github.com/danger/danger-mention/" + end urls = [] files.each do |file| @@ -105,7 +116,12 @@ def parse_blame(url) end def find_reviewers(users, user_blacklist, max_reviewers) - user_blacklist << pr_author + if defined? @dangerfile.gitlab + user_blacklist << gitlab.mr_author + elsif defined? @dangerfile.github + user_blacklist << github.pr_author + end + users = users.select { |k, _| !user_blacklist.include? k } users = users.sort_by { |_, value| value }.reverse diff --git a/lib/version.rb b/lib/version.rb index 4c0e84c..4262af6 100644 --- a/lib/version.rb +++ b/lib/version.rb @@ -1,4 +1,4 @@ module DangerMention - VERSION = '0.5.0'.freeze + VERSION = '0.6.0'.freeze DESCRIPTION = 'Danger plugin to automatically mention potential reviewers on pull requests' end diff --git a/spec/danger_plugin_spec.rb b/spec/danger_plugin_spec.rb index a57441a..8b1b76c 100644 --- a/spec/danger_plugin_spec.rb +++ b/spec/danger_plugin_spec.rb @@ -32,21 +32,22 @@ module Danger describe :compose_urls do before do - allow(@mention).to receive_message_chain('env.request_source.host').and_return('host') + github = Danger::RequestSources::GitHub.new({}, testing_env) + allow(@mention).to receive_message_chain('env.request_source').and_return github allow(@mention).to receive_message_chain('env.ci_source.repo_slug').and_return('slug') allow(@mention).to receive_message_chain('github.branch_for_base').and_return('branch') end it 'composes urls for files' do files = (1..4).to_a.map { |i| format('M%d', i) } - expected = files.map { |f| format('https://host/slug/blame/branch/%s', f) } + expected = files.map { |f| format('https://github.com/slug/blame/branch/%s', f) } results = @mention.compose_urls(files) expect(results).to eq expected end describe :compose_urls do before do - allow(@mention).to receive(:pr_author).and_return('author') + allow(@mention).to receive_message_chain('github.pr_author').and_return('author') end it 'finds potential reviewers' do @@ -88,7 +89,31 @@ module Danger expect(output).to include('@user2') end end + end + + + describe :compose_urls_gitlab do + + it 'composes gitlab urls for files' do + stub_env = { + "DRONE" => true, + "DRONE_REPO" => "k0nserv/danger-test", + "DRONE_PULL_REQUEST" => "593728", + "DANGER_GITLAB_API_TOKEN" => "a86e56d46ac78b" + } + + env = Danger::EnvironmentManager.new(testing_env) + env.request_source = Danger::RequestSources::GitLab.new(Danger::Drone.new(stub_env), testing_env) + danger = Danger::Dangerfile.new(env) + allow(danger.gitlab).to receive(:branch_for_base).and_return("branch_name") + files = (1..4).to_a.map { |i| format('M%d', i) } + expected = files.map { |f| format('https://gitlab.com/artsy/eigen/blame/branch_name/%s', f) } + + results = danger.mention.compose_urls(files) + expect(results).to eq expected + end end end end +