5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-16 06:31:42 +00:00

Merge branch 'issue-12919' into 'rails'
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

Git Push Job

See merge request sutty/sutty!196
This commit is contained in:
fauno 2023-09-27 19:42:27 +00:00
commit 8f7dbfdc9e
8 changed files with 99 additions and 14 deletions

View file

@ -69,7 +69,8 @@ gem 'redis', '~> 4.0', require: %w[redis redis/connection/hiredis]
gem 'redis-rails' gem 'redis-rails'
gem 'rollups', git: 'https://github.com/fauno/rollup.git', branch: 'update' gem 'rollups', git: 'https://github.com/fauno/rollup.git', branch: 'update'
gem 'rubyzip' gem 'rubyzip'
gem 'rugged' gem 'rugged', '1.5.0.1'
gem 'git_clone_url'
gem 'concurrent-ruby-ext' gem 'concurrent-ruby-ext'
gem 'que' gem 'que'
gem 'symbol-fstring', require: 'fstring/all' gem 'symbol-fstring', require: 'fstring/all'

View file

@ -218,6 +218,8 @@ GEM
activerecord (>= 4.0.0) activerecord (>= 4.0.0)
get_process_mem (0.2.7) get_process_mem (0.2.7)
ffi (~> 1.0) ffi (~> 1.0)
git_clone_url (2.0.0)
uri-ssh_git (>= 2.0)
globalid (1.1.0) globalid (1.1.0)
activesupport (>= 5.0) activesupport (>= 5.0)
groupdate (6.2.1) groupdate (6.2.1)
@ -495,7 +497,7 @@ GEM
ruby_parser (3.20.1) ruby_parser (3.20.1)
sexp_processor (~> 4.16) sexp_processor (~> 4.16)
rubyzip (2.3.2) 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) safe_yaml (1.0.6)
safely_block (0.3.0) safely_block (0.3.0)
errbase (>= 0.1.1) errbase (>= 0.1.1)
@ -554,6 +556,7 @@ GEM
unf_ext unf_ext
unf_ext (0.0.8.2-x86_64-linux-musl) unf_ext (0.0.8.2-x86_64-linux-musl)
unicode-display_width (1.8.0) unicode-display_width (1.8.0)
uri-ssh_git (2.0.0)
validates_hostname (1.0.13) validates_hostname (1.0.13)
activerecord (>= 3.0) activerecord (>= 3.0)
activesupport (>= 3.0) activesupport (>= 3.0)
@ -608,6 +611,7 @@ DEPENDENCIES
fast_jsonparser (~> 0.5.0) fast_jsonparser (~> 0.5.0)
flamegraph flamegraph
friendly_id friendly_id
git_clone_url
hairtrigger hairtrigger
haml-lint haml-lint
hamlit-rails hamlit-rails
@ -652,7 +656,7 @@ DEPENDENCIES
rollups! rollups!
rubocop-rails rubocop-rails
rubyzip rubyzip
rugged rugged (= 1.5.0.1)
safe_yaml safe_yaml
safely_block (~> 0.3.0) safely_block (~> 0.3.0)
sassc-rails sassc-rails

11
app/jobs/git_push_job.rb Normal file
View file

@ -0,0 +1,11 @@
# 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)
site.repository.push if site.repository.origin
end
end

View file

@ -478,6 +478,9 @@ class Site < ApplicationRecord
return if jekyll? return if jekyll?
Rugged::Repository.clone_at ENV['SKEL_SUTTY'], path Rugged::Repository.clone_at ENV['SKEL_SUTTY'], path
# Necesita un bloque
repository.rugged.remotes.rename('origin', 'upstream') {}
end end
# Elimina el directorio del sitio # Elimina el directorio del sitio

View file

@ -29,7 +29,7 @@ class Site
# Obtiene el origin # Obtiene el origin
# #
# @return [Rugged::Remote] # @return [Rugged::Remote, nil]
def origin def origin
@origin ||= rugged.remotes.find do |remote| @origin ||= rugged.remotes.find do |remote|
remote.name == 'origin' remote.name == 'origin'
@ -153,27 +153,54 @@ class Site
# #
# @return [Boolean] # @return [Boolean]
def gc def gc
env = { 'PATH' => '/usr/bin', 'LANG' => ENV['LANG'], 'HOME' => path } git_sh("git", "gc")
cmd = 'git gc' end
r = nil # Pushea cambios al repositorio remoto
Open3.popen2e(env, cmd, unsetenv_others: true, chdir: path) do |_, _, t| #
r = t.value # @param :remote [Rugged::Remote]
end # @return [Boolean, nil]
def push(remote = origin)
r&.success? remote.push(rugged.head.canonical_name, credentials: credentials_for(remote))
git_sh('git', 'lfs', 'push', remote.name, default_branch)
end end
private private
# @deprecated
def credentials
@credentials ||= credentials_for(origin)
end
# Si Sutty tiene una llave privada de tipo ED25519, devuelve las # Si Sutty tiene una llave privada de tipo ED25519, devuelve las
# credenciales necesarias para trabajar con repositorios remotos. # credenciales necesarias para trabajar con repositorios remotos.
# #
# @param :remote [Rugged::Remote]
# @return [Nil, Rugged::Credentials::SshKey] # @return [Nil, Rugged::Credentials::SshKey]
def credentials def credentials_for(remote)
return unless File.exist? private_key return unless File.exist? private_key
@credentials ||= Rugged::Credentials::SshKey.new username: 'git', 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
# 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]
# @return [URI, nil]
def parse_url(url)
GitCloneUrl.parse(url)
rescue URI::Error => e
ExceptionNotifier.notify_exception(e, data: { path: path, url: url })
nil
end end
# @return [String] # @return [String]
@ -189,5 +216,20 @@ class Site
def relativize(file) def relativize(file)
Pathname.new(file).relative_path_from(Pathname.new(path)).to_s Pathname.new(file).relative_path_from(Pathname.new(path)).to_s
end end
# 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, *args, unsetenv_others: true, chdir: path) do |_, _, t|
r = t.value
end
r&.success?
end
end end
end end

View file

@ -102,6 +102,8 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do
usuarie: usuarie, usuarie: usuarie,
message: I18n.t("post_service.#{action}", message: I18n.t("post_service.#{action}",
title: post&.title&.value)) title: post&.title&.value))
GitPushJob.perform_later(site)
end end
# Solo permitir cambiar estos atributos de cada articulo # Solo permitir cambiar estos atributos de cada articulo

View file

@ -97,6 +97,8 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do
add: [site.config.path], add: [site.config.path],
message: I18n.t("site_service.#{action}", message: I18n.t("site_service.#{action}",
name: site.name)) name: site.name))
GitPushJob.perform_later(site)
end end
def add_role(temporal: true, rol: 'invitade') def add_role(temporal: true, rol: 'invitade')

View file

@ -0,0 +1,20 @@
# 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') 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
end
# No se puede deshacer
def down; end
end