-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
221 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> | ||
<meta http-equiv="Content-Language" content="en-us"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<title>Snyk Licenses Report</title> | ||
<link rel="icon" type="image/png" href="https://res.cloudinary.com/snyk/image/upload/v1468845142/favicon/favicon.png" | ||
sizes="194x194"> | ||
<link rel="shortcut icon" href="https://res.cloudinary.com/snyk/image/upload/v1468845142/favicon/favicon.ico"> | ||
{{!-- {{> inline-css }} --}} | ||
</head> | ||
|
||
<head> | ||
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> | ||
<meta http-equiv="Content-Language" content="en-us"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<title>Snyk Licenses Report</title> | ||
<link rel="icon" type="image/png" href="https://res.cloudinary.com/snyk/image/upload/v1468845142/favicon/favicon.png" | ||
sizes="194x194"> | ||
<link rel="shortcut icon" href="https://res.cloudinary.com/snyk/image/upload/v1468845142/favicon/favicon.ico"> | ||
{{> inline-css }} | ||
</head> | ||
<body> | ||
<main class="layout-stacked"> | ||
<h1>Hello!</h1> | ||
</main> | ||
</body> | ||
</html> | ||
|
||
<body class="test-remediation-section-projects"> | ||
<main class="layout-stacked"> | ||
{{data}} | ||
</main> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { gte } from 'semver'; | ||
import * as pathLib from 'path'; | ||
import * as debugLib from 'debug'; | ||
import { existsSync, mkdirSync, createWriteStream } from 'fs'; | ||
export const MIN_VERSION_FOR_MKDIR_RECURSIVE = '10.12.0'; | ||
|
||
const debug = debugLib('snyk-licenses:writeContentsToFile'); | ||
|
||
function writeContentsToFileSwallowingErrors( | ||
outputFile: string, | ||
contents: string, | ||
) { | ||
try { | ||
const ws = createWriteStream(outputFile, { flags: 'w' }); | ||
ws.on('error', (err) => { | ||
console.error(err); | ||
}); | ||
ws.write(contents); | ||
ws.end('\n'); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
|
||
export function writeContentsToFile(contents: string, outputFile: string) { | ||
if (!outputFile) { | ||
return; | ||
} | ||
|
||
if (outputFile.constructor.name !== String.name) { | ||
console.error('--json-output-file should be a filename path'); | ||
return; | ||
} | ||
|
||
// create the directory if it doesn't exist | ||
const dirPath = pathLib.dirname(outputFile); | ||
const createDirSuccess = createDirectory(dirPath); | ||
if (createDirSuccess) { | ||
writeContentsToFileSwallowingErrors(outputFile, contents); | ||
} | ||
} | ||
|
||
function createDirectory(newDirectoryFullPath: string): boolean { | ||
// if the path already exists, true | ||
// if we successfully create the directory, return true | ||
// if we can't successfully create the directory, either because node < 10 and recursive or some other failure, catch the error and return false | ||
|
||
if (existsSync(newDirectoryFullPath)) { | ||
return true; | ||
} | ||
|
||
const nodeVersion = process.version; | ||
|
||
try { | ||
if (gte(nodeVersion, MIN_VERSION_FOR_MKDIR_RECURSIVE)) { | ||
// nodeVersion is >= 10.12.0 - required for mkdirsync recursive | ||
const options: any = { recursive: true }; // TODO: remove this after we drop support for node v8 | ||
mkdirSync(newDirectoryFullPath, options); | ||
return true; | ||
} else { | ||
// nodeVersion is < 10.12.0 | ||
mkdirSync(newDirectoryFullPath); | ||
return true; | ||
} | ||
} catch (err) { | ||
debug(`Could not create directory ${newDirectoryFullPath}: ${err}`); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Generate HTML report License HTML Report is generated as expected 1`] = ` | ||
"<!DOCTYPE html> | ||
<html lang=\\"en\\"> | ||
<head> | ||
<meta http-equiv=\\"Content-type\\" content=\\"text/html; charset=utf-8\\"> | ||
<meta http-equiv=\\"Content-Language\\" content=\\"en-us\\"> | ||
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\"> | ||
<meta http-equiv=\\"X-UA-Compatible\\" content=\\"IE=edge\\"> | ||
<title>Snyk Licenses Report</title> | ||
<link rel=\\"icon\\" type=\\"image/png\\" href=\\"https://res.cloudinary.com/snyk/image/upload/v1468845142/favicon/favicon.png\\" | ||
sizes=\\"194x194\\"> | ||
<link rel=\\"shortcut icon\\" href=\\"https://res.cloudinary.com/snyk/image/upload/v1468845142/favicon/favicon.ico\\"> | ||
</head> | ||
<body> | ||
<main class=\\"layout-stacked\\"> | ||
<h1>Hello!</h1> | ||
</main> | ||
</body> | ||
</html> | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { | ||
generateHtmlReport, | ||
} from '../../src/lib/generate-output'; | ||
import { generateLicenseData } from '../../src/lib/generate-org-license-report'; | ||
describe('Generate HTML report', () => { | ||
const OLD_ENV = process.env; | ||
process.env.SNYK_TOKEN = process.env.SNYK_TEST_TOKEN; | ||
const ORG_ID = process.env.TEST_ORG_ID as string; | ||
|
||
afterAll(async () => { | ||
process.env = { ...OLD_ENV }; | ||
}); | ||
test('SNYK_TOKEN & ORG_ID are set', async () => { | ||
expect(process.env.SNYK_TOKEN).not.toBeNull(); | ||
expect(process.env.ORG_ID).not.toBeNull(); | ||
}); | ||
test('License HTML Report is generated as expected', async () => { | ||
const licenseRes = await generateLicenseData(ORG_ID, {}); | ||
const htmlData = await generateHtmlReport(licenseRes); | ||
expect(htmlData).toMatchSnapshot(); | ||
}, 50000); | ||
|
||
test.todo('Test for when API fails aka bad org id provided'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters