From 42b6744f81c8b3399893fba9cf561ef6f96b2b4b Mon Sep 17 00:00:00 2001 From: jazzari Date: Tue, 18 Jul 2023 15:47:26 -0300 Subject: [PATCH 01/17] feat: Job para hacer push #12919 --- app/jobs/git_push_job.rb | 12 ++++++++++++ app/models/site/repository.rb | 31 ++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 app/jobs/git_push_job.rb diff --git a/app/jobs/git_push_job.rb b/app/jobs/git_push_job.rb new file mode 100644 index 00000000..fb3ca662 --- /dev/null +++ b/app/jobs/git_push_job.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# Permite pushear los cambios cada vez que se +# hacen commits en un sitio +class GitPushJob < ApplicationJob + # @param :site [Site] + # @return [nil] + def perform(site) + #detectar que el repo local tiene repo remoto + site.repository.push if site.repository.origin + end +end \ No newline at end of file diff --git a/app/models/site/repository.rb b/app/models/site/repository.rb index 62e4c45e..10ab481d 100644 --- a/app/models/site/repository.rb +++ b/app/models/site/repository.rb @@ -29,7 +29,7 @@ class Site # Obtiene el origin # - # @return [Rugged::Remote] + # @return [Rugged::Remote, nil] def origin @origin ||= rugged.remotes.find do |remote| remote.name == 'origin' @@ -154,17 +154,15 @@ class Site # # @return [Boolean] def gc - env = { 'PATH' => '/usr/bin', 'LANG' => ENV['LANG'], 'HOME' => path } - cmd = 'git gc' + git_sh("git gc") + end - r = nil - Dir.chdir(path) do - Open3.popen2e(env, cmd, unsetenv_others: true) do |_, _, t| - r = t.value - end - end - - r&.success? + # Pushea cambios al repositorio remoto + # + # @return [Boolean, nil] + def push + origin.push(rugged.head.canonical_name, credentials: credentials) + git_sh("git lfs push") end private @@ -192,5 +190,16 @@ class Site def relativize(file) Pathname.new(file).relative_path_from(Pathname.new(path)).to_s end + + def git_sh(cmd) + env = { 'PATH' => '/usr/bin', 'LANG' => ENV['LANG'], 'HOME' => path } + + r = nil + Open3.popen2e(env, cmd, unsetenv_others: true, chdir: path) do |_, _, t| + r = t.value + end + + r&.success? + end end end From ca9fa31cf135d79e0204aad24d716a7f04c917c7 Mon Sep 17 00:00:00 2001 From: jazzari Date: Thu, 20 Jul 2023 15:31:38 -0300 Subject: [PATCH 02/17] fix: change git_sh method to accept array of params #12919 --- app/jobs/git_push_job.rb | 1 - app/models/site/repository.rb | 12 ++++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/jobs/git_push_job.rb b/app/jobs/git_push_job.rb index fb3ca662..3c62bee2 100644 --- a/app/jobs/git_push_job.rb +++ b/app/jobs/git_push_job.rb @@ -6,7 +6,6 @@ class GitPushJob < ApplicationJob # @param :site [Site] # @return [nil] def perform(site) - #detectar que el repo local tiene repo remoto site.repository.push if site.repository.origin end end \ No newline at end of file diff --git a/app/models/site/repository.rb b/app/models/site/repository.rb index 10ab481d..6306730e 100644 --- a/app/models/site/repository.rb +++ b/app/models/site/repository.rb @@ -154,7 +154,7 @@ class Site # # @return [Boolean] def gc - git_sh("git gc") + git_sh("git", "gc") end # Pushea cambios al repositorio remoto @@ -162,7 +162,7 @@ class Site # @return [Boolean, nil] def push origin.push(rugged.head.canonical_name, credentials: credentials) - git_sh("git lfs push") + git_sh("git", "lfs", "push", "origin", "#{default_branch}") end private @@ -191,11 +191,15 @@ class Site Pathname.new(file).relative_path_from(Pathname.new(path)).to_s end - def git_sh(cmd) + # Ejecuta un comando de git + # + # @param :args [Array] + # @return [Boolean] + def git_sh(*args) env = { 'PATH' => '/usr/bin', 'LANG' => ENV['LANG'], 'HOME' => path } r = nil - Open3.popen2e(env, cmd, unsetenv_others: true, chdir: path) do |_, _, t| + Open3.popen2e(env, args, unsetenv_others: true, chdir: path) do |_, _, t| r = t.value end From c2d64e11d7c0c6fb414e6970f6c933db19442561 Mon Sep 17 00:00:00 2001 From: jazzari Date: Thu, 20 Jul 2023 16:26:43 -0300 Subject: [PATCH 03/17] fix: missing splat in git_sh method #12919 --- app/models/site/repository.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/site/repository.rb b/app/models/site/repository.rb index 6306730e..a900fea2 100644 --- a/app/models/site/repository.rb +++ b/app/models/site/repository.rb @@ -162,7 +162,7 @@ class Site # @return [Boolean, nil] def push origin.push(rugged.head.canonical_name, credentials: credentials) - git_sh("git", "lfs", "push", "origin", "#{default_branch}") + git_sh("git", "lfs", "push", "origin", "default_branch") end private @@ -199,7 +199,7 @@ class Site env = { 'PATH' => '/usr/bin', 'LANG' => ENV['LANG'], 'HOME' => path } r = nil - Open3.popen2e(env, args, unsetenv_others: true, chdir: path) do |_, _, t| + Open3.popen2e(env, *args, unsetenv_others: true, chdir: path) do |_, _, t| r = t.value end From b7dc448daaec35c2e45045ab2272fe0f82ada948 Mon Sep 17 00:00:00 2001 From: jazzari Date: Fri, 21 Jul 2023 17:15:36 -0300 Subject: [PATCH 04/17] fix: fix git_sh method in site.repository.rb #12919 --- app/models/site/repository.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/site/repository.rb b/app/models/site/repository.rb index a900fea2..1ce10ed5 100644 --- a/app/models/site/repository.rb +++ b/app/models/site/repository.rb @@ -162,7 +162,7 @@ class Site # @return [Boolean, nil] def push origin.push(rugged.head.canonical_name, credentials: credentials) - git_sh("git", "lfs", "push", "origin", "default_branch") + git_sh("git", "lfs", "push", "origin", default_branch) end private From ae794322787548a79e6416663c45f3e8dd9cad62 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 21 Jul 2023 17:33:23 -0300 Subject: [PATCH 05/17] fix: renombrar el skel a upstream MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit si lo mantenemos como origin por defecto, el GitPushJob va a querer enviar cambios a un repositorio comĂșn, rompiendo todo. --- app/models/site.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/site.rb b/app/models/site.rb index 24644b9c..0fddeff5 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -473,7 +473,7 @@ class Site < ApplicationRecord def clone_skel! return if jekyll? - Rugged::Repository.clone_at ENV['SKEL_SUTTY'], path + Rugged::Repository.clone_at ENV['SKEL_SUTTY'], path, remote: 'upstream' end # Elimina el directorio del sitio From 6bae3a68f167956a7799d1252833ab2f7b0e694a Mon Sep 17 00:00:00 2001 From: f Date: Wed, 16 Aug 2023 17:35:20 -0300 Subject: [PATCH 06/17] feat: enviar cambios luego de commitearlos --- app/services/post_service.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/services/post_service.rb b/app/services/post_service.rb index 7b31867d..601915f8 100644 --- a/app/services/post_service.rb +++ b/app/services/post_service.rb @@ -96,6 +96,8 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do remove: action == :destroyed, message: I18n.t("post_service.#{action}", title: post&.title&.value)) + + GitPushJob.perform_later(site) end # Solo permitir cambiar estos atributos de cada articulo From 5a004b1cad1734af695a37d92d4a271603b818f1 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 16 Aug 2023 17:35:33 -0300 Subject: [PATCH 07/17] =?UTF-8?q?feat:=20enviar=20configuraci=C3=B3n=20lue?= =?UTF-8?q?go=20de=20comitearla?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/site_service.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/services/site_service.rb b/app/services/site_service.rb index 2c29538c..fc82ae84 100644 --- a/app/services/site_service.rb +++ b/app/services/site_service.rb @@ -97,6 +97,8 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do file: site.config.path, message: I18n.t("site_service.#{action}", name: site.name)) + + GitPushJob.perform_later(site) end def add_role(temporal: true, rol: 'invitade') From a2d84bfa96a375bd8c0ce79cda4a2ec20f0f5e9d Mon Sep 17 00:00:00 2001 From: f Date: Thu, 21 Sep 2023 12:25:23 -0300 Subject: [PATCH 08/17] fix: renombrar el remote correctamente --- app/models/site.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/models/site.rb b/app/models/site.rb index 0fddeff5..fa1d7b46 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -473,7 +473,10 @@ class Site < ApplicationRecord def clone_skel! return if jekyll? - Rugged::Repository.clone_at ENV['SKEL_SUTTY'], path, remote: 'upstream' + Rugged::Repository.clone_at ENV['SKEL_SUTTY'], path + + # Necesita un bloque + repository.rugged.remotes.rename('origin', 'upstream') {} end # Elimina el directorio del sitio From 69e1664084cd2b1cfb4fa71dfee745883f546c2d Mon Sep 17 00:00:00 2001 From: f Date: Thu, 21 Sep 2023 13:02:15 -0300 Subject: [PATCH 09/17] fix: renombrar todos los repositorios remotos --- ...921155401_site_rename_origin_to_upstream.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 db/migrate/20230921155401_site_rename_origin_to_upstream.rb diff --git a/db/migrate/20230921155401_site_rename_origin_to_upstream.rb b/db/migrate/20230921155401_site_rename_origin_to_upstream.rb new file mode 100644 index 00000000..c27d7747 --- /dev/null +++ b/db/migrate/20230921155401_site_rename_origin_to_upstream.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +# Renombrar todos los repositorios que apunten a skel como su origin +class SiteRenameOriginToUpstream < ActiveRecord::Migration[6.1] + # Renombrar + def up + Site.find_each do |site| + next unless site.repository.origin&.url == ENV['SKEL_SUTTY'] + + site.repository.rugged.remotes.rename('origin', 'upstream') + rescue Rugged::Error => e + Rails.logger.warn "#{site.name}: #{e.message}" + end + end + + # No se puede deshacer + def down; end +end From e4dc6588f925756d9523b6d344668c099f17b382 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 21 Sep 2023 13:04:25 -0300 Subject: [PATCH 10/17] =?UTF-8?q?fix:=20informar=20qu=C3=A9=20est=C3=A1=20?= =?UTF-8?q?pasando?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/migrate/20230921155401_site_rename_origin_to_upstream.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/db/migrate/20230921155401_site_rename_origin_to_upstream.rb b/db/migrate/20230921155401_site_rename_origin_to_upstream.rb index c27d7747..e32ca6fc 100644 --- a/db/migrate/20230921155401_site_rename_origin_to_upstream.rb +++ b/db/migrate/20230921155401_site_rename_origin_to_upstream.rb @@ -8,6 +8,7 @@ class SiteRenameOriginToUpstream < ActiveRecord::Migration[6.1] next unless site.repository.origin&.url == ENV['SKEL_SUTTY'] site.repository.rugged.remotes.rename('origin', 'upstream') + Rails.logger.info "#{site.name}: renamed origin to upstream" rescue Rugged::Error => e Rails.logger.warn "#{site.name}: #{e.message}" end From 7709b9f79803ccdda400526f5b2dfb9f49ad6cc8 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 21 Sep 2023 13:09:10 -0300 Subject: [PATCH 11/17] =?UTF-8?q?fix:=20capturar=20m=C3=A1s=20errores?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/migrate/20230921155401_site_rename_origin_to_upstream.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/migrate/20230921155401_site_rename_origin_to_upstream.rb b/db/migrate/20230921155401_site_rename_origin_to_upstream.rb index e32ca6fc..4ad53f1b 100644 --- a/db/migrate/20230921155401_site_rename_origin_to_upstream.rb +++ b/db/migrate/20230921155401_site_rename_origin_to_upstream.rb @@ -9,7 +9,7 @@ class SiteRenameOriginToUpstream < ActiveRecord::Migration[6.1] site.repository.rugged.remotes.rename('origin', 'upstream') Rails.logger.info "#{site.name}: renamed origin to upstream" - rescue Rugged::Error => e + rescue Rugged::Error, Rugged::OSError => e Rails.logger.warn "#{site.name}: #{e.message}" end end From b06b829357a607a9567d75fbf3bded07d7002a53 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 21 Sep 2023 13:10:26 -0300 Subject: [PATCH 12/17] fix: necesita un bloque --- db/migrate/20230921155401_site_rename_origin_to_upstream.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/db/migrate/20230921155401_site_rename_origin_to_upstream.rb b/db/migrate/20230921155401_site_rename_origin_to_upstream.rb index 4ad53f1b..864f4c4a 100644 --- a/db/migrate/20230921155401_site_rename_origin_to_upstream.rb +++ b/db/migrate/20230921155401_site_rename_origin_to_upstream.rb @@ -7,8 +7,9 @@ class SiteRenameOriginToUpstream < ActiveRecord::Migration[6.1] Site.find_each do |site| next unless site.repository.origin&.url == ENV['SKEL_SUTTY'] - site.repository.rugged.remotes.rename('origin', 'upstream') - Rails.logger.info "#{site.name}: renamed origin to upstream" + site.repository.rugged.remotes.rename('origin', 'upstream') do |_| + Rails.logger.info "#{site.name}: renamed origin to upstream" + end rescue Rugged::Error, Rugged::OSError => e Rails.logger.warn "#{site.name}: #{e.message}" end From 84f82cbb0fccde12f51cc69678ccdafe151b96b3 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 22 Sep 2023 12:21:55 -0300 Subject: [PATCH 13/17] fix: obtener el usuario del repositorio --- Gemfile | 1 + Gemfile.lock | 4 ++++ app/models/site/repository.rb | 16 +++++++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index dd880219..6edeac5a 100644 --- a/Gemfile +++ b/Gemfile @@ -70,6 +70,7 @@ gem 'redis-rails' gem 'rollups', git: 'https://github.com/fauno/rollup.git', branch: 'update' gem 'rubyzip' gem 'rugged' +gem 'git_clone_url' gem 'concurrent-ruby-ext' gem 'que' gem 'symbol-fstring', require: 'fstring/all' diff --git a/Gemfile.lock b/Gemfile.lock index f71053d7..9549a51d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -217,6 +217,8 @@ GEM activerecord (>= 4.0.0) get_process_mem (0.2.7) ffi (~> 1.0) + git_clone_url (2.0.0) + uri-ssh_git (>= 2.0) globalid (1.1.0) activesupport (>= 5.0) groupdate (6.2.1) @@ -553,6 +555,7 @@ GEM unf_ext unf_ext (0.0.8.2-x86_64-linux-musl) unicode-display_width (1.8.0) + uri-ssh_git (2.0.0) validates_hostname (1.0.13) activerecord (>= 3.0) activesupport (>= 3.0) @@ -606,6 +609,7 @@ DEPENDENCIES fast_jsonparser (~> 0.5.0) flamegraph friendly_id + git_clone_url hairtrigger haml-lint hamlit-rails diff --git a/app/models/site/repository.rb b/app/models/site/repository.rb index 74d874c1..d776efa4 100644 --- a/app/models/site/repository.rb +++ b/app/models/site/repository.rb @@ -173,7 +173,21 @@ class Site def credentials return unless File.exist? private_key - @credentials ||= Rugged::Credentials::SshKey.new username: 'git', publickey: public_key, privatekey: private_key + @credentials ||= + begin + username = parse_url(origin.url)&.user || 'git' + + Rugged::Credentials::SshKey.new username: username, publickey: public_key, privatekey: private_key + end + end + + # @param :url [String] + # @return [URI, nil] + def parse_url(url) + GitCloneUrl.parse(url) + rescue URI::Error => e + ExceptionNotifier.notify_exception(e, data: { site: site.name, url: url }) + nil end # @return [String] From 0c7a37a1d82e7035b05cfb4fb96614c36fd38369 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 22 Sep 2023 12:46:32 -0300 Subject: [PATCH 14/17] fix: mantener rugged 1.5.0.1 versiones posteriores se rompen con urls ssh:// https://github.com/libgit2/libgit2/issues/6496 --- Gemfile | 2 +- Gemfile.lock | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 6edeac5a..ce776647 100644 --- a/Gemfile +++ b/Gemfile @@ -69,7 +69,7 @@ gem 'redis', '~> 4.0', require: %w[redis redis/connection/hiredis] gem 'redis-rails' gem 'rollups', git: 'https://github.com/fauno/rollup.git', branch: 'update' gem 'rubyzip' -gem 'rugged' +gem 'rugged', '1.5.0.1' gem 'git_clone_url' gem 'concurrent-ruby-ext' gem 'que' diff --git a/Gemfile.lock b/Gemfile.lock index 9549a51d..11561776 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -496,7 +496,7 @@ GEM ruby_parser (3.20.1) sexp_processor (~> 4.16) rubyzip (2.3.2) - rugged (1.6.3-x86_64-linux-musl) + rugged (1.5.0.1-x86_64-linux-musl) safe_yaml (1.0.6) safely_block (0.3.0) errbase (>= 0.1.1) @@ -654,7 +654,7 @@ DEPENDENCIES rollups! rubocop-rails rubyzip - rugged + rugged (= 1.5.0.1) safe_yaml safely_block (~> 0.3.0) sassc-rails From 52e22a53bfd3f8bed30a01820ca522304384b619 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 22 Sep 2023 12:47:18 -0300 Subject: [PATCH 15/17] fix: no sabemos el sitio --- app/models/site/repository.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/site/repository.rb b/app/models/site/repository.rb index d776efa4..128a557a 100644 --- a/app/models/site/repository.rb +++ b/app/models/site/repository.rb @@ -186,7 +186,7 @@ class Site def parse_url(url) GitCloneUrl.parse(url) rescue URI::Error => e - ExceptionNotifier.notify_exception(e, data: { site: site.name, url: url }) + ExceptionNotifier.notify_exception(e, data: { path: path, url: url }) nil end From c176a8a4b44b5b62ffe01fb7708c39abf4eecbd9 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 22 Sep 2023 13:19:21 -0300 Subject: [PATCH 16/17] feat: poder obtener el usuario de cualquier remoto --- app/models/site/repository.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/models/site/repository.rb b/app/models/site/repository.rb index 128a557a..119a1495 100644 --- a/app/models/site/repository.rb +++ b/app/models/site/repository.rb @@ -173,12 +173,18 @@ class Site def credentials return unless File.exist? private_key - @credentials ||= - begin - username = parse_url(origin.url)&.user || 'git' + Rugged::Credentials::SshKey.new username: username_for(origin), publickey: public_key, privatekey: private_key + end - Rugged::Credentials::SshKey.new username: username, publickey: public_key, privatekey: private_key - end + # Obtiene el nombre de usuario para el repositorio remoto, por + # defecto git + # + # @param :remote [Rugged::Remote] + # @return [String] + def username_for(remote) + username = parse_url(remote.url)&.user if remote.respond_to? :url + + username || 'git' end # @param :url [String] From aeaf72c27036753b52087acac6c2fd523e923f78 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 22 Sep 2023 13:20:20 -0300 Subject: [PATCH 17/17] feat: poder pushear a cualquier remoto --- app/models/site/repository.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/app/models/site/repository.rb b/app/models/site/repository.rb index 119a1495..2eafb92e 100644 --- a/app/models/site/repository.rb +++ b/app/models/site/repository.rb @@ -158,22 +158,29 @@ class Site # Pushea cambios al repositorio remoto # + # @param :remote [Rugged::Remote] # @return [Boolean, nil] - def push - origin.push(rugged.head.canonical_name, credentials: credentials) - git_sh("git", "lfs", "push", "origin", default_branch) + def push(remote = origin) + remote.push(rugged.head.canonical_name, credentials: credentials_for(remote)) + git_sh('git', 'lfs', 'push', remote.name, default_branch) end private + # @deprecated + def credentials + @credentials ||= credentials_for(origin) + end + # Si Sutty tiene una llave privada de tipo ED25519, devuelve las # credenciales necesarias para trabajar con repositorios remotos. # + # @param :remote [Rugged::Remote] # @return [Nil, Rugged::Credentials::SshKey] - def credentials + def credentials_for(remote) return unless File.exist? private_key - Rugged::Credentials::SshKey.new username: username_for(origin), publickey: public_key, privatekey: private_key + Rugged::Credentials::SshKey.new username: username_for(remote), publickey: public_key, privatekey: private_key end # Obtiene el nombre de usuario para el repositorio remoto, por