Auto linking Jira ticket in PR description via Github Action

Rita Łyczywek
Fandom Engineering

--

I was looking for an easy way to generate a URL to a Jira ticket in the pull request description or as the first comment. The ticket number/code should be automatically extracted from the branch name.

Neat, automated solutions, but unfortunately, all the top articles from Google led me to the opposite problem — how to see PRs or commits from GitHub in Jira.

The final solution is really simple, so maybe someone will use it.

How to automatically add a link to the Jira ticket in the PR description via GitHub Action?

(or any other project management tool)

First things first — What is Github Action?

GitHub Action is a tool added to Github that allows you to automate your entire software development workflow such as CI/CD, custom steps, E2E testing, etc.

There is a lot of things you can just drop into GitHub Action and not have to worry about.

All you need is to create the .github/workflows directory and add there a new .yml file with just 3 keys:

  • name (name your action)
  • on (when to trigger)
  • jobs (which jobs should be executed)

PR update action

You don’t need to create an action from scratch. I found a PR Update Action that allows to prefix title and prefix or suffix body description of a PR based on text extracted from the branch name.

I added .github/workflows/pr_update.yml

name: PR update
on:
pull_request:
types: [opened]


jobs:
update_pr:
name: Update PR
runs-on: ubuntu-latest
steps:
- uses: tzkhan/pr-update-action@v2
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
base-branch-regex: '[A-Za-z\d-_.\\/]+'
head-branch-regex: '^([A-Za-z])+-\d+'
title-template: '%headbranch%: '
body-template: |
Jira issue: %headbranch%
---
body-update-action: 'prefix'
body-uppercase-base-match: false

Quick explanation

  • I want to update my title & PR description once on PR open, so I need to specify the type as [opened].
on:
pull_request:
types: [opened]
  • RegEx for branch name ([A-Za-z\d-_.\\/]+) and extracted Jira ticket name based on a schema (([A-Za-z])+-\d+).
    In my case each ticket is 3 or 4 letters code, separator "-" and numbers e.g. JIRA-001, ABC-123
  • title-template prefix for the title
  • body-template text added to the body of PR description as a prefix or suffix by value in body-update-action

What about not matching branches?

The solution above is perfect if your team keeps the branching pattern.

But let’s be realistic, what about all branches named quick-fix, friday-release-to-prod etc? The same issue occurs with automated dependency updates.

The PR update step is failing. So, we should fix it.

➡️ Simple solution: check the branch name, and conditionally run PR Update.

1st attempt — filter pattern

Documentation provides us filter patterns on branches/tags, but at this moment it doesn’t fully support regex. Examples are poor and you can’t filter a branch with - separator.

2nd attempt — if statement

For this approach, I need to have a github context to get the branch name.

Actions can communicate with the runner machine to set environment variables, output values used by other actions, add debug messages to the output logs, and other tasks.

- name: Check Branch
id: check-branch
run: |
if [[ ${{ github.head_ref }} =~ ^[A-Za-z]+-[0-9]+.*$ ]]; then
echo ::set-output name=match::true
fi
- name: Test
id: test
if: steps.check-branch.outputs.match == 'true'
run: |
echo "Branch ${{ github.head_ref }} is a match"

I added a separate step, that creates a step output (::set-output) and then the output can be used in an if conditional in the next step.

Here’s my final action:

name: PR update
on:
pull_request:
types: [opened]

jobs:
update_pr:
name: PR update
runs-on: ubuntu-latest
steps:
- name: Check Branch
id: check-branch
run: |
if [[ ${{ github.head_ref }} =~ ^[A-Za-z]+-[0-9]+.*$ ]]; then
echo ::set-output name=match::true
fi
- uses: tzkhan/pr-update-action@v2
if: steps.check-branch.outputs.match == 'true'
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
base-branch-regex: '[A-Za-z\d-_.\\/]+'
head-branch-regex: '^([A-Za-z])+-\d+'
title-template: '[%headbranch%] '
body-template: |
Jira issue: %headbranch%
---
body-update-action: 'prefix'
body-uppercase-base-match: false

Hope you were searching for this, and it has helped. Thanks for reading!

Originally published at https://dev.fandom.com.

--

--

Rita Łyczywek
Fandom Engineering

Backend is my comfort zone 💜 Software Engineer at Wizards Of The Coast | flynerd.pl