diff --git a/Gemfile b/Gemfile index f88fcfa93a5865b52010414ad2c74f02bdd0906e..4083cab26aa7b28399a2d39e1f7edebd65c6a9a8 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 4a64144fe0d2e5ae31354c8c5ee58d758cd916c9..1b593fceb40e583a84200191aac2e2f4c8d90b57 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 7080de9403c36b08b3872b4d562013a849303b30..f8e537d9de8fcd438335b3e8d4f7f5451c87e272 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 20265e474734f53ad4ffe159711d7411e7fea984..f00c6def4ea10468377f14a444e40021e6de2387 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 0000000000000000000000000000000000000000..255080dc12a5a930fc3084bb6ac80f2c269325f5 --- /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 37f9f24e783683a86e9c0f9addbd1aaff00591a8..df1ce2f957a12be091aff98b4fa5b74c7b59f029 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 0000000000000000000000000000000000000000..cc8b214a88617869b6e477923c8ebeb24d6d11db --- /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 68028afd4fc9326d7fe80a9eeceebab7c4054f19..722c6e6ad249e6a374b37b477eb2a09e03b80eda 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 11c38ab613d102d5ccb7f8549f95d7d0e63b9fe8..300b47631780246141f01b1ed92921165a480f70 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 0000000000000000000000000000000000000000..600439de01a6dff32df0a2519f06e47a748f28cd --- /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 e7dc9d7ee63cd3e9b26c8ebe04e50d7ebcb1b0ec..f33a5da163de81b690ed20d157d38fc5823300f3 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