From a5ae1be782bb7a648afbde64bfd84278b39b8544 Mon Sep 17 00:00:00 2001 From: Arnaud Levy <contact@arnaudlevy.com> Date: Fri, 18 Feb 2022 14:31:06 +0100 Subject: [PATCH] gitlab provider wip --- Gemfile | 1 + Gemfile.lock | 14 ++++++++++++++ .../admin/communication/websites_controller.rb | 2 +- app/models/communication/website.rb | 6 ++++++ app/services/git/providers/abstract.rb | 12 ++++++++++++ app/services/git/providers/github.rb | 13 +------------ app/services/git/providers/gitlab.rb | 16 ++++++++++++++++ app/services/git/repository.rb | 7 +++++-- .../admin/communication/websites/_form.html.erb | 3 ++- ..._add_git_provider_to_communication_website.rb | 5 +++++ db/schema.rb | 3 ++- 11 files changed, 65 insertions(+), 17 deletions(-) create mode 100644 app/services/git/providers/abstract.rb create mode 100644 app/services/git/providers/gitlab.rb create mode 100644 db/migrate/20220218131148_add_git_provider_to_communication_website.rb diff --git a/Gemfile b/Gemfile index f88fcfa93..4083cab26 100644 --- a/Gemfile +++ b/Gemfile @@ -32,6 +32,7 @@ gem 'simple-navigation' gem 'kaminari' gem 'bootstrap5-kaminari-views' gem 'octokit' +gem 'gitlab' gem 'front_matter_parser' gem 'two_factor_authentication', git: 'https://github.com/noesya/two_factor_authentication.git' # gem 'two_factor_authentication', path: '../two_factor_authentication' diff --git a/Gemfile.lock b/Gemfile.lock index 4a64144fe..1b593fceb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -211,6 +211,9 @@ GEM js_cookie_rails rails sassc-rails + gitlab (4.18.0) + httparty (~> 0.18) + terminal-table (>= 1.5.1) globalid (1.0.0) activesupport (>= 5.0) has_scope (0.8.0) @@ -218,6 +221,9 @@ GEM activesupport (>= 5.2) http-cookie (1.0.4) domain_name (~> 0.5) + httparty (0.20.0) + mime-types (~> 3.0) + multi_xml (>= 0.5.2) i18n (1.9.1) concurrent-ruby (~> 1.0) i18n_data (0.15.0) @@ -272,11 +278,15 @@ GEM nesty (~> 1.0) nokogiri (~> 1.11) method_source (1.0.0) + mime-types (3.4.1) + mime-types-data (~> 3.2015) + mime-types-data (3.2022.0105) mini_magick (4.11.0) mini_mime (1.1.2) mini_portile2 (2.7.1) minitest (5.15.0) msgpack (1.4.4) + multi_xml (0.6.0) multipart-post (2.1.1) mustermann (1.1.1) ruby2_keywords (~> 0.0.1) @@ -396,6 +406,8 @@ GEM actionpack (>= 5.2) activesupport (>= 5.2) sprockets (>= 3.0.0) + terminal-table (3.0.2) + unicode-display_width (>= 1.1.1, < 3) thor (1.2.1) tilt (2.0.10) typhoeus (1.4.0) @@ -405,6 +417,7 @@ GEM unf (0.1.4) unf_ext unf_ext (0.0.8) + unicode-display_width (2.1.0) unicode_utils (1.4.0) warden (1.2.9) rack (>= 2.0.9) @@ -450,6 +463,7 @@ DEPENDENCIES figaro front_matter_parser gdpr + gitlab has_scope (~> 0.8.0) image_processing jbuilder diff --git a/app/controllers/admin/communication/websites_controller.rb b/app/controllers/admin/communication/websites_controller.rb index 7080de940..f8e537d9d 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 + :name, :url, :repository, :access_token, :about_type, :about_id, :git_provider ) end end diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb index 20265e474..f00c6def4 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_provider :integer default(0) # name :string # repository :string # static_pathname_administrators :string default("administrators") @@ -43,6 +44,11 @@ class Communication::Website < ApplicationRecord scope :ordered, -> { order(:name) } + enum git_provider: { + github: 0, + gitlab: 1 + } + def to_s "#{name}" end diff --git a/app/services/git/providers/abstract.rb b/app/services/git/providers/abstract.rb new file mode 100644 index 000000000..255080dc1 --- /dev/null +++ b/app/services/git/providers/abstract.rb @@ -0,0 +1,12 @@ +class Git::Providers::Abstract + attr_reader :access_token, :repository + + def initialize(access_token, repository) + @access_token = access_token + @repository = repository + end + + def valid? + repository.present? && access_token.present? + end +end diff --git a/app/services/git/providers/github.rb b/app/services/git/providers/github.rb index 37f9f24e7..df1ce2f95 100644 --- a/app/services/git/providers/github.rb +++ b/app/services/git/providers/github.rb @@ -1,11 +1,4 @@ -class Git::Providers::Github - attr_reader :access_token, :repository - - def initialize(access_token, repository) - @access_token = access_token - @repository = repository - end - +class Git::Providers::Github < Git::Providers::Abstract def create_file(path, content) batch << { path: path, @@ -61,10 +54,6 @@ class Git::Providers::Github sha end - def valid? - repository.present? && access_token.present? - end - protected def client diff --git a/app/services/git/providers/gitlab.rb b/app/services/git/providers/gitlab.rb new file mode 100644 index 000000000..cc8b214a8 --- /dev/null +++ b/app/services/git/providers/gitlab.rb @@ -0,0 +1,16 @@ +class Git::Providers::Gitlab < Git::Providers::Abstract + def create_file(path, content) + end + + def update_file(path, previous_path, content) + end + + def destroy_file(path) + end + + def push(commit_message) + end + + def git_sha(path) + end +end diff --git a/app/services/git/repository.rb b/app/services/git/repository.rb index 68028afd4..722c6e6ad 100644 --- a/app/services/git/repository.rb +++ b/app/services/git/repository.rb @@ -29,9 +29,12 @@ class Git::Repository protected - # Enhancement add gitlab def provider - @provider ||= Git::Providers::Github.new(website&.access_token, website&.repository) + @provider ||= provider_class.new(website&.access_token, website&.repository) + end + + def provider_class + @provider_class ||= "Git::Providers::#{website.git_provider.titleize}".constantize end def git_files diff --git a/app/views/admin/communication/websites/_form.html.erb b/app/views/admin/communication/websites/_form.html.erb index 11c38ab61..300b47631 100644 --- a/app/views/admin/communication/websites/_form.html.erb +++ b/app/views/admin/communication/websites/_form.html.erb @@ -47,14 +47,15 @@ <h5 class="card-title mb-0"><%= t('metadata') %></h5> </div> <div class="card-body"> - <%= f.input :url %> <p class="mt-4"><strong><%= t('communication.website.git') %></strong></p> <div class="row"> <div class="col-md-6"> + <%= f.input :url %> <%= f.input :repository %> </div> <div class="col-md-6"> + <%= f.input :git_provider, include_blank: false %> <%= f.input :access_token %> </div> </div> diff --git a/db/migrate/20220218131148_add_git_provider_to_communication_website.rb b/db/migrate/20220218131148_add_git_provider_to_communication_website.rb new file mode 100644 index 000000000..600439de0 --- /dev/null +++ b/db/migrate/20220218131148_add_git_provider_to_communication_website.rb @@ -0,0 +1,5 @@ +class AddGitProviderToCommunicationWebsite < ActiveRecord::Migration[6.1] + def change + add_column :communication_websites, :git_provider, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index e7dc9d7ee..f33a5da16 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_17_205201) do +ActiveRecord::Schema.define(version: 2022_02_18_131148) 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_17_205201) do t.string "static_pathname_administrators", default: "administrators" t.string "static_pathname_researchers", default: "researchers" t.string "static_pathname_teachers", default: "teachers" + t.integer "git_provider", default: 0 t.index ["about_type", "about_id"], name: "index_communication_websites_on_about" t.index ["university_id"], name: "index_communication_websites_on_university_id" end -- GitLab