Skip to content

Commit b7bd808

Browse files
markvanlanGrubba27
andauthored
FIX: Moderators can configure GitHub webhooks with the bot token (#277)
* FIX: Moderators can configure GitHub webhooks with the bot token * Update app/controllers/discourse_code_review/repos_controller.rb Co-authored-by: Gabriel Grubba <70247653+Grubba27@users.noreply.github.com> --------- Co-authored-by: Gabriel Grubba <70247653+Grubba27@users.noreply.github.com>
1 parent 1a3eaff commit b7bd808

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

app/controllers/discourse_code_review/repos_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# frozen_string_literal: true
22

33
module DiscourseCodeReview
4-
class ReposController < ::ApplicationController
4+
class ReposController < Admin::AdminController
55
requires_plugin DiscourseCodeReview::PLUGIN_NAME
66

7+
before_action :ensure_admin
78
before_action :set_organization
89
before_action :set_repo, only: %i[has_configured_webhook configure_webhook]
910

spec/requests/discourse_code_review/repos_controller_spec.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
before do
55
SiteSetting.code_review_enabled = true
66
SiteSetting.code_review_github_webhook_secret = "github webhook secret"
7+
SiteSetting.code_review_github_organizations = "org"
8+
end
9+
10+
def github_repositories(*repository_names)
11+
repository_names.map do |repository_name|
12+
repository = mock
13+
repository.stubs(:name).returns(repository_name)
14+
repository
15+
end
716
end
817

918
def set_client(client)
@@ -47,6 +56,19 @@ def set_client(client)
4756
end
4857
end
4958

59+
context "when user is a moderator" do
60+
fab!(:moderator)
61+
62+
before { sign_in(moderator) }
63+
64+
it "cannot configure webhooks" do
65+
post "/admin/plugins/code-review/organizations/org/repos/repo/configure-webhook.json"
66+
67+
expect(response.status).to eq(403)
68+
expect(response.parsed_body["error_type"]).to eq("invalid_access")
69+
end
70+
end
71+
5072
context "when user is admin" do
5173
fab!(:admin)
5274

@@ -91,6 +113,26 @@ def set_client(client)
91113
)
92114
end
93115
end
116+
117+
context "when the organization is not configured" do
118+
let!(:client) do
119+
client = mock
120+
client
121+
.stubs(:organization_repositories)
122+
.with("evil-org")
123+
.returns(github_repositories("repo"))
124+
client
125+
end
126+
127+
before { set_client(client) }
128+
129+
it "lists repositories for admins" do
130+
get "/admin/plugins/code-review/organizations/evil-org/repos.json"
131+
132+
expect(response.status).to eq(200)
133+
expect(response.parsed_body).to eq(["repo"])
134+
end
135+
end
94136
end
95137

96138
describe "#has_configured_webhook" do
@@ -256,6 +298,30 @@ def set_client(client)
256298
end
257299

258300
describe "#configure_webhook" do
301+
context "when the organization is not configured" do
302+
let!(:client) do
303+
client = mock
304+
client.stubs(:hooks).with("evil-org/repo").returns([])
305+
client.expects(:create_hook).with(
306+
"evil-org/repo",
307+
"web",
308+
webhook_config,
309+
events: webhook_events,
310+
active: true,
311+
)
312+
client
313+
end
314+
315+
before { set_client(client) }
316+
317+
it "configures webhooks for admins" do
318+
post "/admin/plugins/code-review/organizations/evil-org/repos/repo/configure-webhook.json"
319+
320+
expect(response.status).to eq(200)
321+
expect(response.parsed_body).to eq("has_configured_webhook" => true)
322+
end
323+
end
324+
259325
context "when no existing webhook hits the right url" do
260326
let!(:client) do
261327
client = mock

0 commit comments

Comments
 (0)