Skip to content

Commit

Permalink
fix: retrieve all orgs from all pages
Browse files Browse the repository at this point in the history
  • Loading branch information
aarlaud committed May 6, 2024
1 parent c91e91a commit ccd9f75
Show file tree
Hide file tree
Showing 7 changed files with 520 additions and 17 deletions.
42 changes: 26 additions & 16 deletions src/lib/snyk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,38 @@ export interface TargetType {
};
}

export const getAllOrgs = async (recordPerPage = 10): Promise<OrgType[]> => {
const snykRequestManager = new requestsManager();
const orgs: OrgType[] = [];
let orgsResponse = await snykRequestManager.request({
verb: 'GET',
url: `/orgs?version=${snykApiVersion}&limit=${recordPerPage}`,
useRESTApi: true,
});
orgs.push(...orgsResponse.data.data);
let isNextPage = orgsResponse.data.links.next ? true : false;
while (isNextPage) {
const nextPageUrl = orgsResponse.data.links.next.replace(/^\/rest/, '');
orgsResponse = await snykRequestManager.request({
verb: 'GET',
url: nextPageUrl,
useRESTApi: true,
});
orgs.push(...orgsResponse.data.data);
isNextPage = orgsResponse.data.links.next ? true : false;
}

return orgs;
};

export const retrieveMonitoredRepos = async (
url: string,
scmType: SourceType,
): Promise<string[]> => {
let snykMonitoredRepos: string[] = [];

try {
const snykRequestManager = new requestsManager();
const orgsResponse = await snykRequestManager.request({
verb: 'GET',
url: `/orgs?version=${snykApiVersion}`,
useRESTApi: true,
});

const orgs = orgsResponse.data.data as OrgType[];
const orgs = await getAllOrgs(100);

snykMonitoredRepos = snykMonitoredRepos.concat(
await retrieveMonitoredReposBySourceType(orgs, scmType),
Expand All @@ -77,14 +94,7 @@ export const retrieveMonitoredRepos = async (
export const retrieveOrgsAndIntegrations = async (): Promise<Integration[]> => {
const integrations: Integration[] = [];
try {
const snykRequestManager = new requestsManager();
const orgsResponse = await snykRequestManager.request({
verb: 'GET',
url: `/orgs?version=${snykApiVersion}`,
useRESTApi: true,
});

const orgs = orgsResponse.data.data as OrgType[];
const orgs = await getAllOrgs(100);

for (let i = 0; i < orgs.length; i++) {
const integrationsInfo = await new Org({ orgId: orgs[i].id })
Expand Down
114 changes: 114 additions & 0 deletions test/fixtures/snyk/all-orgs-page1-rest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

{
"data": [
{
"id": "2aca25e4-6bcc-4ed5-ad16-1f00f08984ed",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "02f2edfc-aff1-434a-8c89-63ab2a15b6d0",
"slug": "02f2edfc-aff1-434a-8c89-63ab2a15b6d0"
}
},
{
"id": "1f2b66e7-f003-47ae-b263-fc38c25422f4",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "0920a431-294f-4f1b-8b51-b7ce11dd9b9e",
"slug": "0920a431-294f-4f1b-8b51-b7ce11dd9b9e"
}
},
{
"id": "6c3a016f-c245-4b9a-8958-7b668786d4ba",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "0ecc08c1-32b1-43a6-b609-036e972c33ef",
"slug": "0ecc08c1-32b1-43a6-b609-036e972c33ef"
}
},
{
"id": "7316d04a-b6e4-4d22-b85e-72b5a2b1a672",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "158615d6-bbd0-45b2-a08c-96f67a0b9566",
"slug": "158615d6-bbd0-45b2-a08c-96f67a0b9566"
}
},
{
"id": "bc68cded-828c-4ef0-81bb-5ea6d74bb4f8",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "186d1421-e247-446e-8f02-c892d0ded4e9",
"slug": "186d1421-e247-446e-8f02-c892d0ded4e9"
}
},
{
"id": "bfba8e25-0600-482d-85d7-d982acb99cfd",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "1cd2b5a0-bce0-4f61-bacd-47bb43c136c3",
"slug": "1cd2b5a0-bce0-4f61-bacd-47bb43c136c3"
}
},
{
"id": "d4456a7b-207e-46dd-becc-2a46f83814e8",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "1d1dcded-93d8-43a3-b74a-24b5c3109050",
"slug": "1d1dcded-93d8-43a3-b74a-24b5c3109050"
}
},
{
"id": "44cef555-a4e7-4981-90c2-66c2804d185c",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "22d62fdb-37fa-4308-9200-d3d1d9f0907b",
"slug": "22d62fdb-37fa-4308-9200-d3d1d9f0907b"
}
},
{
"id": "7943d207-cb50-4228-a227-bc7b567ab06e",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "273cd33e-6593-4615-9eec-6fc35701bf8f",
"slug": "273cd33e-6593-4615-9eec-6fc35701bf8f"
}
},
{
"id": "106e9577-2527-42bf-bbb6-aa382650f858",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "29a42a22-367e-4723-9c50-4b4321b36f71",
"slug": "29a42a22-367e-4723-9c50-4b4321b36f71"
}
}
],
"jsonapi": {
"version": "1.0"
},
"links": {
"self": "/rest/orgs?limit=10&version=2023-09-29~beta",
"first": "/rest/orgs?limit=10&version=2023-09-29~beta",
"prev": "/rest/orgs?ending_before=v1.eyJuYW1lIjoiMDJmMmVkZmMtYWZmMS00MzRhLThjODktNjNhYjJhMTViNmQwIiwic2x1ZyI6IjAyZjJlZGZjLWFmZjEtNDM0YS04Yzg5LTYzYWIyYTE1YjZkMCJ9&limit=10&version=2023-09-29~beta",
"next": "/rest/orgs?limit=10&starting_after=v1.eyJuYW1lIjoiMjlhNDJhMjItMzY3ZS00NzIzLTljNTAtNGI0MzIxYjM2ZjcxIiwic2x1ZyI6IjI5YTQyYTIyLTM2N2UtNDcyMy05YzUwLTRiNDMyMWIzNmY3MSJ9&version=2023-09-29~beta"
}
}
114 changes: 114 additions & 0 deletions test/fixtures/snyk/all-orgs-page2-rest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

