From 88daa5a1320d64ffc67170922ba44530cfef9512 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Tue, 11 Oct 2022 09:29:17 +0200
Subject: [PATCH] Publishing branch (#522) and in_production website

---
 Gemfile.lock                                  | 28 +++++++++----------
 .../communication/websites_controller.rb      |  3 +-
 app/models/communication/website.rb           |  3 ++
 .../communication/website/configs/base_url.rb |  2 ++
 .../website/configs/permalinks.rb             |  2 ++
 app/services/git/providers/abstract.rb        |  5 ++--
 app/services/git/providers/github.rb          |  2 +-
 app/services/git/providers/gitlab.rb          |  8 ++++--
 app/services/git/repository.rb                |  1 +
 .../communication/websites/_form.html.erb     |  4 ++-
 app/views/server/websites/index.html.erb      |  4 +++
 config/locales/communication/en.yml           |  3 ++
 config/locales/communication/fr.yml           |  3 ++
 ...402_add_fields_to_communication_website.rb |  6 ++++
 db/schema.rb                                  |  4 ++-
 test/fixtures/communication/websites.yml      |  2 ++
 16 files changed, 58 insertions(+), 22 deletions(-)
 create mode 100644 db/migrate/20221011070402_add_fields_to_communication_website.rb

diff --git a/Gemfile.lock b/Gemfile.lock
index e6929fc42..6c1606531 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -90,8 +90,8 @@ GEM
     autoprefixer-rails (10.4.7.0)
       execjs (~> 2)
     aws-eventstream (1.2.0)
-    aws-partitions (1.637.0)
-    aws-sdk-core (3.155.0)
+    aws-partitions (1.644.0)
+    aws-sdk-core (3.159.0)
       aws-eventstream (~> 1, >= 1.0.2)
       aws-partitions (~> 1, >= 1.525.0)
       aws-sigv4 (~> 1.1)
@@ -103,7 +103,7 @@ GEM
       aws-sdk-core (~> 3, >= 3.127.0)
       aws-sdk-kms (~> 1)
       aws-sigv4 (~> 1.4)
-    aws-sigv4 (1.5.1)
+    aws-sigv4 (1.5.2)
       aws-eventstream (~> 1, >= 1.0.2)
     bcrypt (3.1.18)
     bindex (0.8.1)
@@ -146,7 +146,7 @@ GEM
       htmlentities
       metainspector (~> 5.12)
       nokogiri
-    delayed_job (4.1.10)
+    delayed_job (4.1.11)
       activesupport (>= 3.0, < 8.0)
     delayed_job_active_record (4.1.7)
       activerecord (>= 3.0, < 8.0)
@@ -176,7 +176,7 @@ GEM
     faceted_search (3.5.15)
       font-awesome-sass
       rails (>= 5.2.0)
-    faraday (2.5.2)
+    faraday (2.6.0)
       faraday-net_http (>= 2.0, < 3.1)
       ruby2_keywords (>= 0.0.4)
     faraday-cookie_jar (0.0.7)
@@ -193,7 +193,7 @@ GEM
       faraday (>= 0.8)
     faraday-multipart (1.0.4)
       multipart-post (~> 2)
-    faraday-net_http (3.0.0)
+    faraday-net_http (3.0.1)
     faraday-retry (2.0.0)
       faraday (~> 2.0)
     fastimage (2.2.6)
@@ -286,7 +286,7 @@ GEM
     mini_mime (1.1.2)
     mini_portile2 (2.8.0)
     minitest (5.16.3)
-    msgpack (1.5.6)
+    msgpack (1.6.0)
     multi_xml (0.6.0)
     multipart-post (2.2.3)
     mustermann (3.0.0)
@@ -332,7 +332,7 @@ GEM
     rack (2.2.4)
     rack-mini-profiler (2.3.4)
       rack (>= 1.2.0)
-    rack-protection (3.0.1)
+    rack-protection (3.0.2)
       rack
     rack-test (2.0.2)
       rack (>= 1.3)
@@ -400,7 +400,7 @@ GEM
     sawyer (0.9.2)
       addressable (>= 2.3.5)
       faraday (>= 0.17.3, < 3)
-    selenium-webdriver (4.4.0)
+    selenium-webdriver (4.5.0)
       childprocess (>= 0.5, < 5.0)
       rexml (~> 3.2, >= 3.2.5)
       rubyzip (>= 1.2.2, < 3.0)
@@ -420,10 +420,10 @@ GEM
     simple_form_password_with_hints (0.0.6)
       rails
       simple_form
-    sinatra (3.0.1)
+    sinatra (3.0.2)
       mustermann (~> 3.0)
       rack (~> 2.2, >= 2.2.4)
-      rack-protection (= 3.0.1)
+      rack-protection (= 3.0.2)
       tilt (~> 2.0)
     sixarm_ruby_unaccent (1.2.0)
     snaky_hash (2.0.1)
@@ -462,7 +462,7 @@ GEM
       activemodel (>= 6.0.0)
       bindex (>= 0.4.0)
       railties (>= 6.0.0)
-    webdrivers (5.1.0)
+    webdrivers (5.2.0)
       nokogiri (~> 1.6)
       rubyzip (>= 1.3.0)
       selenium-webdriver (~> 4.0)
@@ -476,7 +476,7 @@ GEM
     websocket-extensions (0.1.5)
     xpath (3.2.0)
       nokogiri (~> 1.8)
-    zeitwerk (2.6.0)
+    zeitwerk (2.6.1)
     zlib (2.1.1)
 
 PLATFORMS
@@ -551,4 +551,4 @@ RUBY VERSION
    ruby 2.7.6p219
 
 BUNDLED WITH
-   2.3.22
+   2.3.12
diff --git a/app/controllers/admin/communication/websites_controller.rb b/app/controllers/admin/communication/websites_controller.rb
index d56f1e72a..731546ff8 100644
--- a/app/controllers/admin/communication/websites_controller.rb
+++ b/app/controllers/admin/communication/websites_controller.rb
@@ -81,7 +81,8 @@ 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, :git_endpoint, :plausible_url, language_ids: []
+      :name, :url, :repository, :access_token, :about_type, :about_id, :in_production,
+      :git_provider, :git_endpoint, :git_branch, :plausible_url, language_ids: []
     )
   end
 end
diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index 9189e6942..d0ceb1f69 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -5,8 +5,10 @@
 #  id               :uuid             not null, primary key
 #  about_type       :string           indexed => [about_id]
 #  access_token     :string
+#  git_branch       :string
 #  git_endpoint     :string
 #  git_provider     :integer          default("github")
+#  in_production    :boolean          default(FALSE)
 #  name             :string
 #  plausible_url    :string
 #  repository       :string
@@ -52,6 +54,7 @@ class Communication::Website < ApplicationRecord
                           association_foreign_key: 'language_id'
 
   scope :ordered, -> { order(:name) }
+  scope :in_production, -> { where(in_production: true) }
   scope :for_search_term, -> (term) {
     where("
       unaccent(communication_websites.name) ILIKE unaccent(:term) OR
diff --git a/app/models/communication/website/configs/base_url.rb b/app/models/communication/website/configs/base_url.rb
index 2daeee8a2..080e03ddd 100644
--- a/app/models/communication/website/configs/base_url.rb
+++ b/app/models/communication/website/configs/base_url.rb
@@ -5,8 +5,10 @@
 #  id               :uuid             not null, primary key
 #  about_type       :string           indexed => [about_id]
 #  access_token     :string
+#  git_branch       :string
 #  git_endpoint     :string
 #  git_provider     :integer          default("github")
+#  in_production    :boolean          default(FALSE)
 #  name             :string
 #  plausible_url    :string
 #  repository       :string
diff --git a/app/models/communication/website/configs/permalinks.rb b/app/models/communication/website/configs/permalinks.rb
index df46d3c8f..cf836fd47 100644
--- a/app/models/communication/website/configs/permalinks.rb
+++ b/app/models/communication/website/configs/permalinks.rb
@@ -5,8 +5,10 @@
 #  id               :uuid             not null, primary key
 #  about_type       :string           indexed => [about_id]
 #  access_token     :string
+#  git_branch       :string
 #  git_endpoint     :string
 #  git_provider     :integer          default("github")
+#  in_production    :boolean          default(FALSE)
 #  name             :string
 #  plausible_url    :string
 #  repository       :string
diff --git a/app/services/git/providers/abstract.rb b/app/services/git/providers/abstract.rb
index be21a999e..f84f7f768 100644
--- a/app/services/git/providers/abstract.rb
+++ b/app/services/git/providers/abstract.rb
@@ -1,8 +1,9 @@
 class Git::Providers::Abstract
-  attr_reader :endpoint, :access_token, :repository
+  attr_reader :endpoint, :branch, :access_token, :repository
 
-  def initialize(endpoint, access_token, repository)
+  def initialize(endpoint, branch, access_token, repository)
     @endpoint = endpoint
+    @branch = branch
     @access_token = access_token
     @repository = repository
   end
diff --git a/app/services/git/providers/github.rb b/app/services/git/providers/github.rb
index f42b8b01f..f3bdc6107 100644
--- a/app/services/git/providers/github.rb
+++ b/app/services/git/providers/github.rb
@@ -75,7 +75,7 @@ class Git::Providers::Github < Git::Providers::Abstract
   end
 
   def default_branch
-    @default_branch ||= client.repo(repository)[:default_branch]
+    @default_branch ||= branch || client.repo(repository)[:default_branch]
   end
 
   def branch_sha
diff --git a/app/services/git/providers/gitlab.rb b/app/services/git/providers/gitlab.rb
index 937b9c904..37daea7eb 100644
--- a/app/services/git/providers/gitlab.rb
+++ b/app/services/git/providers/gitlab.rb
@@ -38,7 +38,7 @@ class Git::Providers::Gitlab < Git::Providers::Abstract
   def push(commit_message)
     return if !valid? || batch.empty?
     client.create_commit  repository,
-                          'main',
+                          branch,
                           commit_message,
                           batch
     true
@@ -59,6 +59,10 @@ class Git::Providers::Gitlab < Git::Providers::Abstract
     sha
   end
 
+  def branch
+    super || 'main'
+  end
+
   protected
 
   def endpoint
@@ -76,7 +80,7 @@ class Git::Providers::Gitlab < Git::Providers::Abstract
   def find(path)
     client.get_file repository,
                     path,
-                    'main'
+                    branch
   rescue
     nil
   end
diff --git a/app/services/git/repository.rb b/app/services/git/repository.rb
index 6609f0fd5..80449c074 100644
--- a/app/services/git/repository.rb
+++ b/app/services/git/repository.rb
@@ -39,6 +39,7 @@ class Git::Repository
 
   def provider
     @provider ||= provider_class.new  website&.git_endpoint,
+                                      website&.git_branch,
                                       website&.access_token,
                                       website&.repository
   end
diff --git a/app/views/admin/communication/websites/_form.html.erb b/app/views/admin/communication/websites/_form.html.erb
index 9484c19f1..64f4e9484 100644
--- a/app/views/admin/communication/websites/_form.html.erb
+++ b/app/views/admin/communication/websites/_form.html.erb
@@ -13,6 +13,7 @@
           <%= f.input :url %>
           <%= render 'admin/communication/abouts', f: f, i18n_key: 'activerecord.attributes.communication/website.about_' %>
           <%= f.association :languages, as: :check_boxes %>
+          <%= f.input :in_production %>
         </div>
       </div>
     </div>
@@ -25,11 +26,12 @@
           <div class="row">
             <div class="col-md-6">
               <%= f.input :git_provider, include_blank: false %>
+              <%= f.input :git_endpoint %>
             </div>
             <div class="col-md-6">
-              <%= f.input :git_endpoint %>
               <%= f.input :access_token %>
               <%= f.input :repository %>
+              <%= f.input :git_branch %>
             </div>
           </div>
         </div>
diff --git a/app/views/server/websites/index.html.erb b/app/views/server/websites/index.html.erb
index 747e9f8bf..e0766b309 100644
--- a/app/views/server/websites/index.html.erb
+++ b/app/views/server/websites/index.html.erb
@@ -1,11 +1,14 @@
 <% content_for :title, "#{@websites.count} #{Communication::Website.model_name.human(count: @websites.count).downcase}" %>
 
+<p><%= Communication::Website.in_production.count %> en production</p>
+
 <div class="table-responsive">
   <table class="<%= table_classes %>">
     <thead>
       <tr>
         <th><%= Communication::Website.human_attribute_name('name') %></th>
         <th><%= Communication::Website.human_attribute_name('url') %></th>
+        <th><%= Communication::Website.human_attribute_name('in_production') %></th>
         <th><%= University.model_name.human %></th>
       </tr>
     </thead>
@@ -14,6 +17,7 @@
         <tr>
           <td><%= website.name %></td>
           <td><%= link_to website.url, website.url, target: :_blank if website.url.present? %></td>
+          <td><%= t website.in_production %></td>
           <td><%= link_to website.university, [:server, website.university] %></td>
         </tr>
       <% end %>
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index 7f8b75f3d..a9cf0e144 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -59,8 +59,10 @@ en:
         about_Research::Laboratory: Laboratory website
         about_type: About
         access_token: Access token
+        git_branch: Git branch
         git_endpoint: Git endpoint
         git_provider: Git provider
+        in_production: Production
         languages: Languages
         name: Name
         repository: Repository
@@ -638,6 +640,7 @@ en:
   simple_form:
     hints:
       communication_website:
+        git_branch: 'If blank, default branch will be used'
         git_endpoint: 'If blank, default will be used (https://github.com or https://gitlab.com/api/v4)'
         languages: 'If you select at least one language the website will be considered as possibly multilingual, and therefore all urls will be prefixed with the language (/fr, /en)'
       communication_website_category:
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index a14ed5e95..5897adc44 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -59,8 +59,10 @@ fr:
         about_Research::Laboratory: Site de laboratoire
         about_type: Type de site
         access_token: Access token
+        git_branch: Branche
         git_endpoint: Point d'accès Git
         git_provider: Provider Git
+        in_production: Site en production
         languages: Langues
         name: Nom
         repository: Repository
@@ -643,6 +645,7 @@ fr:
   simple_form:
     hints:
       communication_website:
+        git_branch: 'Laisser vide pour la branche par défaut'
         git_endpoint: 'Laisser vide pour les valeurs par défaut (https://github.com ou https://gitlab.com/api/v4)'
         languages: 'Si vous sélectionnez au moins une langue le site sera considéré comme possiblement multilingue et donc toutes les urls seront préfixées avec la langue (/fr, /en)'
       communication_website_category:
diff --git a/db/migrate/20221011070402_add_fields_to_communication_website.rb b/db/migrate/20221011070402_add_fields_to_communication_website.rb
new file mode 100644
index 000000000..eb4de5479
--- /dev/null
+++ b/db/migrate/20221011070402_add_fields_to_communication_website.rb
@@ -0,0 +1,6 @@
+class AddFieldsToCommunicationWebsite < ActiveRecord::Migration[6.1]
+  def change
+    add_column :communication_websites, :git_branch, :string
+    add_column :communication_websites, :in_production, :boolean, default: false
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 8516a1e8c..776268905 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_10_03_144016) do
+ActiveRecord::Schema.define(version: 2022_10_11_070402) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -356,6 +356,8 @@ ActiveRecord::Schema.define(version: 2022_10_03_144016) do
     t.text "style"
     t.date "style_updated_at"
     t.string "plausible_url"
+    t.string "git_branch"
+    t.boolean "in_production", default: false
     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 78817fa75..a72ecbcf7 100644
--- a/test/fixtures/communication/websites.yml
+++ b/test/fixtures/communication/websites.yml
@@ -5,8 +5,10 @@
 #  id               :uuid             not null, primary key
 #  about_type       :string           indexed => [about_id]
 #  access_token     :string
+#  git_branch       :string
 #  git_endpoint     :string
 #  git_provider     :integer          default("github")
+#  in_production    :boolean          default(FALSE)
 #  name             :string
 #  plausible_url    :string
 #  repository       :string
-- 
GitLab