5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-16 17:26:21 +00:00
panel/app/lib/gitlab_api_client.rb

62 lines
1.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require 'httparty'
class GitlabApiClient
include HTTParty
# TODO: Hacer configurable por sitio
base_uri ENV.fetch('GITLAB_URI', 'https://0xacab.org')
no_follow true
# Trae todos los proyectos. Como estamos usando un Project Token,
# siempre va a traer uno solo.
#
# @return [HTTParty::Response]
def projects
self.class.get('/api/v4/projects', { query: params(membership: true) })
end
# Obtiene el identificador del proyecto
#
# @return [Integer]
def project_id
@project_id ||= ENV['GITLAB_PROJECT'] || projects&.first&.dig('id')
end
# Crea un issue
#
# @see https://docs.gitlab.com/ee/api/issues.html#new-issue
# @return [HTTParty::Response]
def new_issue(**args)
self.class.post("/api/v4/projects/#{project_id}/issues", { query: params(**args) })
end
# Modifica un issue
#
# @see https://docs.gitlab.com/ee/api/issues.html#edit-issue
# @return [HTTParty::Response]
def edit_issue(**args)
self.class.put("/api/v4/projects/#{project_id}/issues", { query: params(**args) })
end
# Crea un comentario
#
# @see https://docs.gitlab.com/ee/api/notes.html#create-new-issue-note
# @return [HTTParty::Response]
def new_note(iid:, **args)
self.class.post("/api/v4/projects/#{project_id}/issues/#{iid}/notes", { query: params(**args) })
end
private
def params(**args)
default_params.merge(args)
end
# TODO: Que cada sitio tenga su propio token y uri
def default_params
{ private_token: ENV['GITLAB_TOKEN'] }
end
end