0ad-community-mod-a26/scripts/modio.py

111 lines
3.8 KiB
Python

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:
version_array = tag.split('.')
# Extract the major.minor part of the tag (e.g., '0.26' from '0.26.11')
major_minor_version = '.'.join(version_array[: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]
version_array[2] = str(int(version_array[2]) - 1)
manual_tag = '.'.join(version_array)
if manual_tag in filtered_tags:
current_index = filtered_tags.index(manual_tag)
return filtered_tags[current_index]
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 is not None:
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')
# This must be generated manually from the website's interface.
oauth2_token = os.getenv("MODIO_OAUTH2_TOKEN")
mod_file_path = os.getenv("MOD_FILE_PATH")
mod_version = os.getenv("MOD_VERSION")
mod_name = os.getenv("MOD_NAME")
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'
}
r = requests.get(f'https://api.mod.io/v1/me/mods', params={
'api_key': api_key
}, headers = headers)
print(r.json())
files = {'filedata': open(mod_file_path, 'rb')}
signature = open(f"{mod_name}-{mod_version}.zip.minisign", 'r', encoding="utf-8").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]
})
})
print(rq.json())