2023-07-26 17:44:11 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Api
|
|
|
|
module V1
|
2023-08-10 19:05:14 +00:00
|
|
|
# Recibe webhooks y lanza un PullJob
|
2023-07-26 17:44:11 +00:00
|
|
|
class WebhooksController < BaseController
|
2023-08-10 19:05:14 +00:00
|
|
|
rescue_from ActiveRecord::RecordNotFound, with: :platforms_answer
|
|
|
|
|
2023-08-14 19:49:24 +00:00
|
|
|
# Trae los cambios a partir de un post de Webhooks:
|
|
|
|
# (Gitlab, Github, Guitea, etc)
|
|
|
|
def pull
|
|
|
|
message = I18n.with_locale(site.default_locale) do
|
|
|
|
I18n.t('webhooks.pull.message')
|
|
|
|
end
|
|
|
|
|
|
|
|
GitPullJob.perform_later(site, usuarie, message)
|
|
|
|
platforms_answer
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-08-10 19:05:14 +00:00
|
|
|
def site
|
|
|
|
@site ||= Site.find_by_name!(params[:site_id])
|
|
|
|
end
|
|
|
|
|
2023-08-14 15:50:18 +00:00
|
|
|
# valida el token que envía la plataforma del webhook
|
|
|
|
def token
|
|
|
|
@token ||=
|
|
|
|
begin
|
|
|
|
# Gitlab
|
|
|
|
if request.headers['X-Gitlab-Token']
|
|
|
|
request.headers["X-Gitlab-Token"]
|
|
|
|
# Github
|
|
|
|
elsif request.headers['X-HUB-SIGNATURE-256']
|
|
|
|
signature(request.env['HTTP_X_HUB_SIGNATURE_256'])
|
|
|
|
# Guitea
|
|
|
|
else
|
|
|
|
signature(request.env['HTTP_X_GITEA_SIGNATURE'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def token_from_signature(signature)
|
|
|
|
payload = request.body.read
|
|
|
|
site.roles.where(temporal: false, rol: 'usuarie').pluck(:token).find do |token|
|
|
|
|
new_signature = hash_mac(OpenSSL::Digest.new('sha256'), token, payload)
|
|
|
|
@token ||= Rack::Utils.secure_compare(new_signature, signature)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-10 19:05:14 +00:00
|
|
|
def usuarie
|
|
|
|
@usuarie = site.roles.find_by!(temporal: false, rol: 'usuarie', token: token).usuarie
|
2023-08-14 19:49:24 +00:00
|
|
|
end
|
2023-08-10 19:05:14 +00:00
|
|
|
|
|
|
|
def platforms_answer
|
2023-07-27 20:23:40 +00:00
|
|
|
head :ok
|
2023-07-26 17:44:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|