mirror of
https://github.com/YunoHost-Apps/pytition_ynh.git
synced 2024-09-03 20:16:08 +02:00
Add workflow for future updates
This commit is contained in:
parent
871ae9424c
commit
f45c2b3810
2 changed files with 165 additions and 0 deletions
116
.github/workflows/updater.py
vendored
Executable file
116
.github/workflows/updater.py
vendored
Executable file
|
@ -0,0 +1,116 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import subprocess
|
||||
import textwrap
|
||||
from pathlib import Path
|
||||
from typing import List, Tuple, Any
|
||||
import requests
|
||||
from packaging import version
|
||||
|
||||
logging.getLogger().setLevel(logging.INFO)
|
||||
|
||||
# This script is meant to be run by GitHub Actions
|
||||
# The YunoHost-Apps organisation offers a template Action to run this script periodically
|
||||
# Since each app is different, maintainers can adapt its contents so as to perform
|
||||
# automatic actions when a new upstream release is detected.
|
||||
|
||||
# Remove this exit command when you are ready to run this Action
|
||||
# exit(1)
|
||||
|
||||
#=================================================
|
||||
# Fetching information
|
||||
|
||||
def get_latest_version(repo: str) -> Tuple[version.Version, Any]:
|
||||
api_url = repo.replace("https://github.com/", "https://api.github.com/repos/")
|
||||
releases = requests.get(f"{api_url}/releases").json()
|
||||
release_info = [release for release in releases if not release["prerelease"]][0]
|
||||
return version.Version(release_info["tag_name"]), release_info
|
||||
|
||||
def get_assets_of_release(repo: str, rel_info: Any) -> List[str]:
|
||||
"""May be customized by maintainers for custom urls"""
|
||||
assets = [asset["browse_download_url"] for asset in rel_info["assets"]]
|
||||
assets.append(f"{repo}/archive/refs/tags/{rel_info['tag_name']}.tar.gz")
|
||||
return assets
|
||||
|
||||
#=================================================
|
||||
# Download assets and compute filename / sha256sum
|
||||
|
||||
def sha256sum_of_url(url: str) -> str:
|
||||
"""Compute checksum without saving the file"""
|
||||
checksum = hashlib.sha256()
|
||||
for chunk in requests.get(url, stream=True).iter_content():
|
||||
checksum.update(chunk)
|
||||
return checksum.hexdigest()
|
||||
|
||||
# It has to be adapted in accordance with how the upstream releases look like.
|
||||
def handle_asset(asset_url: str):
|
||||
"""This should be customized by the maintainer"""
|
||||
logging.info("Handling asset at %s", asset_url)
|
||||
if asset_url.endswith(".tar.gz"):
|
||||
src = "app.src"
|
||||
else:
|
||||
logging.info("Asset ignored")
|
||||
return
|
||||
logging.info("Asset is for %s", src)
|
||||
|
||||
# Rewrite source file
|
||||
extension = "tar.gz" if asset_url.endswith(".tar.gz") else Path(asset_url).suffix[1:]
|
||||
with open(f"conf/{src}", "w", encoding="utf-8") as conf_file:
|
||||
conf_file.write(textwrap.dedent(f"""\
|
||||
SOURCE_URL={asset_url}
|
||||
SOURCE_SUM={sha256sum_of_url(asset_url)}
|
||||
SOURCE_SUM_PRG=sha256sum
|
||||
SOURCE_FORMAT={extension}
|
||||
SOURCE_IN_SUBDIR=true
|
||||
SOURCE_EXTRACT=true
|
||||
"""))
|
||||
|
||||
|
||||
def main():
|
||||
with open(os.environ["GITHUB_ENV"], "w", encoding="utf-8") as github_env:
|
||||
github_env.write("PROCEED=false\n")
|
||||
|
||||
with open("manifest.json", "r", encoding="utf-8") as file:
|
||||
manifest = json.load(file)
|
||||
repo = manifest["upstream"]["code"]
|
||||
|
||||
current_version = version.parse(manifest["version"].split("~")[0])
|
||||
logging.info("Current version: %s", current_version)
|
||||
latest_version, release_info = get_latest_version(repo)
|
||||
logging.info("Latest upstream version: %s", latest_version)
|
||||
|
||||
# Proceed only if the retrieved version is greater than the current one
|
||||
if latest_version <= current_version:
|
||||
logging.warning("No new version available")
|
||||
return
|
||||
|
||||
# Proceed only if a PR for this new version does not already exist
|
||||
command = ["git", "ls-remote", "--exit-code", "-h", repo, f"ci-auto-update-v${latest_version}"]
|
||||
if subprocess.run(command, stderr=subprocess.DEVNULL, check=False).returncode == 0:
|
||||
logging.warning("A branch already exists for this update")
|
||||
return
|
||||
|
||||
assets = get_assets_of_release(repo, release_info)
|
||||
logging.info("%d available asset(s)", len(assets))
|
||||
for asset in assets:
|
||||
handle_asset(asset)
|
||||
|
||||
manifest["version"] = f"{latest_version}~ynh1"
|
||||
with open("manifest.json", "w", encoding="utf-8") as manifest_file:
|
||||
json.dump(manifest, manifest_file, indent=4, ensure_ascii=False)
|
||||
manifest_file.write("\n")
|
||||
|
||||
with open(os.environ["GITHUB_ENV"], "w", encoding="utf-8") as github_env:
|
||||
github_env.write(textwrap.dedent(f"""\
|
||||
VERSION={latest_version}
|
||||
REPO={repo}
|
||||
PROCEED=true
|
||||
"""))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
49
.github/workflows/updater.yml
vendored
Normal file
49
.github/workflows/updater.yml
vendored
Normal file
|
@ -0,0 +1,49 @@
|
|||
# This workflow allows GitHub Actions to automagically update your app whenever a new upstream release is detected.
|
||||
# You need to enable Actions in your repository settings, and fetch this Action from the YunoHost-Apps organization.
|
||||
# This file should be enough by itself, but feel free to tune it to your needs.
|
||||
# It calls updater.sh, which is where you should put the app-specific update steps.
|
||||
name: Check for new upstream releases
|
||||
on:
|
||||
# Allow to manually trigger the workflow
|
||||
workflow_dispatch:
|
||||
# Run it every day at 6:00 UTC
|
||||
schedule:
|
||||
- cron: '0 6 * * *'
|
||||
jobs:
|
||||
updater:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Fetch the source code
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: Run the updater script
|
||||
id: run_updater
|
||||
run: |
|
||||
# Setting up Git user
|
||||
git config --global user.name 'yunohost-bot'
|
||||
git config --global user.email 'yunohost-bot@users.noreply.github.com'
|
||||
# Run the updater script
|
||||
.github/workflows/updater.py
|
||||
- name: Commit changes
|
||||
id: commit
|
||||
if: ${{ env.PROCEED == 'true' }}
|
||||
run: |
|
||||
git commit -am "Upgrade to v$VERSION"
|
||||
- name: Create Pull Request
|
||||
id: cpr
|
||||
if: ${{ env.PROCEED == 'true' }}
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: Update to version ${{ env.VERSION }}
|
||||
committer: 'yunohost-bot <yunohost-bot@users.noreply.github.com>'
|
||||
author: 'yunohost-bot <yunohost-bot@users.noreply.github.com>'
|
||||
signoff: false
|
||||
base: testing
|
||||
branch: ci-auto-update-v${{ env.VERSION }}
|
||||
delete-branch: true
|
||||
title: 'Upgrade to version ${{ env.VERSION }}'
|
||||
body: |
|
||||
Upgrade to v${{ env.VERSION }}
|
||||
draft: false
|
Loading…
Reference in a new issue