-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from snyk-tech-services/feat/log-progress
Feat/log progress
- Loading branch information
Showing
14 changed files
with
224 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const IMPORT_LOG_NAME = 'imported-targets.log'; | ||
export const IMPORT_PROJECTS_FILE_NAME= 'import-projects.json'; |
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,8 @@ | ||
export function getConcurrentImportsNumber(): number { | ||
let importsNumber = parseInt(process.env.CONCURRENT_IMPORTS || '5'); | ||
if (importsNumber > 20) { | ||
// never more than 20! | ||
importsNumber = 20; | ||
} | ||
return importsNumber; | ||
} |
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,10 @@ | ||
export function getLoggingPath(): string { | ||
const snykLogPath = process.env.SNYK_LOG_PATH; | ||
if (!snykLogPath) { | ||
throw new Error( | ||
`Please set the SNYK_LOG_PATH e.g. export SNYK_LOG_PATH='~/my/path'`, | ||
); | ||
} | ||
// TODO: what if path is not real? | ||
return snykLogPath; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as fs from 'fs'; | ||
import { Target } from './lib/types'; | ||
import { getLoggingPath } from './lib/get-logging-path'; | ||
|
||
export async function logImportedTarget( | ||
orgId: string, | ||
integrationId: string, | ||
target: Target, | ||
locationUrl: string, | ||
loggingPath: string = getLoggingPath(), | ||
): Promise<void> { | ||
try { | ||
const log = `${orgId}:${integrationId}:${Object.values(target).join(':')},`; | ||
fs.appendFileSync(`${loggingPath}/imported-targets.log`, log); | ||
} catch (e) { | ||
// do nothing | ||
} | ||
} |
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,24 +1,73 @@ | ||
import * as debugLib from 'debug'; | ||
|
||
import * as path from 'path'; | ||
import { loadFile } from '../load-file'; | ||
import { importTargets, pollImportUrls } from '../lib'; | ||
import { Project } from '../lib/types'; | ||
import { Project, ImportTarget } from '../lib/types'; | ||
import { getLoggingPath } from '../lib/get-logging-path'; | ||
import { getConcurrentImportsNumber } from '../lib/get-concurrent-imports-number'; | ||
|
||
const debug = debugLib('snyk:import-projects-script'); | ||
|
||
const regexForTarget = (target: string): RegExp => | ||
new RegExp(`(,?)${target}.*,`, 'm'); | ||
|
||
async function filterOutImportedTargets( | ||
targets: ImportTarget[], | ||
loggingPath: string, | ||
): Promise<ImportTarget[]> { | ||
let logFile: string; | ||
const filterOutImportedTargets: ImportTarget[] = []; | ||
try { | ||
logFile = await loadFile(path.resolve(loggingPath, 'imported-targets.log')); | ||
} catch (e) { | ||
return targets; | ||
} | ||
targets.forEach((targetItem) => { | ||
const { orgId, integrationId, target } = targetItem; | ||
const data = `${orgId}:${integrationId}:${Object.values(target).join(':')}`; | ||
const targetRegExp = regexForTarget(data); | ||
const match = logFile.match(targetRegExp); | ||
if (!match) { | ||
filterOutImportedTargets.push(targetItem); | ||
} else { | ||
debug('Dropped previously imported target: ', JSON.stringify(targetItem)); | ||
} | ||
}); | ||
|
||
return filterOutImportedTargets; | ||
} | ||
|
||
export async function ImportProjects( | ||
fileName: string, | ||
loggingPath: string = getLoggingPath(), | ||
): Promise<Project[]> { | ||
const content = await loadFile(fileName); | ||
const targets = []; | ||
const targets: ImportTarget[] = []; | ||
try { | ||
targets.push(...JSON.parse(content).targets); | ||
} catch (e) { | ||
throw new Error(`Failed to parse targets from ${fileName}`); | ||
} | ||
debug(`Loaded ${targets.length} targets to import`); | ||
const concurrentTargets = getConcurrentImportsNumber(); | ||
const projects: Project[] = []; | ||
//TODO: validation? | ||
const pollingUrls = await importTargets(targets); | ||
const projects = await pollImportUrls(pollingUrls); | ||
const filteredTargets = await filterOutImportedTargets(targets, loggingPath); | ||
if (filteredTargets.length === 0) { | ||
return []; | ||
} | ||
for ( | ||
let targetIndex = 0; | ||
targetIndex < filteredTargets.length; | ||
targetIndex = targetIndex + concurrentTargets | ||
) { | ||
const batch = filteredTargets.slice( | ||
targetIndex, | ||
targetIndex + concurrentTargets, | ||
); | ||
const pollingUrls = await importTargets(batch, loggingPath); | ||
projects.push(...(await pollImportUrls(pollingUrls))); | ||
} | ||
|
||
return projects; | ||
} |
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,16 @@ | ||
import * as fs from 'fs'; | ||
import * as debugLib from 'debug'; | ||
const debug = debugLib('snyk:write-file'); | ||
import { getLoggingPath } from './lib/get-logging-path'; | ||
|
||
export async function writeFile(name: string, content: JSON): Promise<void> { | ||
const ROOT_DIR = getLoggingPath(); | ||
const filename = `${ROOT_DIR}/${name}`; | ||
|
||
try { | ||
return await fs.writeFileSync(filename, JSON.stringify(content)); | ||
} catch (error) { | ||
debug(error); | ||
throw new Error(`Failed to write to file: ${filename}`); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Import skips previously imported succeeds to import targets from file 1`] = ` | ||
"f0125d9b-271a-4b50-ad23-80e12575a1bf:c4de291b-e083-4c43-a72c-113463e0d268:composer-with-vulns:snyk-fixtures:master, | ||
" | ||
`; |
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
13 changes: 13 additions & 0 deletions
13
test/scripts/fixtures/with-import-log/import-projects.json
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,13 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"orgId": "f0125d9b-271a-4b50-ad23-80e12575a1bf", | ||
"integrationId": "c4de291b-e083-4c43-a72c-113463e0d268", | ||
"target": { | ||
"name": "composer-with-vulns", | ||
"owner": "snyk-fixtures", | ||
"branch": "master" | ||
} | ||
} | ||
] | ||
} |
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 @@ | ||
f0125d9b-271a-4b50-ad23-80e12575a1bf:c4de291b-e083-4c43-a72c-113463e0d268:composer-with-vulns:snyk-fixtures:master, |
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