2021-05-29 20:42:45 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'httparty'
|
|
|
|
|
|
|
|
class GitlabApiClient
|
|
|
|
include HTTParty
|
|
|
|
|
|
|
|
# TODO: Hacer configurable por sitio
|
|
|
|
base_uri ENV.fetch('GITLAB_URI', 'https://0xacab.org')
|
2021-05-31 15:08:19 +00:00
|
|
|
# No seguir redirecciones. Si nos olvidamos https:// en la dirección,
|
|
|
|
# las redirecciones nos pueden llevar a cualquier lado y obtener
|
|
|
|
# resultados diferentes.
|
2021-05-29 20:42:45 +00:00
|
|
|
no_follow true
|
|
|
|
|
|
|
|
# Trae todos los proyectos. Como estamos usando un Project Token,
|
|
|
|
# siempre va a traer uno solo.
|
|
|
|
#
|
|
|
|
# @return [HTTParty::Response]
|
|
|
|
def projects
|
2021-05-31 15:08:19 +00:00
|
|
|
self.class.get('/api/v4/projects', { query: { membership: true }, headers: headers })
|
2021-05-29 20:42:45 +00:00
|
|
|
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)
|
2021-05-31 15:08:19 +00:00
|
|
|
self.class.post("/api/v4/projects/#{project_id}/issues", { body: args, headers: headers })
|
2021-05-29 20:42:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Modifica un issue
|
|
|
|
#
|
|
|
|
# @see https://docs.gitlab.com/ee/api/issues.html#edit-issue
|
|
|
|
# @return [HTTParty::Response]
|
2021-05-31 15:08:19 +00:00
|
|
|
def edit_issue(iid:, **args)
|
|
|
|
self.class.put("/api/v4/projects/#{project_id}/issues/#{iid}", { body: args, headers: headers })
|
2021-05-29 20:42:45 +00:00
|
|
|
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)
|
2021-05-31 15:08:19 +00:00
|
|
|
self.class.post("/api/v4/projects/#{project_id}/issues/#{iid}/notes", { body: args, headers: headers })
|
2021-05-29 20:42:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-05-31 15:08:19 +00:00
|
|
|
def headers(extra = {})
|
|
|
|
{ 'Authorization' => "Bearer #{ENV['GITLAB_TOKEN']}" }.merge(extra)
|
2021-05-29 20:42:45 +00:00
|
|
|
end
|
|
|
|
end
|