Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SOME-123: Add unit tests run in CI #33

Merged
merged 13 commits into from
Jun 9, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ jobs:
name: Auto jira link commenter
steps:
- name: Checkout
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Comment PR
uses: sbimochan/jira-link-commenter@v2.4

uses: sbimochan/jira-link-commenter@v3.0
with:
jira-project-url: https://jira.atlassian.net/browse
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
custom-comment: 'Thank you for your contribution!!! :confetti_ball:'
custom-comment: "Thank you for your contribution!!! :confetti_ball:"
20 changes: 20 additions & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
on: pull_request

jobs:
run_tests:
runs-on: ubuntu-latest
name: Run tests
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "20"

- name: Install dependencies
run: npm install

- name: Run tests
run: npm test
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jira-link-commenter",
"version": "3.0",
"version": "3.1",
"description": "This action auto comments in pull request with Jira link to it.",
"main": "src/main.js",
"scripts": {
Expand Down
11 changes: 9 additions & 2 deletions src/__tests__/ticket.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const grabTicket = require('../main').grabTicket;
const ticketRegex = require('../main').DEFAULT_TICKET_REGEX
const grabTicket = require('../ticket').grabTicket;
const ticketRegex = require('../ticket').DEFAULT_TICKET_REGEX

describe('grabTicket', () => {
it('should return ticket id without colon', () => {
Expand All @@ -23,6 +23,13 @@ describe('grabTicket', () => {
expect(result).toBe('ABCDEFGH-12345678');
});

it('should return not return ticket without colon', () => {
const title = 'ABCDEFGH-12345678 New Feature';
const result = grabTicket(title, ticketRegex);

expect(result).toBeNull();
});

it('should return null if no ticket id is found', () => {
const title = 'No ticket id here';
const result = grabTicket(title, ticketRegex);
Expand Down
19 changes: 1 addition & 18 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const core = require('@actions/core');
const github = require('@actions/github');

const DEFAULT_TICKET_REGEX = /^[A-Z,a-z]{2,}-\d{1,}:/g;
const { grabTicket, DEFAULT_TICKET_REGEX } = require('./ticket');

async function runMain() {
try {
Expand Down Expand Up @@ -53,20 +52,4 @@ async function checkIfOldCommentExists(octokit, context, pullRequestNumber) {
return isPrevComment;
}

/**
* Searches with first Ticket like structure with colon and later removes it.
*
* @param {string} title
*/
function grabTicket(title, ticketRegex) {
const ticketIdWithColon = title.match(ticketRegex)?.[0];
if (!ticketIdWithColon) {
return null;
}

return ticketIdWithColon.slice(0, -1);
}

module.exports = { grabTicket, DEFAULT_TICKET_REGEX }

runMain();
17 changes: 17 additions & 0 deletions src/ticket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const DEFAULT_TICKET_REGEX = /^[A-Z,a-z]{2,}-\d{1,}:/g;

/**
* Searches with first Ticket like structure with colon and later removes it.
*
* @param {string} title
*/
function grabTicket(title, ticketRegex) {
const ticketIdWithColon = title.match(ticketRegex)?.[0];
if (!ticketIdWithColon) {
return null;
}

return ticketIdWithColon.slice(0, -1);
}

module.exports = { grabTicket, DEFAULT_TICKET_REGEX }