Skip to content

Commit

Permalink
- Added unique Aikido identifier check
Browse files Browse the repository at this point in the history
- Added deletion of Aikido comments if no longer in findings
  • Loading branch information
Brenden Cambier committed Apr 4, 2024
1 parent 3cbb4cd commit aa09476
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 12 deletions.
46 changes: 40 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,11 @@ async function run() {
const options = {};
//const findings = result.outcome?.findings
// TODO: replace MOCK
// Unique identifief for findings has temporarily been agreed on having a unique Aikido link within the body referencing the unique Aikido finding
const findings = [
{ commit_id: 'fc773d95213d1c1e35acaceac6e37b036abcd09e', path: 'dist/index.js', line: 117, body: 'Test 1 https://app.aikido.dev/featurebranch/scan/' },
{ commit_id: 'fc773d95213d1c1e35acaceac6e37b036abcd09e', path: 'dist/index.js', line: 120, body: 'Test 2 https://app.aikido.dev/featurebranch/scan/' },
{ commit_id: 'fc773d95213d1c1e35acaceac6e37b036abcd09e', path: 'dist/index.js', line: 124, body: 'Test 3 https://app.aikido.dev/featurebranch/scan/' }
{ commit_id: 'fc773d95213d1c1e35acaceac6e37b036abcd09e', path: 'dist/index.js', line: 117, body: 'Test 1 https://app.aikido.dev/finding/123/' },
{ commit_id: 'fc773d95213d1c1e35acaceac6e37b036abcd09e', path: 'dist/index.js', line: 120, body: 'Test 2 https://app.aikido.dev/finding/124/' },
{ commit_id: 'fc773d95213d1c1e35acaceac6e37b036abcd09e', path: 'dist/index.js', line: 124, body: 'Test 3 https://app.aikido.dev/finding/125/' }
];
await (0, postReviewComment_1.postFindingsAsReviewComments)(findings);
}
Expand Down Expand Up @@ -412,6 +413,14 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.postFindingsAsReviewComments = void 0;
const core = __importStar(__nccwpck_require__(2186));
const github = __importStar(__nccwpck_require__(5438));
const parseUniqueAikidoID = (body) => {
const regex = new RegExp('.*app\.aikido\.dev\/finding\/(.*)\/.*', 'i');
const match = regex.exec(body.toLowerCase());
if (match == null) {
return undefined;
}
return match[1];
};
const postFindingsAsReviewComments = async (findings) => {
var _a, _b;
const githubToken = core.getInput('github-token');
Expand All @@ -431,12 +440,37 @@ const postFindingsAsReviewComments = async (findings) => {
repo: context.repo.repo,
pull_number: pullRequestNumber
});
// Delete review comments that are not in current findings
for (const comment of reviewComments) {
const isBot = ((_a = comment.user) === null || _a === void 0 ? void 0 : _a.type) === 'Bot';
const existingCommentId = parseUniqueAikidoID(comment.body);
if (!isBot || existingCommentId === undefined)
continue;
let matchedFinding = undefined;
for (const finding of findings) {
const findingId = parseUniqueAikidoID(finding.body);
if (findingId != existingCommentId)
continue;
matchedFinding = finding;
}
if (typeof matchedFinding === 'undefined') {
await octokit.rest.pulls.deleteReviewComment({
...context.repo,
pull_number: pullRequestNumber,
comment_id: comment.id
});
}
}
// Add new review comments
for (const finding of findings) {
const findingId = parseUniqueAikidoID(finding.body);
if (findingId === undefined)
continue;
let existingFinding = undefined;
for (const comment of reviewComments) {
const isBot = ((_a = comment.user) === null || _a === void 0 ? void 0 : _a.type) === 'Bot';
const isAikidoScannerBot = (_b = comment.body) === null || _b === void 0 ? void 0 : _b.toLowerCase().includes('https://app.aikido.dev/featurebranch/scan/');
if (!isBot || !isAikidoScannerBot || comment.commit_id != finding.commit_id, comment.path != finding.path || comment.line != finding.line || comment.body != finding.body)
const isBot = ((_b = comment.user) === null || _b === void 0 ? void 0 : _b.type) === 'Bot';
const existingCommentId = parseUniqueAikidoID(comment.body);
if (!isBot || existingCommentId === undefined || findingId != existingCommentId)
continue;
existingFinding = comment;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ async function run(): Promise<void> {
const options = {};
//const findings = result.outcome?.findings
// TODO: replace MOCK
// Unique identifief for findings has temporarily been agreed on having a unique Aikido link within the body referencing the unique Aikido finding
const findings = [
{ commit_id: 'fc773d95213d1c1e35acaceac6e37b036abcd09e', path: 'dist/index.js', line: 117, body: 'Test 1 https://app.aikido.dev/featurebranch/scan/' },
{ commit_id: 'fc773d95213d1c1e35acaceac6e37b036abcd09e', path: 'dist/index.js', line: 120, body: 'Test 2 https://app.aikido.dev/featurebranch/scan/' },
{ commit_id: 'fc773d95213d1c1e35acaceac6e37b036abcd09e', path: 'dist/index.js', line: 124, body: 'Test 3 https://app.aikido.dev/featurebranch/scan/' }
{ commit_id: 'fc773d95213d1c1e35acaceac6e37b036abcd09e', path: 'dist/index.js', line: 117, body: 'Test 1 https://app.aikido.dev/finding/123/' },
{ commit_id: 'fc773d95213d1c1e35acaceac6e37b036abcd09e', path: 'dist/index.js', line: 120, body: 'Test 2 https://app.aikido.dev/finding/124/' },
{ commit_id: 'fc773d95213d1c1e35acaceac6e37b036abcd09e', path: 'dist/index.js', line: 124, body: 'Test 3 https://app.aikido.dev/finding/125/' }

]
await postFindingsAsReviewComments(findings);
Expand Down
43 changes: 41 additions & 2 deletions src/postReviewComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import * as github from '@actions/github';

type TFinding = { commit_id: string, path: string, line: number, body: string }

const parseUniqueAikidoID = (body: string): string | undefined => {
const regex = new RegExp('.*app\.aikido\.dev\/finding\/(.*)\/.*', 'i');
const match = regex.exec(body.toLowerCase())
if (match == null) {
return undefined
}
return match[1]
}

export const postFindingsAsReviewComments = async (findings: TFinding[]): Promise<void> => {
const githubToken = core.getInput('github-token');
if (!githubToken || githubToken === '') {
Expand All @@ -26,13 +35,43 @@ export const postFindingsAsReviewComments = async (findings: TFinding[]): Promis
pull_number: pullRequestNumber
});

// Delete review comments that are not in current findings
for (const comment of reviewComments) {
const isBot = comment.user?.type === 'Bot';
const existingCommentId = parseUniqueAikidoID(comment.body)

if (!isBot || existingCommentId === undefined) continue;

let matchedFinding = undefined
for (const finding of findings) {
const findingId = parseUniqueAikidoID(finding.body)

if (findingId != existingCommentId) continue;

matchedFinding = finding
}

if (typeof matchedFinding === 'undefined') {
await octokit.rest.pulls.deleteReviewComment({
...context.repo,
pull_number: pullRequestNumber,
comment_id: comment.id
});
}
}

// Add new review comments
for (const finding of findings) {
const findingId = parseUniqueAikidoID(finding.body)

if (findingId === undefined) continue;

let existingFinding = undefined
for (const comment of reviewComments) {
const isBot = comment.user?.type === 'Bot';
const isAikidoScannerBot = comment.body?.toLowerCase().includes('https://app.aikido.dev/featurebranch/scan/');
const existingCommentId = parseUniqueAikidoID(comment.body)

if (!isBot || !isAikidoScannerBot || comment.commit_id != finding.commit_id, comment.path != finding.path || comment.line != finding.line || comment.body != finding.body) continue;
if (!isBot || existingCommentId === undefined || findingId != existingCommentId) continue;

existingFinding = comment
}
Expand Down

0 comments on commit aa09476

Please sign in to comment.