feat: add tags to repositories screated by gitea CI scripts

This commit is contained in:
Aravinth Manivannan 2022-05-04 22:53:08 +05:30
parent 6355835135
commit 0acfd137be
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88

View file

@ -4,6 +4,7 @@ from time import sleep
import random import random
from requests import Session from requests import Session
from requests.auth import HTTPBasicAuth
import requests import requests
GITEA_USER = "bot" GITEA_USER = "bot"
@ -13,6 +14,7 @@ GITEA_PASSWORD = "foobarpassword"
TOTAL_NUM_REPOS = 100 TOTAL_NUM_REPOS = 100
REPOS = [] REPOS = []
def check_online(): def check_online():
count = 0 count = 0
while True: while True:
@ -128,7 +130,6 @@ def register(client: HTMLClient):
resp = client.session.post(url, data=payload, allow_redirects=False) resp = client.session.post(url, data=payload, allow_redirects=False)
def login(client: HTMLClient): def login(client: HTMLClient):
url = "http://localhost:8080/user/login" url = "http://localhost:8080/user/login"
csrf = client.get_csrf_token(url) csrf = client.get_csrf_token(url)
@ -144,14 +145,12 @@ def login(client: HTMLClient):
print("User logged in") print("User logged in")
return return
raise Exception( raise Exception(f"[ERROR] Authentication failed. status code {resp.status_code}")
f"[ERROR] Authentication failed. status code {resp.status_code}"
)
def create_repositories(client: HTMLClient): def create_repositories(client: HTMLClient):
print("foo") print("foo")
def get_repository_payload(csrf: str, name: str): def get_repository_payload(csrf: str, name: str):
data = { data = {
"_csrf": csrf, "_csrf": csrf,
@ -164,16 +163,32 @@ def create_repositories(client: HTMLClient):
"license": "", "license": "",
"readme": "Default", "readme": "Default",
"default_branch": "master", "default_branch": "master",
"trust_model": "default" "trust_model": "default",
} }
return data return data
url = "http://localhost:8080/repo/create" url = "http://localhost:8080/repo/create"
for repo in REPOS: for repo in REPOS:
csrf = client.get_csrf_token(url) csrf = client.get_csrf_token(url)
resp = client.session.post(url, data=get_repository_payload(csrf, repo)) resp = client.session.post(url, data=get_repository_payload(csrf, repo))
print(f"Created repository {repo}") print(f"Created repository {repo}")
if resp.status_code != 302 and resp.status_code != 200: if resp.status_code != 302 and resp.status_code != 200:
raise Exception(f"Error while creating repository: {repo} {resp.status_code}") raise Exception(
f"Error while creating repository: {repo} {resp.status_code}"
)
add_tag(repo, client)
def add_tag(repo: str, client: HTMLClient):
print("adding tags")
tag = "testing"
url = f"http://{GITEA_USER}:{GITEA_PASSWORD}@localhost:8080/api/v1/repos/{GITEA_USER}/{repo}/topics/{tag}"
resp = requests.put(url)
if resp.status_code != 204:
print(f"Error while adding tags repository: {repo} {resp.status_code}")
raise Exception(
f"Error while adding tags repository: {repo} {resp.status_code}"
)
if __name__ == "__main__": if __name__ == "__main__":
@ -193,8 +208,8 @@ if __name__ == "__main__":
create_repositories(client) create_repositories(client)
break break
except Exception as e: except Exception as e:
print(f"Error: {e}") print(f"Error: {e}")
print(f"Retrying {count} time") print(f"Retrying {count} time")
count += 1 count += 1
sleep(5) sleep(5)
continue continue