From 0c830127ec67ba1f74892092c08c70c30da1cf2a Mon Sep 17 00:00:00 2001 From: Histausse Date: Sat, 25 Mar 2023 21:02:31 +0100 Subject: [PATCH] Generate gitea project automagically --- TODO.md | 1 - cookiecutter.json | 6 ++++-- hooks/post_gen_project.py | 42 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/TODO.md b/TODO.md index 0b026f6..fa45df9 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,4 @@ # TODO: -- generate gitea project - tests - add AGPL diff --git a/cookiecutter.json b/cookiecutter.json index 86b806c..248c345 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -5,8 +5,10 @@ "email": "histausse@protonmail.com", "git_user": "histausse", "project_slug": "{{ cookiecutter.project_name.lower().replace(' ', '_').replace('-', '_') }}", - "project_url": "https://git.mineau.eu/{{ cookiecutter.git_user }}/{{ cookiecutter.project_slug }}", - "git_ogirin": "git@git.mineau.eu/{{ cookiecutter.git_user }}/{{ cookiecutter.project_slug }}.git", + "gitea_url": "https://git.mineau.eu", + "project_url": "{{ cookiecutter.gitea_url }}/{{ cookiecutter.git_user }}/{{ cookiecutter.project_slug }}", + "generate_gitea_project": true, + "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": ["GNU General Public License v3", "MIT license", "BSD license", "ISC license", "Apache Software License 2.0", "Not open source"], "python_min_version": "3.10" diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index e01a4e3..0330ee6 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -5,7 +5,45 @@ 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 }}"]) -subprocess.call(["git", "remote", "add", "origin", "{{ cookiecutter.git_ogirin }}"]) -subprocess.call(["git", "branch", "-u", "origin/master"]) 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() + try: + import secretstorage + + connection = secretstorage.dbus_init() + collection = secretstorage.get_default_collection(connection) + collection.unlock() + secret = collection.search_items({"Title": "Gitea Token"}).__next__() + secret.unlock() + API_KEY = secret.get_secret().decode("utf-8") + except (ModuleNotFoundError, StopIteration): + try: + import getpass + + my_input = getpass.getpass + except ModuleNotFoundError: + my_input = input + API_KEY = my_input( + "Secret service or secret {'Title': 'Gitea Token'} not available," + "please enter you gitea api key:" + ) + 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 }}" + repo = giteapy.CreateRepoOption( + name="{{ cookiecutter.project_slug }}", private=True + ) + api_instance.admin_create_repo(username, repo) + + subprocess.call(["git", "remote", "add", "origin", "{{ cookiecutter.git_origin }}"]) + subprocess.call(["git", "push", "-u", "origin", "master"])