Skip to content

Commit 7464f26

Browse files
committed
Switch to support for JIRA Cloud
1 parent fc62f08 commit 7464f26

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11

22
![](out.gif)
33

4+
# git-jira
5+
6+
A simple CLI to switch to git branches based on one's JIRA tickets, only supports Jira Cloud.
7+
48
## Installation
59

610
```bash
711
brew tap freenowtech/cli
812
brew install freenowtech/cli/git-jira
913
```
1014

11-
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).
15+
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/).
1216

13-
Add `$JIRA_PAT` and `$JIRA_INSTANCE` to your favorite shell:
17+
Add `$JIRA_USER`, `$JIRA_API_TOKEN` and `$JIRA_INSTANCE` to your favorite shell:
1418

1519
```
16-
echo -n 'export JIRA_PAT=YOUR_PAT' >> ~/.zshrc
17-
echo -n 'export JIRA_INSTANCE=YOUR_JIRA_INSTANCE' >> ~/.zshrc
20+
echo -n 'export JIRA_USER=YOUR_USER' >> ~/.zshrc
21+
echo -n 'export JIRA_API_TOKEN=YOUR_API_TOKEN' >> ~/.zshrc
22+
echo -n 'export JIRA_INSTANCE=YOUR_INSTANCE' >> ~/.zshrc
1823
source ~/.zshrc
1924
```
2025

git_jira/git_jira.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
import json
3-
import os
3+
from os import environ
44
import re
55
import subprocess
66
from urllib import request
@@ -14,17 +14,29 @@
1414

1515

1616
def load_branches():
17-
instance = os.environ.get("JIRA_INSTANCE")
18-
token = os.environ.get("JIRA_PAT")
17+
instance = environ.get("JIRA_INSTANCE")
18+
user = environ.get("JIRA_USER")
19+
token = environ.get("JIRA_API_TOKEN")
1920
if not instance:
20-
raise Exception("Please disclose your jira instance as $JIRA_INSTANCE")
21+
raise ValueError("Please disclose your jira instance as $JIRA_INSTANCE")
22+
if not user:
23+
raise ValueError("Please disclose your jira token as $JIRA_USER")
2124
if not token:
22-
raise Exception("Please disclose your jira token as $JIRA_PAT")
25+
raise ValueError("Please disclose your jira token as $JIRA_API_TOKEN")
26+
27+
password_mgr = request.HTTPPasswordMgrWithPriorAuth()
28+
password_mgr.add_password(None, instance, user, token, is_authenticated=True)
29+
30+
auth_handler = request.HTTPBasicAuthHandler(password_mgr)
31+
opener = request.build_opener(auth_handler)
32+
33+
request.install_opener(opener)
34+
2335
req = request.Request(
24-
instance + '/rest/api/2/search?'
36+
instance + '/rest/api/3/search?'
2537
'jql=assignee=currentUser()+order+by+updated&fields=id,key,summary,issuetype,assignee',
2638
method="GET")
27-
req.add_header('Authorization', f'Bearer {token}')
39+
req.add_header('Accept', 'application/json')
2840
response = request.urlopen(req).read().decode('utf-8')
2941
response = json.loads(response)
3042

@@ -41,7 +53,7 @@ def main(prefix: Annotated[str, typer.Option(help="Prefix that is being used for
4153
"""
4254
CLI to switch to git branches based on one's JIRA tickets.
4355
44-
If --prefix is used, it will add a specific prefix to the branch (e.g. feature -> "feature/")
56+
If --prefix is used, it adds a specific prefix to the branch (e.g. feature -> "feature/")
4557
--no-prefix will omit the default "feature/" prefix.
4658
"""
4759
tasks = load_branches()

0 commit comments

Comments
 (0)