Skip to content

Commit

Permalink
refacto before release
Browse files Browse the repository at this point in the history
  • Loading branch information
sbierbaumRDR committed Jan 18, 2024
1 parent 2a4ca68 commit 9197075
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 47 deletions.
40 changes: 30 additions & 10 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
*/

import * as core from '@actions/core'
import * as main from '../src/main'
import { VulnerabilitiesTransformer } from '../src/vulnerabilitiesTransformer'
import { GithubissueCreator } from '../src/github/githubissueCreator'
import { GithubissueLister } from '../src/github/githubissueLister'
import { Issue } from '../src/types/issue'

// Mock the GitHub Actions core library
let getInputMock: jest.SpyInstance
Expand Down Expand Up @@ -63,14 +67,30 @@ describe('action', () => {
expect(uniqueVulnerabilities.vulnerabilities.length).toBe(6)
})

// it('should ', async () => {
// const failedReports =
// vulnerabilitiesTransformer.getFailedReports(vulnerabilities)
// const uniqueVulnerabilities =
// vulnerabilitiesTransformer.removeAllDuplicateVulnerabilities(
// failedReports
// )[0]
//
//
// })
it('should init a githubIssueCreator', async () => {
const githubissueCreator = main.initIssueCreator()

expect(githubissueCreator).toBeInstanceOf(GithubissueCreator)
})

it('should init a githubIssueLister', async () => {
const githubissueLister = main.initIssueLister()

expect(githubissueLister).toBeInstanceOf(GithubissueLister)
})

it('should extract ids from a list of issues from github', async () => {
const listIssues: Issue[] = [
{ title: '5.3 - Improper Input Validation [SNYK-JS-POSTCSS-5926692]' },
{
title:
'7.5 - Regular Expression Denial of Service (ReDoS) [SNYK-JS-NTHCHECK-1586032]'
}
]

const extractedIds = main.extractIssuesSnykIds(listIssues)

expect(extractedIds.length).toBe(2)
expect(extractedIds[0]).toEqual('SNYK-JS-POSTCSS-5926692')
})
})
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 21 additions & 14 deletions dist/index.js

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

50 changes: 28 additions & 22 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GithubissueLister } from './github/githubissueLister'
import { SnykReport } from './types/snykReport'
import { Issue } from './types/issue'

function initIssueCreator(): GithubissueCreator {
export function initIssueCreator(): GithubissueCreator {
const octokit = new Octokit({
auth: core.getInput('gh-token')
})
Expand All @@ -18,7 +18,7 @@ function initIssueCreator(): GithubissueCreator {
return new GithubissueCreator(octokit, owner, repository, assignee)
}

function initIssueLister(): GithubissueLister {
export function initIssueLister(): GithubissueLister {
const octokit = new Octokit({
auth: core.getInput('gh-token')
})
Expand All @@ -29,7 +29,7 @@ function initIssueLister(): GithubissueLister {
return new GithubissueLister(octokit, owner, repository)
}

function extractVulnerabilitiesReport(): SnykReport[] {
export function extractVulnerabilitiesReport(): SnykReport[] {
const vulnerabilitiesTransformer = new VulnerabilitiesTransformer()

const vulnerabilities =
Expand All @@ -42,7 +42,7 @@ function extractVulnerabilitiesReport(): SnykReport[] {
return uniqueVulnerabilitiesByReport
}

function extractIssuesSnykIds(listIssues: Issue[]): string[] {
export function extractIssuesSnykIds(listIssues: Issue[]): string[] {
const listIssuesIds = listIssues.map(issue => {
//get text beetween [ & ]
const match = issue.title.match(/\[(.*?)]/)
Expand All @@ -52,31 +52,37 @@ function extractIssuesSnykIds(listIssues: Issue[]): string[] {
return listIssuesIds
}

async function createGitHubIssuesForReports(
vulnerabilitiesReport: SnykReport[]
): Promise<void> {
const issueCreator = initIssueCreator()
const issueLister = initIssueLister()

const listIssues = await issueLister.getListIssues()
if (listIssues !== undefined) {
const listIssuesTitle = extractIssuesSnykIds(listIssues)

for (const report of vulnerabilitiesReport) {
for (const vulnerability of report.vulnerabilities) {
if (!listIssuesTitle.includes(vulnerability.id)) {
await issueCreator.createIssue(
`${vulnerability.cvssScore} - ${vulnerability.title} [${vulnerability.id}]`,
vulnerability.description
)
}
}
}
}
}

/**
* The main function for the action.
* @returns {Promise<void>} Resolves when the action is complete.
*/
export async function run(): Promise<void> {
try {
const vulnerabilitiesReport = extractVulnerabilitiesReport()
const issueCreator = initIssueCreator()
const issueLister = initIssueLister()

const listIssues = await issueLister.getListIssues()
if (listIssues !== undefined) {
const listIssuesTitle = extractIssuesSnykIds(listIssues)

for (const report of vulnerabilitiesReport) {
for (const vulnerability of report.vulnerabilities) {
if (!listIssuesTitle.includes(vulnerability.id)) {
await issueCreator.createIssue(
`${vulnerability.cvssScore} - ${vulnerability.title} [${vulnerability.id}]`,
vulnerability.description
)
}
}
}
}
await createGitHubIssuesForReports(vulnerabilitiesReport)
} catch (error) {
// Fail the workflow run if an error occurs
console.log('error', error)
Expand Down

0 comments on commit 9197075

Please sign in to comment.