Skip to content

Commit

Permalink
Merge pull request #45 from galblond/was-key-found
Browse files Browse the repository at this point in the history
feat(jira-issue-found): adding a new output indicating whether the jira issue was found or not
  • Loading branch information
cakeinpanic committed Sep 3, 2023
2 parents 7bedf56 + ab2134d commit 0994162
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 15 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ When a PR passes the above check, `jira-description-action` will also add the is
| `custom-issue-number-regexp` | Custom regexp to extract issue number from branch name. If not specified, default regexp would be used. | false | none |
| `fail-when-jira-issue-not-found` | Should action fail if jira issue is not found in jira | false | false |

### Outputs

| key | description
| ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `jira-issue-found` | Indication whether a jira issue was found or not |
| `jira-issue-source` | Indication how the jira issue was found, by - `branch \| pr-title \| null` |

Tokens are private, so it's suggested adding them as [GitHub secrets](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets).

### `jira-token`
Expand Down
32 changes: 24 additions & 8 deletions __tests__/github-connector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,19 @@ describe('Github connector()', () => {
(getInputs as any).mockImplementation(() => ({ ...INPUTS_MOCK, WHAT_TO_USE: ESource.branch }));
connector = new GithubConnector();

expect(connector.getIssueKeyFromTitle()).toEqual(getJIRAIssueKeyReturnValue);
const jiraIssue = connector.getIssueKeyFromTitle();
expect(jiraIssue.key).toEqual(getJIRAIssueKeyReturnValue);
expect(jiraIssue.source).toEqual(ESource.branch);
expect(getJIRAIssueKeyByDefaultRegexp).toHaveBeenCalledWith(BRANCH_NAME);
});

it('calls getJIRAIssueKeyByDefaultRegexp method with PR title if USE_BRANCH_NAME == pr-title', () => {
(getInputs as any).mockImplementation(() => ({ ...INPUTS_MOCK, WHAT_TO_USE: ESource.prTitle }));
connector = new GithubConnector();

expect(connector.getIssueKeyFromTitle()).toEqual(getJIRAIssueKeyReturnValue);
const jiraIssue = connector.getIssueKeyFromTitle();
expect(jiraIssue.key).toEqual(getJIRAIssueKeyReturnValue);
expect(jiraIssue.source).toEqual(ESource.prTitle);
expect(getJIRAIssueKeyByDefaultRegexp).toHaveBeenCalledWith(PR_TITLE);
});

