python/hooks/post_gen_project.py

115 lines
3.8 KiB
Python
Raw Normal View History

2023-05-15 13:23:02 +02:00
import subprocess
2023-05-19 19:35:18 +02:00
import os
REMOVE_PATHS = [
{% if cookiecutter.open_source_license == "Proprietary" %} "LICENSE", {% endif %}
{% if cookiecutter.configure_ci == "False" %} ".woodpecker.yml", {% endif %}
]
for path in REMOVE_PATHS:
if path and os.path.exists(path):
if os.path.isdir(path):
os.rmdir(path)
else:
os.unlink(path)
2023-05-15 13:23:02 +02:00
subprocess.call(["git", "init"])
subprocess.call(["git", "checkout", "-b", "main"])
subprocess.call(["git", "add", "*"])
subprocess.call(["git", "commit", "-m", "Initial commit"])
subprocess.call(["git", "config", "user.name", "{{ cookiecutter.git_user }}"])
subprocess.call(["git", "config", "user.email", "{{ cookiecutter.email }}"])
2023-05-15 21:35:47 +02:00
# subprocess.call(["python", "-m", "venv", "venv"])
2023-05-15 13:23:02 +02:00
2023-05-19 17:16:55 +02:00
def get_secret(secret_name: str):
2023-05-15 13:23:02 +02:00
try:
import secretstorage
connection = secretstorage.dbus_init()
collection = secretstorage.get_default_collection(connection)
collection.unlock()
2023-05-19 17:16:55 +02:00
secret = collection.search_items({"Title": secret_name}).__next__()
2023-05-15 13:23:02 +02:00
secret.unlock()
2023-05-19 17:16:55 +02:00
return secret.get_secret().decode("utf-8")
2023-05-15 13:23:02 +02:00
except (ModuleNotFoundError, StopIteration):
2023-05-19 17:16:55 +02:00
import getpass
return getpass.getpass(
f"Secret service or secret {'Title': 'secret_name'} not available,"
2023-05-15 13:23:02 +02:00
"please enter you gitea api token:"
)
2023-05-19 17:16:55 +02:00
if {{cookiecutter.generate_gitea_project}}:
try:
import giteapy
except ModuleNotFoundError:
print("module `giteapy` is not availabled, repository not created")
exit()
API_KEY = get_secret("Gitea Token")
2023-05-15 13:23:02 +02:00
configuration = giteapy.Configuration()
configuration.api_key["access_token"] = API_KEY
client = giteapy.ApiClient(configuration)
client.configuration.host = "{{ cookiecutter.gitea_url }}/api/v1"
2023-05-19 17:16:55 +02:00
api_instance = giteapy.UserApi(client)
2023-05-15 13:23:02 +02:00
repo = giteapy.CreateRepoOption(
name="{{ cookiecutter.project_slug }}", private=True
)
2023-05-19 18:21:14 +02:00
api_instance.create_current_user_repo(body=repo)
2023-05-15 13:23:02 +02:00
2023-05-19 17:16:55 +02:00
if {{cookiecutter.generate_gitea_project}} and {{cookiecutter.configure_ci}}:
import http.client
# import json
api_instance = giteapy.RepositoryApi(client)
options = giteapy.AddCollaboratorOption("read")
api_instance.repo_add_collaborator(
"{{ cookiecutter.git_user }}",
"{{ cookiecutter.project_slug }}",
"{{ cookiecutter.woodpecker_gitea_user }}",
2023-05-19 18:21:14 +02:00
body=options,
2023-05-19 17:16:55 +02:00
)
API_KEY = get_secret("Woodpecker Token")
WOODPECKER_SERVER = "{{ cookiecutter.woodpecker_server }}".removeprefix("https://")
2023-05-19 18:21:14 +02:00
origin = "{{ cookiecutter.git_origin }}"
repo = "/".join(origin.removesuffix(".git").split(":")[-1].split("/")[-2:])
2023-05-19 17:16:55 +02:00
connection = http.client.HTTPSConnection(WOODPECKER_SERVER)
2023-05-19 18:21:14 +02:00
connection.request(
"GET",
"/api/user/repos?all=true&flush=true",
headers={"Authorization": f"Bearer {API_KEY}"},
)
response = connection.getresponse()
status = response.status
response.read()
if status != 200:
print(
f"\033[38;2;255;0;0mInvalid response from woodpecker while loading repos: {status} ({response.reason})\033[0m"
)
exit(1)
2023-05-19 17:16:55 +02:00
connection.request(
"POST",
f"/api/repos/{repo}",
headers={
"Authorization": f"Bearer {API_KEY}",
"referer": f"https://{WOODPECKER_SERVER}/repo/add",
},
)
response = connection.getresponse()
status = response.status
if status != 200:
2023-05-19 18:21:14 +02:00
print(
f"\033[38;2;255;0;0mInvalid response from woodpecker while linking repo: {status} ({response.reason})\033[0m"
)
exit(1)
if {{cookiecutter.generate_gitea_project}}:
subprocess.call(["git", "remote", "add", "origin", "{{ cookiecutter.git_origin }}"])
subprocess.call(["git", "push", "-u", "origin", "main"])