From 1ab4e0b097ced0114f4c9e49cfd1475eb5484633 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Fri, 25 Feb 2022 11:30:31 +0100
Subject: [PATCH] endpoint

---
 .../admin/communication/websites_controller.rb           | 2 +-
 app/models/communication/website.rb                      | 1 +
 app/models/communication/website/configs/base_url.rb     | 1 +
 app/models/communication/website/configs/permalinks.rb   | 1 +
 app/services/git/providers/abstract.rb                   | 5 +++--
 app/services/git/providers/gitlab.rb                     | 9 ++++++++-
 app/services/git/repository.rb                           | 4 +++-
 app/views/admin/communication/websites/_form.html.erb    | 5 ++---
 ...5101556_add_git_endpoint_to_communication_websites.rb | 5 +++++
 db/schema.rb                                             | 3 ++-
 test/fixtures/communication/websites.yml                 | 1 +
 11 files changed, 28 insertions(+), 9 deletions(-)
 create mode 100644 db/migrate/20220225101556_add_git_endpoint_to_communication_websites.rb

diff --git a/app/controllers/admin/communication/websites_controller.rb b/app/controllers/admin/communication/websites_controller.rb
index f8e537d9d..ff18a21ad 100644
--- a/app/controllers/admin/communication/websites_controller.rb
+++ b/app/controllers/admin/communication/websites_controller.rb
@@ -67,7 +67,7 @@ class Admin::Communication::WebsitesController < Admin::Communication::Applicati
 
   def website_params
     params.require(:communication_website).permit(
-      :name, :url, :repository, :access_token, :about_type, :about_id, :git_provider
+      :name, :url, :repository, :access_token, :about_type, :about_id, :git_provider, :git_endpoint
     )
   end
 end
diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index c55bf1712..b72ab03ad 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -5,6 +5,7 @@
 #  id                                :uuid             not null, primary key
 #  about_type                        :string           indexed => [about_id]
 #  access_token                      :string
+#  git_endpoint                      :string
 #  git_provider                      :integer          default("github")
 #  name                              :string
 #  repository                        :string
diff --git a/app/models/communication/website/configs/base_url.rb b/app/models/communication/website/configs/base_url.rb
index dfaa1d5cb..ffae45935 100644
--- a/app/models/communication/website/configs/base_url.rb
+++ b/app/models/communication/website/configs/base_url.rb
@@ -5,6 +5,7 @@
 #  id                                :uuid             not null, primary key
 #  about_type                        :string           indexed => [about_id]
 #  access_token                      :string
+#  git_endpoint                      :string
 #  git_provider                      :integer          default("github")
 #  name                              :string
 #  repository                        :string
diff --git a/app/models/communication/website/configs/permalinks.rb b/app/models/communication/website/configs/permalinks.rb
index 13ffdd2c6..0f269522a 100644
--- a/app/models/communication/website/configs/permalinks.rb
+++ b/app/models/communication/website/configs/permalinks.rb
@@ -5,6 +5,7 @@
 #  id                                :uuid             not null, primary key
 #  about_type                        :string           indexed => [about_id]
 #  access_token                      :string
+#  git_endpoint                      :string
 #  git_provider                      :integer          default("github")
 #  name                              :string
 #  repository                        :string
diff --git a/app/services/git/providers/abstract.rb b/app/services/git/providers/abstract.rb
index 90b9081a9..bd3778d0f 100644
--- a/app/services/git/providers/abstract.rb
+++ b/app/services/git/providers/abstract.rb
@@ -1,7 +1,8 @@
 class Git::Providers::Abstract
-  attr_reader :access_token, :repository
+  attr_reader :endpoint, :access_token, :repository
 
-  def initialize(access_token, repository)
+  def initialize(endpoint, access_token, repository)
+    @endpoint = endpoint
     @access_token = access_token
     @repository = repository
   end
