Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@

![](out.gif)

# git-jira

A simple CLI to switch to git branches based on one's JIRA tickets, only supports Jira Cloud.

## Installation

```bash
brew tap freenowtech/cli
brew install freenowtech/cli/git-jira
```

Create a Personal Access Token (PAT) in **JIRA** (not Confluence ⚠️) as per [instruction](https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html#UsingPersonalAccessTokens-CreatingPATsintheapplication).
Create a Personal Access Token (PAT) in **JIRA** as per [instruction](https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/).

Add `$JIRA_PAT` and `$JIRA_INSTANCE` to your favorite shell:
Add `$JIRA_USER`, `$JIRA_API_TOKEN` and `$JIRA_INSTANCE` to your favorite shell:

```
echo -n 'export JIRA_PAT=YOUR_PAT' >> ~/.zshrc
echo -n 'export JIRA_INSTANCE=YOUR_JIRA_INSTANCE' >> ~/.zshrc
echo -n 'export JIRA_USER=YOUR_USER' >> ~/.zshrc
echo -n 'export JIRA_API_TOKEN=YOUR_API_TOKEN' >> ~/.zshrc
echo -n 'export JIRA_INSTANCE=YOUR_INSTANCE' >> ~/.zshrc
source ~/.zshrc
```

Expand Down
28 changes: 20 additions & 8 deletions git_jira/git_jira.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
import json
import os
from os import environ
import re
import subprocess
from urllib import request
Expand All @@ -14,17 +14,29 @@


def load_branches():
instance = os.environ.get("JIRA_INSTANCE")
token = os.environ.get("JIRA_PAT")
instance = environ.get("JIRA_INSTANCE")
user = environ.get("JIRA_USER")
token = environ.get("JIRA_API_TOKEN")
if not instance:
raise Exception("Please disclose your jira instance as $JIRA_INSTANCE")
raise ValueError("Please disclose your jira instance as $JIRA_INSTANCE")
if not user:
raise ValueError("Please disclose your jira token as $JIRA_USER")
if not token:
raise Exception("Please disclose your jira token as $JIRA_PAT")
raise ValueError("Please disclose your jira token as $JIRA_API_TOKEN")

password_mgr = request.HTTPPasswordMgrWithPriorAuth()
password_mgr.add_password(None, instance, user, token, is_authenticated=True)

auth_handler = request.HTTPBasicAuthHandler(password_mgr)
opener = request.build_opener(auth_handler)

request.install_opener(opener)

req = request.Request(
instance + '/rest/api/2/search?'
instance + '/rest/api/3/search?'
'jql=assignee=currentUser()+order+by+updated&fields=id,key,summary,issuetype,assignee',
method="GET")
req.add_header('Authorization', f'Bearer {token}')
req.add_header('Accept', 'application/json')
response = request.urlopen(req).read().decode('utf-8')
response = json.loads(response)

Expand All @@ -41,7 +53,7 @@ def main(prefix: Annotated[str, typer.Option(help="Prefix that is being used for
"""
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/")
If --prefix is used, it adds a specific prefix to the branch (e.g. feature -> "feature/")
--no-prefix will omit the default "feature/" prefix.
"""
tasks = load_branches()
Expand Down
Loading