-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgitlab.py
145 lines (125 loc) · 4.04 KB
/
gitlab.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import urllib.parse
import requests
from client import VCSClient, VCSRepoClient
from common import (
CredentialsValidationError,
PullRequestCreationError,
PullRequestInfo,
RepositoryAccessError,
RepositoryInfo,
)
class GitLabClient(VCSClient):
@property
def common_url_path(self) -> str | None:
return "api/v4"
def validate_credentials(self) -> None:
try:
resp = self.get("version")
resp.json()
if resp.status_code == 401:
raise CredentialsValidationError(
f"Invalid Gitlab token for {self.instance_url}"
)
except (
requests.exceptions.ConnectionError,
requests.exceptions.JSONDecodeError,
):
raise CredentialsValidationError(
f"Can't connect to Gitlab at {self.instance_url}"
)
def get_repository_info(self, repo_name: str) -> RepositoryInfo:
"""
Get the info about the Gitlab project
"""
repo_name_safe = urllib.parse.quote_plus(repo_name)
resp = self.get(f"projects/{repo_name_safe}")
if not resp.ok:
raise RepositoryAccessError(
f"Can't get repository info for {repo_name}: {resp.json()['message']}"
)
data = resp.json()
default_branch = data.get("default_branch", None)
name = data["name_with_namespace"]
repo_id = data["id"]
if not default_branch:
raise RepositoryAccessError(
f"Repository does not have a default branch: {repo_name}"
)
resp = self.get(f"projects/{repo_id}/languages")
data = resp.json()
language = None
if data:
language = list(data.keys())[0].lower()
return RepositoryInfo(
main_language=language,
default_branch=default_branch,
name=name,
id=repo_id,
)
class GitLabRepoClient(VCSRepoClient):
@property
def common_url_path(self) -> str | None:
return f"api/v4/projects/{self.repo_info.id}"
def create_new_branch(
self,
branch_name: str,
target_branch: str,
) -> None:
"""
Create a new branch on a GitLab project with the name branch_name from the target_branch
"""
resp = self.post(
"repository/branches",
json={
"ref": target_branch,
"branch": branch_name,
},
)
if not resp.ok:
raise PullRequestCreationError(
f"Can't create new branch: {resp.json()['message']}"
)
def create_new_commit(
self, pr_info: PullRequestInfo, repo_info: RepositoryInfo
) -> None:
json_payload = {
"branch": pr_info.branch,
"commit_message": pr_info.commit_message,
"actions": [
{
"action": "create",
"file_path": pr_info.filename,
"content": pr_info.content,
}
],
}
resp = self.post(
"repository/commits",
json=json_payload,
)
if not resp.ok:
raise PullRequestCreationError(
f"Error in git commit creation: {resp.json()['message']}"
)
def create_pull_request(
self, branch_name: str, target_branch: str, title: str
) -> str:
resp = self.post(
"merge_requests",
json={
"source_branch": branch_name,
"target_branch": target_branch,
"title": title,
"description": "",
},
)
if not resp.ok:
raise PullRequestCreationError(
f"Failed to create pull request: {resp.json()['message']}"
)
data = resp.json()
return data["web_url"]
def delete_branch(self, branch_name: str) -> None:
self.delete(
f"repository/branches/{branch_name}",
)