-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbitbucket.py
166 lines (140 loc) · 4.92 KB
/
bitbucket.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from dataclasses import dataclass
import requests
from client import VCSClient, VCSRepoClient
from common import (
CredentialsValidationError,
PullRequestCreationError,
PullRequestInfo,
RepositoryAccessError,
RepositoryInfo,
)
@dataclass
class BitBucketRepositoryInfo(RepositoryInfo):
project: str
class BitBucketClient(VCSClient):
@property
def common_url_path(self) -> str | None:
return "rest/api/1.0"
def validate_credentials(self):
try:
resp = self.get("users")
resp.json()
if not resp.ok:
raise CredentialsValidationError(
f"Invalid BitBucket token for {self.instance_url}"
)
except (
requests.exceptions.ConnectionError,
requests.exceptions.JSONDecodeError,
):
raise CredentialsValidationError(
f"Can't connect to BitBucket at {self.instance_url}"
)
def get_repository_info(self, repo_name: str) -> RepositoryInfo:
repo_paths = repo_name.split("/")
if len(repo_paths) != 2:
raise RepositoryAccessError(
"Project name should be included in the repository path: project/repository"
)
project_key, repository_slug = repo_paths
resp = self.get(f"projects/{project_key}/repos/{repository_slug}")
if not resp.ok:
raise RepositoryAccessError(
f"Can't get repository info for {repo_name}: {resp.json()['errors'][0]['message']}"
)
data = resp.json()
repo_id = data["id"]
resp = self.get(
f"projects/{project_key}/repos/{repository_slug}/default-branch"
)
if not resp.ok:
raise RepositoryAccessError(
f"Can't get default branch for {repo_name}: {resp.json()['errors'][0]['message']}"
)
data = resp.json()
default_branch = data["id"]
return BitBucketRepositoryInfo(
main_language=None,
default_branch=default_branch,
name=repository_slug,
id=repo_id,
project=project_key,
)
class BitBucketRepoClient(VCSRepoClient):
@property
def common_url_path(self) -> str | None:
return f"rest/api/1.0/projects/{self.repo_info.project}/repos/{self.repo_info.name}"
def create_new_branch(self, branch_name: str, target_branch_name: str):
resp = self.post(
"branches",
json={"name": branch_name, "startPoint": target_branch_name},
)
if not resp.ok:
raise PullRequestCreationError(
f"Can't create new branch: {resp.json()['errors'][0]['message']}"
)
def create_new_commit(
self, pr_info: PullRequestInfo, repo_info: RepositoryInfo
) -> None:
resp = self.put(
f"browse/{pr_info.filename}",
data={
"content": pr_info.content,
"message": pr_info.commit_message,
"branch": pr_info.branch,
},
)
if not resp.ok:
raise PullRequestCreationError(
f"Can't create new commit: {resp.json()['errors'][0]['message']}"
)
def create_pull_request(
self,
branch_name: str,
target_branch_name: str,
title: str,
) -> str:
project = self.repo_info.project
repo = self.repo_info.name
payload = {
"title": title,
"description": "",
"state": "OPEN",
"open": True,
"closed": False,
"fromRef": {
"id": f"refs/heads/{branch_name}",
"repository": {
"slug": repo,
"project": {"key": project},
},
},
"toRef": {
"id": target_branch_name,
"repository": {
"slug": repo,
"project": {"key": project},
},
},
"locked": False,
}
resp = self.post("pull-requests", json=payload)
if not resp.ok:
raise PullRequestCreationError(
f"Failed to create pull request: {resp.json()['errors'][0]['message']}"
)
data = resp.json()
return data["links"]["self"][0]["href"]
def delete_branch(self, branch_name: str) -> None:
# use here directly requests since different api is used to delete branch
resp = requests.delete(
f"{self.instance_url}/rest/branch-utils/1.0/projects/{self.repo_info.project}/"
f"repos/{self.repo_info.name}/"
"branches",
headers=self.headers,
json={"name": branch_name},
)
if not resp.ok:
raise PullRequestCreationError(
f"Can't delete branch: {resp.json()['errors'][0]['message']}"
)