From 5901883c80da541e9e9dad90152de151179b90d3 Mon Sep 17 00:00:00 2001 From: Maksim Gruzdev Date: Mon, 23 Oct 2023 21:38:43 +0300 Subject: [PATCH] use old /changes endpoint instead of /diffs --- sarif2gl.js | 63 +++++++++++++------ .../rp.gitlab.merge_requests.42.changes.json | 37 +++++++++++ ...tlab.merge_requests.42.changes.parsed.json | 1 + tests/sarif2gl.test.js | 14 +++++ 4 files changed, 95 insertions(+), 20 deletions(-) create mode 100644 tests/data/rp.gitlab.merge_requests.42.changes.json create mode 100644 tests/data/rp.gitlab.merge_requests.42.changes.parsed.json diff --git a/sarif2gl.js b/sarif2gl.js index b4e17d8..05e004e 100644 --- a/sarif2gl.js +++ b/sarif2gl.js @@ -1,4 +1,4 @@ -#! /usr/bin/env node +#! /usr/bin/env node const fs = require ('fs') @@ -118,6 +118,18 @@ const parse = (sarif) => { return result } +const parse_diff = async (rp) => { + + let idx = {} + + for (let i of rp.changes || []) { + if (i.deleted_file) continue + idx [i.new_path] = i + } + + return {} +} + const find_note = (discussions, sign) => { for (let discussion of discussions || []) { @@ -134,7 +146,34 @@ const find_note = (discussions, sign) => { return {} } -const post2gl = async (note) => { +const main = async () => { + + let todo = [] + + for (let f of sarif_files) { + + let s = JSON.parse (fs.readFileSync (f, 'utf8')) + + let t = parse (s) + + todo = todo.concat (t) + + } + + + let url_diff = `merge_requests/${CI_MERGE_REQUEST_IID}/changes` + + let diffs = await gitlab_rq ({body: '', url_diff, method: 'GET'}) + + let idx = await parse_diff (diffs) + + todo = todo.filter (t => idx [t.src]) + + + let note = to_note (todo) + + console.log (note) + let url = `merge_requests/${CI_MERGE_REQUEST_IID}/discussions` @@ -208,25 +247,9 @@ const to_note = (todo) => { return lines.join ("\n") } -let todo = [] - -for (let f of sarif_files) { - - let s = JSON.parse (fs.readFileSync (f, 'utf8')) - - let t = parse (s) - - todo = todo.concat (t) - -} - -let note = to_note (todo) - -console.log (note) - - module.exports = { parse, + parse_diff, } if (!CI_MERGE_REQUEST_IID) { @@ -236,4 +259,4 @@ if (!CI_MERGE_REQUEST_IID) { return } -post2gl (note) +main () diff --git a/tests/data/rp.gitlab.merge_requests.42.changes.json b/tests/data/rp.gitlab.merge_requests.42.changes.json new file mode 100644 index 0000000..4ea7efe --- /dev/null +++ b/tests/data/rp.gitlab.merge_requests.42.changes.json @@ -0,0 +1,37 @@ +{ + "id": 4242, + "iid": 42, + "changes": [ + { + "old_path": "back/lib/Content/reports.js", + "new_path": "back/lib/Content/reports.js", + "a_mode": "100644", + "b_mode": "100644", + "new_file": false, + "renamed_file": false, + "deleted_file": false, + "diff": "@@ -24,6 +24,8 @@\n \"feature\": false,\n \"Path\": \"/var/feature\"\n \n" + }, + { + "old_path": "back/conf/old.conf.template", + "new_path": "", + "a_mode": "0", + "b_mode": "100644", + "new_file": true, + "renamed_file": false, + "deleted_file": false, + "diff": "" + }, + { + "old_path": "back/conf/old.conf.template", + "new_path": "back/conf/old.conf.template", + "a_mode": "0", + "b_mode": "100644", + "new_file": false, + "renamed_file": false, + "deleted_file": true, + "diff": "@@ -1,48 +0,0 @@\n-{}\n" + } + ], + "overflow": false +} \ No newline at end of file diff --git a/tests/data/rp.gitlab.merge_requests.42.changes.parsed.json b/tests/data/rp.gitlab.merge_requests.42.changes.parsed.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/tests/data/rp.gitlab.merge_requests.42.changes.parsed.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tests/sarif2gl.test.js b/tests/sarif2gl.test.js index 2d82e58..809f5f6 100755 --- a/tests/sarif2gl.test.js +++ b/tests/sarif2gl.test.js @@ -25,3 +25,17 @@ describe('parse', () => { assert.deepStrictEqual(sarif2gl.parse (inp), out) }) }) + + +describe('filter_findings', async () => { + + it ('should load MR diff', async (t) => { + const inp_rp = load_file('./tests/data/rp.gitlab.merge_requests.42.changes.json') + + const inp = await sarif2gl.parse_diff (inp_rp) + const out = load_file('./tests/data/rp.gitlab.merge_requests.42.changes.parsed.json') + + assert.deepStrictEqual(inp, out) + }) + +}) \ No newline at end of file