Skip to content

Commit

Permalink
Merge pull request #408 from europ/Review_command
Browse files Browse the repository at this point in the history
Review request command
  • Loading branch information
bdunne committed Mar 1, 2018
2 parents f764857 + 01b7dd0 commit 9c5a36a
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ gem 'more_core_extensions', '~> 2.0.0', :require => 'more_core_extensions/all'
gem 'rubocop', '~> 0.52.0', :require => false
gem 'rugged', :require => false

gem 'octokit', '~> 4.6.0', :require => false
gem 'octokit', '~> 4.8.0', :require => false
gem 'faraday', '~> 0.9.1'
gem 'faraday-http-cache', '~> 2.0.0'

Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ GEM
net-http-pipeline (1.0.1)
nokogiri (1.8.1)
mini_portile2 (~> 2.3.0)
octokit (4.6.2)
octokit (4.8.0)
sawyer (~> 0.8.0, >= 0.5.3)
parallel (1.12.1)
parser (2.4.0.2)
Expand Down Expand Up @@ -374,7 +374,7 @@ DEPENDENCIES
listen
minigit (~> 0.0.4)
more_core_extensions (~> 2.0.0)
octokit (~> 4.6.0)
octokit (~> 4.8.0)
pg
rails (~> 4.2.4)
rspec
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ underscores replaced with hyphens.

Example: `@miq-bot assign @user`

- **`add_reviewer [@]user`**
Request for pull request review the specified user. The leading `@` for the
user is optional. The user must be in the Assignees list.

Example: `@miq-bot add_reviewer @user`

- **`set_milestone milestone_name`**
Set the specified milestone on the issue. Do not wrap the
milestone in quotes.
Expand Down
1 change: 1 addition & 0 deletions lib/github_notification_monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class GithubNotificationMonitor
"remove_label" => :remove_labels,
"rm_label" => :remove_labels,
"assign" => :assign,
"add_reviewer" => :add_reviewer,
"set_milestone" => :set_milestone
).freeze

Expand Down
17 changes: 17 additions & 0 deletions lib/github_service/commands/add_reviewer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module GithubService
module Commands
class AddReviewer < Base
private

def _execute(issuer:, value:)
user = value.strip.delete('@')

if valid_assignee?(user)
issue.add_reviewer(user)
else
issue.add_comment("@#{issuer} '#{user}' is an invalid reviewer, ignoring...")
end
end
end
end
end
8 changes: 0 additions & 8 deletions lib/github_service/commands/assign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ def _execute(issuer:, value:)
issue.add_comment("@#{issuer} '#{user}' is an invalid assignee, ignoring...")
end
end

def valid_assignee?(user)
# First reload the cache if it's an invalid assignee
GithubService.refresh_assignees(issue.fq_repo_name) unless GithubService.valid_assignee?(issue.fq_repo_name, user)

# Then see if it's *still* invalid
GithubService.valid_assignee?(issue.fq_repo_name, user)
end
end
end
end
8 changes: 8 additions & 0 deletions lib/github_service/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ def user_permitted?(issuer)
end
end

def valid_assignee?(user)
# First reload the cache if it's an invalid assignee
GithubService.refresh_assignees(issue.fq_repo_name) unless GithubService.valid_assignee?(issue.fq_repo_name, user)

# Then see if it's *still* invalid
GithubService.valid_assignee?(issue.fq_repo_name, user)
end

VALID_RESTRICTIONS = [:organization].freeze

module CommandMethods
Expand Down
4 changes: 4 additions & 0 deletions lib/github_service/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def assign(user)
GithubService.update_issue(fq_repo_name, number, "assignee" => user)
end

def add_reviewer(user)
GithubService.request_pull_request_review(fq_repo_name, number, [user]) if pull_request?
end

def set_milestone(milestone)
if GithubService.valid_milestone?(fq_repo_name, milestone)
milestone_id = GithubService.milestones(fq_repo_name)[milestone]
Expand Down
33 changes: 33 additions & 0 deletions spec/lib/github_service/commands/add_reviewer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'spec_helper'

RSpec.describe GithubService::Commands::AddReviewer do
subject { described_class.new(issue) }
let(:issue) { double(:fq_repo_name => "org/repo") }
let(:command_issuer) { "nickname" }

before do
allow(GithubService).to receive(:valid_assignee?).with("org/repo", "good_user") { true }
allow(GithubService).to receive(:valid_assignee?).with("org/repo", "bad_user") { false }
end

after do
subject.execute!(:issuer => command_issuer, :value => command_value)
end

context "with a valid user" do
let(:command_value) { "good_user" }

it "review request that user" do
expect(issue).to receive(:add_reviewer).with("good_user")
end
end

context "with an invalid user" do
let(:command_value) { "bad_user" }

it "does not review request, reports failure" do
expect(issue).not_to receive(:add_reviewer)
expect(issue).to receive(:add_comment).with("@#{command_issuer} 'bad_user' is an invalid reviewer, ignoring...")
end
end
end

0 comments on commit 9c5a36a

Please sign in to comment.