-
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: Api to create orgs + script to generate orgs
- Loading branch information
Showing
5 changed files
with
116 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -9,3 +9,4 @@ package-lock.json | |
yarn.lock | ||
.eslintcache | ||
local.* | ||
test/research |
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,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; | ||
} |
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,3 +1,4 @@ | ||
export * from './import'; | ||
export * from './poll-import'; | ||
export * from './project'; | ||
export * from './group'; |
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,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; | ||
} |
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