From a618ead2c96bc403d1d4f43c0b5e77865b25eb86 Mon Sep 17 00:00:00 2001 From: "j.cabarcas" Date: Wed, 31 Jan 2024 18:12:58 +0100 Subject: [PATCH 1/3] Includes the auto_branch_prefix Identifies the ticket type to create branches with prefixes like: feature, bugfix, or refactor --- git_jira/git_jira.py | 52 ++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/git_jira/git_jira.py b/git_jira/git_jira.py index 75a4487..f39e935 100644 --- a/git_jira/git_jira.py +++ b/git_jira/git_jira.py @@ -12,6 +12,12 @@ MAX_RESULT = 5 +# Mapping of ticket types to branch prefixes +TICKET_TYPE_TO_PREFIX = { + "Story": "feature", + "Bug": "bugfix", + "Refactoring": "refactor" +} def load_branches(): instance = os.environ.get("JIRA_INSTANCE") @@ -22,7 +28,7 @@ def load_branches(): raise Exception("Please disclose your jira token as $JIRA_PAT") req = request.Request( instance + '/rest/api/2/search?' - 'jql=assignee=currentUser()+order+by+updated&fields=id,key,summary,issuetype,assignee', + 'jql=assignee=currentUser()+order+by+updated&fields=id,key,summary,issuetype,assignee', method="GET") req.add_header('Authorization', f'Bearer {token}') response = request.urlopen(req).read().decode('utf-8') @@ -32,28 +38,42 @@ def load_branches(): for issue in response['issues']: formatted = issue['key'] + " " + issue['fields']['summary'] - formatted_branches.append(re.sub(r"[^a-zA-Z0-9]+", ' ', formatted)) + formatted_summary = re.sub(r"[^a-zA-Z0-9]+", ' ', formatted) + formatted_branches.append({ + 'summary': formatted_summary, + 'issuetype': issue['fields']['issuetype'] # Added to retrieve the ticket type + }) return formatted_branches[:MAX_RESULT] - def main(prefix: Annotated[str, typer.Option(help="Prefix that is being used for the new branch.")] = "feature", - no_prefix: Annotated[bool, typer.Option("--no-prefix", help="Will not use a prefix")] = False): + no_prefix: Annotated[bool, typer.Option("--no-prefix", help="Will not use a prefix")] = False, + auto_branch_prefix: Annotated[bool, typer.Option("--auto-branch-prefix", help="Automatically determine branch prefix based on ticket type")] = False): """ - CLI to switch to git branches based on one's JIRA tickets. + CLI to switch to git branches based on one's JIRA tickets. - If --prefix is used, it will add a specific prefix to the branch (e.g. feature -> "feature/") - --no-prefix will omit the default "feature/" prefix. - """ + If --prefix is used, it will add a specific prefix to the branch (e.g. feature -> "feature/") + --no-prefix will omit the default "feature/" prefix. + --auto_branch_prefix will enable the ticket type to set the prefix: feature, bugfix, or refactor + """ tasks = load_branches() - terminal_menu = TerminalMenu(tasks) + formatted_tasks = [f"{task['summary']}" for task in tasks] # Display only the summary + terminal_menu = TerminalMenu(formatted_tasks) menu_entry_index = terminal_menu.show() - selected_task = tasks[menu_entry_index] - prefix = None if no_prefix else prefix - formatted_branch = format_branch(selected_task, prefix) - print(f"Switching to branch: {formatted_branch}") - process = subprocess.Popen(['git', 'switch', '-c', formatted_branch], - stdout=subprocess.PIPE) - process.communicate() + if menu_entry_index is not None: + selected_task = tasks[menu_entry_index] + prefix = None if no_prefix else prefix + if auto_branch_prefix: + auto_prefix = TICKET_TYPE_TO_PREFIX.get(selected_task['issuetype']['name'], None) + print(f"Ticket Type: {selected_task['issuetype']}") + formatted_branch = format_branch(selected_task['summary'], auto_prefix) + else: + formatted_branch = format_branch(selected_task['summary'], prefix) + print(f"Switching to branch: {formatted_branch}") + process = subprocess.Popen(['git', 'switch', '-c', formatted_branch], + stdout=subprocess.PIPE) + process.communicate() + else: + print("No menu entry selected. Exiting.") def format_branch(selected_task: str, prefix: Optional[str]): From 7415a566f217344d6e4c80061c4c12e1c8065599 Mon Sep 17 00:00:00 2001 From: "j.cabarcas" Date: Wed, 31 Jan 2024 18:24:38 +0100 Subject: [PATCH 2/3] Removes unnecessary comments and reverts extra space --- git_jira/git_jira.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/git_jira/git_jira.py b/git_jira/git_jira.py index f39e935..62c8692 100644 --- a/git_jira/git_jira.py +++ b/git_jira/git_jira.py @@ -28,7 +28,7 @@ def load_branches(): raise Exception("Please disclose your jira token as $JIRA_PAT") req = request.Request( instance + '/rest/api/2/search?' - 'jql=assignee=currentUser()+order+by+updated&fields=id,key,summary,issuetype,assignee', + 'jql=assignee=currentUser()+order+by+updated&fields=id,key,summary,issuetype,assignee', method="GET") req.add_header('Authorization', f'Bearer {token}') response = request.urlopen(req).read().decode('utf-8') @@ -41,7 +41,7 @@ def load_branches(): formatted_summary = re.sub(r"[^a-zA-Z0-9]+", ' ', formatted) formatted_branches.append({ 'summary': formatted_summary, - 'issuetype': issue['fields']['issuetype'] # Added to retrieve the ticket type + 'issuetype': issue['fields']['issuetype'] }) return formatted_branches[:MAX_RESULT] @@ -56,7 +56,7 @@ def main(prefix: Annotated[str, typer.Option(help="Prefix that is being used for --auto_branch_prefix will enable the ticket type to set the prefix: feature, bugfix, or refactor """ tasks = load_branches() - formatted_tasks = [f"{task['summary']}" for task in tasks] # Display only the summary + formatted_tasks = [f"{task['summary']}" for task in tasks] terminal_menu = TerminalMenu(formatted_tasks) menu_entry_index = terminal_menu.show() if menu_entry_index is not None: From 39d3592e0d40ac731e7af762dc06641752172e2a Mon Sep 17 00:00:00 2001 From: "j.cabarcas" Date: Wed, 31 Jan 2024 19:01:33 +0100 Subject: [PATCH 3/3] As suggested by a colleague matches Task with a feature prefix. --- git_jira/git_jira.py | 1 + 1 file changed, 1 insertion(+) diff --git a/git_jira/git_jira.py b/git_jira/git_jira.py index 62c8692..fb67b62 100644 --- a/git_jira/git_jira.py +++ b/git_jira/git_jira.py @@ -15,6 +15,7 @@ # Mapping of ticket types to branch prefixes TICKET_TYPE_TO_PREFIX = { "Story": "feature", + "Task": "feature", "Bug": "bugfix", "Refactoring": "refactor" }