From ccf19d078860f455ff64565682a86c8754874d23 Mon Sep 17 00:00:00 2001 From: ghe Date: Fri, 29 May 2020 22:05:08 +0100 Subject: [PATCH] feat: improve readme & add netrypoint --- README.md | 39 ++++++++++++++++++++++++++++++++++---- src/index.ts | 13 +++++++++++++ src/lib/get-import-path.ts | 9 +++++++++ tsconfig.json | 2 +- 4 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 src/index.ts create mode 100644 src/lib/get-import-path.ts diff --git a/README.md b/README.md index 8a75c1e7..6f03f525 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,41 @@ You will need to set the following environment variable: Run the tests with `npm test` -### Running the script -- `SNYK_HOST` +## To kick off an import +This script is intended to help import projects into Snyk with a controlled pace via API. The script will kick off an import in batches, wait for completion and then keep going. Failure logs will be generated at `SNYK_LOG_PATH` directory. +1. Create the `import-projects.json` file: +`orgId` - Can be found in https://app.snyk.io/org/YOUR_ORG/manage/settings +`integrationId` - Can be found in Integrations menu for each SCM https://app.snyk.io/org/YOUR_ORG/manage/settings + + +``` +{ + "targets": [ + { + "orgId": "******, + "integrationId": "******", + "target": { + "name": "shallow-goof-policy", + "owner": "snyk-fixtures", + "branch": "master" // optional! + } + }, + { + "orgId": "******, + "integrationId": "******", + "target": { + "name": "shallow-goof-policy", + "owner": "snyk-fixtures", + "branch": "master" + } + }, + ] +} +``` +2. Set the env vars mentioned: - `SNYK_API_TOKEN` - your [Snyk api token](https://app.snyk.io/account) - `SNYK_LOG_PATH` - the path to folder where all logs should be saved -- `CONCURRENT_IMPORTS` (optional) defaults to 5 which is the recommended amount to import at once as a max. - +- `CONCURRENT_IMPORTS` (optional) defaults to 5 repos at a time, which is the recommended amount to import at once as a max. Just 1 repo may have many projects inside. (10 may also be okay if all repos are small) +- `SNYK_HOST` (optional) defaults to `https://snyk.io` +3. `npm run build` and then `DEBUG=snyk* node dist/index.js` diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 00000000..acc6eaa0 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,13 @@ +import * as debugLib from 'debug'; +const debug = debugLib('snyk:import-projects-script'); + +export * from './lib'; +import { ImportProjects } from './scripts/import-projects'; +import { getImportProjectsFile } from './lib/get-import-path'; + +try { + const importFile = getImportProjectsFile(); + ImportProjects(importFile); +} catch (e) { + debug('Failed to kick off import.\n' + e); +} diff --git a/src/lib/get-import-path.ts b/src/lib/get-import-path.ts new file mode 100644 index 00000000..52121a17 --- /dev/null +++ b/src/lib/get-import-path.ts @@ -0,0 +1,9 @@ +export function getImportProjectsFile(): string { + const snykImportPath = process.env.SNYK_LOG_PATH; + if (!snykImportPath) { + throw new Error( + `Please set the SNYK_IMPORT_PATH e.g. export SNYK_IMPORT_PATH='~/my/path/to/file'`, + ); + } + return snykImportPath; +} diff --git a/tsconfig.json b/tsconfig.json index d5fa7d07..5462fe02 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,5 +9,5 @@ "importHelpers": true, "strict": true }, - "include": ["./src/lib/**/*"] + "include": ["./src/**/**/*"] }