From 7464f26014650f6e11e50c463b48920c4fc85f30 Mon Sep 17 00:00:00 2001 From: neugartf Date: Fri, 2 May 2025 15:33:00 +0200 Subject: [PATCH] Switch to support for JIRA Cloud --- README.md | 13 +++++++++---- git_jira/git_jira.py | 28 ++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 3d330e6..9761c63 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ ![](out.gif) +# git-jira + +A simple CLI to switch to git branches based on one's JIRA tickets, only supports Jira Cloud. + ## Installation ```bash @@ -8,13 +12,14 @@ 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 ``` diff --git a/git_jira/git_jira.py b/git_jira/git_jira.py index 75a4487..8f2d9b6 100644 --- a/git_jira/git_jira.py +++ b/git_jira/git_jira.py @@ -1,6 +1,6 @@ #!/usr/bin/env python import json -import os +from os import environ import re import subprocess from urllib import request @@ -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) @@ -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()