diff --git a/src/cmds/generate.ts b/src/cmds/generate.ts index ab99c5d..daa5fd2 100644 --- a/src/cmds/generate.ts +++ b/src/cmds/generate.ts @@ -76,6 +76,7 @@ export async function handler(argv: { }), ); getApiToken(); + const orgData = await getOrgData(orgPublicId); const options = { filters: { projects: _.castArray(project), @@ -85,7 +86,6 @@ export async function handler(argv: { orgPublicId, options, ); - const orgData = await getOrgData(orgPublicId); const reportData = await generateHtmlReport( orgPublicId, licenseData, @@ -103,6 +103,6 @@ export async function handler(argv: { `${outputFormat.toUpperCase()} license report saved at: ${fileName}`, ); } catch (e) { - console.error(e); + console.error(e.message || e); } } diff --git a/src/lib/get-org-data.ts b/src/lib/get-org-data.ts index cea1faf..bdeb42c 100644 --- a/src/lib/get-org-data.ts +++ b/src/lib/get-org-data.ts @@ -24,7 +24,12 @@ export async function getOrgData(orgPublicId: string): Promise { try { const snykApiClient = await new snykApiSdk.Orgs(); const allOrgs: OrgData[] = _.get(await snykApiClient.get(), 'orgs', []); - const orgData = allOrgs.filter(org => org.id === orgPublicId)[0]; + const orgData = allOrgs.filter((org) => org.id === orgPublicId)[0]; + if (_.isEmpty(orgData)) { + throw new Error( + `No organization data found for provided ID: ${orgPublicId}.\nPlease check that the --orgPublicId is the correct public ID found in Organization Settings page on https://app.snyk.io/org//manage/settings`, + ); + } return orgData; } catch (e) { debug('❌ Failed to get org data for org ID:' + orgPublicId); diff --git a/test/system/__snapshots__/generate.test.ts.snap b/test/system/__snapshots__/generate.test.ts.snap index b6af730..b3adf39 100644 --- a/test/system/__snapshots__/generate.test.ts.snap +++ b/test/system/__snapshots__/generate.test.ts.snap @@ -1,5 +1,10 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`\`snyk-licenses-report generate <...>\` Shows error when invalid --orgPublicId 1`] = ` +"No organization data found for provided ID: my-org. +Please check that the --orgPublicId is the correct public ID found in Organization Settings page on https://app.snyk.io/org//manage/settings" +`; + exports[`\`snyk-licenses-report generate <...>\` Shows error when missing --orgPublicId 1`] = ` "Command failed: node ./dist/index.js generate index.js generate diff --git a/test/system/generate.test.ts b/test/system/generate.test.ts index 8604085..f84ff86 100644 --- a/test/system/generate.test.ts +++ b/test/system/generate.test.ts @@ -22,6 +22,24 @@ describe('`snyk-licenses-report generate <...>`', () => { ); }); + it('Shows error when invalid --orgPublicId', async (done) => { + exec( + `node ${main} generate --orgPublicId=my-org`, + { + env: { + PATH: process.env.PATH, + SNYK_TOKEN: process.env.SNYK_TEST_TOKEN, + }, + }, + (err, stdout, stderr) => { + expect(stdout).toBe(''); + expect(err).toBeNull(); + expect(stderr.trim()).toMatchSnapshot(); + done(); + }, + ); + }); + it('generated the report successfully with default params', (done) => { exec( `node ${main} generate --orgPublicId=${ORG_ID}`, @@ -37,7 +55,7 @@ describe('`snyk-licenses-report generate <...>`', () => { done(); }, ); - }, 50000); + }, 80000); it.skip('generated the report successfully as PDF', (done) => { exec( @@ -54,7 +72,7 @@ describe('`snyk-licenses-report generate <...>`', () => { done(); }, ); - }, 50000); + }, 80000); it('generated the report successfully with custom template', (done) => { exec( `node ${main} generate --orgPublicId=${ORG_ID} --template=${__dirname + @@ -71,7 +89,7 @@ describe('`snyk-licenses-report generate <...>`', () => { done(); }, ); - }, 50000); + }, 80000); it.todo('API is down'); it.todo('Requested org has no licenses policy'); });