diff --git a/app/models/administration/qualiopi/indicator.rb b/app/models/administration/qualiopi/indicator.rb index 8231f9cdebe7ef68ac431bbe809ef6f386098580..c43737ecec0ff0f33f11a12b85d2730fc6652174 100644 --- a/app/models/administration/qualiopi/indicator.rb +++ b/app/models/administration/qualiopi/indicator.rb @@ -20,7 +20,7 @@ # # Foreign Keys # -# fk_rails_31f1a0a2c9 (criterion_id => administration_qualiopi_criterions.id) +# fk_rails_eed87f7acf (criterion_id => administration_qualiopi_criterions.id) # class Administration::Qualiopi::Indicator < ApplicationRecord belongs_to :criterion diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb index e61b8cb241b5debee53d54946c70bf579af4d934..a5ff5c58040dd786e9de27571e4cf684669065bc 100644 --- a/app/models/communication/website.rb +++ b/app/models/communication/website.rb @@ -30,7 +30,7 @@ # # Foreign Keys # -# fk_rails_94b0abd85b (university_id => universities.id) +# fk_rails_bb6a496c08 (university_id => universities.id) # class Communication::Website < ApplicationRecord include WithAbouts diff --git a/app/models/communication/website/git_file.rb b/app/models/communication/website/git_file.rb index 219a46db117213af546e104020e633fe9f2e0094..0a38acbe9b01ff83ec1b0ac33c4cab4516c01b73 100644 --- a/app/models/communication/website/git_file.rb +++ b/app/models/communication/website/git_file.rb @@ -2,15 +2,14 @@ # # Table name: communication_website_git_files # -# id :uuid not null, primary key -# about_type :string not null, indexed => [about_id] -# previous_path :string -# previous_sha :string -# previous_sha256 :string -# created_at :datetime not null -# updated_at :datetime not null -# about_id :uuid not null, indexed => [about_type] -# website_id :uuid not null, indexed +# id :uuid not null, primary key +# about_type :string not null, indexed => [about_id] +# previous_path :string +# previous_sha :string +# created_at :datetime not null +# updated_at :datetime not null +# about_id :uuid not null, indexed => [about_type] +# website_id :uuid not null, indexed # # Indexes # @@ -19,7 +18,7 @@ # # Foreign Keys # -# fk_rails_b6b8b2cce2 (website_id => communication_websites.id) +# fk_rails_8505d649e8 (website_id => communication_websites.id) # class Communication::Website::GitFile < ApplicationRecord belongs_to :website, class_name: 'Communication::Website' @@ -58,16 +57,6 @@ class Communication::Website::GitFile < ApplicationRecord @path ||= about.git_path(website)&.gsub(/\/+/, '/') end - def sha - # Git SHA-1 is calculated from the String "blob <length>\x00<contents>" - # Source: https://alblue.bandlem.com/2011/08/git-tip-of-week-objects.html - @sha ||= OpenSSL::Digest::SHA1.hexdigest "blob #{to_s.bytesize}\x00#{to_s}" - end - - def sha256 - @sha256 ||= OpenSSL::Digest::SHA256.hexdigest to_s - end - def to_s @to_s ||= ApplicationController.render( template: "admin/#{about.class.name.underscore.pluralize}/static", @@ -81,6 +70,16 @@ class Communication::Website::GitFile < ApplicationRecord protected + # Real sha on the git repo + def git_sha + @git_sha ||= website.git_repository.git_sha previous_path + end + + # Based on content, with the provider's algorithm (sha1 or sha256) + def computed_sha + @computed_sha ||= website.git_repository.computed_sha to_s + end + def exists_on_git? !git_sha.nil? end @@ -97,10 +96,6 @@ class Communication::Website::GitFile < ApplicationRecord end def different_sha? - previous_sha != sha && previous_sha != sha256 - end - - def git_sha - @git_sha ||= website.git_repository.git_sha previous_path + previous_sha != computed_sha end end diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb index c54c59b6224f2a52687615750949cb77fdd5c1cb..fa9ffcd3bd2594f0ee911d97d1290f4b407b65bc 100644 --- a/app/models/communication/website/page.rb +++ b/app/models/communication/website/page.rb @@ -33,8 +33,8 @@ # Foreign Keys # # fk_rails_1a42003f06 (parent_id => communication_website_pages.id) -# fk_rails_2315541232 (related_category_id => communication_website_categories.id) # fk_rails_280107c62b (communication_website_id => communication_websites.id) +# fk_rails_47b37cf8b2 (related_category_id => communication_website_categories.id) # fk_rails_d208d15a73 (university_id => universities.id) # diff --git a/app/models/education/program.rb b/app/models/education/program.rb index cec653a4a5952ba56db12f73f460b52a52e3a8b6..d2328f5b296f94eda3123c877eb6a665834bcb76 100644 --- a/app/models/education/program.rb +++ b/app/models/education/program.rb @@ -39,7 +39,7 @@ # # Foreign Keys # -# fk_rails_6e16107511 (university_id => universities.id) +# fk_rails_08b351087c (university_id => universities.id) # fk_rails_ec1f16f607 (parent_id => education_programs.id) # class Education::Program < ApplicationRecord diff --git a/app/models/university/person.rb b/app/models/university/person.rb index da61385fdd3ecc61cd4d361c0f79d2d65920f330..49718973f6abed6db2b9bf4b1e863f47a5e4bb4b 100644 --- a/app/models/university/person.rb +++ b/app/models/university/person.rb @@ -27,8 +27,8 @@ # # Foreign Keys # -# fk_rails_433744b4e8 (user_id => users.id) -# fk_rails_49ac392937 (university_id => universities.id) +# fk_rails_b47a769440 (user_id => users.id) +# fk_rails_da35e70d61 (university_id => universities.id) # class University::Person < ApplicationRecord include WithGit diff --git a/app/models/university/person/administrator.rb b/app/models/university/person/administrator.rb index d11f849b2ce178c8d0c5dd953fbe2d54640c2846..b7bca44426417b0705317eb94dbdb4af47d11eeb 100644 --- a/app/models/university/person/administrator.rb +++ b/app/models/university/person/administrator.rb @@ -27,8 +27,8 @@ # # Foreign Keys # -# fk_rails_433744b4e8 (user_id => users.id) -# fk_rails_49ac392937 (university_id => universities.id) +# fk_rails_b47a769440 (user_id => users.id) +# fk_rails_da35e70d61 (university_id => universities.id) # class University::Person::Administrator < University::Person def self.polymorphic_name diff --git a/app/models/university/person/author.rb b/app/models/university/person/author.rb index 5c5fb92cc87d8f5011cd93e33aad406c4a772a22..5bf2ee2c4a68b86044a237691a08fd9b4391e150 100644 --- a/app/models/university/person/author.rb +++ b/app/models/university/person/author.rb @@ -27,8 +27,8 @@ # # Foreign Keys # -# fk_rails_433744b4e8 (user_id => users.id) -# fk_rails_49ac392937 (university_id => universities.id) +# fk_rails_b47a769440 (user_id => users.id) +# fk_rails_da35e70d61 (university_id => universities.id) # class University::Person::Author < University::Person def self.polymorphic_name diff --git a/app/models/university/person/researcher.rb b/app/models/university/person/researcher.rb index 989f141a54956e1ead39198db4380378636287c1..9ca52242568e5da3fee487a5a4868d3bb28aaa88 100644 --- a/app/models/university/person/researcher.rb +++ b/app/models/university/person/researcher.rb @@ -27,8 +27,8 @@ # # Foreign Keys # -# fk_rails_433744b4e8 (user_id => users.id) -# fk_rails_49ac392937 (university_id => universities.id) +# fk_rails_b47a769440 (user_id => users.id) +# fk_rails_da35e70d61 (university_id => universities.id) # class University::Person::Researcher < University::Person def self.polymorphic_name diff --git a/app/models/university/person/teacher.rb b/app/models/university/person/teacher.rb index e83a8b826202f29d4ad9ff7869853e1705f05b6f..2f75bd1ab96b16846fa83af4670c2724d9d116e4 100644 --- a/app/models/university/person/teacher.rb +++ b/app/models/university/person/teacher.rb @@ -27,8 +27,8 @@ # # Foreign Keys # -# fk_rails_433744b4e8 (user_id => users.id) -# fk_rails_49ac392937 (university_id => universities.id) +# fk_rails_b47a769440 (user_id => users.id) +# fk_rails_da35e70d61 (university_id => universities.id) # class University::Person::Teacher < University::Person def self.polymorphic_name diff --git a/app/services/git/providers/github.rb b/app/services/git/providers/github.rb index 4b123493862ac7e23cdc93c442396e1b380dce7b..2e7ed383faa44ba64044b371637e5542d64690b7 100644 --- a/app/services/git/providers/github.rb +++ b/app/services/git/providers/github.rb @@ -44,6 +44,12 @@ class Git::Providers::Github < Git::Providers::Abstract true end + def computed_sha(string) + # Git SHA-1 is calculated from the String "blob <length>\x00<contents>" + # Source: https://alblue.bandlem.com/2011/08/git-tip-of-week-objects.html + OpenSSL::Digest::SHA1.hexdigest "blob #{string.bytesize}\x00#{string}" + end + def git_sha(path) begin content = client.content repository, path: path diff --git a/app/services/git/providers/gitlab.rb b/app/services/git/providers/gitlab.rb index 87c3012331c39eacfc6a5c67b88baeeaccc3e1fa..b549e9c411b90487d977057ce3e0cbfa6f297ca8 100644 --- a/app/services/git/providers/gitlab.rb +++ b/app/services/git/providers/gitlab.rb @@ -42,6 +42,10 @@ class Git::Providers::Gitlab < Git::Providers::Abstract true end + def computed_sha(string) + OpenSSL::Digest::SHA256.hexdigest string + end + # https://gitlab.com/gitlab-org/gitlab/-/issues/23504 def git_sha(path) begin diff --git a/app/services/git/repository.rb b/app/services/git/repository.rb index f5b1579b7eaa59f665d5f9f57d1d40e687ceef82..1397acb8589ca807cbe0ea27d7f71c5d39cdbe7b 100644 --- a/app/services/git/repository.rb +++ b/app/services/git/repository.rb @@ -19,6 +19,10 @@ class Git::Repository mark_as_synced if provider.push(commit_message) end + def computed_sha(string) + provider.computed_sha(string) + end + def git_sha(path) provider.git_sha path end @@ -55,9 +59,10 @@ class Git::Repository def mark_as_synced git_files.each do |git_file| - git_file.update previous_path: git_file.path, - previous_sha: git_file.sha, - previous_sha256: git_file.sha256 + path = git_file.path + sha = provider.git_sha path + git_file.update previous_path: path, + previous_sha: sha end end end diff --git a/db/migrate/20220223082242_remove_sha256_from_communication_website_git_file.rb b/db/migrate/20220223082242_remove_sha256_from_communication_website_git_file.rb new file mode 100644 index 0000000000000000000000000000000000000000..c19f5d84a630b0b2c7cb418311d6862ba599c73b --- /dev/null +++ b/db/migrate/20220223082242_remove_sha256_from_communication_website_git_file.rb @@ -0,0 +1,5 @@ +class RemoveSha256FromCommunicationWebsiteGitFile < ActiveRecord::Migration[6.1] + def change + remove_column :communication_website_git_files, :previous_sha256 + end +end diff --git a/db/schema.rb b/db/schema.rb index e07eaf67d85c3da947119c993fe1848370d57121..f5d25a912d340fb97e42bc27caf04e639dc7cc09 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_18_173530) do +ActiveRecord::Schema.define(version: 2022_02_23_082242) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -129,7 +129,6 @@ ActiveRecord::Schema.define(version: 2022_02_18_173530) do t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.string "previous_sha" - t.string "previous_sha256" t.index ["about_type", "about_id"], name: "index_communication_website_github_files_on_about" t.index ["website_id"], name: "index_communication_website_git_files_on_website_id" end diff --git a/test/fixtures/communication/website/git_files.yml b/test/fixtures/communication/website/git_files.yml index c9b6c57aa206ea8330b69a387b90e8d4cbbe00ba..953b550527c2514df3a20355d1af0826e88849c0 100644 --- a/test/fixtures/communication/website/git_files.yml +++ b/test/fixtures/communication/website/git_files.yml @@ -1,3 +1,25 @@ +# == Schema Information +# +# Table name: communication_website_git_files +# +# id :uuid not null, primary key +# about_type :string not null, indexed => [about_id] +# previous_path :string +# previous_sha :string +# created_at :datetime not null +# updated_at :datetime not null +# about_id :uuid not null, indexed => [about_type] +# website_id :uuid not null, indexed +# +# Indexes +# +# index_communication_website_git_files_on_website_id (website_id) +# index_communication_website_github_files_on_about (about_type,about_id) +# +# Foreign Keys +# +# fk_rails_8505d649e8 (website_id => communication_websites.id) +# git_file_1: about: test_post (Communication::Website::Post) website: website_with_github diff --git a/test/fixtures/communication/website/posts.yml b/test/fixtures/communication/website/posts.yml index b723b9dfc22d7e41b5996204251a4637b8e918fe..14b3a688e24252e2ba26b5f7cfd8e5358c0451f6 100644 --- a/test/fixtures/communication/website/posts.yml +++ b/test/fixtures/communication/website/posts.yml @@ -1,3 +1,36 @@ +# == Schema Information +# +# Table name: communication_website_posts +# +# id :uuid not null, primary key +# description :text +# featured_image_alt :string +# github_path :text +# old_text :text +# pinned :boolean default(FALSE) +# published :boolean default(FALSE) +# published_at :datetime +# slug :text +# text :text +# title :string +# created_at :datetime not null +# updated_at :datetime not null +# author_id :uuid indexed +# communication_website_id :uuid not null, indexed +# university_id :uuid not null, indexed +# +# Indexes +# +# index_communication_website_posts_on_author_id (author_id) +# index_communication_website_posts_on_communication_website_id (communication_website_id) +# index_communication_website_posts_on_university_id (university_id) +# +# Foreign Keys +# +# fk_rails_1e0d058a25 (university_id => universities.id) +# fk_rails_d1c1a10946 (communication_website_id => communication_websites.id) +# fk_rails_e0eec447b0 (author_id => university_people.id) +# test_post: university: default_university website: website_with_github diff --git a/test/fixtures/communication/websites.yml b/test/fixtures/communication/websites.yml index 037203e47c76f67db27a94a614abecafc9bd2853..fd38f58df36e9f13aeb078596a55051017d5d4cc 100644 --- a/test/fixtures/communication/websites.yml +++ b/test/fixtures/communication/websites.yml @@ -1,3 +1,37 @@ +# == Schema Information +# +# Table name: communication_websites +# +# id :uuid not null, primary key +# about_type :string indexed => [about_id] +# access_token :string +# git_provider :integer default("github") +# name :string +# repository :string +# static_pathname_administrators :string default("administrators") +# static_pathname_authors :string default("authors") +# static_pathname_posts :string default("posts") +# static_pathname_programs :string default("programs") +# static_pathname_research_articles :string default("articles") +# static_pathname_research_volumes :string default("volumes") +# static_pathname_researchers :string default("researchers") +# static_pathname_staff :string default("staff") +# static_pathname_teachers :string default("teachers") +# url :string +# created_at :datetime not null +# updated_at :datetime not null +# about_id :uuid indexed => [about_type] +# university_id :uuid not null, indexed +# +# Indexes +# +# index_communication_websites_on_about (about_type,about_id) +# index_communication_websites_on_university_id (university_id) +# +# Foreign Keys +# +# fk_rails_bb6a496c08 (university_id => universities.id) +# website_with_github: university: default_university name: Site de test diff --git a/test/fixtures/universities.yml b/test/fixtures/universities.yml index d5c83b9875f709ee879d928f1a18b927d3725f20..34655ffa67d9a34e443bc257df8af07ac9443ed9 100644 --- a/test/fixtures/universities.yml +++ b/test/fixtures/universities.yml @@ -1,2 +1,20 @@ +# == Schema Information +# +# Table name: universities +# +# id :uuid not null, primary key +# address :string +# city :string +# country :string +# identifier :string +# mail_from_address :string +# mail_from_name :string +# name :string +# private :boolean +# sms_sender_name :string +# zipcode :string +# created_at :datetime not null +# updated_at :datetime not null +# default_university: name: Université de test diff --git a/test/models/.keep b/test/models/.keep deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/test/models/communication/website/git_file_test.rb b/test/models/communication/website/git_file_test.rb index bbde17e003d519d9c9831658635469b6fcdb1902..4f80fa1cba391474b02e72eb01f2cc67ab431bc1 100644 --- a/test/models/communication/website/git_file_test.rb +++ b/test/models/communication/website/git_file_test.rb @@ -1,3 +1,25 @@ +# == Schema Information +# +# Table name: communication_website_git_files +# +# id :uuid not null, primary key +# about_type :string not null, indexed => [about_id] +# previous_path :string +# previous_sha :string +# created_at :datetime not null +# updated_at :datetime not null +# about_id :uuid not null, indexed => [about_type] +# website_id :uuid not null, indexed +# +# Indexes +# +# index_communication_website_git_files_on_website_id (website_id) +# index_communication_website_github_files_on_about (about_type,about_id) +# +# Foreign Keys +# +# fk_rails_8505d649e8 (website_id => communication_websites.id) +# require "test_helper" class Communication::Website::GitFileTest < ActiveSupport::TestCase