Skip to content

Commit

Permalink
feat: Api to create orgs + script to generate orgs
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed May 28, 2020
1 parent fec0ab6 commit 73d792c
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ package-lock.json
yarn.lock
.eslintcache
local.*
test/research
56 changes: 56 additions & 0 deletions src/lib/group/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'source-map-support/register';
import * as needle from 'needle';
import * as debugLib from 'debug';
import { getApiToken } from '../get-api-token';
import { getSnykHost } from '../get-snyk-host';

const debug = debugLib('snyk:api-group');

export interface CreatedOrgResponse {
id: string;
name: string;
created: string;
}
export async function createOrg(
groupId: string,
name: string,
sourceOrgId?: string,
): Promise<CreatedOrgResponse> {
const apiToken = getApiToken();
debug('Creating a new org:' + name);

if (!groupId || !name) {
throw new Error(
`Missing required parameters. Please ensure you have set: groupId, name.
\nFor more information see: https://snyk.docs.apiary.io/#reference/0/organizations-in-groups/create-a-new-organization-in-the-group`,
);
}
const body: {
name: string;
sourceOrgId?: string;
} = {
name,
sourceOrgId,
};
const SNYK_HOST = getSnykHost();

const res = await needle(
'post',
`${SNYK_HOST}/api/v1/group/${groupId}/org`,
body,
{
json: true,
// eslint-disable-next-line @typescript-eslint/camelcase
read_timeout: 30000,
headers: {
Authorization: `token ${apiToken}`,
},
},
);
if (res.statusCode && res.statusCode !== 200) {
throw new Error(
'Expected a 200 response, instead received: ' + JSON.stringify(res.body),
);
}
return res.body;
}
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './import';
export * from './poll-import';
export * from './project';
export * from './group';
54 changes: 54 additions & 0 deletions src/scripts/create-orgs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as debugLib from 'debug';
import * as fs from 'fs';

import { loadFile } from '../load-file';
import { CreatedOrgResponse, createOrg } from '../lib';
import { getLoggingPath } from '../lib/get-logging-path';

const debug = debugLib('snyk:import-projects-script');

interface CreateOrgData {
groupId: string;
name: string;
sourceOrgId?: string;
}

export async function logCreatedOrg(
origName: string,
orgData: CreatedOrgResponse,
loggingPath: string = getLoggingPath(),
): Promise<void> {
try {
const { id, name, created } = orgData;
const log = `${origName},${name},${id},${created}\n`;
fs.appendFileSync(`${loggingPath}/created-orgs.csv`, log);
} catch (e) {
// do nothing
}
}
export async function CreateOrgs(
fileName: string,
loggingPath: string,
): Promise<CreatedOrgResponse[]> {
const content = await loadFile(fileName);
const orgsData: CreateOrgData[] = [];
try {
orgsData.push(...JSON.parse(content).orgs);
} catch (e) {
throw new Error(`Failed to parse orgs from ${fileName}`);
}
debug(`Loaded ${orgsData.length} orgs to create ${Date.now()}`);
const createdOrgs: CreatedOrgResponse[] = [];
orgsData.forEach(async (orgData) => {
try {
const { groupId, name, sourceOrgId } = orgData;
const org = await createOrg(groupId, name, sourceOrgId);
createdOrgs.push(org);
logCreatedOrg(name, org, loggingPath);
} catch (e) {
debug(`Failed to create org with data: ${JSON.stringify(orgsData)}`);
}
});

return createdOrgs;
}
5 changes: 4 additions & 1 deletion src/scripts/import-projects.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import * as debugLib from 'debug';
import * as path from 'path';
import { loadFile } from '../load-file';
import { importTargets, pollImportUrls } from '../lib';
import {
importTargets,
pollImportUrls,
} from '../lib';
import { Project, ImportTarget } from '../lib/types';
import { getLoggingPath } from '../lib/get-logging-path';
import { getConcurrentImportsNumber } from '../lib/get-concurrent-imports-number';
Expand Down

0 comments on commit 73d792c

Please sign in to comment.