danger-gitlab_reviewbot 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c8f3b6c9f3e73ce3bdd2a7715f556dd2d3a3f220e10525a6e1843405a9b465d5
4
+ data.tar.gz: 9f3dad2be11de00ebde8a301a9b8fdfefcf6b6642b8c32edcb2fdb712177ac4f
5
+ SHA512:
6
+ metadata.gz: 72083bfe33060a8be99029d18fd12f8954ebb689e0c39d47f482c492c16524ca3c6e91ba6dd77339a08ab86d7dbe1a987fa1e832c4317890fb10e2656f6a1942
7
+ data.tar.gz: 36b6f7c72796643d677e63291ad328635901fbb154dcc726ed763d8dfb25bec90ac8d34930b7c464d6c4f5a5c98efd30d65e563cc0aabcefce2d6e8906591bae
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .DS_Store
2
+ pkg
3
+ .idea/
4
+ .yardoc
data/.rubocop.yml ADDED
@@ -0,0 +1,148 @@
1
+ # Defaults can be found here: https://github.com/bbatsov/rubocop/blob/master/config/default.yml
2
+
3
+ # If you don't like these settings, just delete this file :)
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.4
7
+
8
+ Style/StringLiterals:
9
+ EnforcedStyle: double_quotes
10
+ Enabled: true
11
+
12
+ # kind_of? is a good way to check a type
13
+ Style/ClassCheck:
14
+ EnforcedStyle: kind_of?
15
+
16
+ # specs sometimes have useless assignments, which is fine
17
+ Lint/UselessAssignment:
18
+ Exclude:
19
+ - '**/spec/**/*'
20
+
21
+ # We could potentially enable the 2 below:
22
+ Layout/FirstHashElementIndentation:
23
+ Enabled: false
24
+
25
+ Layout/HashAlignment:
26
+ Enabled: false
27
+
28
+ # HoundCI doesn't like this rule
29
+ Layout/DotPosition:
30
+ Enabled: false
31
+
32
+ # We allow !! as it's an easy way to convert ot boolean
33
+ Style/DoubleNegation:
34
+ Enabled: false
35
+
36
+ # Cop supports --auto-correct.
37
+ Lint/UnusedBlockArgument:
38
+ Enabled: false
39
+
40
+ # We want to allow class Fastlane::Class
41
+ Style/ClassAndModuleChildren:
42
+ Enabled: false
43
+
44
+ Metrics/AbcSize:
45
+ Max: 60
46
+
47
+ # The %w might be confusing for new users
48
+ Style/WordArray:
49
+ MinSize: 19
50
+
51
+ # raise and fail are both okay
52
+ Style/SignalException:
53
+ Enabled: false
54
+
55
+ # Better too much 'return' than one missing
56
+ Style/RedundantReturn:
57
+ Enabled: false
58
+
59
+ # Having if in the same line might not always be good
60
+ Style/IfUnlessModifier:
61
+ Enabled: false
62
+
63
+ # and and or is okay
64
+ Style/AndOr:
65
+ Enabled: false
66
+
67
+ # Configuration parameters: CountComments.
68
+ Metrics/ClassLength:
69
+ Max: 350
70
+
71
+ Metrics/CyclomaticComplexity:
72
+ Max: 17
73
+
74
+ # Configuration parameters: AllowURI, URISchemes.
75
+ Metrics/LineLength:
76
+ Max: 370
77
+
78
+ # Configuration parameters: CountKeywordArgs.
79
+ Metrics/ParameterLists:
80
+ Max: 10
81
+
82
+ Metrics/PerceivedComplexity:
83
+ Max: 18
84
+
85
+ # Sometimes it's easier to read without guards
86
+ Style/GuardClause:
87
+ Enabled: false
88
+
89
+ # something = if something_else
90
+ # that's confusing
91
+ Style/ConditionalAssignment:
92
+ Enabled: false
93
+
94
+ # Better to have too much self than missing a self
95
+ Style/RedundantSelf:
96
+ Enabled: false
97
+
98
+ Metrics/MethodLength:
99
+ Max: 60
100
+
101
+ # We're not there yet
102
+ Style/Documentation:
103
+ Enabled: false
104
+
105
+ # Adds complexity
106
+ Style/IfInsideElse:
107
+ Enabled: false
108
+
109
+ # danger specific
110
+
111
+ Style/BlockComments:
112
+ Enabled: false
113
+
114
+ Layout/MultilineMethodCallIndentation:
115
+ EnforcedStyle: indented
116
+
117
+ # FIXME: 25
118
+ Metrics/BlockLength:
119
+ Max: 345
120
+ Exclude:
121
+ - "**/*_spec.rb"
122
+
123
+ Style/MixinGrouping:
124
+ Enabled: false
125
+
126
+ Style/FileName:
127
+ Enabled: false
128
+
129
+ Layout/HeredocIndentation:
130
+ Enabled: false
131
+
132
+ Style/SpecialGlobalVars:
133
+ Enabled: false
134
+
135
+ PercentLiteralDelimiters:
136
+ PreferredDelimiters:
137
+ "%": ()
138
+ "%i": ()
139
+ "%q": ()
140
+ "%Q": ()
141
+ "%r": "{}"
142
+ "%s": ()
143
+ "%w": ()
144
+ "%W": ()
145
+ "%x": ()
146
+
147
+ Security/YAMLLoad:
148
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in danger-gitlab_reviewbot.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,157 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ danger-gitlab_reviewbot (1.0.0)
5
+ danger-gitlab
6
+ danger-plugin-api (~> 1.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.7.0)
12
+ public_suffix (>= 2.0.2, < 5.0)
13
+ ast (2.4.0)
14
+ claide (1.0.3)
15
+ claide-plugins (0.9.2)
16
+ cork
17
+ nap
18
+ open4 (~> 1.3)
19
+ coderay (1.1.2)
20
+ colored2 (3.1.2)
21
+ cork (0.3.0)
22
+ colored2 (~> 3.1)
23
+ danger (6.3.2)
24
+ claide (~> 1.0)
25
+ claide-plugins (>= 0.9.2)
26
+ colored2 (~> 3.1)
27
+ cork (~> 0.1)
28
+ faraday (~> 0.9)
29
+ faraday-http-cache (~> 2.0)
30
+ git (~> 1.6)
31
+ kramdown (~> 2.0)
32
+ kramdown-parser-gfm (~> 1.0)
33
+ no_proxy_fix
34
+ octokit (~> 4.7)
35
+ terminal-table (~> 1)
36
+ danger-gitlab (7.0.0)
37
+ danger (~> 6.0)
38
+ gitlab (~> 4.2, >= 4.2.0)
39
+ danger-plugin-api (1.0.0)
40
+ danger (> 2.0)
41
+ diff-lcs (1.3)
42
+ faraday (0.17.3)
43
+ multipart-post (>= 1.2, < 3)
44
+ faraday-http-cache (2.2.0)
45
+ faraday (>= 0.8)
46
+ ffi (1.12.2)
47
+ formatador (0.2.5)
48
+ git (1.7.0)
49
+ rchardet (~> 1.8)
50
+ gitlab (4.14.1)
51
+ httparty (~> 0.14, >= 0.14.0)
52
+ terminal-table (~> 1.5, >= 1.5.1)
53
+ guard (2.16.2)
54
+ formatador (>= 0.2.4)
55
+ listen (>= 2.7, < 4.0)
56
+ lumberjack (>= 1.0.12, < 2.0)
57
+ nenv (~> 0.1)
58
+ notiffany (~> 0.0)
59
+ pry (>= 0.9.12)
60
+ shellany (~> 0.0)
61
+ thor (>= 0.18.1)
62
+ guard-compat (1.2.1)
63
+ guard-rspec (4.7.3)
64
+ guard (~> 2.1)
65
+ guard-compat (~> 1.1)
66
+ rspec (>= 2.99.0, < 4.0)
67
+ httparty (0.18.0)
68
+ mime-types (~> 3.0)
69
+ multi_xml (>= 0.5.2)
70
+ jaro_winkler (1.5.4)
71
+ kramdown (2.2.1)
72
+ rexml
73
+ kramdown-parser-gfm (1.1.0)
74
+ kramdown (~> 2.0)
75
+ listen (3.0.7)
76
+ rb-fsevent (>= 0.9.3)
77
+ rb-inotify (>= 0.9.7)
78
+ lumberjack (1.2.4)
79
+ method_source (1.0.0)
80
+ mime-types (3.3.1)
81
+ mime-types-data (~> 3.2015)
82
+ mime-types-data (3.2020.0425)
83
+ multi_xml (0.6.0)
84
+ multipart-post (2.1.1)
85
+ nap (1.1.0)
86
+ nenv (0.3.0)
87
+ no_proxy_fix (0.1.2)
88
+ notiffany (0.1.3)
89
+ nenv (~> 0.1)
90
+ shellany (~> 0.0)
91
+ octokit (4.18.0)
92
+ faraday (>= 0.9)
93
+ sawyer (~> 0.8.0, >= 0.5.3)
94
+ open4 (1.3.4)
95
+ parallel (1.19.1)
96
+ parser (2.7.1.2)
97
+ ast (~> 2.4.0)
98
+ pry (0.13.1)
99
+ coderay (~> 1.1)
100
+ method_source (~> 1.0)
101
+ public_suffix (4.0.4)
102
+ rainbow (3.0.0)
103
+ rake (10.5.0)
104
+ rb-fsevent (0.10.4)
105
+ rb-inotify (0.10.1)
106
+ ffi (~> 1.0)
107
+ rchardet (1.8.0)
108
+ rexml (3.2.4)
109
+ rspec (3.9.0)
110
+ rspec-core (~> 3.9.0)
111
+ rspec-expectations (~> 3.9.0)
112
+ rspec-mocks (~> 3.9.0)
113
+ rspec-core (3.9.2)
114
+ rspec-support (~> 3.9.3)
115
+ rspec-expectations (3.9.1)
116
+ diff-lcs (>= 1.2.0, < 2.0)
117
+ rspec-support (~> 3.9.0)
118
+ rspec-mocks (3.9.1)
119
+ diff-lcs (>= 1.2.0, < 2.0)
120
+ rspec-support (~> 3.9.0)
121
+ rspec-support (3.9.3)
122
+ rubocop (0.82.0)
123
+ jaro_winkler (~> 1.5.1)
124
+ parallel (~> 1.10)
125
+ parser (>= 2.7.0.1)
126
+ rainbow (>= 2.2.2, < 4.0)
127
+ rexml
128
+ ruby-progressbar (~> 1.7)
129
+ unicode-display_width (>= 1.4.0, < 2.0)
130
+ ruby-progressbar (1.10.1)
131
+ sawyer (0.8.2)
132
+ addressable (>= 2.3.5)
133
+ faraday (> 0.8, < 2.0)
134
+ shellany (0.0.1)
135
+ terminal-table (1.8.0)
136
+ unicode-display_width (~> 1.1, >= 1.1.1)
137
+ thor (1.0.1)
138
+ unicode-display_width (1.7.0)
139
+ yard (0.9.25)
140
+
141
+ PLATFORMS
142
+ ruby
143
+
144
+ DEPENDENCIES
145
+ bundler (~> 2.1)
146
+ danger-gitlab_reviewbot!
147
+ guard (~> 2.14)
148
+ guard-rspec (~> 4.7)
149
+ listen (= 3.0.7)
150
+ pry
151
+ rake (~> 10.0)
152
+ rspec (~> 3.4)
153
+ rubocop
154
+ yard
155
+
156
+ BUNDLED WITH
157
+ 2.1.4
data/Guardfile ADDED
@@ -0,0 +1,19 @@
1
+ # A guardfile for making Danger Plugins
2
+ # For more info see https://github.com/guard/guard#readme
3
+
4
+ # To run, use `bundle exec guard`.
5
+
6
+ guard :rspec, cmd: 'bundle exec rspec' do
7
+ require 'guard/rspec/dsl'
8
+ dsl = Guard::RSpec::Dsl.new(self)
9
+
10
+ # RSpec files
11
+ rspec = dsl.rspec
12
+ watch(rspec.spec_helper) { rspec.spec_dir }
13
+ watch(rspec.spec_support) { rspec.spec_dir }
14
+ watch(rspec.spec_files)
15
+
16
+ # Ruby files
17
+ ruby = dsl.ruby
18
+ dsl.watch_spec_files_for(ruby.lib_files)
19
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2020 Fabio Gallonetto <fabio.gallonetto@imaginecurve.com>
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # danger-gitlab_reviewbot
2
+
3
+ A description of danger-gitlab_reviewbot.
4
+
5
+ ## Installation
6
+
7
+ $ gem install danger-gitlab_reviewbot
8
+
9
+ ## Usage
10
+
11
+ Methods and attributes from this plugin are available in
12
+ your `Dangerfile` under the `gitlab_reviewbot` namespace.
13
+
14
+ ## Development
15
+
16
+ 1. Clone this repo
17
+ 2. Run `bundle install` to setup dependencies.
18
+ 3. Run `bundle exec rake spec` to run the tests.
19
+ 4. Use `bundle exec guard` to automatically have tests run as you make changes.
20
+ 5. Make your changes.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:specs)
6
+
7
+ task default: :specs
8
+
9
+ task :spec do
10
+ Rake::Task['specs'].invoke
11
+ # Rake::Task['rubocop'].invoke
12
+ Rake::Task['spec_docs'].invoke
13
+ end
14
+
15
+ desc 'Run RuboCop on the lib/specs directory'
16
+ RuboCop::RakeTask.new(:rubocop) do |task|
17
+ task.patterns = ['lib/**/*.rb', 'spec/**/*.rb']
18
+ end
19
+
20
+ desc 'Ensure that the plugin passes `danger plugins lint`'
21
+ task :spec_docs do
22
+ sh 'bundle exec danger plugins lint'
23
+ end
@@ -0,0 +1,50 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gitlab_reviewbot/gem_version.rb'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'danger-gitlab_reviewbot'
8
+ spec.version = GitlabReviewbot::VERSION
9
+ spec.authors = ['Fabio Gallonetto']
10
+ spec.email = ['fabio.gallonetto@curve.com']
11
+ spec.description = %q{A review raffle bot for Gitlab }
12
+ spec.summary = %q{A review raffle bot for Gitlab.}
13
+ spec.homepage = 'https://github.com/curve-technology/danger-gitlab_reviewbot'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'danger-plugin-api', '~> 1.0'
22
+ spec.add_runtime_dependency 'danger-gitlab'
23
+
24
+ # General ruby development
25
+ spec.add_development_dependency 'bundler', '~> 2.1'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+
28
+ # Testing support
29
+ spec.add_development_dependency 'rspec', '~> 3.4'
30
+
31
+ # Linting code and docs
32
+ spec.add_development_dependency "rubocop"
33
+ spec.add_development_dependency "yard"
34
+
35
+ # Makes testing easy via `bundle exec guard`
36
+ spec.add_development_dependency 'guard', '~> 2.14'
37
+ spec.add_development_dependency 'guard-rspec', '~> 4.7'
38
+
39
+ # If you want to work on older builds of ruby
40
+ spec.add_development_dependency 'listen', '3.0.7'
41
+
42
+ # This gives you the chance to run a REPL inside your tests
43
+ # via:
44
+ #
45
+ # require 'pry'
46
+ # binding.pry
47
+ #
48
+ # This will stop test execution and let you inspect the results
49
+ spec.add_development_dependency 'pry'
50
+ end
@@ -0,0 +1 @@
1
+ require "gitlab_reviewbot/gem_version"
@@ -0,0 +1 @@
1
+ require "gitlab_reviewbot/plugin"
@@ -0,0 +1,3 @@
1
+ module GitlabReviewbot
2
+ VERSION = "1.0.0".freeze
3
+ end
@@ -0,0 +1,61 @@
1
+ require 'gitlab'
2
+
3
+ module Gitlab
4
+ class User
5
+ attr_accessor :username
6
+ attr_accessor :id
7
+ attr_accessor :review_count
8
+
9
+ def initialize(id, username, review_count = 0)
10
+ @id = id
11
+ @username = username
12
+ @review_count = review_count
13
+ end
14
+ end
15
+
16
+ class Client < API
17
+ def fetch_users_for_group(group_name)
18
+ group_id = search_group(group_name)
19
+ return nil if group_id.nil?
20
+
21
+ res = group_members(group_id)
22
+
23
+ developer_access_level = 30
24
+ res.select { |u| u.state == 'active' && u.access_level >= developer_access_level }.map { |u| User.new(u.id, u.username) }
25
+ end
26
+
27
+ def assign_mr_to_users(project_id, mr_iid, users)
28
+ user_ids = users.map(&:id)
29
+ update_merge_request(project_id, mr_iid, 'assignee_ids' => user_ids)
30
+ end
31
+
32
+ def fetch_author_for_mr(project_id, mr_iid)
33
+ res = merge_request(project_id, mr_iid)
34
+ User.new(res.author.id, res.author.name)
35
+ end
36
+
37
+ def fetch_mrs_requiring_review(project_id)
38
+ merge_requests(project_id, :state => 'opened', :per_page => '100').select { |mr| mr.merge_status != 'can_be_merged' }
39
+ end
40
+
41
+ def users_with_pending_mr_review(project_id)
42
+ outstanding_mrs = fetch_mrs_requiring_review(project_id)
43
+ outstanding_mrs.reduce([]) { |acc, mr| acc + mr.assignees}
44
+ .map { |a| User.new(a['id'], a['username']) }
45
+ end
46
+
47
+ private
48
+ def search_group(group_name)
49
+ short_name = group_name.split('/').last
50
+ res = group_search(short_name)
51
+ res = res.find { |i| i.full_path == group_name }
52
+
53
+ if res.nil?
54
+ nil
55
+ else
56
+ res.id
57
+ end
58
+ end
59
+ end
60
+ end
61
+
@@ -0,0 +1,91 @@
1
+ require 'gitlab_reviewbot/strategies'
2
+
3
+
4
+ module Danger
5
+ # This is your plugin class. Any attributes or methods you expose here will
6
+ # be available from within your Dangerfile.
7
+ #
8
+ # To be published on the Danger plugins site, you will need to have
9
+ # the public interface documented. Danger uses [YARD](http://yardoc.org/)
10
+ # for generating documentation from your plugin source, and you can verify
11
+ # by running `danger plugins lint` or `bundle exec rake spec`.
12
+ #
13
+ # You should replace these comments with a public description of your library.
14
+ #
15
+ # @example Ensure people are well warned about merging on Mondays
16
+ #
17
+ # my_plugin.warn_on_mondays
18
+ #
19
+ # @see Fabio Gallonetto/danger-gitlab_reviewbot
20
+ # @tags monday, weekends, time, rattata
21
+ #
22
+ class DangerGitlabReviewbot < Plugin
23
+
24
+ # Define the group to take the reviewers from.
25
+ # NOTE: This is the group full path as in 'tech/iOS' instead of just the group name
26
+ #
27
+ # @return String
28
+ attr_accessor :gitlab_group
29
+
30
+ # Define the amount of reviewers to add to the merge requests.
31
+ # Default is 1.
32
+ # NOTE: The plugin won't remove existing assigned reviewers
33
+ #
34
+ # @return Int
35
+ attr_accessor :assignees_amount
36
+ def assignees_amount
37
+ @assignees_amount || 1
38
+ end
39
+
40
+ # Define the strategy for chosing reviewers.
41
+ # Valid values are:
42
+ # * Danger::AssignStrategies::RandomStrategy - assigns N reviewers at random from the group
43
+ # (excluding the author).
44
+ # * Danger::AssignStrategies::LeastBusyStrategy - assign the N users with the least amount of open MRs
45
+ # to review
46
+ #
47
+ attr_accessor :strategy
48
+ def strategy
49
+ @strategy || Danger::AssignStrategies::RandomStrategy
50
+ end
51
+
52
+ # Add verbosity to the logs
53
+ # @return Bool
54
+ attr_accessor :verbose
55
+
56
+ # Call this method from the Dangerfile to assign reviewers to your merge requests
57
+ # @return The usernames list of assigned reviewes [Array<String>]
58
+ #
59
+ def assign!
60
+ project_id = ENV['CI_PROJECT_ID']
61
+ mr_iid = ENV['CI_MERGE_REQUEST_IID']
62
+ if mr_iid.nil?
63
+ raise "Env variable CI_MERGE_REQUEST_IID doesn't point to a valid merge request iid"
64
+ end
65
+
66
+ if project_id.nil?
67
+ raise "Env variable CI_PROJECT_ID doesn't point to a valid project id"
68
+ end
69
+
70
+ current_assignees = (ENV['CI_MERGE_REQUEST_ASSIGNEES'] || '').split(',')
71
+ required_assignees_count = [assignees_amount - current_assignees.length, 0].max
72
+
73
+ puts "Project ID: #{project_id}" if @verbose
74
+ puts "MR IID: #{mr_iid}" if @verbose
75
+ puts "Currently assigned: #{current_assignees}" if @verbose
76
+ puts "Required: #{required_assignees_count}" if @verbose
77
+ if required_assignees_count == 0
78
+ puts "Nothing to do" if @verbose
79
+ return
80
+ end
81
+
82
+ strategy_class = strategy.new(client: gitlab.api, project: project_id, mr: mr_iid, group: gitlab_group)
83
+
84
+ assignees = strategy_class.assign! required_assignees_count
85
+
86
+ puts "Assigning: #{assignees}" if @verbose
87
+ return assignees
88
+ end
89
+ end
90
+ end
91
+
@@ -0,0 +1,24 @@
1
+ require 'gitlab_reviewbot/gitlab'
2
+
3
+ module Danger
4
+ module AssignStrategies
5
+ class LeastBusyStrategy < Strategy
6
+ def assignees(amount)
7
+ review_counter = client.fetch_users_for_group(group_name).reduce({}) do |counter, user|
8
+ counter[user.id] = user
9
+ counter
10
+ end
11
+
12
+ users = client.users_with_pending_mr_review(project_id) do |counter, user|
13
+ next if counter[user.id].nil?
14
+ counter[user.id].review_count += 1
15
+ counter
16
+ end
17
+ users.filter { |u| u.id != author.id }
18
+ .sort_by(&:review_count)
19
+ .last(amount)
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,13 @@
1
+ require 'gitlab_reviewbot/gitlab'
2
+
3
+ module Danger
4
+ module AssignStrategies
5
+ class RandomStrategy < Strategy
6
+ def assignees(amount)
7
+ client.fetch_users_for_group(group_name)
8
+ .filter { |u| u.id != author.id }
9
+ .sample(amount)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,36 @@
1
+ module Danger
2
+ module AssignStrategies
3
+ class Strategy
4
+ attr_accessor :mr_iid
5
+ attr_accessor :project_id
6
+ attr_accessor :group_name
7
+ attr_accessor :client
8
+
9
+ def initialize(client:, project:, mr:, group:)
10
+ @client = client
11
+ @project_id = project
12
+ @mr_iid = mr
13
+ @group_name = group
14
+ end
15
+
16
+ def assign!(amount)
17
+ to_be_assigned = assignees(amount)
18
+ response = client.assign_mr_to_users(project_id, mr_iid, to_be_assigned)
19
+ return to_be_assigned.map(&:username)
20
+ end
21
+
22
+ def assignees(amount)
23
+ raise "To be implemented in the subclasses"
24
+ end
25
+
26
+ def author
27
+ client.fetch_author_for_mr(@project_id, @mr_iid)
28
+ end
29
+
30
+ def users_in_group
31
+ client.fetch_users_for_group(group_name)
32
+ end
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,8 @@
1
+ module Danger
2
+ module AssignStrategies
3
+ require 'gitlab_reviewbot/strategies/strategy'
4
+ require 'gitlab_reviewbot/strategies/random'
5
+ require 'gitlab_reviewbot/strategies/least_busy'
6
+ end
7
+ end
8
+
@@ -0,0 +1,75 @@
1
+ require File.expand_path("../spec_helper", __FILE__)
2
+
3
+ module Danger
4
+ describe Danger::DangerGitlabReviewbot do
5
+ it "should be a plugin" do
6
+ expect(Danger::DangerGitlabReviewbot.new(nil)).to be_a Danger::Plugin
7
+ end
8
+
9
+ #
10
+ # You should test your custom attributes and methods here
11
+ #
12
+ describe "with Dangerfile" do
13
+ before do
14
+ testing_env.each { |k,v| ENV[k] = "#{v}" }
15
+
16
+ @dangerfile = testing_dangerfile
17
+ @plugin = @dangerfile.gitlab_reviewbot
18
+ @plugin.strategy = Danger::AssignStrategies::RandomStrategy
19
+ @strategy_mock = instance_double(Danger::AssignStrategies::Strategy)
20
+ allow(Danger::AssignStrategies::RandomStrategy).to receive(:new).and_return(@strategy_mock)
21
+ end
22
+
23
+ it "Assign one reviewer" do
24
+ @plugin.gitlab_group = 'tech/ios'
25
+
26
+ expect(@strategy_mock).to receive(:assign!).with(1).and_return(['Sam'])
27
+
28
+ @plugin.assign!
29
+ end
30
+ it "Assign one reviewer" do
31
+ @plugin.gitlab_group = 'tech/ios'
32
+
33
+ expect(@strategy_mock).to receive(:assign!).with(1).and_return(['Sam'])
34
+
35
+ @plugin.assign!
36
+ end
37
+
38
+ it "Assign multiple reviewers" do
39
+ @plugin.gitlab_group = 'tech/ios'
40
+ @plugin.assignees_amount = 2
41
+
42
+ expect(@strategy_mock).to receive(:assign!).with(2).and_return(['Sam, Nic'])
43
+
44
+ @plugin.assign!
45
+ end
46
+
47
+ it "Doesn't assign if already asssigned" do
48
+ ENV['CI_MERGE_REQUEST_ASSIGNEES'] = 'Sam'
49
+ @plugin.gitlab_group = 'tech/ios'
50
+
51
+ expect(@strategy_mock).not_to receive(:assign!)
52
+
53
+ @plugin.assign!
54
+ end
55
+
56
+ it "Only assigns delta" do
57
+ ENV['CI_MERGE_REQUEST_ASSIGNEES'] = 'Sam,Nic'
58
+ @plugin.gitlab_group = 'tech/ios'
59
+ @plugin.assignees_amount = 3
60
+
61
+ expect(@strategy_mock).to receive(:assign!).with(1).and_return(['Rob'])
62
+
63
+ @plugin.assign!
64
+ end
65
+
66
+ ['CI_PROJECT_ID', 'CI_MERGE_REQUEST_IID'].each do |var|
67
+ it "Fails when required #{var} variables are not available" do
68
+ ENV[var] = nil
69
+ expect{@plugin.assign!}.to raise_error(RuntimeError)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+
@@ -0,0 +1,47 @@
1
+ require File.expand_path("../spec_helper", __FILE__)
2
+
3
+ module Danger
4
+ describe Danger::AssignStrategies::RandomStrategy do
5
+ before do
6
+ testing_env.each { |k,v| ENV[k] = "#{v}" }
7
+ @dangerfile = testing_dangerfile
8
+
9
+ @mock_client = double(Gitlab::Client)
10
+ @author = Gitlab::User.new(1, 'Nic', 0)
11
+ @members = [@author, Gitlab::User.new(2, 'Tom'), Gitlab::User.new(3, 'Sam')]
12
+ allow(@mock_client).to receive(:fetch_author_for_mr).and_return(@author)
13
+ allow(@mock_client).to receive(:fetch_users_for_group).with(2200).and_return(@members)
14
+
15
+ @strategy = AssignStrategies::LeastBusyStrategy.new(client: @mock_client, project: 10, mr: 110, group: 2200)
16
+ end
17
+
18
+ it "Assign the one least busy" do
19
+ users_with_pending_mr_review = [@author, Gitlab::User.new(2, 'Tom', 1), Gitlab::User.new(3, 'Sam',2)]
20
+ expect(@mock_client).to receive(:users_with_pending_mr_review).and_return(users_with_pending_mr_review)
21
+
22
+ expect(@mock_client).to receive(:assign_mr_to_users) do |project, mr, users|
23
+ expect(project).to be == 10
24
+ expect(mr).to be == 110
25
+ expect(users).target.count eq 1
26
+ expect(users).target[0].username == 'Tom'
27
+ end
28
+
29
+ @strategy.assign!(1)
30
+ end
31
+
32
+ it "Assign the one least busy (if two are available)" do
33
+ users_with_pending_mr_review = [@author, Gitlab::User.new(2, 'Tom', 1), Gitlab::User.new(3, 'Sam',1)]
34
+ expect(@mock_client).to receive(:users_with_pending_mr_review).and_return(users_with_pending_mr_review)
35
+
36
+ expect(@mock_client).to receive(:assign_mr_to_users) do |project, mr, users|
37
+ expect(project).to be == 10
38
+ expect(mr).to be == 110
39
+ expect(users).target.count eq 1
40
+ expect(users).target[0].username == 'Tom'
41
+ end
42
+
43
+ @strategy.assign!(1)
44
+ end
45
+ end
46
+ end
47
+
@@ -0,0 +1,40 @@
1
+ require File.expand_path("../spec_helper", __FILE__)
2
+
3
+ module Danger
4
+ describe Danger::AssignStrategies::RandomStrategy do
5
+ before do
6
+ testing_env.each { |k,v| ENV[k] = "#{v}" }
7
+ @dangerfile = testing_dangerfile
8
+
9
+ @mock_client = double(Gitlab::Client)
10
+ @author = Gitlab::User.new(1, 'Nic')
11
+ @members = [@author, Gitlab::User.new(2, 'Tom'), Gitlab::User.new(3, 'Sam')]
12
+ allow(@mock_client).to receive(:fetch_author_for_mr).and_return(@author)
13
+ allow(@mock_client).to receive(:fetch_users_for_group).with(2200).and_return(@members)
14
+
15
+ @strategy = AssignStrategies::RandomStrategy.new(client: @mock_client, project: 10, mr: 110, group: 2200)
16
+ end
17
+
18
+ it "Assign the right amount of reviewers" do
19
+ expect(@mock_client).to receive(:assign_mr_to_users) do |project, mr, users|
20
+ expect(project).to be == 10
21
+ expect(mr).to be == 110
22
+ expect(users).target.count eq 2
23
+ end
24
+
25
+ @strategy.assign!(2)
26
+ end
27
+
28
+ it "Doesn't assign author" do
29
+ expect(@mock_client).to receive(:assign_mr_to_users) do |project, mr, users|
30
+ expect(project).to be == 10
31
+ expect(mr).to be == 110
32
+ expect(users).target.count eq 2
33
+ end
34
+
35
+ @strategy.assign!(3)
36
+ end
37
+
38
+ end
39
+ end
40
+
@@ -0,0 +1,68 @@
1
+ require "pathname"
2
+ ROOT = Pathname.new(File.expand_path("../../", __FILE__))
3
+ $:.unshift((ROOT + "lib").to_s)
4
+ $:.unshift((ROOT + "spec").to_s)
5
+
6
+ require "bundler/setup"
7
+ require "pry"
8
+
9
+ require "rspec"
10
+ require "danger"
11
+
12
+ if `git remote -v` == ''
13
+ puts "You cannot run tests without setting a local git remote on this repo"
14
+ puts "It's a weird side-effect of Danger's internals."
15
+ exit(0)
16
+ end
17
+
18
+ # Use coloured output, it's the best.
19
+ RSpec.configure do |config|
20
+ config.filter_gems_from_backtrace "bundler"
21
+ config.color = true
22
+ config.tty = true
23
+ end
24
+
25
+ require "danger_plugin"
26
+
27
+ # These functions are a subset of https://github.com/danger/danger/blob/master/spec/spec_helper.rb
28
+ # If you are expanding these files, see if it's already been done ^.
29
+
30
+ # A silent version of the user interface,
31
+ # it comes with an extra function `.string` which will
32
+ # strip all ANSI colours from the string.
33
+
34
+ # rubocop:disable Lint/NestedMethodDefinition
35
+ def testing_ui
36
+ @output = StringIO.new
37
+ def @output.winsize
38
+ [20, 9999]
39
+ end
40
+
41
+ cork = Cork::Board.new(out: @output)
42
+ def cork.string
43
+ out.string.gsub(/\e\[([;\d]+)?m/, "")
44
+ end
45
+ cork
46
+ end
47
+ # rubocop:enable Lint/NestedMethodDefinition
48
+
49
+ # Example environment (ENV) that would come from
50
+ # running a PR on TravisCI
51
+ def testing_env
52
+ {
53
+ 'CI_MERGE_REQUEST_IID' => '549',
54
+ 'CI_MERGE_REQUEST_PROJECT_PATH' => '...',
55
+ 'CI_MERGE_REQUEST_PROJECT_URL' => '...',
56
+ 'DANGER_GITLAB_HOST' => 'github.com', # This needs to be the same as where the repo is stored due to Danger internals :facepalm:
57
+ 'CI_API_V4_URL' => "https://gitlab.com/api/v4",
58
+ 'CI_PROJECT_ID' => '346',
59
+ "GITLAB_CI" => true,
60
+ "DANGER_GITLAB_API_TOKEN" => "token-token-token"
61
+ }
62
+ end
63
+
64
+ # A stubbed out Dangerfile for use in tests
65
+ def testing_dangerfile
66
+ env = Danger::EnvironmentManager.new(testing_env)
67
+ Danger::Dangerfile.new(env, testing_ui)
68
+ end
metadata ADDED
@@ -0,0 +1,223 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: danger-gitlab_reviewbot
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Fabio Gallonetto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: danger-plugin-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: danger-gitlab
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.14'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '2.14'
125
+ - !ruby/object:Gem::Dependency
126
+ name: guard-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '4.7'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '4.7'
139
+ - !ruby/object:Gem::Dependency
140
+ name: listen
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '='
144
+ - !ruby/object:Gem::Version
145
+ version: 3.0.7
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '='
151
+ - !ruby/object:Gem::Version
152
+ version: 3.0.7
153
+ - !ruby/object:Gem::Dependency
154
+ name: pry
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: 'A review raffle bot for Gitlab '
168
+ email:
169
+ - fabio.gallonetto@curve.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - ".rubocop.yml"
176
+ - Gemfile
177
+ - Gemfile.lock
178
+ - Guardfile
179
+ - LICENSE.txt
180
+ - README.md
181
+ - Rakefile
182
+ - danger-gitlab_reviewbot.gemspec
183
+ - lib/danger_gitlab_reviewbot.rb
184
+ - lib/danger_plugin.rb
185
+ - lib/gitlab_reviewbot/gem_version.rb
186
+ - lib/gitlab_reviewbot/gitlab.rb
187
+ - lib/gitlab_reviewbot/plugin.rb
188
+ - lib/gitlab_reviewbot/strategies.rb
189
+ - lib/gitlab_reviewbot/strategies/least_busy.rb
190
+ - lib/gitlab_reviewbot/strategies/random.rb
191
+ - lib/gitlab_reviewbot/strategies/strategy.rb
192
+ - spec/gitlab_reviewbot_spec.rb
193
+ - spec/least_busy_strategy_spec.rb
194
+ - spec/random_strategy_spec.rb
195
+ - spec/spec_helper.rb
196
+ homepage: https://github.com/curve-technology/danger-gitlab_reviewbot
197
+ licenses:
198
+ - MIT
199
+ metadata: {}
200
+ post_install_message:
201
+ rdoc_options: []
202
+ require_paths:
203
+ - lib
204
+ required_ruby_version: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ required_rubygems_version: !ruby/object:Gem::Requirement
210
+ requirements:
211
+ - - ">="
212
+ - !ruby/object:Gem::Version
213
+ version: '0'
214
+ requirements: []
215
+ rubygems_version: 3.0.3
216
+ signing_key:
217
+ specification_version: 4
218
+ summary: A review raffle bot for Gitlab.
219
+ test_files:
220
+ - spec/gitlab_reviewbot_spec.rb
221
+ - spec/least_busy_strategy_spec.rb
222
+ - spec/random_strategy_spec.rb
223
+ - spec/spec_helper.rb