From 9e895e34b622ad3f23565d6b96b62a4a709e58a3 Mon Sep 17 00:00:00 2001 From: Stephan Kulow Date: Sun, 6 Nov 2022 12:18:16 +0100 Subject: [PATCH] Adding a gitea remote when creating the git repo --- lib/git.py | 28 +++++++++++++++++++++++++++- lib/git_exporter.py | 7 ++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/git.py b/lib/git.py index 19561d9..ce5e238 100644 --- a/lib/git.py +++ b/lib/git.py @@ -1,9 +1,11 @@ import fnmatch import logging +import os import pathlib import subprocess import pygit2 +import requests from lib.binary import BINARY @@ -30,10 +32,12 @@ class Git: def create(self): """Create a local git repository""" self.path.mkdir(parents=True, exist_ok=True) + self.open() + + def open(self): # Convert the path to string, to avoid some limitations in # older pygit2 self.repo = pygit2.init_repository(str(self.path)) - return self def is_dirty(self): """Check if there is something to commit""" @@ -188,3 +192,25 @@ class Git: if file.name in patterns: patterns.remove(file.name) self.add_specific_lfs_gitattributes(patterns) + + def add_gitea_remote(self, package): + repo_name = package.replace("+", "_") + org_name = "rpm" + + if not os.getenv("GITEA_TOKEN"): + logging.warning("Not adding a remote due to missing $GITEA_TOKEN") + return + + url = f"https://gitea.opensuse.org/api/v1/org/{org_name}/repos" + response = requests.post( + url, + data={"name": repo_name}, + headers={"Authorization": f"token {os.getenv('GITEA_TOKEN')}"}, + timeout=10, + ) + # 409 Conflict (Already existing) + # 201 Created + if response.status_code not in (201, 409): + print(response.data) + url = f"gitea@gitea.opensuse.org:{org_name}/{repo_name}.git" + self.repo.remotes.create("origin", url) diff --git a/lib/git_exporter.py b/lib/git_exporter.py index 5540f37..80333d2 100644 --- a/lib/git_exporter.py +++ b/lib/git_exporter.py @@ -22,7 +22,12 @@ class GitExporter: repodir / package, committer="Git OBS Bridge", committer_email="obsbridge@suse.de", - ).create() + ) + if self.git.exists(): + self.git.open() + else: + self.git.create() + self.git.add_gitea_remote(package) self.state_file = os.path.join(self.git.path, ".git", "_flat_state.yaml") self.gc_interval = 200 self.cachedir = cachedir