{
"data": [
{
"id": "b0cd6709-9f44-4ca2-b167-680227431f89",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "321f937d-a202-49f8-b23f-7511d6a92534",
"slug": "321f937d-a202-49f8-b23f-7511d6a92534"
}
},
{
"id": "bac85b92-8f24-4643-8377-b852ac999a8a",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "32a0afc2-4240-48e6-b03c-1ce98b70e678",
"slug": "32a0afc2-4240-48e6-b03c-1ce98b70e678"
}
},
{
"id": "d0ace317-79ea-44b3-bc2b-b272d002e9e0",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "351e27bf-aa15-4bf8-9245-b23cd243e9e2",
"slug": "351e27bf-aa15-4bf8-9245-b23cd243e9e2"
}
},
{
"id": "e968564a-d080-4c0f-8727-cf8d430635cc",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "3a40ca93-404d-4937-8ded-d83187f4a0f6",
"slug": "3a40ca93-404d-4937-8ded-d83187f4a0f6"
}
},
{
"id": "296edbde-4b39-4345-9e59-88bf4fafec16",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "4429059a-17e7-4dfa-9ca1-213f8cb34157",
"slug": "4429059a-17e7-4dfa-9ca1-213f8cb34157"
}
},
{
"id": "0e7547c4-0c86-4264-be90-2fd4c2b7d678",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "48d71d9f-767b-48f6-a60c-fd36da3741e8",
"slug": "48d71d9f-767b-48f6-a60c-fd36da3741e8"
}
},
{
"id": "53d77d49-a54e-4495-a883-814fdb1d5828",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "4b1c97c3-3309-4e47-af68-d299c3448374",
"slug": "4b1c97c3-3309-4e47-af68-d299c3448374"
}
},
{
"id": "c06366f3-b6c8-4351-8c11-aa7162a927ed",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "4caa620c-6d30-4dac-8452-43d154c77931",
"slug": "4caa620c-6d30-4dac-8452-43d154c77931"
}
},
{
"id": "fd91f0a3-6bfa-4f16-b937-77f9e61da408",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "4ed9550d-0f8f-4007-bf9f-2baa765c1d90",
"slug": "4ed9550d-0f8f-4007-bf9f-2baa765c1d90"
}
},
{
"id": "7db13969-9384-4579-9acc-b460197dce4b",
"type": "org",
"attributes": {
"group_id": "7a3e11b1-f568-4d7f-a6cb-021bc38d8ea6",
"is_personal": false,
"name": "523b377b-cc24-4406-a02d-0b02ba819df1",
"slug": "523b377b-cc24-4406-a02d-0b02ba819df1"
}
}
],
"jsonapi": {
"version": "1.0"
},
"links": {
"self": "/rest/orgs?limit=10&starting_after=v1.eyJuYW1lIjoiMjlhNDJhMjItMzY3ZS00NzIzLTljNTAtNGI0MzIxYjM2ZjcxIiwic2x1ZyI6IjI5YTQyYTIyLTM2N2UtNDcyMy05YzUwLTRiNDMyMWIzNmY3MSJ9&version=2023-09-29~beta",
"first": "/rest/orgs?limit=10&version=2023-09-29~beta",
"prev": "/rest/orgs?ending_before=v1.eyJuYW1lIjoiMzIxZjkzN2QtYTIwMi00OWY4LWIyM2YtNzUxMWQ2YTkyNTM0Iiwic2x1ZyI6IjMyMWY5MzdkLWEyMDItNDlmOC1iMjNmLTc1MTFkNmE5MjUzNCJ9&limit=10&version=2023-09-29~beta",
"next": "/rest/orgs?limit=10&starting_after=v1.eyJuYW1lIjoiNTIzYjM3N2ItY2MyNC00NDA2LWEwMmQtMGIwMmJhODE5ZGYxIiwic2x1ZyI6IjUyM2IzNzdiLWNjMjQtNDQwNi1hMDJkLTBiMDJiYTgxOWRmMSJ9&version=2023-09-29~beta"
}
}
Loading

0 comments on commit ccd9f75

Please sign in to comment.