diff --git a/app/services/git/providers/gitlab.rb b/app/services/git/providers/gitlab.rb
index b549e9c41..937b9c904 100644
--- a/app/services/git/providers/gitlab.rb
+++ b/app/services/git/providers/gitlab.rb
@@ -1,4 +1,6 @@
 class Git::Providers::Gitlab < Git::Providers::Abstract
+  DEFAULT_ENDPOINT = 'https://gitlab.com/api/v4'
+
   def create_file(path, content)
     batch << {
       action: 'create',
@@ -59,9 +61,14 @@ class Git::Providers::Gitlab < Git::Providers::Abstract
 
   protected
 
+  def endpoint
+    @endpoint.blank?  ? DEFAULT_ENDPOINT
+                      : @endpoint
+  end
+
   def client
     @client ||= Gitlab.client(
-      endpoint: 'https://gitlab.com/api/v4',
+      endpoint: endpoint,
       private_token: access_token
     )
   end
diff --git a/app/services/git/repository.rb b/app/services/git/repository.rb
index 1397acb85..318604565 100644
--- a/app/services/git/repository.rb
+++ b/app/services/git/repository.rb
@@ -34,7 +34,9 @@ class Git::Repository
   protected
 
   def provider
-    @provider ||= provider_class.new(website&.access_token, website&.repository)
+    @provider ||= provider_class.new  website&.git_endpoint,
+                                      website&.access_token,
+                                      website&.repository
   end
 
   def provider_class
diff --git a/app/views/admin/communication/websites/_form.html.erb b/app/views/admin/communication/websites/_form.html.erb
index 8227d6c68..0c21118ca 100644
--- a/app/views/admin/communication/websites/_form.html.erb
+++ b/app/views/admin/communication/websites/_form.html.erb
@@ -53,13 +53,12 @@
               <%= f.input :git_provider, include_blank: false %>
             </div>
             <div class="col-md-6">
+              <%= f.input :git_endpoint,
+                          placeholder: 'If blank, default will be used (https://gitlab.com/api/v4)' %>
               <%= f.input :access_token %>
-            </div>
-            <div class="col-md-6">
               <%= f.input :repository %>
             </div>
           </div>
-
         </div>
       </div>
     </div>
diff --git a/db/migrate/20220225101556_add_git_endpoint_to_communication_websites.rb b/db/migrate/20220225101556_add_git_endpoint_to_communication_websites.rb
new file mode 100644
index 000000000..8a821a2c6
--- /dev/null
+++ b/db/migrate/20220225101556_add_git_endpoint_to_communication_websites.rb
@@ -0,0 +1,5 @@
+class AddGitEndpointToCommunicationWebsites < ActiveRecord::Migration[6.1]
+  def change
+    add_column :communication_websites, :git_endpoint, :string
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 51b023ae3..1bfa2d8e9 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 2022_02_24_144107) do
+ActiveRecord::Schema.define(version: 2022_02_25_101556) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -356,6 +356,7 @@ ActiveRecord::Schema.define(version: 2022_02_24_144107) do
     t.string "static_pathname_researchers", default: "researchers"
     t.string "static_pathname_teachers", default: "teachers"
     t.integer "git_provider", default: 0
+    t.string "git_endpoint"
     t.index ["about_type", "about_id"], name: "index_communication_websites_on_about"
     t.index ["university_id"], name: "index_communication_websites_on_university_id"
   end
diff --git a/test/fixtures/communication/websites.yml b/test/fixtures/communication/websites.yml
index ba99545b9..33965b28a 100644
--- a/test/fixtures/communication/websites.yml
+++ b/test/fixtures/communication/websites.yml
@@ -5,6 +5,7 @@
 #  id                                :uuid             not null, primary key
 #  about_type                        :string           indexed => [about_id]
 #  access_token                      :string
+#  git_endpoint                      :string
 #  git_provider                      :integer          default("github")
 #  name                              :string
 #  repository                        :string
-- 
GitLab