Do you use Jira as the issue tracker in your company? Do you link GitHub PRs with Jira issues, and you want to ensure that link exists in every created PR of your repository? This GitHub Action is for you!
Workflow
Defining a Jira "prefix" issue, Jira Issue Key Checker
will verify every PR created in your repository contains a linked Jira issue key in both the PR title and description. You can see how to configure and trigger the action here:
https://github.com/fgiuliani/jira-issue-key-checker/blob/master/README.md
To see the complete code of the project, you can go here: https://github.com/fgiuliani/jira-issue-key-checker
Init project and install packages
As we'll use Node.js to create this GitHub Action, we will run npm init
in our directory to generate package.json
file. In this file we will set the details of our project. We will also install some dependencies that will help us to integrate our code with GitHub.
npm install @actions/core
npm install @actions/github
The @actions/core
package provides an interface to the workflow commands, input and output variables, exit statuses, and debug messages.
The @actions/github
package grants access to GitHub Actions contexts.
Create an action metadata file
Create a file called action.yml
. It defines action's inputs and details. It also tells the action runner how to start running this JavaScript action. Write the following example code in the file:
name: "Jira Issue Key Checker"
description: "Checks If a PR contains its linked Jira issue key in both the title and the description."
on: [pull_request]
inputs:
GITHUB_TOKEN:
required: true
jira-prefix:
required: true
description: "Jira issue key prefix. Ex. ABC-1111, prefix would be ABC"
runs:
using: "node12"
main: "index.js"
Write the action code
Let's create index.js
file, which will containg the action code.
const core = require("@actions/core");
const github = require("@actions/github");
const jiraPrefix = core.getInput("jira-prefix");
async function run() {
try {
const prTitle = github.context.payload.pull_request.title;
const prBody = github.context.payload.pull_request.body;
let regex = new RegExp(`${jiraPrefix}-[0-9]+`);
if (!regex.test(prTitle) || !regex.test(prBody)) {
core.setFailed("Jira Issue Key missing in PR title or description.");
return;
}
} catch (error) {
core.info(error);
}
}
run();
Some aspects to consider about the code:
- GitHub Actions provide context information about the webhook event, workflow, action, etc. To access the context information, you can use the
github
package. core.getInput("jira-prefix")
will get the prefix we defined for the issues we want to check.github.context.payload.pull_request
exposes information related to the PR we're evaluating with the action.core.setFailed
marks the PR as failed If it doesn't pass the evaluation of the Regular Expression that validates both PR title and description contain the linked Jira issue key.
Commit, tag, and push the action to GitHub
Commit all the files inside the directory of your project. You will also need to commit node_modules
folder.
git add .
git commit -m "My GitHub Action"
It's a good practice to also add a version tag for releases of your action. Let's create a version and push the changes to the repository.
git tag -a -m "My GitHub Action" v1
git push --follow-tags
Test and use the action
Take any GitHub repository and create the file .github/workflows/jira-issue-key-checker.yml
. In the file, paste this example code:
name: "Jira Issue Key Checker"
on:
pull_request:
types:
- opened
- edited
- synchronize
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: [your-user]/[your-action-repository]@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jira-prefix: "ABC"
Add this file to your GitHub repository. After this, whenever a new PR is created in that repository, the GitHub Action will run and evaluate it. You will be able to see the workflow execution and details about that clicking on the Actions tab.