Skip to content

Commit

Permalink
add automagic modlinks MR Action
Browse files Browse the repository at this point in the history
  • Loading branch information
PrashantMohta committed Aug 21, 2024
1 parent 49d726c commit 470d24f
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
79 changes: 79 additions & 0 deletions .github/workflows/auto-modlinks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
on:
push:
tags:
- '*'

name: on-new-release-raise-modlinks-mr

jobs:
send-pull-requests:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
with:
fetch-depth: 0 # Checkout everything to get access to the tags
ref: ${{github.event.pull_request.head.ref}}
repository: ${{github.event.pull_request.head.repo.full_name}}
token: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/setup-python@v5
with:
python-version: '3.9'
#cache: 'pip' # caching pip dependencies
- name: Update into modlinks and send pull-request
run: |
CURR_REPO_NAME="${{github.repository}}"
ACCESS_TOKEN="${{ secrets.ACCESS_TOKEN }}"
PROJECT_NAME="${{vars.PROJECT_NAME}}"
USER_NAME="${{vars.USER_NAME }}"
USER_EMAIL="${{vars.USER_EMAIL }}"
ASSET_NAME="${{vars.ASSET_NAME}}"
REPOSITORY="$USER_NAME/modlinks"
LATEST_TAG=$(git describe --tags --always --abbrev=0)
FOLDER="bin/$REPOSITORY"
BRANCH_NAME="update-$CURR_REPO_NAME-to-$LATEST_TAG"
DOWNLOAD_LINK="https://github.com/$CURR_REPO_NAME/releases/download/$LATEST_TAG/$ASSET_NAME"
# Clone the remote repository and change working directory to the
# folder it was cloned to.
git clone \
--depth=1 \
--branch=main \
https://$USER_NAME:$ACCESS_TOKEN@github.com/$REPOSITORY \
$FOLDER
cd $FOLDER
# Setup the committers identity.
git config user.email "$USER_EMAIL"
git config user.name "$USER_NAME"
# Create a new feature branch for the changes.
git checkout -b $BRANCH_NAME
# Update the files to the latest version.
cp ../../../.github/workflows/sha256.py ./sha256.py
cp ../../../.github/workflows/update-modlinks.py ./update-modlinks.py
#install pip packages
#cp ../../../.github/workflows/requirements.txt ./requirements.txt
#pip install -r requirements.txt
curl --output $ASSET_NAME $DOWNLOAD_LINK
SHA256=$(python sha256.py $ASSET_NAME)
python update-modlinks.py "$PROJECT_NAME" "$LATEST_TAG" "$DOWNLOAD_LINK" "$SHA256"
# Commit the changes and push the feature branch to origin
git add ModLinks.xml
git commit -m "update $PROJECT_NAME to $LATEST_TAG"
git push origin $BRANCH_NAME --force
echo $ACCESS_TOKEN > pat.txt
# Authorize GitHub CLI for the current repository and
# create a pull-requests containing the updates.
gh auth login --with-token < pat.txt
gh pr create \
--body "" \
--title "Update $PROJECT_NAME to $LATEST_TAG" \
--head "$BRANCH_NAME" \
--base "main"
27 changes: 27 additions & 0 deletions .github/workflows/sha256.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import sys
import hashlib

name=''
if len(sys.argv) < 2:
print('''
Missing Parameters
Usage : python sha256.py <file path>
''')
exit(1)
else:
name = sys.argv[1]


BUF_SIZE = 65536
md5 = hashlib.md5()
sha256 = hashlib.sha256()

with open(name, 'rb') as f:
while True:
data = f.read(BUF_SIZE)
if not data:
break
md5.update(data)
sha256.update(data)

print("{0}".format(sha256.hexdigest()))
64 changes: 64 additions & 0 deletions .github/workflows/update-modlinks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import xml.etree.ElementTree as ET
import sys

name = ''
new_version = ''
new_link = ''
new_sha = ""

if len(sys.argv) < 5:
print('''
Missing Parameters
Usage : python update-modlinks.py <mod name> <new version> <new download link> <asset sha256>
''')
exit(1)
else:
name = sys.argv[1]
new_version = sys.argv[2]
new_link = sys.argv[3]
new_sha = sys.argv[4]

#create a valid version name for modlinks
new_version=''.join(c for c in new_version if c.isdigit() or c == ".")
version_seperators = new_version.count(".")
if(version_seperators > 3):
print(new_version.split(".",4)[0:4])
new_version = ".".join(new_version.split(".",4)[0:4])
else:
need_seperators = 3 - version_seperators
while need_seperators > 0:
new_version = new_version + ".0"
need_seperators-=1

print({'name':name,'version':new_version,'link':new_link , 'sha': new_sha})
tag_name = "{https://github.com/HollowKnight-Modding/HollowKnight.ModLinks/HollowKnight.ModManager}Name"
tag_version = "{https://github.com/HollowKnight-Modding/HollowKnight.ModLinks/HollowKnight.ModManager}Version"
tag_link = "{https://github.com/HollowKnight-Modding/HollowKnight.ModLinks/HollowKnight.ModManager}Link"

old_modlinks_data = {'name':'','version':'','link':'' , 'sha': ''}

tree = ET.parse('ModLinks.xml')
root = tree.getroot()
for child in root:
if child.find(tag_name).text == name :
old_modlinks_data['name'] = child.find(tag_name).text.strip()
old_modlinks_data['version'] = child.find(tag_version).text.strip()
old_modlinks_data['link'] = child.find(tag_link).text.strip()
old_modlinks_data['sha'] = child.find(tag_link).get('SHA256').strip()
break

print(old_modlinks_data)

modlink_str = open('ModLinks.xml','r').read()

if len(old_modlinks_data['version']) > 0:
modlink_str = modlink_str.replace(old_modlinks_data['version'],new_version)

if len(old_modlinks_data['link']) > 0:
modlink_str = modlink_str.replace(old_modlinks_data['link'],new_link)

if len(old_modlinks_data['sha']) > 0:
modlink_str = modlink_str.replace(old_modlinks_data['sha'],new_sha)

with open("ModLinks.xml", "w") as f:
f.write(modlink_str)

0 comments on commit 470d24f

Please sign in to comment.