From 5ceb648dc97e8a2d99460441998f9ecac66c7283 Mon Sep 17 00:00:00 2001 From: Histausse Date: Fri, 19 May 2023 17:16:55 +0200 Subject: [PATCH] first test for woodpecjer --- cookiecutter.json | 3 + hooks/post_gen_project.py | 69 ++++++++++++++----- .../.woodpecker.yml | 5 ++ 3 files changed, 59 insertions(+), 18 deletions(-) create mode 100644 {{ cookiecutter.project_slug }}/.woodpecker.yml diff --git a/cookiecutter.json b/cookiecutter.json index ba2add7..34664c8 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -8,6 +8,9 @@ "gitea_url": "", "project_url": "{{ cookiecutter.gitea_url }}/{{ cookiecutter.git_user }}/{{ cookiecutter.project_slug }}", "generate_gitea_project": [ true, false ], + "configure_ci": [ true, false ], + "woodpecker_gitea_user": "ci", + "woodpecker_server": "ci.pains-perdus.fr", "git_origin": "{{ cookiecutter.gitea_url.replace('https://', 'gitea@').replace('http://', 'gitea') }}:{{ cookiecutter.git_user }}/{{ cookiecutter.project_slug }}.git", "version": "0.1.0", "open_source_license": ["AGPL-3.0-only", "GPL-3.0-only", "MIT", "BSD-3-Clause", "ISC", "Apache-2.0", "Proprietary"], diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index f236e63..cd6c85d 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -9,42 +9,75 @@ subprocess.call(["git", "config", "user.email", "{{ cookiecutter.email }}"]) # subprocess.call(["python", "-m", "venv", "venv"]) -if {{cookiecutter.generate_gitea_project}}: - try: - import giteapy - except ModuleNotFoundError: - print("giteapy is not availabled, repository not created") - exit() + +def get_secret(secret_name: str): try: import secretstorage connection = secretstorage.dbus_init() collection = secretstorage.get_default_collection(connection) collection.unlock() - secret = collection.search_items({"Title": "Gitea Token"}).__next__() + secret = collection.search_items({"Title": secret_name}).__next__() secret.unlock() - API_KEY = secret.get_secret().decode("utf-8") + return secret.get_secret().decode("utf-8") except (ModuleNotFoundError, StopIteration): - try: - import getpass + import getpass - my_input = getpass.getpass - except ModuleNotFoundError: - my_input = input - API_KEY = my_input( - "Secret service or secret {'Title': 'Gitea Token'} not available," + return getpass.getpass( + f"Secret service or secret {'Title': 'secret_name'} not available," "please enter you gitea api token:" ) + + +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") configuration = giteapy.Configuration() configuration.api_key["access_token"] = API_KEY client = giteapy.ApiClient(configuration) client.configuration.host = "{{ cookiecutter.gitea_url }}/api/v1" - api_instance = giteapy.AdminApi(client) - username = "{{ cookiecutter.git_user }}" + api_instance = giteapy.UserApi(client) repo = giteapy.CreateRepoOption( name="{{ cookiecutter.project_slug }}", private=True ) - api_instance.admin_create_repo(username, repo) + api_instance.create_current_user_repo(repo) subprocess.call(["git", "remote", "add", "origin", "{{ cookiecutter.git_origin }}"]) subprocess.call(["git", "push", "-u", "origin", "main"]) + +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 }}", + options, + ) + + API_KEY = get_secret("Woodpecker Token") + WOODPECKER_SERVER = "{{ cookiecutter.woodpecker_server }}".removeprefix("https://") + origin = "cookiecutter.git_origin" + repo = "/".join(origin.removesuffix(".git").split("/")[-2:]) + connection = http.client.HTTPSConnection(WOODPECKER_SERVER) + 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 + # body = json.load(response) + if status != 200: + print(f"Invalid response: {status}") diff --git a/{{ cookiecutter.project_slug }}/.woodpecker.yml b/{{ cookiecutter.project_slug }}/.woodpecker.yml new file mode 100644 index 0000000..300eafc --- /dev/null +++ b/{{ cookiecutter.project_slug }}/.woodpecker.yml @@ -0,0 +1,5 @@ +pipeline: + a-test-step: + image: debian + commands: + - echo "Testing.."