FIX: Allow dots in repo names (#75)

FIX: Allow dots in repo names (#75)

Rails does not allow dots in route params by default. This can be allowed using a route constraint. I used the same one we use for usernames in core. Without this, a 404 error is raised on has_webhooks for any repo with a . in the name.

diff --git a/plugin.rb b/plugin.rb
index 2f1e13f..6eda43a 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -228,7 +228,10 @@ after_initialize do
 
       scope format: true, constraints: { format: 'json' } do
         resources :organizations, only: [:index] do
-          resources :repos, only: [:index] do
+
+          # need to allow dots in the id, use the same username
+          # regex from core
+          resources :repos, only: [:index], id: /[\w.\-]+?/ do
             member do
               get '/has-configured-webhook' => 'repos#has_configured_webhook'
               post '/configure-webhook' => 'repos#configure_webhook'
diff --git a/spec/requests/discourse_code_review/repos_controller_spec.rb b/spec/requests/discourse_code_review/repos_controller_spec.rb
index 230a909..c58f5c1 100644
--- a/spec/requests/discourse_code_review/repos_controller_spec.rb
+++ b/spec/requests/discourse_code_review/repos_controller_spec.rb
@@ -105,9 +105,10 @@ describe DiscourseCodeReview::ReposController do
       end
 
       context "when a webhook is configured" do
+        let(:repo_name) { "repo" }
         let!(:client) do
           client = mock
-          client.stubs(:hooks).with('org/repo').returns([
+          client.stubs(:hooks).with("org/#{repo_name}").returns([
             {
               events: webhook_events,
               config: {
@@ -125,9 +126,19 @@ describe DiscourseCodeReview::ReposController do
         end
 
         it "says yes" do
-          get '/admin/plugins/code-review/organizations/org/repos/repo/has-configured-webhook.json'
+          get "/admin/plugins/code-review/organizations/org/repos/#{repo_name}/has-configured-webhook.json"
           expect(JSON.parse(response.body)).to eq('has_configured_webhook' => true)
         end
+
+        context "when repo name has a . in it" do
+          let(:repo_name) { "Some-coolrepo.org" }
+
+          it "returns the right response" do
+            get "/admin/plugins/code-review/organizations/org/repos/#{repo_name}/has-configured-webhook.json"
+            expect(response.status).to eq(200)
+            expect(JSON.parse(response.body)).to eq('has_configured_webhook' => true)
+          end
+        end
       end
 
       context "when no webhook is configured" do

GitHub sha: feda2b27b71f2f86fbb7b8ce63ddcc64298793cd

This commit appears in #75 which was approved by lis2. It was merged by martin.