60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
from urllib.parse import quote
|
|
|
|
import requests
|
|
from git_flow import GitFlowError
|
|
from git_flow.remote.base import RemoteAPI
|
|
|
|
|
|
class GitlabRemoteAPI(RemoteAPI):
|
|
def __init__(self, base_url: str, repository: str, token: str) -> None:
|
|
super().__init__(repository, token)
|
|
self.base_url = base_url
|
|
|
|
def create_pull_request(
|
|
self,
|
|
source: str,
|
|
destination: str,
|
|
title: str | None = None,
|
|
description: str | None = None,
|
|
close_source_branch: bool = True
|
|
) -> str:
|
|
response = requests.post(
|
|
self.get_endpoint("/merge_requests"),
|
|
headers=self.get_headers(),
|
|
json={
|
|
"title": title,
|
|
"description": description,
|
|
"source_branch": source,
|
|
"target_branch": destination,
|
|
"remove_source_branch": close_source_branch,
|
|
},
|
|
)
|
|
|
|
if response.ok:
|
|
return "PR creado exitosamente: " + response.json()["web_url"]
|
|
else:
|
|
raise GitFlowError("Ocurrió un error al crear el PR: " + response.text)
|
|
|
|
def create_tag(self, tag: str, commit: str) -> str:
|
|
response = requests.post(
|
|
self.get_endpoint("/repository/tags"),
|
|
headers=self.get_headers(),
|
|
json={"tag_name": tag, "ref": commit},
|
|
)
|
|
|
|
if response.ok:
|
|
return "Tag creado exitosamente: " + tag
|
|
else:
|
|
raise GitFlowError("Ocurrió un error al crear el tag: " + response.text)
|
|
|
|
def get_endpoint(self, resource: str) -> str:
|
|
project_id = quote(self.repository, safe="")
|
|
return f"{self.base_url}/api/v4/projects/{project_id}{resource}"
|
|
|
|
def get_headers(self):
|
|
return {
|
|
"Accept": "application/json",
|
|
"Content-Type": "application/json",
|
|
"PRIVATE-TOKEN": self.token,
|
|
}
|