|
| 1 | +#!/usr/bin/env nix-shell |
| 2 | +#! nix-shell -i python3 -p "python3.withPackages (ps: with ps; [ PyGithub ])" nvfetcher |
| 3 | +# pyright: reportMissingImports=false |
| 4 | +import subprocess |
| 5 | +from os import environ as env |
| 6 | +from pathlib import Path |
| 7 | +from tempfile import NamedTemporaryFile |
| 8 | + |
| 9 | +from github import Github |
| 10 | +from github.GithubException import GithubException |
| 11 | +from github.Repository import Repository |
| 12 | + |
| 13 | +API_TOKEN = env["GITHUB_TOKEN"] |
| 14 | +REPOSITORY = env["GITHUB_REPOSITORY"] |
| 15 | +BASE_BRANCH = env.get("GITHUB_BASE_BRANCH", "main") |
| 16 | +DRY_RUN = bool(env.get("GITHUB_DRY_RUN", False)) |
| 17 | + |
| 18 | +USER_NAME = "github-actions[bot]" |
| 19 | +USER_EMAIL = "github-actions[bot]@users.noreply.github.com" |
| 20 | + |
| 21 | + |
| 22 | +def create_pr( |
| 23 | + repo: Repository, |
| 24 | + pr_branch_name: str, |
| 25 | + pr_title: str, |
| 26 | + pr_body: str, |
| 27 | +): |
| 28 | + try: |
| 29 | + repo.get_branch(pr_branch_name) |
| 30 | + print(f"Branch '{pr_branch_name}' already exist. Skipping update.") |
| 31 | + except GithubException as ex: |
| 32 | + if ex.status != 404: |
| 33 | + raise |
| 34 | + else: |
| 35 | + return |
| 36 | + |
| 37 | + subprocess.run(["git", "add", "."], check=True) |
| 38 | + subprocess.run( |
| 39 | + ["git", "commit", "-m", f"{pr_title}\n\n{pr_body}"], |
| 40 | + check=True, |
| 41 | + env={ |
| 42 | + "GIT_AUTHOR_NAME": USER_NAME, |
| 43 | + "GIT_COMMITTER_NAME": USER_NAME, |
| 44 | + "GIT_AUTHOR_EMAIL": USER_EMAIL, |
| 45 | + "GIT_COMMITTER_EMAIL": USER_EMAIL, |
| 46 | + }, |
| 47 | + ) |
| 48 | + subprocess.run(["git", "push", "origin", f"+HEAD:{pr_branch_name}"], check=True) |
| 49 | + pr = repo.create_pull( |
| 50 | + title=pr_title, body=pr_body, head=pr_branch_name, base=BASE_BRANCH |
| 51 | + ) |
| 52 | + pr.add_to_labels("automated", "dependencies") |
| 53 | + |
| 54 | + |
| 55 | +def update_dependencies(): |
| 56 | + with NamedTemporaryFile() as log: |
| 57 | + subprocess.run( |
| 58 | + ["nvfetcher", "--build-dir", "nix/_sources", "--changelog", log.name] |
| 59 | + ) |
| 60 | + return Path(log.name).read_text() |
| 61 | + |
| 62 | + |
| 63 | +def main(): |
| 64 | + github = Github(API_TOKEN) |
| 65 | + |
| 66 | + repo = github.get_repo(REPOSITORY) |
| 67 | + |
| 68 | + changes = update_dependencies() |
| 69 | + if not changes: |
| 70 | + print("-- Everything is up-to date") |
| 71 | + return |
| 72 | + |
| 73 | + title = "chore(deps): Updating vendored nix dependencies" |
| 74 | + |
| 75 | + body = f"""\ |
| 76 | +### Changes in dependencies: |
| 77 | +
|
| 78 | +{changes} |
| 79 | +""" |
| 80 | + |
| 81 | + print(f"-- Creating PR\nTitle: {title}\nBody:\n{body}") |
| 82 | + if DRY_RUN: |
| 83 | + print("DRY-RUN: NOT creating PR...") |
| 84 | + return |
| 85 | + |
| 86 | + pr_branch_name = "refs/heads/update/nix-vendored-dependencies" |
| 87 | + create_pr( |
| 88 | + repo, |
| 89 | + pr_branch_name, |
| 90 | + title, |
| 91 | + body, |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + main() |
0 commit comments