generate new gitea token for the CI

This commit is contained in:
Histausse 2023-05-23 12:38:31 +02:00 committed by Jean-Marie Mineau
parent 09c4d95376
commit 721d134c6a

View file

@ -1,4 +1,6 @@
import subprocess import subprocess
import http.client
import json
import os import os
REMOVE_PATHS = [ REMOVE_PATHS = [
@ -41,6 +43,33 @@ def get_secret(secret_name: str):
"please enter you gitea api token:" "please enter you gitea api token:"
) )
def get_new_gitea_token(
connection: http.client.HTTPSConnection, new_token_name: str, user: str, token: str
) -> str:
auth = b64encode(f"{user}:{token}".encode("utf-8")).decode("ascii")
connection.request(
"POST",
f"/api/v1/users/{user}/tokens",
json.dumps(
{
"name": new_token_name,
}
),
{
"Authorization": f"Basic {auth}",
"content-type": "application/json",
},
)
response = connection.getresponse()
status = response.status
if status != 201:
print(
f"\033[38;2;255;0;0mInvalid response from gitea while creating a new token: {status} ({response.reason})\033[0m"
)
exit(1)
data = json.load(response)
return data["sha1"]
if {{cookiecutter.generate_gitea_project}}: if {{cookiecutter.generate_gitea_project}}:
try: try:
@ -48,9 +77,9 @@ if {{cookiecutter.generate_gitea_project}}:
except ModuleNotFoundError: except ModuleNotFoundError:
print("module `giteapy` is not availabled, repository not created") print("module `giteapy` is not availabled, repository not created")
exit() exit()
API_KEY = get_secret("Gitea Token") GITEA_API_KEY = get_secret("Gitea Token")
configuration = giteapy.Configuration() configuration = giteapy.Configuration()
configuration.api_key["access_token"] = API_KEY configuration.api_key["access_token"] = GITEA_API_KEY
client = giteapy.ApiClient(configuration) client = giteapy.ApiClient(configuration)
client.configuration.host = "{{ cookiecutter.gitea_url }}/api/v1" client.configuration.host = "{{ cookiecutter.gitea_url }}/api/v1"
api_instance = giteapy.UserApi(client) api_instance = giteapy.UserApi(client)
@ -61,10 +90,6 @@ if {{cookiecutter.generate_gitea_project}}:
if {{cookiecutter.generate_gitea_project}} and {{cookiecutter.configure_ci}}: if {{cookiecutter.generate_gitea_project}} and {{cookiecutter.configure_ci}}:
import http.client
# import json
api_instance = giteapy.RepositoryApi(client) api_instance = giteapy.RepositoryApi(client)
options = giteapy.AddCollaboratorOption("read") options = giteapy.AddCollaboratorOption("read")
api_instance.repo_add_collaborator( api_instance.repo_add_collaborator(
@ -73,7 +98,8 @@ if {{cookiecutter.generate_gitea_project}} and {{cookiecutter.configure_ci}}:
"{{ cookiecutter.woodpecker_gitea_user }}", "{{ cookiecutter.woodpecker_gitea_user }}",
body=options, body=options,
) )
connection_gitea = http.client.HTTPSConnection("git.pains-perdus.fr")
GITEA_API_KEY = get_new_gitea_token(connection_gitea, "CI {{ cookiecutter.git_user }}/{{ cookiecutter.project_slug }}", "{{ cookiecutter.git_user }}", GITEA_API_KEY)
API_KEY = get_secret("Woodpecker Token") API_KEY = get_secret("Woodpecker Token")
WOODPECKER_SERVER = "{{ cookiecutter.woodpecker_server }}".removeprefix("https://") WOODPECKER_SERVER = "{{ cookiecutter.woodpecker_server }}".removeprefix("https://")
origin = "{{ cookiecutter.git_origin }}" origin = "{{ cookiecutter.git_origin }}"
@ -111,4 +137,21 @@ if {{cookiecutter.generate_gitea_project}} and {{cookiecutter.configure_ci}}:
if {{cookiecutter.generate_gitea_project}}: if {{cookiecutter.generate_gitea_project}}:
subprocess.call(["git", "remote", "add", "origin", "{{ cookiecutter.git_origin }}"]) subprocess.call(["git", "remote", "add", "origin", "{{ cookiecutter.git_origin }}"])
subprocess.call(["git", "push", "-u", "origin", "main"]) subpr def get_new_gitea_token():
""""""
connection.request(
"POST",
f"/api/repos/{repo}/secrets",
json.dumps(
{
"name": "test_token",
"value": "loren ispum",
"image": ["test"],
"event": ["push", "tag"],
}
),
{
"Authorization": f"Bearer {API_KEY}",
"content-type": "application/json",
},
)ocess.call(["git", "push", "-u", "origin", "main"])