|
| 1 | +#!/usr/bin/env python |
| 2 | +import json |
| 3 | +import os |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | +from urllib import request |
| 7 | + |
| 8 | +import typer |
| 9 | +from simple_term_menu import TerminalMenu |
| 10 | +from typing_extensions import Annotated |
| 11 | +from typing import Optional |
| 12 | + |
| 13 | +MAX_RESULT = 5 |
| 14 | + |
| 15 | + |
| 16 | +def load_branches(): |
| 17 | + instance = os.environ.get("JIRA_INSTANCE") |
| 18 | + token = os.environ.get("JIRA_PAT") |
| 19 | + if not instance: |
| 20 | + raise Exception("Please disclose your jira instance as $JIRA_INSTANCE") |
| 21 | + if not token: |
| 22 | + raise Exception("Please disclose your jira token as $JIRA_PAT") |
| 23 | + req = request.Request( |
| 24 | + instance + '/rest/api/2/search?' |
| 25 | + 'jql=assignee=currentUser()+order+by+updated&fields=id,key,summary,issuetype,assignee', |
| 26 | + method="GET") |
| 27 | + req.add_header('Authorization', f'Bearer {token}') |
| 28 | + response = request.urlopen(req).read().decode('utf-8') |
| 29 | + response = json.loads(response) |
| 30 | + |
| 31 | + formatted_branches = [] |
| 32 | + |
| 33 | + for issue in response['issues']: |
| 34 | + formatted = issue['key'] + " " + issue['fields']['summary'] |
| 35 | + formatted_branches.append(re.sub(r"[^a-zA-Z0-9]+", ' ', formatted)) |
| 36 | + return formatted_branches[:MAX_RESULT] |
| 37 | + |
| 38 | + |
| 39 | +def main(prefix: Annotated[str, typer.Option(help="Prefix that is being used for the new branch.")] = "feature", |
| 40 | + no_prefix: Annotated[bool, typer.Option("--no-prefix", help="Will not use a prefix")] = False): |
| 41 | + """ |
| 42 | + CLI to switch to git branches based on one's JIRA tickets. |
| 43 | +
|
| 44 | + If --prefix is used, it will add a specific prefix to the branch (e.g. feature -> "feature/") |
| 45 | + --no-prefix will omit the default "feature/" prefix. |
| 46 | + """ |
| 47 | + tasks = load_branches() |
| 48 | + terminal_menu = TerminalMenu(tasks) |
| 49 | + menu_entry_index = terminal_menu.show() |
| 50 | + selected_task = tasks[menu_entry_index] |
| 51 | + prefix = None if no_prefix else prefix |
| 52 | + formatted_branch = format_branch(selected_task, prefix) |
| 53 | + print(f"Switching to branch: {formatted_branch}") |
| 54 | + process = subprocess.Popen(['git', 'switch', '-c', formatted_branch], |
| 55 | + stdout=subprocess.PIPE) |
| 56 | + process.communicate() |
| 57 | + |
| 58 | + |
| 59 | +def format_branch(selected_task: str, prefix: Optional[str]): |
| 60 | + print(f"Using prefix: {prefix}") |
| 61 | + selected_task = re.sub(r'\s', '-', |
| 62 | + selected_task).lower() |
| 63 | + jira_key = re.match(r'[A-Za-z]{2,}-\d+', selected_task).group() |
| 64 | + selected_task = selected_task.replace(jira_key, jira_key.upper()) |
| 65 | + formatted_branch = (f"{prefix}/" if prefix else '') + selected_task |
| 66 | + return formatted_branch |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + typer.run(main) |
0 commit comments