DEV Community

Cover image for How integrate jira with my git branches with python
Andres 🐍 in 🇨🇦
Andres 🐍 in 🇨🇦

Posted on

1 1

How integrate jira with my git branches with python

At work I have some development ruls like:

Every change on code should be comes with a Pull request and the branch name should be named like ID of jira.

Then we have branches like this:

  $ git branch -a
  remotes/origin/DEV-118
  remotes/origin/DEV-120
  remotes/origin/DEV-123
  remotes/origin/DEV-130
  remotes/origin/DEV-133
  remotes/origin/DEV-137
  remotes/origin/DEV-141
....
Enter fullscreen mode Exit fullscreen mode

with that you don't know each branch what is it ... then lets make some fun:

#!/bin/env python
"""
use the api token from jira: https://id.atlassian.com/manage-profile/security/api-tokens
# add to ~/.bashrc
export JIRA_API_TOKEN=""
export JIRA_USER=""
export JIRA_URL="https://myjira.atlassian.net/"
# install
pip install jira colorama
"""
from jira import JIRA
import os
import subprocess
import re
from colorama import Fore, Back, Style
token = os.environ.get("JIRA_API_TOKEN")
user = os.environ.get("JIRA_USER")
jira_url = os.environ.get("JIRA_URL")
jira = JIRA(server=jira_url, basic_auth=(user, token))
r = subprocess.check_output("git branch", shell=True)
for i in r.decode("utf-8").split('\n'):
m = re.search(r'([A-Za-z]{2,3})-([0-9]{3})',i.strip())
if m:
jira_issue = m.group()
j = jira.issue(jira_issue)
print(f"{Fore.GREEN} {i.lstrip()} {Fore.LIGHTWHITE_EX} {j.fields.status} {Fore.CYAN}{j.fields.summary} {Fore.RESET}{j.permalink()}")
else:
print(f"{Fore.MAGENTA} {i.strip()}")
view raw jira-list hosted with ❤ by GitHub

With this you will receive:

DEV-XXX <Assigned Name> <Status> <Title> <url>

Now you can lost between branches and jira issues :>

output

is this the integration ? comeon zodman! show me more!

Ok ok Imagine you have assigned the ticket DEV-foo1

In git you do:

git checkout -b DEV-foo1

and start hacking .... when you finish and everything looks good. you should create the pull request then you type:


gh pr create -B dev --title "$(jira-list | grep $(git rev-parse --abbrev-ref HEAD))"

Enter fullscreen mode Exit fullscreen mode

PUM! you created a pull request with ID and title from jira.

if you don't know what is gh its a github cli

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay