Generating Git Branches from Your Issue Tracker Using Alfred

Quickly generate unique, descriptive git branches from issue trackers such as Trello and Jira using a simple Alfred workflow.

Joe Tannenbaum

This post is part of a series of useful commands I've built for myself as part of my day-to-day workflow.

The start of just about any of my tasks involves diving into a repo and branching off of main. And here's what I don't want to do: manually type out the name of the branch. Ideally, the branch is formatted as follows:

[issue number from issue tracker]-[description of bug/feature/etc]

For example, 425-please-fix-login-its-completely-broken. This makes it a) unique b) descriptive and c) easy to trace back to the source.

Many of my clients use different issue-tracking software, but the two most popular are Trello and Jira.

I created a simple Alfred workflow to quickly and automatically generate these branch names for me. Let's check it out:

The objects in my Issue to Git Branch Alfred workflow
The objects in my Issue to Git Branch Alfred workflow

Ok, so pretty straightforward!

  • Trigger the workflow with a hotkey, in this case ⇧⌘I

  • Grab the current selection

  • Run a script

  • Copy the result to the clipboard

  • Notify me of the results

This is how it works. For Trello, I simply click into the card detail, highlight the URL, press ⇧⌘I, and we're in business.

A sample Trello URL
A sample Trello URL
The beautiful output I can just paste directly into the terminal
The beautiful output I can just paste directly into the terminal

The Workflow

Let's go through the workflow, object-by-object:

Hotkey

The Hotkey object settings
The Hotkey object settings

Hot tip for your Hotkey object: This hotkey will run regardless of what app you're in and will override any keyboard shortcuts that are in that app. So especially for a workflow like this, it might be smart to specify which apps the hotkey should be active for. I've set mine to Chrome since the issue trackers I work with are web-based:

Don't go messing up my keyboard shortcuts in other apps, thanks
Don't go messing up my keyboard shortcuts in other apps, thanks

Run Script

For the Run Script action, we're simply running:

1php generate-branch.php "{query}"

Let's see what generate-branch.php looks like:

1<?php
2 
3$query = $argv[1];
4 
5// Helper function to slugify a string
6function slug($string)
7{
8 return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string), '-'));
9}
10 
11// This is coming from Jira ('PROJECT-' is the issue prefix)
12if (strstr($query, 'PROJECT-')) {
13 $parts = explode("\n", $query);
14 $parts = array_values(array_filter($parts));
15 $issueNumber = str_replace('PROJECT-', '', $parts[0]);
16 echo sprintf('git checkout -b %s-%s', $issueNumber, slug($parts[1]));
17}
18 
19// This is coming from Trello
20if (strstr($query, 'trello.com')) {
21 preg_match('/https:\/\/trello\.com\/c\/[^\/]+\/([^"]+)/', $query, $matches);
22 
23 echo sprintf('git checkout -b %s', $matches[1]);
24}

Short and sweet. We're testing the string for indicators of its source and handling it accordingly.

Copy to Clipboard

The Copy to Clipboard object simply copies the argument returned from the script:

Post Notification

The Post Notification Object simply outputs the argument as well:

Make It Your Own

The workflow is easily modifiable for whatever issue tracker you have. Simply determine a unique indicator for that source, then apply the proper transformation to get the branch format you'd like. Happy branching!

(Excellent) syntax highlighting provided by Torchlight