Skip to content

Commit

Permalink
Merge pull request #360 from snyk-tech-services/feat/GetAllTargetForRepo
Browse files Browse the repository at this point in the history
feat: implements get target per org + test
  • Loading branch information
mathild3r authored Oct 12, 2022
2 parents a46214c + b4fe29e commit 312a53a
Show file tree
Hide file tree
Showing 5 changed files with 1,680 additions and 19 deletions.
124 changes: 117 additions & 7 deletions src/lib/api/org/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { requestsManager } from 'snyk-request-manager';
import * as debugLib from 'debug';
import { getApiToken } from '../../get-api-token';
import { getSnykHost } from '../../get-snyk-host';
import { SnykProject, v3ProjectData } from '../../types';
import { SnykProject, SnykTarget, TargetsResponse, RESTProjectData } from '../../types';
import { StringNullableChain } from 'lodash';
import { Console } from 'console';
import { url } from 'inspector';

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

Expand Down Expand Up @@ -142,8 +144,8 @@ interface ProjectsResponse {
}


interface v3ProjectsResponse {
data: v3ProjectData[];
interface RESTProjectsResponse {
data: RESTProjectData[];
jsonapi: {
version: string;
};
Expand Down Expand Up @@ -247,16 +249,16 @@ async function getProject(requestManager: requestsManager,
);
}

const v3responseData = res.data as v3ProjectsResponse
const RESTresponseData = res.data as RESTProjectsResponse

const projects = convertToSnykProject(v3responseData.data)
const next = v3responseData.links.next
const projects = convertToSnykProject(RESTresponseData.data)
const next = RESTresponseData.links.next

return { projects, next}

}

function convertToSnykProject(projectData: v3ProjectData[]) : SnykProject[] {
function convertToSnykProject(projectData: RESTProjectData[]) : SnykProject[] {

const projects: SnykProject[] = [];

Expand All @@ -275,3 +277,111 @@ function convertToSnykProject(projectData: v3ProjectData[]) : SnykProject[] {
return projects
}

export async function listTargets(
requestManager: requestsManager,
orgId: string,
config?: {
limit?: number,
isEmpty?: boolean,
origin?: string,
}
): Promise<TargetsResponse> {
getApiToken();
getSnykHost();
debug(`Listing all targets for org: ${orgId}`);

if (!orgId && orgId.length > 0) {
throw new Error(
`Missing required parameters. Please ensure you have set: orgId, settings.
\nFor more information see: https://apidocs.snyk.io/?version=2022-09-15%7Ebeta#get-/orgs/-org_id-/targets`,
);
}

const targets = await listAllSnykTarget(requestManager, orgId, config)

return {targets: targets};
}

export async function listAllSnykTarget(requestManager: requestsManager,
orgId: string,
config?: {
limit?: number,
isEmpty?: boolean,
origin?: string,
}
): Promise<SnykTarget[]> {

let lastPage = false;
let SnykTargetList: SnykTarget[] = [];
let pageCount = 1;
let nextPageLink: string | undefined = undefined;
while (!lastPage) {
try {
debug(`Fetching page ${pageCount} of get target for ${orgId}\n`);
const {
SnykTarget,
next,
}: {
SnykTarget: SnykTarget[];
next?: string;
} = await getSnykTarget(requestManager, orgId, nextPageLink, config);

SnykTargetList = SnykTargetList.concat(SnykTarget);
next
? ((lastPage = false), (nextPageLink = next))
: ((lastPage = true), (nextPageLink = ''));
pageCount++;
} catch (e) {
debug('Failed to get targets for ', orgId, e);
throw e;
}
}
return SnykTargetList
}

export async function getSnykTarget(requestManager: requestsManager,
orgId: string,
nextPageLink?: string,
config?: {
limit?: number,
isEmpty?: boolean,
origin?: string,
}): Promise< { SnykTarget: SnykTarget[], next?: string } > {

const link = nextPageLink ? nextPageLink : `https://api.dev.snyk.io/rest/orgs/${orgId.trim()}/targets?version=2022-09-15~beta`
const url = new URL(link)

if (config) {
if (config.isEmpty) {
url.searchParams.set('isEmpty', String(config.isEmpty))
}
if (config.limit) {
url.searchParams.set('limit', String(config.limit))
}
if (config.origin) {
url.searchParams.set('origin', config.origin)
}
}

const res = await requestManager.request({
verb: 'get',
url: url.toString(),
body: undefined,
useRESTApi: true,
});

const statusCode = res.statusCode || res.status;
if (!statusCode || statusCode !== 200) {
throw new Error(
'Expected a 200 response, instead received: ' +
JSON.stringify({ data: res.data, status: statusCode }),
);
}

const responseData = res.data
const SnykTarget = responseData.data
const next = responseData.links.next

return { SnykTarget, next }

}
79 changes: 68 additions & 11 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

export interface ImportTarget {
orgId: string;
integrationId: string;
Expand Down Expand Up @@ -138,14 +139,14 @@ export interface Org {
}


export interface v3ProjectData {
attributes: v3ProjectsAttributes;
export interface RESTProjectData {
attributes: RESTProjectsAttributes;
id: string;
relationships: v3ProjectsRelationships;
relationships: RESTProjectsRelationships;
type: string
}

export interface v3ProjectsAttributes {
export interface RESTProjectsAttributes {
businessCriticality: string;
created: string;
environment: string;
Expand All @@ -158,18 +159,74 @@ export interface v3ProjectsAttributes {
type: string;
}

export interface v3ProjectsRelationships {
importingUser: v3ProjectsRelashionshipData;
org: v3ProjectsRelashionshipData;
owner: v3ProjectsRelashionshipData;
target: v3ProjectsRelashionshipData;
export interface RESTProjectsRelationships {
importingUser: RESTProjectsRelashionshipData;
org: RESTProjectsRelashionshipData;
owner: RESTProjectsRelashionshipData;
target: RESTProjectsRelashionshipData;
}

export interface v3ProjectsRelashionshipData {
export interface RESTProjectsRelashionshipData {
data: {
id: string;
type: string;
}
links: unknown;
links: {
first?: string;
last?: string;
next?: string;
prev?: string;
related?: string;
self?: string;
};
meta: unknown;
}

export interface SnykTargetRelationships {
org: {
data : {
id: string;
type: string;
}
links: {
first?: string;
last?: string;
next?: string;
prev?: string;
related?: string;
self?: string;
};
meta: unknown;
}
}

export interface SnykTargetData {
attributes: {
displayName: string;
isPrivate: boolean;
origin: string;
remoteUrl: string | null;
}
id: string;
relationships: SnykTargetRelationships;
type: string;
}

export interface SnykTarget {
data: SnykTargetData[];
jsonapi: {
version: string;
}
links: {
first?: string;
last?: string;
next?: string;
prev?: string;
related?: string;
self?: string;
};
}

export interface TargetsResponse {
targets: SnykTarget[]
}
Loading

0 comments on commit 312a53a

Please sign in to comment.