Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Includes an auto_branch_prefix property #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions git_jira/git_jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

MAX_RESULT = 5

# Mapping of ticket types to branch prefixes
TICKET_TYPE_TO_PREFIX = {
Copy link
Collaborator

@neugartf neugartf Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd honestly move this to an environment variable, so everyone can define the mapping themselves (more extendable).
JIRA_TYPE_MAPPING="Story=feature,Task=feature"
https://github.com/sloria/environs can read these maps.

"Story": "feature",
"Task": "feature",
"Bug": "bugfix",
"Refactoring": "refactor"
}

def load_branches():
instance = os.environ.get("JIRA_INSTANCE")
Expand All @@ -32,28 +39,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']
})
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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do https://github.com/freenowtech/git-jira/pull/2/files#r1477907772, I think you can drop this (=> only map if the variable is defined).

"""
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]
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.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! :)



def format_branch(selected_task: str, prefix: Optional[str]):
Expand Down