2024-02-07 14:49:55 +01:00
|
|
|
#!/usr/bin/env python3
|
2023-01-29 18:32:39 +01:00
|
|
|
|
2024-02-07 14:49:55 +01:00
|
|
|
import json
|
|
|
|
import os
|
2023-01-29 18:32:39 +01:00
|
|
|
import re
|
2024-02-07 14:49:55 +01:00
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import urllib.request
|
2023-01-29 18:32:39 +01:00
|
|
|
|
2023-02-24 00:50:32 +01:00
|
|
|
import github
|
2024-02-07 14:49:55 +01:00
|
|
|
from github import Github
|
2024-03-11 17:34:33 +01:00
|
|
|
|
2023-01-29 18:32:39 +01:00
|
|
|
# Debug
|
|
|
|
from rich.traceback import install
|
2024-02-07 14:49:55 +01:00
|
|
|
|
2023-02-24 00:50:32 +01:00
|
|
|
install(width=150, show_locals=True, locals_max_length=None, locals_max_string=None)
|
2023-01-29 18:32:39 +01:00
|
|
|
|
|
|
|
#####
|
|
|
|
#
|
|
|
|
# CONFIG
|
|
|
|
#
|
|
|
|
#####
|
|
|
|
|
|
|
|
# API token for yunohost-bot, need public.repo permission
|
|
|
|
g = Github(open(".github_token").read().strip())
|
|
|
|
|
|
|
|
# Path to the file to be updated
|
2024-03-11 17:34:33 +01:00
|
|
|
path = ".github/workflows/updater.yml"
|
2023-01-29 18:32:39 +01:00
|
|
|
|
2023-02-24 00:50:32 +01:00
|
|
|
# Title of the PR
|
2024-03-11 17:34:33 +01:00
|
|
|
title = "[autopatch] Upgrade auto-updater"
|
2023-02-24 00:50:32 +01:00
|
|
|
|
2023-01-29 18:32:39 +01:00
|
|
|
# Body of the PR message
|
2024-03-11 17:34:33 +01:00
|
|
|
body = """
|
2023-01-29 18:32:39 +01:00
|
|
|
Auto-updater actions need upgrading to continue working:
|
|
|
|
- actions/checkout@v3
|
|
|
|
- peter-evans/create-pull-request@v4
|
|
|
|
"""
|
|
|
|
|
2023-02-24 00:50:32 +01:00
|
|
|
# Author of the commit
|
2024-03-11 17:34:33 +01:00
|
|
|
author = github.InputGitAuthor(
|
|
|
|
open(".github_login").read().strip(), open(".github_email").read().strip()
|
|
|
|
)
|
2023-02-24 00:50:32 +01:00
|
|
|
|
2023-01-29 18:32:39 +01:00
|
|
|
# Name of the branch created for the PR
|
2024-03-11 17:34:33 +01:00
|
|
|
new_branch = "upgrade-auto-updater"
|
2023-01-29 18:32:39 +01:00
|
|
|
|
2023-02-24 00:50:32 +01:00
|
|
|
#####
|
|
|
|
#
|
|
|
|
# CACHE
|
|
|
|
#
|
|
|
|
#####
|
|
|
|
|
2024-03-11 17:34:33 +01:00
|
|
|
with open("processed.txt") as f:
|
2023-02-24 00:50:32 +01:00
|
|
|
processed = f.read().splitlines()
|
|
|
|
|
2023-01-29 18:32:39 +01:00
|
|
|
#####
|
|
|
|
#
|
|
|
|
# CRAWL REPOSITORIES
|
|
|
|
#
|
|
|
|
#####
|
|
|
|
|
|
|
|
u = g.get_user("yunohost-bot")
|
|
|
|
org = g.get_organization("yunohost-apps")
|
|
|
|
|
|
|
|
# For each repositories belonging to the bot (user `u`)
|
2024-03-11 17:34:33 +01:00
|
|
|
i = 0
|
2023-01-29 18:32:39 +01:00
|
|
|
for repo in org.get_repos():
|
2023-02-24 00:50:32 +01:00
|
|
|
if repo.full_name not in processed:
|
|
|
|
|
|
|
|
# Determine base branch, either `testing` or default branch
|
|
|
|
try:
|
|
|
|
base_branch = repo.get_branch("testing").name
|
|
|
|
except:
|
|
|
|
base_branch = repo.default_branch
|
|
|
|
|
|
|
|
# Make sure the repository has an auto-updater
|
|
|
|
try:
|
2024-03-11 17:34:33 +01:00
|
|
|
repo.get_contents(path, ref="refs/heads/" + base_branch)
|
2023-02-24 00:50:32 +01:00
|
|
|
except:
|
2024-03-11 17:34:33 +01:00
|
|
|
with open("processed.txt", "a") as pfile:
|
|
|
|
pfile.write(repo.full_name + "\n")
|
2023-02-24 00:50:32 +01:00
|
|
|
time.sleep(1.5)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Process the repo
|
2024-03-11 17:34:33 +01:00
|
|
|
print("Processing " + repo.full_name)
|
2023-01-29 18:32:39 +01:00
|
|
|
|
2023-02-24 00:50:32 +01:00
|
|
|
try:
|
|
|
|
# Get the commit base for the new branch, and create it
|
|
|
|
commit_sha = repo.get_branch(base_branch).commit.sha
|
2024-03-11 17:34:33 +01:00
|
|
|
new_branch_ref = repo.create_git_ref(
|
|
|
|
ref="refs/heads/" + new_branch, sha=commit_sha
|
|
|
|
)
|
2023-02-24 00:50:32 +01:00
|
|
|
except:
|
2024-03-11 17:34:33 +01:00
|
|
|
new_branch_ref = repo.get_git_ref(ref="heads/" + new_branch)
|
2023-01-29 18:32:39 +01:00
|
|
|
|
|
|
|
# Get current file contents
|
|
|
|
contents = repo.get_contents(path, ref=new_branch_ref.ref)
|
|
|
|
|
|
|
|
# Update the file
|
|
|
|
updater_yml = contents.decoded_content.decode("unicode_escape")
|
2024-03-11 17:34:33 +01:00
|
|
|
updater_yml = re.sub(
|
|
|
|
r"(?m)uses: actions/checkout@v[\d]+",
|
|
|
|
"uses: actions/checkout@v3",
|
|
|
|
updater_yml,
|
|
|
|
)
|
|
|
|
updater_yml = re.sub(
|
|
|
|
r"(?m)uses: peter-evans/create-pull-request@v[\d]+",
|
|
|
|
"uses: peter-evans/create-pull-request@v4",
|
|
|
|
updater_yml,
|
|
|
|
)
|
|
|
|
updated = repo.update_file(
|
|
|
|
contents.path,
|
|
|
|
message=title,
|
|
|
|
content=updater_yml,
|
|
|
|
sha=contents.sha,
|
|
|
|
branch=new_branch,
|
|
|
|
author=author,
|
|
|
|
)
|
2023-02-24 00:50:32 +01:00
|
|
|
|
|
|
|
# Wait a bit to preserve the API rate limit
|
|
|
|
time.sleep(1.5)
|
2023-01-29 18:32:39 +01:00
|
|
|
|
|
|
|
# Open the PR
|
2024-03-11 17:34:33 +01:00
|
|
|
pr = repo.create_pull(
|
|
|
|
title="Upgrade auto-updater", body=body, head=new_branch, base=base_branch
|
|
|
|
)
|
2023-01-29 18:32:39 +01:00
|
|
|
|
2024-03-11 17:34:33 +01:00
|
|
|
print(repo.full_name + " updated with PR #" + str(pr.id))
|
|
|
|
i = i + 1
|
2023-02-24 00:50:32 +01:00
|
|
|
|
|
|
|
# Wait a bit to preserve the API rate limit
|
|
|
|
time.sleep(1.5)
|
|
|
|
|
2024-03-11 17:34:33 +01:00
|
|
|
with open("processed.txt", "a") as pfile:
|
|
|
|
pfile.write(repo.full_name + "\n")
|
2023-02-24 00:50:32 +01:00
|
|
|
|
2024-03-11 17:34:33 +01:00
|
|
|
print("Done. " + str(i) + " repos processed")
|