Skip to content

Commit

Permalink
use old /changes endpoint instead of /diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
jonny64 committed Oct 23, 2023
1 parent b6a91d4 commit 5901883
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 20 deletions.
63 changes: 43 additions & 20 deletions sarif2gl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /usr/bin/env node
#! /usr/bin/env node

const fs = require ('fs')

Expand Down Expand Up @@ -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 || []) {
Expand All @@ -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`

Expand Down Expand Up @@ -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) {
Expand All @@ -236,4 +259,4 @@ if (!CI_MERGE_REQUEST_IID) {
return
}

post2gl (note)
main ()
37 changes: 37 additions & 0 deletions tests/data/rp.gitlab.merge_requests.42.changes.json
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions tests/data/rp.gitlab.merge_requests.42.changes.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
14 changes: 14 additions & 0 deletions tests/sarif2gl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

})

0 comments on commit 5901883

Please sign in to comment.