Skip to content

Instantly share code, notes, and snippets.

@willkg
Last active December 13, 2021 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willkg/12d206f11f02e4f531af2a8172b2490d to your computer and use it in GitHub Desktop.
Save willkg/12d206f11f02e4f531af2a8172b2490d to your computer and use it in GitHub Desktop.
git subcommand for looking up bugzilla bugs and generating branch names
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, you can obtain one at http://mozilla.org/MPL/2.0/.
# This expects Python 3
from distutils.spawn import find_executable
from http.client import HTTPSConnection
import json
import string
import subprocess
import sys
GITCMD = find_executable("git")
HOST = "bugzilla.mozilla.org"
def get_bug_data(num=""):
while True:
if num:
conn = HTTPSConnection(HOST)
headers = {"Accept": "application/json"}
conn.request("GET", "/rest/bug/%s" % num.strip(), headers=headers)
resp = conn.getresponse()
if resp.status != 200:
print("Error %s: %s" % (resp.status, resp.reason))
raise Exception("Bad response")
data = json.loads(resp.read())
bug = data["bugs"][0]
print("%s: %s" % (bug["id"], bug["summary"]))
if input("Is this right? (Y/n) ").strip() == "y":
return (bug["id"], bug["summary"])
num = input("Bug number?: ")
ALLOWED = string.ascii_letters + string.digits + " "
NIX_WORDS = [
"traceback",
]
def generate_branch_name(bug_id, bug_sum):
bug_sum = "".join([c.lower() if c in ALLOWED else "" for c in bug_sum])
bug_sum = bug_sum.replace(" ", "-")
for nix_word in NIX_WORDS:
if bug_sum.startswith(nix_word):
bug_sum = bug_sum[len(nix_word):]
bug_sum = bug_sum.strip("-")
branch_name = f"bug-{bug_id}-{bug_sum[:10]}"
return branch_name.strip("-")
def main(argv):
bug_id, bug_sum = get_bug_data(argv and argv[0] or "")
branch_name = generate_branch_name(bug_id, bug_sum)
ret = input(f"Branch name? [{branch_name}] or bug-{bug_id}-").strip()
if ret:
branch_name = f"bug-{bug_id}-{ret}"
subprocess.check_output([GITCMD, "checkout", "-b", branch_name])
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment