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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| import io import base64 import urllib.parse import ujson from .async_client import aClient
def strip_magic_prefix(func): async def wrapper(*args, **kwargs): data = await func(*args, **kwargs) return data[5:] return wrapper
class aGerrit(aClient): def __init__(self, gerrit_url, user, passwd): super().__init__(gerrit_url, user, passwd)
def project(self, project: str): return aProject(self, project)
async def get_change(self, change_id: str): change_id = urllib.parse.quote(str(change_id), safe='') uri = f'/a/changes/{change_id}' resp = await self._request('get', uri) return aChange(self, ujson.loads(resp[5:]))
class aProject: def __init__(self, gerrit: aGerrit, project: str): self._request = gerrit._request self.project = project proj_id = urllib.parse.quote(project, safe='') self.uri_prefix = f"/a/projects/{proj_id}"
@strip_magic_prefix async def list_branches(self) -> str: uri = f'{self.uri_prefix}/branches/' return await self._request('get', uri)
@strip_magic_prefix async def get_branch(self, branch: str, /) -> str: branch_id = urllib.parse.quote(branch, safe='') uri = f'{self.uri_prefix}/branches/{branch_id}' return await self._request('get', uri)
async def get_content(self, branch: str, file: str, /) -> str: branch = urllib.parse.quote(branch, safe='') file = urllib.parse.quote(file, safe='') uri = f'{self.uri_prefix}/branches/{branch}/files/{file}/content' data = await self._request('get', uri) return base64.b64decode(data).decode('utf-8')
@strip_magic_prefix async def create_branch(self, branch: str, revision: str) -> str: branch = urllib.parse.quote(branch, safe='') data = {"revision": revision} uri = f'{self.uri_prefix}/branches/{branch}' return await self._request('put', uri, json=data)
async def create_change(self, subject: str, branch, /, **extra_infos): data = {"project": self.project, "subject": subject, "branch": branch} data.update(extra_infos) resp = await self._request('post', '/a/changes/', json=data) return aChange(self, ujson.loads(resp[5:]))
def commit(self): return aCommit(self)
class aCommit: def __init__(self, gerrit: aGerrit): self._request = gerrit._request self.uri_prefix = f"{gerrit.uri_prefix}/commits"
@strip_magic_prefix async def get(self, commit: str): commit = urllib.parse.quote(commit, safe='') uri = f"{self.uri_prefix}/{commit}" return await self._request('get', uri)
async def get_content(self, commit: str, file: str, /) -> str: commit = urllib.parse.quote(commit, safe='') file = urllib.parse.quote(file, safe='') uri = f"{self.uri_prefix}/{commit}/files/{file}/content" data = await self._request('get', uri) return base64.b64decode(data).decode('utf-8')
@strip_magic_prefix async def list_files(self, commit: str): commit = urllib.parse.quote(commit, safe='') uri = f'{self.uri_prefix}/{commit}/files/' return await self._request('get', uri)
class aChange: def __init__(self, gerrit: aGerrit, change: dict): self._request = gerrit._request self.info = change self.number = change["_number"] self.uri_prefix = f"/a/changes/{change['_number']}"
def edit(self): return aEdit(self)
async def set_to_wip(self): uri = f"{self.uri_prefix}/wip" data = {"message": "set to work in progress."} await self._request('post', uri, json=data)
async def set_to_ready(self): uri = f"{self.uri_prefix}/ready" data = {"message": "change is ready for review."} await self._request('post', uri, json=data)
async def abandon(self): uri = f"{self.uri_prefix}/abandon" data = {"message": "no files changed, so abandon change."} await self._request('post', uri, json=data)
def reviewer(self): return aReviewer(self)
def revision(self, revision="current"): return aRevision(self, revision)
async def submit(self): uri = f"{self.uri_prefix}/submit" await self._request('post', uri, json={})
class aRevision: def __init__(self, change: aChange, revision="current"): self.change_number = change.number self._request = change._request self.uri_prefix = f"{change.uri_prefix}/revisions" self.revision = revision
async def review(self, review_input: dict): uri = f'{self.uri_prefix}/{self.revision}/review' await self._request('post', uri, json=review_input)
class aReviewer: def __init__(self, change: aChange): self.change_number = change.number self._request = change._request self.uri_prefix = f"{change.uri_prefix}/reviewers"
async def add(self, user: str) -> None: data = {"reviewer": user.split('@', 1)[0]} await self._request('post', self.uri_prefix, json=data)
@strip_magic_prefix async def list(self,) -> None: uri = self.uri_prefix + '/' return await self._request('get', uri)
class aEdit: def __init__(self, change: aChange): self.change_number = change.number self._request = change._request self.uri_prefix = f"{change.uri_prefix}/edit"
async def change_file(self, file, content: bytes = b'', fd: io.BufferedReader = None): uri = f"{self.uri_prefix}/{file}" if content: encoded_string = base64.b64encode(content).decode('utf-8') elif isinstance(fd, io.BufferedReader): encoded_string = base64.b64encode(fd.read()).decode('utf-8') data_uri = 'data:text/xml;base64,' + encoded_string data = {"binary_content": data_uri} codes = {200, 201, 204, 409} await self._request('put', uri, expect_codes=codes, json=data)
async def delete_file(self, file): uri = f"{self.uri_prefix}/{file}" await self._request('delete', uri)
async def publish(self) -> bool: uri = f"{self.uri_prefix}:publish" pub_data = {"notify": "NONE"} codes = {200, 201, 204, 409} exit_code, _ = await self._request( 'post', uri, exit_code=True, expect_codes=codes, json=pub_data ) if exit_code != 409: return True return False
async def delete(self): await self._request('delete', self.uri_prefix)
|