forked from lineageos-infra/gerrit-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.py
39 lines (30 loc) · 1.39 KB
/
lib.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
import json
import os
from urllib.parse import quote_plus
import requests
class Config:
GERRIT_USER = os.environ.get("GERRIT_USER")
GERRIT_PASS = os.environ.get("GERRIT_PASS")
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN")
class Gerrit:
def __init__(self):
self.auth = requests.auth.HTTPBasicAuth(Config.GERRIT_USER, Config.GERRIT_PASS)
def get_projects(self):
url = "https://review.lineageos.org/a/projects/?t"
resp = requests.get(url, auth=self.auth)
if resp.status_code != 200:
raise Exception(f"Error communicating with gerrit: {resp.text}")
projects = json.loads(resp.text[5:])
return projects
def update_parent(self, child, parent, auth=None):
child = quote_plus(child)
url = f"https://review.lineageos.org/a/projects/{child}/parent"
print(f"Updating {child}'s parent to {parent}")
resp = requests.put(url, json=({"parent": parent, "commit_message": "Auto update from gerrit_config"}), auth=self.auth)
if resp.status_code != 200:
raise Exception(f"Error communicating with gerrit: {resp.text}")
def create_project(self, name):
url = f"https://review.lineageos.org/a/projects/{name.replace('/', '%2f')}"
resp = requests.put(url, auth=self.auth)
if resp.status_code != 201:
raise Exception(f"Error communicating with gerrit: {resp.text}")