Expand All @@ -74,7 +78,9 @@ describe('Github connector()', () => {
(getJIRAIssueKeyByDefaultRegexp as any).mockImplementation((str: string) => (str === PR_TITLE ? PR_TITLE : null));
connector = new GithubConnector();

expect(connector.getIssueKeyFromTitle()).toEqual(PR_TITLE);
const jiraIssue = connector.getIssueKeyFromTitle();
expect(jiraIssue.key).toEqual(PR_TITLE);
expect(jiraIssue.source).toEqual(ESource.prTitle);
expect(getJIRAIssueKeyByDefaultRegexp).toHaveBeenCalledWith(PR_TITLE);
expect(getJIRAIssueKeyByDefaultRegexp).not.toHaveBeenCalledWith(BRANCH_NAME);
});
Expand All @@ -84,7 +90,9 @@ describe('Github connector()', () => {
(getJIRAIssueKeyByDefaultRegexp as any).mockImplementation((str: string) => (str === PR_TITLE ? null : BRANCH_NAME));
connector = new GithubConnector();

expect(connector.getIssueKeyFromTitle()).toEqual(BRANCH_NAME);
const jiraIssue = connector.getIssueKeyFromTitle();
expect(jiraIssue.key).toEqual(BRANCH_NAME);
expect(jiraIssue.source).toEqual(ESource.branch);
expect(getJIRAIssueKeyByDefaultRegexp).toHaveBeenCalledWith(PR_TITLE);
expect(getJIRAIssueKeyByDefaultRegexp).toHaveBeenCalledWith(BRANCH_NAME);
});
Expand All @@ -110,7 +118,9 @@ describe('Github connector()', () => {
(getInputs as any).mockImplementation(() => ({ ...INPUTS_MOCK, WHAT_TO_USE: ESource.branch }));
connector = new GithubConnector();

expect(connector.getIssueKeyFromTitle()).toEqual(getJIRAIssueKeysByCustomRegexpReturnValue);
const jiraIssue = connector.getIssueKeyFromTitle();
expect(jiraIssue.key).toEqual(getJIRAIssueKeysByCustomRegexpReturnValue);
expect(jiraIssue.source).toEqual(ESource.branch);
expect(getJIRAIssueKeysByCustomRegexp).toHaveBeenCalledWith(
BRANCH_NAME,
INPUTS_MOCK.CUSTOM_ISSUE_NUMBER_REGEXP,
Expand All @@ -122,7 +132,9 @@ describe('Github connector()', () => {
(getInputs as any).mockImplementation(() => ({ ...INPUTS_MOCK, WHAT_TO_USE: ESource.prTitle }));
connector = new GithubConnector();

expect(connector.getIssueKeyFromTitle()).toEqual(getJIRAIssueKeysByCustomRegexpReturnValue);
const jiraIssue = connector.getIssueKeyFromTitle();
expect(jiraIssue.key).toEqual(getJIRAIssueKeysByCustomRegexpReturnValue);
expect(jiraIssue.source).toEqual(ESource.prTitle);
expect(getJIRAIssueKeysByCustomRegexp).toHaveBeenCalledWith(PR_TITLE, INPUTS_MOCK.CUSTOM_ISSUE_NUMBER_REGEXP, INPUTS_MOCK.JIRA_PROJECT_KEY);
});

Expand All @@ -132,7 +144,9 @@ describe('Github connector()', () => {
(getJIRAIssueKeysByCustomRegexp as any).mockImplementation((str: string) => (str === PR_TITLE ? PR_TITLE : null));
connector = new GithubConnector();

expect(connector.getIssueKeyFromTitle()).toEqual(PR_TITLE);
const jiraIssue = connector.getIssueKeyFromTitle();
expect(jiraIssue.key).toEqual(PR_TITLE);
expect(jiraIssue.source).toEqual(ESource.prTitle);
expect(getJIRAIssueKeysByCustomRegexp).toHaveBeenCalledWith(PR_TITLE, INPUTS_MOCK.CUSTOM_ISSUE_NUMBER_REGEXP, INPUTS_MOCK.JIRA_PROJECT_KEY);
expect(getJIRAIssueKeysByCustomRegexp).not.toHaveBeenCalledWith(
BRANCH_NAME,
Expand All @@ -146,7 +160,9 @@ describe('Github connector()', () => {
(getJIRAIssueKeysByCustomRegexp as any).mockImplementation((str: string) => (str === PR_TITLE ? null : BRANCH_NAME));
connector = new GithubConnector();

expect(connector.getIssueKeyFromTitle()).toEqual(BRANCH_NAME);
const jiraIssue = connector.getIssueKeyFromTitle();
expect(jiraIssue.key).toEqual(BRANCH_NAME);
expect(jiraIssue.source).toEqual(ESource.branch);
expect(getJIRAIssueKeysByCustomRegexp).toHaveBeenCalledWith(PR_TITLE, INPUTS_MOCK.CUSTOM_ISSUE_NUMBER_REGEXP, INPUTS_MOCK.JIRA_PROJECT_KEY);
expect(getJIRAIssueKeysByCustomRegexp).toHaveBeenCalledWith(
BRANCH_NAME,
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ inputs:
description: ' Mark the PR status check as failed when a jira issue is not found'
required: false
default: 'false'
outputs:
jira-issue-found:
description: 'Indication whether a jira issue was found or not'
jira-issue-source:
description: 'Indication how the jira issue was found, by - branch | pr-title | null'
runs:
using: 'node16'
main: 'lib/index.js'
Expand Down
20 changes: 15 additions & 5 deletions src/github-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,41 @@ export class GithubConnector {
return this.githubData.pullRequest.head.ref;
}

getIssueKeyFromTitle(): string {
getIssueKeyFromTitle(): { key: string; source: ESource } {
const { WHAT_TO_USE } = getInputs();

const prTitle = this.githubData.pullRequest.title || '';
const branchName = this.headBranch;

let keyFound: string | null = null;
let source: ESource | null = null;

switch (WHAT_TO_USE) {
case ESource.branch:
keyFound = this.getIssueKeyFromString(branchName);
source = keyFound ? ESource.branch : null;
break;
case ESource.prTitle:
keyFound = this.getIssueKeyFromString(prTitle);
source = keyFound ? ESource.prTitle : null;
break;
case ESource.both:
keyFound = this.getIssueKeyFromString(prTitle) || this.getIssueKeyFromString(branchName);
const keyByPRTitle = this.getIssueKeyFromString(prTitle);
if (keyByPRTitle) {
keyFound = keyByPRTitle;
source = ESource.prTitle;
} else {
keyFound = this.getIssueKeyFromString(branchName);
source = keyFound ? ESource.branch : null;
}
break;
}

if (!keyFound) {
if (!keyFound || !source) {
throw new Error('JIRA key not found');
}
console.log(`JIRA key found -> ${keyFound}`);
return keyFound;
console.log(`JIRA key found -> ${keyFound} from ${source}`);
return { key: keyFound, source };
}

private getIssueKeyFromString(stringToParse: string): string | null {
Expand Down
13 changes: 11 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { JiraConnector } from './jira-connector';

async function run(): Promise<void> {
const { FAIL_WHEN_JIRA_ISSUE_NOT_FOUND } = getInputs();
let jiraIssueFound = false;
let jiraIssueSource = null;

try {
const { BRANCH_IGNORE_PATTERN } = getInputs();

Expand All @@ -21,10 +24,13 @@ async function run(): Promise<void> {
process.exit(0);
}

const issueKey = githubConnector.getIssueKeyFromTitle();
const { key, source } = githubConnector.getIssueKeyFromTitle();

const details = await jiraConnector.getTicketDetails(issueKey);
const details = await jiraConnector.getTicketDetails(key);
await githubConnector.updatePrDetails(details);

jiraIssueFound = true;
jiraIssueSource = source;
} catch (error) {
console.log('Failed to add JIRA description to PR.');
core.error(error.message);
Expand All @@ -35,6 +41,9 @@ async function run(): Promise<void> {
} else {
process.exit(0);
}
} finally {
core.setOutput('jira-issue-found', jiraIssueFound.toString());
core.setOutput('jira-issue-source', jiraIssueSource);
}
}

Expand Down

0 comments on commit 0994162

Please sign in to comment.