Skip to content

Commit

Permalink
TODO: choose a json2csv library
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkhala committed Jan 12, 2025
1 parent 655e3f0 commit eaf4ee9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
3 changes: 2 additions & 1 deletion nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
},
"devDependencies": {
"@davidkhala/logger": "latest",
"mocha": "latest"
"mocha": "latest",
"@davidkhala/light": "latest"
},
"scripts": {
"test": "mocha test"
Expand Down
44 changes: 42 additions & 2 deletions nodejs/security.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import API from "./rest.js";
import {html} from "mocha/lib/reporters/index.js";

export class CodeScan extends API {
async listForOrg(org) {
Expand All @@ -8,11 +9,50 @@ export class CodeScan extends API {
return data
}

async listForRepo(org, repo) {
static prettyRule({rule}) {
delete rule.id // mostly equivalent to .name
delete rule.description // just a short form of full_description
}

static as_report({
number, html_url,
created_at, updated_at,
state,
fixed_at, dismissed_by, dismissed_at,
rule, most_recent_instance
}) {
const {security_severity_level, tags, full_description, name} = rule // the dimensional table
const cweTag = tags.find(tag => tag.startsWith('external/cwe/cwe-'))
const cwe = cweTag ? cweTag.substring(17) : undefined

const {ref, location, commit_sha} = most_recent_instance

return {
severity_level: security_severity_level, cwe,
name, number, url: html_url, description: full_description,
created_at, updated_at, fixed_at,
dismissed_by, dismissed_at,
state, ref, commit: commit_sha, location: JSON.stringify(location)
}
}


static pretty(item) {
delete item.url // For internal usage only
delete item.instances_url // For internal usage only
CodeScan.prettyRule(item)
delete item.tool // assuming { name: 'CodeQL', guid: null, version: '2.20.0' }
return item
}

async listForRepo(org, repo, pretty) {
const {data} = await this.client.codeScanning.listAlertsForRepo({
repo,
owner: org
owner: org,
})
if (pretty) {
return data.map(CodeScan.pretty)
}
return data
}
}
Expand Down
1 change: 1 addition & 0 deletions nodejs/test/artifacts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.json
6 changes: 4 additions & 2 deletions nodejs/test/security.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {CodeScan, Dependabot, SecretScan} from "../security.js";
import assert from "assert";
import {JSONReadable} from '@davidkhala/light/format.js'
import fs from "fs";
const pat = process.env.GITHUB_TOKEN
describe('codeScan', function () {
this.timeout(0)
Expand All @@ -8,8 +10,8 @@ describe('codeScan', function () {

const org = 'davidkhala'
const repo = 'ci-cd-utils'
const data = await api.listForRepo(org, repo)
console.debug(data)
const data = await api.listForRepo(org, repo, true)
fs.writeFileSync('test/artifacts/codeScan.json', JSONReadable(data))
await assert.rejects(async()=>{
await api.listForRepo(undefined, `${org}/${repo}`)
}, 'HttpError: Not Found - https://docs.github.com/rest')
Expand Down

0 comments on commit eaf4ee9

Please sign in to comment.