From 4ad8e3605b75747fcaa989802d863eace70469e9 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 30 Jun 2024 17:23:05 +0100 Subject: [PATCH] Use git log as fallback for release notes Part of #545 --- app/tasks/importtasks.py | 10 ++++++++-- app/tests/unit/utils/test_git.py | 16 ++++++++++++++-- app/utils/git.py | 24 +++++++++++++++++++++++- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/app/tasks/importtasks.py b/app/tasks/importtasks.py index 0c9264cd..258b76ff 100644 --- a/app/tasks/importtasks.py +++ b/app/tasks/importtasks.py @@ -35,7 +35,7 @@ from app.models import AuditSeverity, db, NotificationType, PackageRelease, Meta from app.tasks import celery, TaskError from app.utils import random_string, post_bot_message, add_system_notification, add_system_audit_log, \ get_games_from_list, add_audit_log -from app.utils.git import clone_repo, get_latest_tag, get_latest_commit, get_temp_dir +from app.utils.git import clone_repo, get_latest_tag, get_latest_commit, get_temp_dir, get_release_notes from .minetestcheck import build_tree, MinetestCheckError, ContentType, PackageTreeNode from .webhooktasks import post_discord_webhook from app import app @@ -197,6 +197,12 @@ def post_release_check_update(self, release: PackageRelease, path): except IOError: pass + # Build release notes from git log + if release.commit_hash and release.release_notes is None: + previous_release = package.releases.filter(PackageRelease.id != release.id).first() + if previous_release and previous_release.commit_hash: + release.release_notes = get_release_notes(package.repo, previous_release.commit_hash, release.commit_hash) + # Update game support if package.type == PackageType.MOD or package.type == PackageType.TXP: game_is_supported = {} @@ -335,6 +341,7 @@ def make_vcs_release(self, id, branch): raise TaskError("No package attached to release") with clone_repo(release.package.repo, ref=branch, recursive=True) as repo: + release.commit_hash = repo.head.object.hexsha post_release_check_update(self, release, repo.working_tree_dir) filename = random_string(10) + ".zip" @@ -352,7 +359,6 @@ def make_vcs_release(self, id, branch): release.url = "/uploads/" + filename release.task_id = None - release.commit_hash = repo.head.object.hexsha release.approve(release.package.author) db.session.commit() diff --git a/app/tests/unit/utils/test_git.py b/app/tests/unit/utils/test_git.py index c99af72d..7638b2ed 100644 --- a/app/tests/unit/utils/test_git.py +++ b/app/tests/unit/utils/test_git.py @@ -16,8 +16,7 @@ import os -from app.utils.git import get_latest_tag, get_latest_commit, clone_repo - +from app.utils.git import get_latest_tag, get_latest_commit, clone_repo, get_commit_list test_repo = "https://gitlab.com/rubenwardy/testmod" master_head = "23d12265ff6de84548b2e3e90dc7351a54f63f00" @@ -37,6 +36,19 @@ def test_get_latest_commit(): assert get_latest_commit(test_repo, "test-branch") == test_branch_head +def test_get_commit_list(): + result = get_commit_list(test_repo, "4fd0d06df2cc502b0cbc3ee932217b540f0d92ad", "23d12265ff6de84548b2e3e90dc7351a54f63f00") + assert result == [ + "Update .cdb.json", + "Update mod.conf", + "Update mod.conf", + "Update mod.conf", + "Update mod.conf", + "Update mod.conf", + "Update mod.conf", + ] + + def test_git_clone_head(): with clone_repo(test_repo, recursive=True) as repo: assert repo.head.commit.hexsha == master_head diff --git a/app/utils/git.py b/app/utils/git.py index ae1e06cd..609c50e5 100644 --- a/app/utils/git.py +++ b/app/utils/git.py @@ -16,6 +16,8 @@ import contextlib +from typing import List, Optional + import git import gitdb import os @@ -26,7 +28,7 @@ from urllib.parse import urlsplit from git import GitCommandError from app.tasks import TaskError -from app.utils import random_string +from app.utils import random_string, normalize_line_endings def generate_git_url(urlstr): @@ -122,3 +124,23 @@ def get_latest_tag(git_url): tag = hash_ref_list[1].replace("refs/tags/", "") commit_hash = repo.git.rev_parse(tag + "^{}") return tag, commit_hash + + +def get_commit_list(git_url: str, start: str, end: str) -> List[str]: + with (get_temp_dir() as git_dir): + repo = git.Repo.init(git_dir) + origin = repo.create_remote("origin", url=git_url) + origin.fetch() + + commits = repo.iter_commits(f"{start}..{end}") + ret = [commit.summary for commit in commits] + ret.reverse() + return ret + + +def get_release_notes(git_url: str, start: str, end: str) -> Optional[str]: + commits = get_commit_list(git_url, start, end) + if len(commits) == 0: + return None + + return normalize_line_endings("\n".join(map(lambda x: f"- {x}", commits)) + f"\n")