Skip to content

Commit

Permalink
Regex Fix - #1820 (#1821)
Browse files Browse the repository at this point in the history
* - Correct Regex applied

* - Correct Regex added
- Test cases added

* - minor update in test case

* - minor update in test case

Co-authored-by: Kayub Maharjan <[email protected]>
  • Loading branch information
krazziekay and krazziekay authored Jan 5, 2023
1 parent ca5516e commit e4f8a1d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/jira/util/jira-client-util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ describe("Jira util", () => {
fields: {
summary: "Fourth Issue"
}
},
{
key: "TEST1-2021",
fields: {
summary: "Fifth Issue"
}
}
];

Expand Down
4 changes: 2 additions & 2 deletions src/jira/util/jira-client-util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { envVars } from "config/env";
import { getLogger } from "config/logger";
import { JiraIssue } from "interfaces/jira";
import { jiraIssueRegex } from "utils/jira-utils";
import { jiraIssueInSquareBracketsRegex } from "utils/jira-utils";

const logger = getLogger("jira.util");

Expand Down Expand Up @@ -33,7 +33,7 @@ export const getJiraUtil = (jiraClient) => {
};

const addJiraIssueLinks = (text: string, issues: JiraIssue[]): string => {
const referenceRegex = jiraIssueRegex();
const referenceRegex = jiraIssueInSquareBracketsRegex();
const issueMap = issues.reduce((acc, issue) => ({
...acc,
[issue.key]: issue
Expand Down
14 changes: 14 additions & 0 deletions src/util/jira-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ export const jiraIssueRegex = (): RegExp => {
return /(^|[^\p{L}\p{Nd}])([\p{L}][\p{L}\p{Nd}_]{1,255}-\p{Nd}{1,255})/giu;
};

/**
* Same as the `jiraIssueRegex`,
* but this Regex captures only those issue keys that are surrounded by square brackets
* This regex is used when adding links to Jira issues in GitHub PR issue/descriptions.
*/
export const jiraIssueInSquareBracketsRegex = (): RegExp => {
if (issueKeyRegexCharLimitFeature) {
return /(^|[^A-Z\d])\[([A-Z][A-Z\d]{1,255}-[1-9]\d{0,255})\]/giu;
} else if (regexFixFeature) {
return /(^|[^A-Z\d])\[([A-Z][A-Z\d]+-[1-9]\d*)\]/giu;
}

return /(^|[^\p{L}\p{Nd}])\[([\p{L}][\p{L}\p{Nd}_]{1,255}-\p{Nd}{1,255})\]/giu;
};

/**
* Parses strings for Jira issue keys for commit messages,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Meanwhile these issues shouldn't have links:
- [A11B22-2020]
- [A11B2C33-2021]

And anything not enclosed inside square brackets won't have any links added.
- KEY-2018
- TEST1-2021

[KEY-2018]: http://example.com/browse/KEY-2018
[A1-2019]: http://example.com/browse/A1-2019
[A1B2-2020]: http://example.com/browse/A1B2-2020
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ These issues must have links:
Meanwhile these issues shouldn't have links:
- [A11-2019]
- [A11B22-2020]
- [A11B2C33-2021]
- [A11B2C33-2021]

And anything not enclosed inside square brackets won't have any links added.
- KEY-2018
- TEST1-2021

0 comments on commit e4f8a1d

Please sign in to comment.