From c75be5d4922a9bfaae58379548ab8d5ef1183573 Mon Sep 17 00:00:00 2001 From: Bimochan Shrestha Date: Sun, 9 Jun 2024 19:48:37 +0545 Subject: [PATCH] ci-tests: Move grabticket to separate file to prevent runMain execution --- .github/workflows/run_tests.yml | 2 -- src/__tests__/ticket.test.js | 11 +++++++++-- src/main.js | 19 +------------------ src/ticket.js | 17 +++++++++++++++++ 4 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 src/ticket.js diff --git a/.github/workflows/run_tests.yml b/.github/workflows/run_tests.yml index 95db202..9f9c45c 100644 --- a/.github/workflows/run_tests.yml +++ b/.github/workflows/run_tests.yml @@ -18,5 +18,3 @@ jobs: - name: Run tests run: npm test - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/src/__tests__/ticket.test.js b/src/__tests__/ticket.test.js index 31cfcb4..c07ae4e 100644 --- a/src/__tests__/ticket.test.js +++ b/src/__tests__/ticket.test.js @@ -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', () => { @@ -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); diff --git a/src/main.js b/src/main.js index e56ec55..24d9d95 100644 --- a/src/main.js +++ b/src/main.js @@ -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 { @@ -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(); diff --git a/src/ticket.js b/src/ticket.js new file mode 100644 index 0000000..d678e0c --- /dev/null +++ b/src/ticket.js @@ -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 }