From c4a41ac791ee072a42b1cd2a53e69099f887f066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A1imon=20Kala?= Date: Mon, 18 Nov 2024 20:11:00 +0100 Subject: [PATCH] More robust and configurable commit url creation --- config/repos.toml.sample | 3 +++ src/app.py | 5 +++-- src/repository.py | 8 +++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/config/repos.toml.sample b/config/repos.toml.sample index 79f943f..431478b 100644 --- a/config/repos.toml.sample +++ b/config/repos.toml.sample @@ -4,6 +4,9 @@ # specified by [reponame] with listed config values. # +[common] +gitea_url='https://git.fykos.cz' # url for accessing gitea in a browser + [fykos37] # reponame git_path='gitea@fykos.cz:FYKOS/fykos37.git' # ssh address of git repository, string allowed_roles=['fksdb-fykos'] # array of allowed users, array of strings diff --git a/src/app.py b/src/app.py index 61ee145..1704544 100644 --- a/src/app.py +++ b/src/app.py @@ -28,9 +28,10 @@ # load repos with open('/data/config/repos.toml', 'rb') as file: - reposConfig = tomllib.load(file) + repos_config = tomllib.load(file) -repos = {repo: Repository(repo, '/data/repos/', reposConfig[repo]) for repo in reposConfig} +common_repo_config = repos_config.pop("common") +repos = {repo: Repository(repo, '/data/repos/', {**common_repo_config, **repos_config[repo]}) for repo in repos_config} def repo_required(f): @wraps(f) diff --git a/src/repository.py b/src/repository.py index 6a197dd..f731a89 100644 --- a/src/repository.py +++ b/src/repository.py @@ -216,13 +216,19 @@ def _format_time_ago(time_str): except ValueError: return time_str # orig if parse fails + def _git_remote_ssh_to_url(self, remote: str) -> str: + parts = remote.split(':')[1].split('/') + org_name = parts[0] + repo_name = parts[1].replace('.git', '') + return f"{self.config['gitea_url'].strip('/')}/{org_name}/{repo_name}/" + def get_commit_info(self, commit_hash): localpath = os.path.join(self.repodir, self.name) repo = Repo(localpath) commit = repo.commit(commit_hash) commit_msg = commit.message.strip().split('\n')[0] commit_author = commit.author.name - commit_url = f"https://git.fykos.cz/FYKOS/{self.name}/commit/{commit.hexsha}" + commit_url = self._git_remote_ssh_to_url(repo.remote().url) + 'commit/' + commit_hash return commit_msg, commit_author, commit_url def get_current_build_status(self):