Skip to content

Commit

Permalink
Use git log as fallback for release notes
Browse files Browse the repository at this point in the history
Part of #545
  • Loading branch information
rubenwardy committed Jun 30, 2024
1 parent c21337b commit 4ad8e36
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
10 changes: 8 additions & 2 deletions app/tasks/importtasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
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
Expand Down Expand Up @@ -197,6 +197,12 @@ def get_meta_packages(names):
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 = {}
Expand Down Expand Up @@ -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"
Expand All @@ -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()

Expand Down
16 changes: 14 additions & 2 deletions app/tests/unit/utils/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
24 changes: 23 additions & 1 deletion app/utils/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@


import contextlib
from typing import List, Optional

import git
import gitdb
import os
Expand All @@ -26,7 +28,7 @@
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):
Expand Down Expand Up @@ -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<!-- auto from {start} to {end} -->")

0 comments on commit 4ad8e36

Please sign in to comment.