Repository navigation
Expand file tree
/
Copy pathsubmit_to_gitlab.py
More file actions
94 lines (83 loc) · 3.15 KB
/
Copy pathsubmit_to_gitlab.py
File metadata and controls
94 lines (83 loc) · 3.15 KB
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
import urllib.request
import urllib.parse
import json
import time
import sys
TOKEN = "glpat-g232XSJ384Oq_SBaGjb4iWM6MQpvOjEKdTpuZ2VrYw8.01.170vgbtvo"
HEADERS = {"PRIVATE-TOKEN": TOKEN, "Content-Type": "application/json"}
BASE_URL = "https://gitlab.com/api/v4"
UPSTREAM_PROJECT_ID = "fdroid/fdroiddata"
upstream_id_encoded = urllib.parse.quote(UPSTREAM_PROJECT_ID, safe="")
def request(method, url, data=None):
if data:
data = json.dumps(data).encode("utf-8")
req = urllib.request.Request(url, data=data, headers=HEADERS, method=method)
try:
with urllib.request.urlopen(req) as response:
return json.loads(response.read().decode())
except urllib.error.HTTPError as e:
body = e.read().decode()
if e.code == 401:
print("ERROR: Unauthorized. The token is invalid.")
sys.exit(1)
return {"error": True, "code": e.code, "body": body}
print("1. Getting user details...")
user = request("GET", f"{BASE_URL}/user")
if "error" in user:
print(f"Failed to get user details: {user}")
sys.exit(1)
username = user["username"]
print(f"Authenticated as {username}")
print("2. Forking fdroid/fdroiddata...")
fork = request("POST", f"{BASE_URL}/projects/{upstream_id_encoded}/fork")
if type(fork) is dict and fork.get("error"):
if fork["code"] == 409:
print("Fork already exists, continuing...")
else:
print(f"Fork failed: {fork}")
fork_id = urllib.parse.quote(f"{username}/fdroiddata", safe="")
print("Waiting for fork to complete...")
for i in range(10):
project = request("GET", f"{BASE_URL}/projects/{fork_id}")
if type(project) is dict and not project.get("error"):
if project.get("import_status") == "finished" or project.get("import_status") is None:
break
time.sleep(3)
print("3. Creating branch...")
branch_name = "add-calyx"
branch = request("POST", f"{BASE_URL}/projects/{fork_id}/repository/branches", {"branch": branch_name, "ref": "master"})
if type(branch) is dict and branch.get("error"):
print("Branch might already exist.")
print("4. Committing file...")
try:
with open("fdroid_metadata.yml", "r") as f:
content = f.read()
except FileNotFoundError:
print("fdroid_metadata.yml not found!")
sys.exit(1)
payload = {
"branch": branch_name,
"commit_message": "Add Calyx (com.gurkenwerfer.calyx)",
"actions": [
{
"action": "create",
"file_path": "metadata/com.gurkenwerfer.calyx.yml",
"content": content
}
]
}
commit = request("POST", f"{BASE_URL}/projects/{fork_id}/repository/commits", payload)
if type(commit) is dict and commit.get("error"):
print(f"Commit failed (file might already exist): {commit}")
print("5. Creating Merge Request...")
mr_payload = {
"source_branch": branch_name,
"target_branch": "master",
"title": "Add Calyx (com.gurkenwerfer.calyx)",
"target_project_id": 163351, # ID of fdroid/fdroiddata
}
mr = request("POST", f"{BASE_URL}/projects/{fork_id}/merge_requests", mr_payload)
if type(mr) is dict and mr.get("error"):
print(f"Failed to create MR: {mr}")
else:
print(f"SUCCESS! Merge Request created: {mr['web_url']}")