├── requirements.in ├── multitask.png ├── one_task.png ├── screenshot.png ├── requirements.txt ├── Dockerfile ├── LICENSE ├── README.md └── merge_and_cleanup_branch.py /requirements.in: -------------------------------------------------------------------------------- 1 | requests 2 | -------------------------------------------------------------------------------- /multitask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexwlchan/auto_merge_my_pull_requests/development/multitask.png -------------------------------------------------------------------------------- /one_task.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexwlchan/auto_merge_my_pull_requests/development/one_task.png -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexwlchan/auto_merge_my_pull_requests/development/screenshot.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This file is autogenerated by pip-compile 3 | # To update, run: 4 | # 5 | # pip-compile 6 | # 7 | certifi==2019.3.9 # via requests 8 | chardet==3.0.4 # via requests 9 | idna==2.8 # via requests 10 | requests==2.21.0 11 | urllib3==1.24.1 # via requests 12 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3-alpine 2 | 3 | MAINTAINER Alex Chan 4 | 5 | LABEL "com.github.actions.name"="Auto-merge my pull requests" 6 | LABEL "com.github.actions.description"="Merge and clean-up the pull request after the checks pass" 7 | LABEL "com.github.actions.icon"="activity" 8 | LABEL "com.github.actions.color"="green" 9 | 10 | COPY requirements.txt /requirements.txt 11 | RUN pip3 install -r /requirements.txt 12 | 13 | COPY merge_and_cleanup_branch.py /merge_and_cleanup_branch.py 14 | 15 | ENTRYPOINT ["python3", "/merge_and_cleanup_branch.py"] 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019 Alex Chan 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **Archived 26 May 2022.** This repo is unmaintained and out-of-date; you should probably look at a different Action if you want to auto-merge your pull requests. 2 | 3 | > I only ever used this action on a single repository, [alexwlchan/alexwlchan.net], where I would use the following workflow: 4 | > 5 | > * Write a new blog post 6 | > * Open a PR for the blog post 7 | > * Use Azure Pipelines to build and test the new post (e.g. look for broken links, missing alt text) 8 | > * If the build succeeded, merge the post and deploy it to my site 9 | > 10 | > It used the [`check_run` event][check_run] to notice when the Azure Pipelines build was done, and auto-merge the pull request. 11 | > 12 | > I always thought I'd use it in more places, but that never happened. 13 | > 14 | > In May 2022 I stopped using Azure Pipelines, and I switched that repo's CI exclusively to GitHub Actions. 15 | > I couldn't find an easy way to use this Action with the rest of my new Actions-based pipeline, so I replaced it with [a shell script][script] that lives in that repo. 16 | > 17 | > There are other Actions for doing auto-merging of pull requests which are better maintained and more full-featured. 18 | > I've left the README and code in place in case it's useful for somebody building a new Action, but I don't recommend anybody use this for new projects. 19 | 20 | [alexwlchan/alexwlchan.net]: https://github.com/alexwlchan/alexwlchan.net 21 | [check_run]: https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#check_run 22 | [script]: https://github.com/alexwlchan/alexwlchan.net/blob/f01cca80db3bbab80efba87e5a6639bc1f0f24af/.github/workflows/build_site.yml#L25-L29 23 | 24 | --- 25 | 26 | # Auto merge my pull requests 27 | 28 | A GitHub Action to automatically merge pull requests on my repositories if: 29 | 30 | * I opened the PR 31 | * The test suite is passing 32 | * I haven't marked the PR as a WIP 33 | 34 | After the PR is merged, it deletes the branch to keep things neat and tidy. 35 | 36 | ![](screenshot.png) 37 | 38 | 39 | 40 | ## Motivation 41 | 42 | I have a bunch of repos where I'm the only contributor, and I want to merge pull requests as soon as tests pass. 43 | (The repo with [my blog](https://github.com/alexwlchan/alexwlchan.net), for example.) 44 | 45 | This Action saves me the work of actually pushing the button, and means they get merged a little faster. 46 | 47 | The Action is defined in a separate repo that doesn't have auto-merging pull requests so that somebody can't merge a PR with malicious code by editing the underlying Action. 48 | 49 | 50 | 51 | ## Usage 52 | 53 | Fork this repo, add your own rules in `merge_and_cleanup_branch.py`. 54 | 55 | Reference the Action in your `.workflow` file: 56 | 57 | ```hcl 58 | workflow "merge_and_cleanup" { 59 | on = "pull_request" 60 | resolves = ["when tests pass, merge and cleanup"] 61 | } 62 | 63 | action "when tests pass, merge and cleanup" { 64 | uses = "yourname/auto_merge_my_pull_requests@development" 65 | secrets = ["GITHUB_TOKEN"] 66 | } 67 | ``` 68 | 69 | 70 | 71 | ## Limitations 72 | 73 | * This will only merge pull requests which I opened. 74 | If you use this Action unmodified, you'll grant me magic PR-merging powers. 75 | 76 | * I'm only using this on repos that have a single test task. 77 | So it can handle this: 78 | 79 | ![](onetask.png) 80 | 81 | but it gets confused by this: 82 | 83 | ![](multitask.png) 84 | 85 | It will try to merge the pull request as soon as one of those checks completes. 86 | I only have a single task on each of my repos, so that's fine for me -- something like the `check_suite` trigger is probably more appropriate for larger builds. 87 | 88 | 89 | 90 | ## License 91 | 92 | MIT. 93 | -------------------------------------------------------------------------------- /merge_and_cleanup_branch.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- encoding: utf-8 3 | 4 | import json 5 | import os 6 | import sys 7 | 8 | import requests 9 | 10 | 11 | def neutral_exit(): 12 | # In the early versions of GitHub Actions, you could use exit code 78 to 13 | # mark a run as "neutral". 14 | # 15 | # This got removed in later versions because it's not ambiguous. 16 | # https://twitter.com/zeitgeistse/status/1163444737057132547 17 | # 18 | # (Can I say "I told you so"?) 19 | # 20 | sys.exit(0) 21 | 22 | 23 | def get_session(github_token): 24 | sess = requests.Session() 25 | sess.headers = { 26 | "Accept": "; ".join([ 27 | "application/vnd.github.v3+json", 28 | "application/vnd.github.antiope-preview+json", 29 | ]), 30 | "Authorization": f"token {github_token}", 31 | "User-Agent": f"GitHub Actions script in {__file__}" 32 | } 33 | 34 | def raise_for_status(resp, *args, **kwargs): 35 | try: 36 | resp.raise_for_status() 37 | except Exception: 38 | print(resp.text) 39 | sys.exit("Error: Invalid repo, token or network issue!") 40 | 41 | sess.hooks["response"].append(raise_for_status) 42 | return sess 43 | 44 | 45 | if __name__ == '__main__': 46 | github_token = os.environ["GITHUB_TOKEN"] 47 | github_repository = os.environ["GITHUB_REPOSITORY"] 48 | 49 | github_event_path = os.environ["GITHUB_EVENT_PATH"] 50 | event_data = json.load(open(github_event_path)) 51 | 52 | check_run = event_data["check_run"] 53 | name = check_run["name"] 54 | 55 | sess = get_session(github_token) 56 | 57 | if len(check_run["pull_requests"]) == 0: 58 | print("*** Check run is not part of a pull request, so nothing to do") 59 | neutral_exit() 60 | 61 | # We should only merge pull requests that have the conclusion "succeeded". 62 | # 63 | # We get a check_run event in GitHub Actions when the underlying run is 64 | # scheduled and completed -- if it doesn't have a conclusion, this field is 65 | # set to "null". In that case, we give up -- we'll get a second event when 66 | # the run completes. 67 | # 68 | # See https://developer.github.com/v3/activity/events/types/#checkrunevent 69 | # 70 | conclusion = check_run["conclusion"] 71 | print(f"*** Conclusion of {name} is {conclusion}") 72 | 73 | if conclusion is None: 74 | print(f"*** Check run {name} has not completed, skipping") 75 | neutral_exit() 76 | 77 | if conclusion != "success": 78 | print(f"*** Check run {name} has failed, will not merge PR") 79 | sys.exit(1) 80 | 81 | # If the check_run has completed, we want to check the pull request data 82 | # before we declare this PR safe to merge. 83 | assert len(check_run["pull_requests"]) == 1 84 | pull_request = check_run["pull_requests"][0] 85 | pr_number = pull_request["number"] 86 | pr_src = pull_request["head"]["ref"] 87 | pr_dst = pull_request["base"]["ref"] 88 | 89 | print(f"*** Checking pull request #{pr_number}: {pr_src} ~> {pr_dst}") 90 | pr_data = sess.get(pull_request["url"]).json() 91 | 92 | pr_title = pr_data["title"] 93 | print(f"*** Title of PR is {pr_title!r}") 94 | if pr_title.startswith("[WIP] "): 95 | print("*** This is a WIP PR, will not merge") 96 | neutral_exit() 97 | 98 | pr_user = pr_data["user"]["login"] 99 | print(f"*** This PR was opened by {pr_user}") 100 | if pr_user != "alexwlchan": 101 | print("*** This PR was opened by somebody who isn't me; requires manual merge") 102 | neutral_exit() 103 | 104 | print("*** This PR is ready to be merged.") 105 | merge_url = pull_request["url"] + "/merge" 106 | sess.put(merge_url) 107 | 108 | print("*** Cleaning up PR branch") 109 | pr_ref = pr_data["head"]["ref"] 110 | api_base_url = pr_data["base"]["repo"]["url"] 111 | ref_url = f"{api_base_url}/git/refs/heads/{pr_ref}" 112 | sess.delete(ref_url) 113 | --------------------------------------------------------------------------------