Streamline Your Workflow: Automating the Attachment of Jira Ticket Link to GitHub Pull Request Description

Elnatan Derech
Israeli Tech Radar
Published in
4 min readAug 21, 2023

As a developer, one of the most critical tasks is to understand and maintain the codebase. This often involves investigating the program code and trying to understand the reasoning behind certain lines of code. However, this can be a time-consuming and tedious process, especially when trying to trace the history of a particular piece of code. One way to streamline this process is by automating the connection between the Jira ticket with the working branch and the attachment of the Jira ticket link to a Github pull request description. This allows developers to easily trace the history of a particular piece of code and understand why it was written.

In this article, we will go through the step-by-step process of automating this process, and how it can improve the efficiency and organization of your workflow.

So, let’s start with automating the linking between the Jira ticket and the related working branch.

First, you’ll (or the Jira administrator) need to set up an integration between Jira and Github. This can be done by going to the “Jira settings” in your Github account and connecting it to your Jira account.

Next, Create a GitHub branch by clicking on “create branch” (you also can create it locally by copying it from the box)

Do your task and then when you are ready, click on the “Create New Pull Request” button.

A GitHub pull request window opens up, and the title is pre-populated with the corresponding Jira ticket title. You can edit the PR title but important that the ticket number remains

Now, let’s focus on creating the following Workflow that streamlines the process of extracting the Jira ticket number from the pull request title, generating the corresponding Jira ticket link, and appending it to the pull request description.

Create a new Github workflow with the following *.yml configuration file:

name: append jira ticket link
on:
pull_request:
types: [opened,edited]

jobs:
generate_jira_ticket_link:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
issues: write
pull-requests: write
steps:
- uses: actions/github-script@v6
with:
script: |

const title = context.payload.pull_request.title;
const match = title.match(/(mob|uet)\s*([\w-]+)/i);
if (match) {
const ticketNumber = match[2].replace(/\s/g, "-");
const jiraTicketLink = `https://your_organization.atlassian.net/browse/${ticketNumber}`;
let description = context.payload.pull_request.body ?? ``;
description = `${description}\nJira ticket: ${jiraTicketLink}`;
github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
body: description
});
}

Let’s dive deeper into this code.

This GitHub workflow code runs on every pull request, whether it is opened or edited

on:
pull_request:
types: [opened,edited]

It also must contain the read-write permissions set

permissions:
id-token: write
contents: read
issues: write
pull-requests: write

The step uses the actions/github-script@v6 action to run JavaScript code and easily communicate with the GitHub API. It is a very useful tool. You can read about it here

- uses: actions/github-script@v6

It extracts the Jira ticket number from the PR title using Regex

const title = context.payload.pull_request.title;
const match = title.match(/(mob|uet)\s*([\w-]+)/i);

If the Jira ticket number exists in the PR Title, it appends it to the PR description

if (match) {
const ticketNumber = match[2].replace(/\s/g, "-");
const jiraTicketLink = `https://your_organization.atlassian.net/browse/${ticketNumber}`;
let description = context.payload.pull_request.body ?? ``;
description = `${description}\nJira ticket: ${jiraTicketLink}`;

github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
body: description
});
}

And.. that’s it, from now on, for every Pull Request a Jira ticket link will append to the description.

Here is a short video that demonstrates the process, starting the code editing phase in a new branch:

In summary, implementing this automation can significantly enhance the efficiency of tracking the history and intent of individual lines of code.

I hope you find this article useful, let me know by commenting below.

P.S.

There are several pre-built Github Actions such as Jira-Pull-Request and Jira-Description that do the same job. However, I found them to be too complex or not well-maintained for my organization’s specific needs and requirements.

--

--

Elnatan Derech
Israeli Tech Radar

Senior Mobile Developer @ Tikal - Home of Tech Experts