2024-05-20 18:00:15 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2024-03-29 06:46:00 +01:00
|
|
|
import time
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import shlex
|
|
|
|
import asyncio
|
|
|
|
import tempfile
|
2024-04-01 18:14:22 +02:00
|
|
|
import requests
|
2024-03-29 06:46:00 +01:00
|
|
|
|
|
|
|
from make_readme import generate_READMEs
|
2024-03-29 06:50:47 +01:00
|
|
|
from pathlib import Path
|
2024-03-29 06:46:00 +01:00
|
|
|
|
2024-05-20 18:00:15 +02:00
|
|
|
TOOLS_DIR = Path(__file__).resolve().parent.parent
|
2024-03-29 06:46:00 +01:00
|
|
|
|
2024-05-28 19:11:13 +02:00
|
|
|
secret = (
|
|
|
|
(TOOLS_DIR / ".github_webhook_secret").open("r", encoding="utf-8").read().strip()
|
|
|
|
)
|
2024-05-20 18:00:15 +02:00
|
|
|
login = (TOOLS_DIR / ".github_login").open("r", encoding="utf-8").read().strip()
|
|
|
|
token = (TOOLS_DIR / ".github_token").open("r", encoding="utf-8").read().strip()
|
2024-03-29 06:46:00 +01:00
|
|
|
|
|
|
|
my_env = os.environ.copy()
|
|
|
|
my_env["GIT_TERMINAL_PROMPT"] = "0"
|
|
|
|
my_env["GIT_AUTHOR_NAME"] = "yunohost-bot"
|
|
|
|
my_env["GIT_AUTHOR_EMAIL"] = "yunohost@yunohost.org"
|
|
|
|
my_env["GIT_COMMITTER_NAME"] = "yunohost-bot"
|
|
|
|
my_env["GIT_COMMITTER_EMAIL"] = "yunohost@yunohost.org"
|
|
|
|
|
|
|
|
|
|
|
|
async def git(cmd, in_folder=None):
|
|
|
|
if not isinstance(cmd, list):
|
|
|
|
cmd = cmd.split()
|
2024-03-29 06:57:44 +01:00
|
|
|
|
2024-03-29 06:46:00 +01:00
|
|
|
if in_folder:
|
|
|
|
cmd = ["-C", in_folder] + cmd
|
2024-03-29 06:57:44 +01:00
|
|
|
|
2024-03-29 06:46:00 +01:00
|
|
|
cmd = ["git"] + cmd
|
|
|
|
cmd = " ".join(map(shlex.quote, cmd))
|
|
|
|
print(cmd)
|
2024-03-29 06:57:44 +01:00
|
|
|
|
2024-03-30 01:05:33 +01:00
|
|
|
command = await asyncio.create_subprocess_shell(
|
|
|
|
cmd,
|
|
|
|
env=my_env,
|
|
|
|
stdout=asyncio.subprocess.PIPE,
|
|
|
|
stderr=asyncio.subprocess.STDOUT,
|
|
|
|
)
|
2024-03-29 06:46:00 +01:00
|
|
|
data = await command.stdout.read()
|
2024-03-29 06:57:44 +01:00
|
|
|
|
2024-03-29 06:46:00 +01:00
|
|
|
return data.decode().strip()
|
|
|
|
|
|
|
|
|
|
|
|
async def regen_readme(repository, branch):
|
|
|
|
|
2024-03-29 06:53:54 +01:00
|
|
|
print()
|
2024-03-29 06:46:00 +01:00
|
|
|
print(f"{repository} -> branch '{branch}'")
|
2024-03-29 06:53:54 +01:00
|
|
|
print("=" * len(f"{repository} -> branch '{branch}'"))
|
2024-03-29 06:46:00 +01:00
|
|
|
|
2024-04-01 18:14:22 +02:00
|
|
|
branches = requests.get(
|
|
|
|
f"https://api.github.com/repos/{repository}/branches",
|
|
|
|
headers={
|
|
|
|
"Authorization": f"Bearer {token}",
|
|
|
|
"X-GitHub-Api-Version": "2022-11-28",
|
|
|
|
"Accept": "application/vnd.github+json",
|
2024-04-01 18:14:44 +02:00
|
|
|
},
|
2024-04-01 18:14:22 +02:00
|
|
|
).json()
|
|
|
|
|
|
|
|
branches = {x["name"] for x in branches}
|
|
|
|
if "testing" in branches:
|
|
|
|
branch = "testing"
|
|
|
|
|
2024-03-29 06:46:00 +01:00
|
|
|
with tempfile.TemporaryDirectory() as folder:
|
2024-03-30 01:05:33 +01:00
|
|
|
await git(
|
|
|
|
[
|
|
|
|
"clone",
|
|
|
|
f"https://{login}:{token}@github.com/{repository}",
|
|
|
|
"--single-branch",
|
|
|
|
"--branch",
|
|
|
|
branch,
|
|
|
|
folder,
|
|
|
|
]
|
|
|
|
)
|
2024-03-29 06:57:44 +01:00
|
|
|
|
2024-03-29 06:50:47 +01:00
|
|
|
generate_READMEs(Path(folder))
|
2024-03-29 06:46:00 +01:00
|
|
|
|
|
|
|
await git(["add", "README*.md"], in_folder=folder)
|
2024-03-29 06:48:21 +01:00
|
|
|
await git(["add", "ALL_README.md"], in_folder=folder)
|
2024-03-29 06:46:00 +01:00
|
|
|
|
2024-03-30 01:05:33 +01:00
|
|
|
diff_not_empty = await asyncio.create_subprocess_shell(
|
|
|
|
" ".join(["git", "diff", "HEAD", "--compact-summary"]),
|
|
|
|
cwd=folder,
|
|
|
|
stdout=asyncio.subprocess.PIPE,
|
|
|
|
stderr=asyncio.subprocess.STDOUT,
|
|
|
|
)
|
2024-03-29 06:46:00 +01:00
|
|
|
diff_not_empty = await diff_not_empty.stdout.read()
|
|
|
|
diff_not_empty = diff_not_empty.decode().strip()
|
2024-03-29 06:58:59 +01:00
|
|
|
if not diff_not_empty:
|
2024-03-29 06:46:00 +01:00
|
|
|
print("nothing to do")
|
|
|
|
return
|
|
|
|
|
2024-03-30 01:05:33 +01:00
|
|
|
await git(
|
|
|
|
[
|
|
|
|
"commit",
|
|
|
|
"-a",
|
|
|
|
"-m",
|
|
|
|
"Auto-update README",
|
|
|
|
"--author='yunohost-bot <yunohost@yunohost.org>'",
|
|
|
|
],
|
|
|
|
in_folder=folder,
|
|
|
|
)
|
2024-03-29 06:46:00 +01:00
|
|
|
await git(["push", "origin", branch, "--quiet"], in_folder=folder)
|
|
|
|
|
2024-03-29 06:57:44 +01:00
|
|
|
print(f"Updated https://github.com/{repository}")
|
2024-03-29 06:46:00 +01:00
|
|
|
|
2024-03-29 06:46:40 +01:00
|
|
|
|
2024-04-01 18:14:44 +02:00
|
|
|
if __name__ == "__main__":
|
2024-03-29 06:46:40 +01:00
|
|
|
apps = json.load(open("../../builds/default/v3/apps.json"))["apps"]
|
2024-03-29 06:57:44 +01:00
|
|
|
|
2024-03-29 06:46:40 +01:00
|
|
|
for app, infos in apps.items():
|
|
|
|
if "github.com" not in infos["git"]["url"]:
|
|
|
|
continue
|
2024-03-29 06:57:44 +01:00
|
|
|
|
2024-03-29 06:46:40 +01:00
|
|
|
time.sleep(2)
|
2024-03-30 01:05:33 +01:00
|
|
|
asyncio.run(
|
|
|
|
regen_readme(
|
|
|
|
infos["git"]["url"].replace("https://github.com/", ""),
|
|
|
|
infos["git"]["branch"],
|
|
|
|
)
|
2024-04-01 18:14:44 +02:00
|
|
|
)
|