Skip to content

Instantly share code, notes, and snippets.

@ndevenish
Last active August 22, 2024 08:40
Show Gist options
  • Save ndevenish/45af3dfbc83635a27d2e588e1a7a5b4f to your computer and use it in GitHub Desktop.
Save ndevenish/45af3dfbc83635a27d2e588e1a7a5b4f to your computer and use it in GitHub Desktop.
Get a github automerge message, after it's been activated
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "requests",
# ]
# ///
import os
import sys
import requests
from argparse import ArgumentParser
if "GITHUB_TOKEN" not in os.environ:
sys.exit("Error: Please set GITHUB_TOKEN environment variable")
GITHUB_TOKEN = os.environ["GITHUB_TOKEN"]
BOLD = "\033[1m"
R = "\033[31m"
G = "\033[32m"
B = "\033[34m"
NC = "\033[0m"
def request_gql(query):
verb = "POST"
url = "https://api.github.com/graphql"
# args = dict(kwargs)
headers = {
"Authorization": f"bearer {GITHUB_TOKEN}",
# **args.pop("headers", {}),
}
r = requests.request(verb, f"{url}", headers=headers, json={"query": query})
try:
r.raise_for_status()
except Exception as e:
breakpoint()
raise
return r
if __name__ == "__main__":
parser = ArgumentParser(description="Get the automerge message for a PR")
parser.add_argument("repo", help="The owner/name string pointing to the repo")
parser.add_argument("pr_number", help="The PR number for the repo", type=int)
args = parser.parse_args()
owner, name = args.repo.split("/")
print(
f"Checking automerge for {B}{owner}/{name}#{args.pr_number}{NC}",
file=sys.stderr,
)
resp = request_gql(f"""
query {{
repository(owner: "{owner}", name: "{name}") {{
pullRequest(number: {args.pr_number}) {{
id
number
title
autoMergeRequest {{
commitHeadline
commitBody
}}
}}
}}
}}
""")
data = resp.json()
automerge = data["data"]["repository"]["pullRequest"]["autoMergeRequest"]
print(
f"""
{B}{automerge['commitHeadline']})
{automerge['commitBody']}{NC}""".lstrip(),
file=sys.stderr,
)
print(resp.content.decode("utf-8"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment