From 470d24ff8e8f5fc71fff3c0747bf276a43254189 Mon Sep 17 00:00:00 2001 From: Prashant Mohta Date: Thu, 22 Aug 2024 02:25:11 +0700 Subject: [PATCH] add automagic modlinks MR Action --- .github/workflows/auto-modlinks.yml | 79 ++++++++++++++++++++++++++++ .github/workflows/sha256.py | 27 ++++++++++ .github/workflows/update-modlinks.py | 64 ++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 .github/workflows/auto-modlinks.yml create mode 100644 .github/workflows/sha256.py create mode 100644 .github/workflows/update-modlinks.py diff --git a/.github/workflows/auto-modlinks.yml b/.github/workflows/auto-modlinks.yml new file mode 100644 index 0000000..62b3798 --- /dev/null +++ b/.github/workflows/auto-modlinks.yml @@ -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" diff --git a/.github/workflows/sha256.py b/.github/workflows/sha256.py new file mode 100644 index 0000000..f842092 --- /dev/null +++ b/.github/workflows/sha256.py @@ -0,0 +1,27 @@ +import sys +import hashlib + +name='' +if len(sys.argv) < 2: + print(''' +Missing Parameters + Usage : python sha256.py +''') + 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())) \ No newline at end of file diff --git a/.github/workflows/update-modlinks.py b/.github/workflows/update-modlinks.py new file mode 100644 index 0000000..a9e3689 --- /dev/null +++ b/.github/workflows/update-modlinks.py @@ -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 +''') + 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) \ No newline at end of file