Skip to content

Commit

Permalink
Merge pull request #315 from snyk-tech-services/fix/exit-code-1-orgs-…
Browse files Browse the repository at this point in the history
…create

feat: exit code 1 if all orgs fail to create
  • Loading branch information
lili2311 authored Mar 23, 2022
2 parents 1492ece + 7c977fb commit 25e2e10
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 32 deletions.
7 changes: 4 additions & 3 deletions src/cmds/orgs:create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as debugLib from 'debug';
import * as yargs from 'yargs';
const debug = debugLib('snyk:orgs-create-script');

import { getLoggingPath } from '../lib';
Expand Down Expand Up @@ -46,9 +47,9 @@ export async function handler(argv: {

console.log(orgsMessage);
} catch (e) {
const errorMessage = `ERROR! Failed to create organizations.\nTry running with \`DEBUG=snyk* <command> for more info\`.\nERROR: ${e.message}`;
debug('Failed to create organizations.\n' + e);
console.error(
`${e}.\nTry running with \`DEBUG=snyk* <command> for more info\`.\nERROR: ${e}`,
);
console.error(errorMessage);
setTimeout(() => yargs.exit(1, new Error(errorMessage)), 3000);
}
}
1 change: 0 additions & 1 deletion src/loggers/log-failed-org.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export async function logFailedOrg(
},
],
});

try {
log.error({ origName, groupId, errorMessage }, 'Failed to create org');
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/create-orgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function createNewOrgs(
origName: name,
sourceOrgId,
});
logCreatedOrg(groupId, name, org, integrations, loggingPath);
await logCreatedOrg(groupId, name, org, integrations, loggingPath);
} catch (e) {
failed.push({ groupId, name, sourceOrgId });
const errorMessage = e.data ? e.data.message : e.message;
Expand Down
6 changes: 3 additions & 3 deletions test/lib/org.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Org notification settings', () => {
});
afterAll(async () => {
process.env = { ...OLD_ENV };
});
}, 1000);
it('Can change the notification settings for org', async () => {
const res = await setNotificationPreferences(
requestManager,
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('listProjects', () => {
});
afterAll(async () => {
process.env = { ...OLD_ENV };
});
}, 1000);
it('Lists projects in a given Org', async () => {
const res = await listProjects(requestManager, ORG_ID);
expect(res).toMatchObject({
Expand All @@ -82,5 +82,5 @@ describe('listProjects', () => {
name: expect.any(String),
branch: expect.any(String),
});
});
}, 5000);
});
45 changes: 21 additions & 24 deletions test/system/orgs:create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,25 @@ Options:
},
},
(err, stdout, stderr) => {
if (err) {
throw err;
}
expect(stderr).toMatch(
'All requested organizations failed to be created. Review the errors in',
);
expect(err).toBeNull();
expect(stdout).toEqual('');
const file = fs.readFileSync(
path.resolve(logPath, `abc.${FAILED_ORG_LOG_NAME}`),
'utf8',
expect(err!.message).toMatch(
'All requested organizations failed to be created. Review the errors in',
);
expect(file).toContain('Failed to create org');
deleteFiles([path.resolve(logPath, `abc.${FAILED_ORG_LOG_NAME}`)]);
expect(stdout).toEqual('');
},
).on('exit', (code) => {
expect(code).toEqual(0);
expect(code).toEqual(1);
const file = fs.readFileSync(
path.join(logPath, `abc.${FAILED_ORG_LOG_NAME}`),
'utf8',
);
expect(file).toContain('Failed to create org');
deleteFiles([path.resolve(logPath, `abc.${FAILED_ORG_LOG_NAME}`)]);
done();
});
}, 20000);
}, 40000);

it('Fails to create an org as expected for non existing group ID `abc` file not in the same location as logs', (done) => {
const pathToBadJson = path.resolve(
Expand All @@ -99,13 +98,12 @@ Options:
},
},
(err, stdout, stderr) => {
if (err) {
throw err;
}
expect(stderr).toMatch(
'All requested organizations failed to be created. Review the errors in',
);
expect(err).toBeNull();
expect(err!.message).toMatch(
'All requested organizations failed to be created. Review the errors in',
);
expect(stdout).toEqual('');
const file = fs.readFileSync(
path.resolve(logPath, `abc.${FAILED_ORG_LOG_NAME}`),
Expand All @@ -115,10 +113,10 @@ Options:
deleteFiles([path.resolve(logPath, `abc.${FAILED_ORG_LOG_NAME}`)]);
},
).on('exit', (code) => {
expect(code).toEqual(0);
expect(code).toEqual(1);
done();
});
}, 20000);
}, 40000);

it('Fails to create orgs in --noDuplicateNames mode when org already exists ', (done) => {
const pathToBadJson = path.resolve(
Expand All @@ -139,13 +137,12 @@ Options:
},
},
(err, stdout, stderr) => {
if (err) {
throw err;
}
expect(stderr).toMatch(
'All requested organizations failed to be created. Review the errors in',
);
expect(err).toBeNull();
expect(err!.message).toMatch(
'All requested organizations failed to be created. Review the errors in',
);
expect(stdout).toEqual('');
const file = fs.readFileSync(
path.resolve(logPath, `${GROUP_ID}.${FAILED_ORG_LOG_NAME}`),
Expand All @@ -157,8 +154,8 @@ Options:
]);
},
).on('exit', (code) => {
expect(code).toEqual(0);
expect(code).toEqual(1);
done();
});
}, 20000);
}, 40000);
});

0 comments on commit 25e2e10

Please sign in to comment.