Skip to content

Commit

Permalink
remove package with vulnerability and add new logic to handle checkin…
Browse files Browse the repository at this point in the history
…g for issue keys in reference links (atlassian#422)

Co-authored-by: Rachelle Rathbone <[email protected]>
  • Loading branch information
rachellerathbone and rachellerathbone authored Apr 12, 2021
1 parent e566ff7 commit 9624e75
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 29 deletions.
36 changes: 28 additions & 8 deletions lib/jira/util/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
const { markdown } = require('markdown');

module.exports = function (jiraClient) {
const containsReferenceLink = (line) => {
// reference text links should have 2 parts only
if (line.split(' ').length === 2) {
const hasSquareBrackets = line.charAt(0) === '[' && line.includes(']:');
const hasUrl = line.includes('http://') || line.includes('https://');

return hasSquareBrackets && hasUrl;
}

return false;
};

// Parse our existing issue text, pulling out any existing reference links.
// if reference links exist, returns array of issue keys. For example, the following
// reference links would return [ 'TEST-2019', 'TEST-2020' ]
// [TEST-2019]: http://example.com/browse/TEST-2019
// [TEST-2020]: https://example.com/browse/TEST-2020
// if no issue keys exist, return []
const checkForReferenceText = (text) => {
const splitTextByNewLine = text.split('\n');

return splitTextByNewLine
.filter((line) => containsReferenceLink(line))
.map((referenceLink) => referenceLink.slice(1, referenceLink.indexOf(']')));
};

function addJiraIssueLinks(text, issues) {
const referenceRegex = /\[([A-Z]+-[0-9]+)\](?!\()/g;
const issueMap = issues.reduce((issueMap, issue) => ({
Expand All @@ -9,10 +33,7 @@ module.exports = function (jiraClient) {
}), {});

const links = [];

// Attempt to parse our existing issue text, pulling out any existing reference links.
const [, { references }] = markdown.parse(text);
const keys = references ? Object.keys(references) : [];
const keys = checkForReferenceText(text);

// Parse the text up to a maximum amount of characters.
while (referenceRegex.lastIndex < 1000) {
Expand All @@ -23,9 +44,8 @@ module.exports = function (jiraClient) {
}

const [, key] = match;

// If we already have a reference link, or the issue is not valid, skip it.
if (keys.includes(key.toLowerCase()) || !issueMap[key]) {
if (keys.includes(key) || !issueMap[key]) {
continue;
}

Expand Down
18 changes: 0 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"helmet": "^3.21.2",
"hot-shots": "^7.4.1",
"ioredis": "^4.16.3",
"markdown": "^0.5.0",
"moment": "^2.24.0",
"moo": "^0.5.0",
"newrelic": "^6.2.0",
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/text/find-existing-references.rendered.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#### [TEST-2019]

[TEST-2019]: https://example.com/browse/TEST-2019 This issue is related to [TEST-2020] and [TEST-2021] and this is [TEST-2020]: https://example.com/browse/TEST-2020 is randomly placed in the text.

[TEST-2019]: http://example.com/browse/TEST-2019
[TEST-2020]: https://example.com/browse/TEST-2020

[TEST-2021]: http://example.com/browse/TEST-2021
6 changes: 6 additions & 0 deletions test/fixtures/text/find-existing-references.source.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#### [TEST-2019]

[TEST-2019]: https://example.com/browse/TEST-2019 This issue is related to [TEST-2020] and [TEST-2021] and this is [TEST-2020]: https://example.com/browse/TEST-2020 is randomly placed in the text.

[TEST-2019]: http://example.com/browse/TEST-2019
[TEST-2020]: https://example.com/browse/TEST-2020
31 changes: 29 additions & 2 deletions test/unit/jira/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Jira util', () => {
util = getJiraUtil(jiraClient);
});

test('it should handle multiple Jira references appropriately', () => {
it('it should handle multiple Jira references appropriately', () => {
const { source, rendered } = loadFixture('multiple-links');
const issues = [
{
Expand Down Expand Up @@ -76,7 +76,6 @@ describe('Jira util', () => {
},
},
];

const result = util.addJiraIssueLinks(source, issues);
expect(result).toBe(rendered);
});
Expand Down Expand Up @@ -104,6 +103,34 @@ describe('Jira util', () => {
const result = util.addJiraIssueLinks(source, issues);
expect(result).toBe(rendered);
});

it('should only pull issue keys from reference links', () => {
const { source, rendered } = loadFixture('find-existing-references');
const issues = [
{
key: 'TEST-2019',
fields: {
summary: 'First Issue',
},
},
{
key: 'TEST-2020',
fields: {
summary: 'Second Issue',
},
},
{
key: 'TEST-2021',
fields: {
summary: 'Third Issue',
},
},
];

const result = util.addJiraIssueLinks(source, issues);

expect(result).toBe(rendered);
});
});

describe('#getJiraId', () => {
Expand Down

0 comments on commit 9624e75

Please sign in to comment.