-
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.
feat: skip previously imported targets
- Loading branch information
Showing
13 changed files
with
191 additions
and
27 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,57 @@ | ||
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'; | ||
|
||
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`); | ||
//TODO: validation? | ||
const pollingUrls = await importTargets(targets); | ||
const filteredTargets = await filterOutImportedTargets(targets, loggingPath); | ||
const pollingUrls = await importTargets(filteredTargets, loggingPath); | ||
const projects = 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,10 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Import projects script Skips any badly formatted targets 1`] = `"f0125d9b-271a-4b50-ad23-80e12575a1bf:c4de291b-e083-4c43-a72c-113463e0d268:shallow-goof-policy:snyk-fixtures:master:https://dev.snyk.io/api/v1/org/f0125d9b-271a-4b50-ad23-80e12575a1bf/integrations/c4de291b-e083-4c43-a72c-113463e0d268/import/63f06690-1485-40cf-b81f-218104cfb277,f0125d9b-271a-4b50-ad23-80e12575a1bf:c4de291b-e083-4c43-a72c-113463e0d268:ruby-with-versions:snyk-fixtures:master:https://dev.snyk.io/api/v1/org/f0125d9b-271a-4b50-ad23-80e12575a1bf/integrations/c4de291b-e083-4c43-a72c-113463e0d268/import/63f06690-1485-40cf-b81f-218104cfb277,f0125d9b-271a-4b50-ad23-80e12575a1bf:c4de291b-e083-4c43-a72c-113463e0d268:composer-with-vulns:snyk-fixtures:master:https://dev.snyk.io/api/v1/org/f0125d9b-271a-4b50-ad23-80e12575a1bf/integrations/c4de291b-e083-4c43-a72c-113463e0d268/import/63f06690-1485-40cf-b81f-218104cfb277,f0125d9b-271a-4b50-ad23-80e12575a1bf:c4de291b-e083-4c43-a72c-113463e0d268:shallow-goof-policy:snyk-fixtures:master:https://dev.snyk.io/api/v1/org/f0125d9b-271a-4b50-ad23-80e12575a1bf/integrations/c4de291b-e083-4c43-a72c-113463e0d268/import/8ba73ea5-e1ff-4513-ad5a-8931ba3bb4d1,"`; | ||
|
||
exports[`Import projects script succeeds to import targets from file 1`] = `"f0125d9b-271a-4b50-ad23-80e12575a1bf:c4de291b-e083-4c43-a72c-113463e0d268:ruby-with-versions:snyk-fixtures:master,f0125d9b-271a-4b50-ad23-80e12575a1bf:c4de291b-e083-4c43-a72c-113463e0d268:composer-with-vulns:snyk-fixtures:master,f0125d9b-271a-4b50-ad23-80e12575a1bf:c4de291b-e083-4c43-a72c-113463e0d268:shallow-goof-policy:snyk-fixtures:master,"`; | ||
|
||
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, | ||
" | ||
`; |
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