Generate changelog from git history fixes #1

This commit is contained in:
Stanislas Daniel Claude Dolcini 2024-09-09 21:17:46 +03:00
parent 9d9f0e441c
commit cec9a35216
Signed by: Stan
GPG Key ID: 244943DFF8370D60

View File

@ -1,8 +1,56 @@
import json
import os
import requests
from . import MOD_PATH
import subprocess
def run_git_command(command):
""" Run a git command and return its output. """
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if result.returncode != 0:
raise Exception(f"Git command failed: {result.stderr}")
return result.stdout.strip()
def get_current_commit():
""" Get the current commit hash. """
return run_git_command(['git', 'rev-parse', 'HEAD'])
def get_commit_for_tag(tag):
""" Get the commit hash for a tag, or return None if the tag doesn't exist. """
try:
commit_hash = run_git_command(['git', 'rev-list', '-n', '1', tag])
return commit_hash
except Exception:
return None
def get_previous_tag_in_series(tag):
""" Get the previous tag in the same minor version series (e.g., 0.26.X-1). """
try:
# Extract the major.minor part of the tag (e.g., '0.26' from '0.26.11')
major_minor_version = '.'.join(tag.split('.')[:2])
# List all tags, reverse sorted by version number
tags = run_git_command(['git', 'tag', '--sort=-v:refname']).splitlines()
# Filter tags that match the major.minor version
filtered_tags = [t for t in tags if t.startswith(major_minor_version)]
if tag in filtered_tags:
current_index = filtered_tags.index(tag)
if current_index + 1 < len(filtered_tags):
return filtered_tags[current_index + 1]
return None
except Exception as e:
raise Exception(f"Failed to get previous tag in series: {e}")
def get_changelog(current_commit, previous_tag):
""" Get the changelog between the current commit and the previous tag. """
try:
if previous_tag:
changelog = run_git_command(['git', 'log', f'{previous_tag}..{current_commit}', '--oneline'])
else:
# No previous tag, so get the log from the start of the repository
changelog = run_git_command(['git', 'log', current_commit, '--oneline'])
return changelog
except Exception as e:
raise Exception(f"Failed to generate changelog: {e}")
api_key = os.getenv('MODIO_API_KEY')
@ -15,6 +63,18 @@ mod_version = os.getenv("MOD_VERSION")
community_mod_id = 2144305
zeroad_id = 5
mod_json = json.load(open(MOD_PATH / 'mod.json', 'r'))
tag = mod_json['version']
commit = get_commit_for_tag(tag)
if not commit:
print(f"Tag {tag} does not exist. Using current commit as fallback.")
commit = get_current_commit()
previous_tag = get_previous_tag_in_series(tag)
changelog = get_changelog(commit, previous_tag)
headers = {
'Authorization': f'Bearer {oauth2_token}',
'Accept': 'application/json'
@ -26,15 +86,15 @@ r = requests.get(f'https://api.mod.io/v1/me/mods', params={
print(r.json())
files = {'filedata': open(mod_file_path, 'rb')}
files = {'filedata': open(mod_file_path, 'rb', encoding='utf-8')}
mod_json = json.load(open(MOD_PATH / 'mod.json', 'r'))
signature = open('signature.minisign', 'r').read()
rq = requests.post(f'https://api.mod.io/v1/games/{zeroad_id}/mods/{community_mod_id}/files', files=files, headers=headers, data={
'version': mod_version,
'active': True,
'changelog': changelog,
'metadata_blob' : json.dumps({
'dependencies': mod_json['dependencies'],
'minisigs': [signature]