From 30eee512351ca9369cdea7ccfee747264336dc62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Vask=C3=B3?= Date: Fri, 1 Jan 2021 01:18:07 +0000 Subject: [PATCH] github actions: action to update init.el --- .github/workflows/update-dependencies.yml | 6 ++ .github/workflows/update_init_el.py | 109 ++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100755 .github/workflows/update_init_el.py diff --git a/.github/workflows/update-dependencies.yml b/.github/workflows/update-dependencies.yml index 9cad175..19d504b 100644 --- a/.github/workflows/update-dependencies.yml +++ b/.github/workflows/update-dependencies.yml @@ -23,3 +23,9 @@ jobs: GITHUB_BASE_BRANCH: develop run: | nix develop -c .github/workflows/update_flake_lock.py + - name: Create PR for updated init.el + env: + GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_BASE_BRANCH: develop + run: | + nix develop -c .github/workflows/update_init_el.py diff --git a/.github/workflows/update_init_el.py b/.github/workflows/update_init_el.py new file mode 100755 index 0000000..6f6210e --- /dev/null +++ b/.github/workflows/update_init_el.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +import sys +from difflib import unified_diff + +from os import environ as env +from typing import cast +from github import Github +from github.ContentFile import ContentFile +from github.GithubException import GithubException +from github.Repository import Repository + +API_TOKEN = env["GITHUB_API_TOKEN"] +REPOSITORY = env["GITHUB_REPOSITORY"] +BASE_BRANCH = env.get("GITHUB_BASE_BRANCH", "master") +DRY_RUN = bool(env.get("GITHUB_DRY_RUN", False)) + +INIT_EL = "test/doom.d/init.el" +UPSTREAM_INIT_EL = "init.example.el" +DOOM_UPSTREAM = "hlissner/doom-emacs" +UPSTREAM_BRANCH = "develop" + + +def create_pr( + repo: Repository, + pr_branch_name: str, + head: str, + file: ContentFile, + updated_content: str, + pr_title: str, + pr_body: str, +): + try: + repo.get_branch(pr_branch_name) + print(f"Branch '{pr_branch_name}' already exist. Skipping update.") + return + except GithubException as ex: + if ex.status != 404: + raise + + pr_branch = repo.create_git_ref(pr_branch_name, head) + repo.update_file( + file.path, + f"{pr_title}\n\n{pr_body}", + updated_content, + file.sha, + branch=pr_branch_name, + ) + repo.create_pull(title=pr_title, body=pr_body, head=pr_branch.ref, base=BASE_BRANCH) + + +def main(): + if API_TOKEN: + github = Github(API_TOKEN) + else: + print("GITHUB_API_TOKEN is required") + sys.exit(1) + + repo = github.get_repo(REPOSITORY) + head = repo.get_branch(BASE_BRANCH).commit.sha + init_el = cast(ContentFile, repo.get_contents(INIT_EL, ref=BASE_BRANCH)) + doom_repo = github.get_repo(DOOM_UPSTREAM) + upstream_init_el = cast( + ContentFile, doom_repo.get_contents(UPSTREAM_INIT_EL, ref=UPSTREAM_BRANCH) + ) + + diff = "".join( + unified_diff( + init_el.decoded_content.decode().splitlines(keepends=True), + upstream_init_el.decoded_content.decode().splitlines(keepends=True), + ) + ) + + if not diff: + print(f"{INIT_EL} is up-to date") + return + + print(f"{INIT_EL} updated.") + print(diff) + + upstream_rev = doom_repo.get_branch(UPSTREAM_BRANCH).commit.sha + title = f"{INIT_EL}: Updating from {DOOM_UPSTREAM} - {upstream_rev[:8]}" + + body = f"""\ +### Changes for {INIT_EL} + +```diff +{diff} +``` +""" + + print(f"[{INIT_EL}] - Creating PR\nTitle: {title}\nBody:\n{body}") + if DRY_RUN: + print(f"DRY-RUN: NOT creating PR...") + return + + pr_branch_name = f"refs/heads/update/init.el-{upstream_rev}" + create_pr( + repo, + pr_branch_name, + head, + init_el, + upstream_init_el.decoded_content.decode(), + title, + body, + ) + + +if __name__ == "__main__": + main()