diff --git a/Gemfile b/Gemfile index 7a2fc16d1aef27afdb3ed431b4ef0703fcecaee9..0d676693844fe906b4e1b0c42c2011732d9fb910 100644 --- a/Gemfile +++ b/Gemfile @@ -25,6 +25,7 @@ gem 'country_select' gem 'breadcrumbs_on_rails' gem 'simple-navigation' gem 'kaminari' +gem 'octokit' # Front gem 'jquery-rails' diff --git a/Gemfile.lock b/Gemfile.lock index e2c3650415ef3f51c026db37f44c499ab3ed194b..b9b9590686c755927747d5ea4965adf760fa47bd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -137,6 +137,25 @@ GEM activesupport (>= 3.0.0) erubi (1.10.0) execjs (2.8.1) + faraday (1.7.1) + faraday-em_http (~> 1.0) + faraday-em_synchrony (~> 1.0) + faraday-excon (~> 1.1) + faraday-httpclient (~> 1.0.1) + faraday-net_http (~> 1.0) + faraday-net_http_persistent (~> 1.1) + faraday-patron (~> 1.0) + faraday-rack (~> 1.0) + multipart-post (>= 1.2, < 3) + ruby2_keywords (>= 0.0.4) + faraday-em_http (1.0.0) + faraday-em_synchrony (1.0.0) + faraday-excon (1.1.0) + faraday-httpclient (1.0.1) + faraday-net_http (1.0.1) + faraday-net_http_persistent (1.2.0) + faraday-patron (1.0.0) + faraday-rack (1.0.0) ffi (1.15.3) globalid (0.5.2) activesupport (>= 5.0) @@ -183,12 +202,16 @@ GEM mini_portile2 (2.6.1) minitest (5.14.4) msgpack (1.4.2) + multipart-post (2.1.1) mustermann (1.1.1) ruby2_keywords (~> 0.0.1) nio4r (2.5.8) nokogiri (1.12.2) mini_portile2 (~> 2.6.1) racc (~> 1.4) + octokit (4.21.0) + faraday (>= 0.9) + sawyer (~> 0.8.0, >= 0.5.3) orm_adapter (0.5.0) pg (1.2.3) popper_js (2.9.2) @@ -252,6 +275,9 @@ GEM sprockets (> 3.0) sprockets-rails tilt + sawyer (0.8.2) + addressable (>= 2.3.5) + faraday (> 0.8, < 2.0) selenium-webdriver (3.142.7) childprocess (>= 0.5, < 4.0) rubyzip (>= 1.2.2) @@ -324,6 +350,7 @@ DEPENDENCIES kamifusen kaminari listen (~> 3.3) + octokit pg (~> 1.1) puma rack-mini-profiler (~> 2.0) diff --git a/app/controllers/admin/research/journals_controller.rb b/app/controllers/admin/research/journals_controller.rb index d13022c195095544be4b467fd4c1ff5e98ff8894..4255f2d300cd4d004c5f3c90a998f6e2e0467145 100644 --- a/app/controllers/admin/research/journals_controller.rb +++ b/app/controllers/admin/research/journals_controller.rb @@ -45,6 +45,6 @@ class Admin::Research::JournalsController < Admin::Research::Journal::Applicatio protected def journal_params - params.require(:research_journal).permit(:title, :description) + params.require(:research_journal).permit(:title, :description, :access_token, :repository) end end diff --git a/app/models/research/journal.rb b/app/models/research/journal.rb index db5c3985cdfb90fbfedcf76fe0674b088e0efff0..40fc64ecfb42e4ac64f22c8442bb2d6830473404 100644 --- a/app/models/research/journal.rb +++ b/app/models/research/journal.rb @@ -3,7 +3,9 @@ # Table name: research_journals # # id :uuid not null, primary key +# access_token :string # description :text +# repository :string # title :string # created_at :datetime not null # updated_at :datetime not null diff --git a/app/models/research/journal/volume.rb b/app/models/research/journal/volume.rb index 0756dba4487d693c7d8d4ca2857af3b18092bea5..38b1f2cdcc15f5d448fb2e9a90fd9913b8db4890 100644 --- a/app/models/research/journal/volume.rb +++ b/app/models/research/journal/volume.rb @@ -26,7 +26,33 @@ class Research::Journal::Volume < ApplicationRecord belongs_to :journal, foreign_key: :research_journal_id has_many :articles, foreign_key: :research_journal_volume_id + after_save :publish + def to_s "##{ number } #{ title }" end + + protected + + def publish + return if journal.repository.blank? + data = "---\ntitle: #{ title }\nnumber: #{number}\n---" + directory = "tmp/volumes" + FileUtils.mkdir_p directory + local_file = "#{directory}/#{id}.md" + File.write local_file, data + remote_file = "_volumes/#{id}.md" + client = Octokit::Client.new access_token: journal.access_token + begin + content = client.content journal.repository, path: remote_file + sha = content[:sha] + rescue + sha = nil + end + client.create_contents journal.repository, + remote_file, + "Save volume #{ title }", + file: local_file, + sha: sha + end end diff --git a/app/views/admin/research/journals/_form.html.erb b/app/views/admin/research/journals/_form.html.erb index 0350626e53c6c76d1873dd09f028b50b5755e9ea..3b3c86b7154b7e2a86fa96685e474da19d56a989 100644 --- a/app/views/admin/research/journals/_form.html.erb +++ b/app/views/admin/research/journals/_form.html.erb @@ -2,6 +2,8 @@ <div class="row"> <div class="col-md-6"> <%= f.input :title %> + <%= f.input :repository %> + <%= f.input :access_token %> </div> <div class="col-md-6"> <%= f.input :description %> diff --git a/db/migrate/20210831122745_add_github_to_research_journals.rb b/db/migrate/20210831122745_add_github_to_research_journals.rb new file mode 100644 index 0000000000000000000000000000000000000000..5ef1fba9e34cdea3a4e21c5be6aef785fec6ce59 --- /dev/null +++ b/db/migrate/20210831122745_add_github_to_research_journals.rb @@ -0,0 +1,6 @@ +class AddGithubToResearchJournals < ActiveRecord::Migration[6.1] + def change + add_column :research_journals, :access_token, :string + add_column :research_journals, :repository, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 375624afea4e61cbed006a1adafad11aeabaa406..44e5ac9a5a875c43b661a62e270fbf0e98ee2c24 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: 2021_08_21_121439) do +ActiveRecord::Schema.define(version: 2021_08_31_122745) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -120,6 +120,8 @@ ActiveRecord::Schema.define(version: 2021_08_21_121439) do t.text "description" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.string "access_token" + t.string "repository" t.index ["university_id"], name: "index_research_journals_on_university_id" end diff --git a/docs/websites/prototype.md b/docs/websites/prototype.md index 68081275afd575f1e910e19b06b70dbee31faca4..da5e7b933f1492c19e1eb19f8f354cd258ee5905 100644 --- a/docs/websites/prototype.md +++ b/docs/websites/prototype.md @@ -17,7 +17,8 @@ ### Points à vérifier -- [ ] Lecture écriture github -- [ ] Performance avec un backend github +- [ ] Lecture écriture Github +- [ ] Performance avec un backend Github - [ ] Relations avec les auteurs et les droits (doublon DB, autre...) - [ ] Articulation avec les objets en DB (volumes et articles par ex.) +- [x] Gestion des mises à jour des templates -> utiliser un template Github diff --git a/test/fixtures/research/journals.yml b/test/fixtures/research/journals.yml index b4aba939921e9ae7a32314533c975b01e5891b0c..1fa62f29ecfa676fa07a0ee0f3b934efa8be1a58 100644 --- a/test/fixtures/research/journals.yml +++ b/test/fixtures/research/journals.yml @@ -3,7 +3,9 @@ # Table name: research_journals # # id :uuid not null, primary key +# access_token :string # description :text +# repository :string # title :string # created_at :datetime not null # updated_at :datetime not null diff --git a/test/models/research/journal_test.rb b/test/models/research/journal_test.rb index 91a25b8a41efd5c4e4cd9e20d9d52a49b3462ed7..7326e54168ec8ceda7e367a43c8ea42a79a3808d 100644 --- a/test/models/research/journal_test.rb +++ b/test/models/research/journal_test.rb @@ -3,7 +3,9 @@ # Table name: research_journals # # id :uuid not null, primary key +# access_token :string # description :text +# repository :string # title :string # created_at :datetime not null # updated_at :datetime not null