Skip to content

Commit

Permalink
Merge branch 'master' into fix/getTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
mathild3r authored Oct 21, 2022
2 parents 90b4930 + 804150d commit f33254b
Show file tree
Hide file tree
Showing 30 changed files with 1,075 additions and 129 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"semantic-release": "17.3.0",
"ts-jest": "27.0.3",
"tsc-watch": "^4.1.0",
"typescript": "4.3.5"
"typescript": "4.3.5",
"uuid": "9.0.0"
},
"pkg": {
"scripts": [
Expand Down
46 changes: 46 additions & 0 deletions src/lib/api/feature-flags/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { requestsManager } from 'snyk-request-manager';
import * as debugLib from 'debug';

const debug = debugLib('snyk:get-feature-flag');

export async function getFeatureFlag(
requestManager: requestsManager,
featureFlagName: string,
orgId: string,
): Promise<boolean> {
debug(
`Checking if feature flag ${featureFlagName} is enabled for Snyk org ${orgId}`,
);
try {
const url = `cli-config/feature-flags/${featureFlagName}?org=${orgId}`;
const res = await requestManager.request({
verb: 'get',
url: url,
useRESTApi: false,
});

debug(`Feature flag ${featureFlagName} is enabled for Org ${orgId}`);

const enabled: boolean = res.data['ok'];

return enabled;
} catch (err: any) {
console.log(err.message)
const res = err.message?.response?.data;
const message = res?.userMessage || res?.message || err.message?.message;

if (res && res.ok === false && res?.userMessage?.includes('feature enabled')) {
debug(
`Feature flag ${featureFlagName} is not enabled for Org ${orgId}`,
);
return false;
}

debug(
`Could not fetch the ${featureFlagName} feature flag for ${orgId}\n ${JSON.stringify(
err,
)}`,
);
throw new Error(message);
}
}
13 changes: 6 additions & 7 deletions src/lib/api/org/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export async function deleteOrg(
return res.data;
}

interface ProjectsResponse {
export interface ProjectsResponse {
org: {
id: string;
};
Expand All @@ -166,7 +166,7 @@ interface ProjectsFilters {
origin?: string; //If supplied, only projects that exactly match this origin will be returned
type?: string; //If supplied, only projects that exactly match this type will be returned
isMonitored?: boolean; // If set to true, only include projects which are monitored, if set to false, only include projects which are not monitored
targetId?: string;
targetId?: string; // The target ID to be used in sunc functions
}

export async function listProjects(
Expand All @@ -176,8 +176,7 @@ export async function listProjects(
): Promise<ProjectsResponse> {
getApiToken();
getSnykHost();
debug(`Listing all projects for org: ${orgId}`);

debug(`Listing all projects for org: ${orgId} with filter ${JSON.stringify(filters)}`);
if (!orgId) {
throw new Error(
`Missing required parameters. Please ensure you have set: orgId, settings.
Expand Down Expand Up @@ -285,7 +284,7 @@ function convertToSnykProject(projectData: RESTProjectData[]): SnykProject[] {

return projects;
}
interface TargetFilters {
export interface TargetFilters {
remoteUrl?: string;
limit?: number;
isPrivate?: boolean;
Expand All @@ -309,12 +308,12 @@ export async function listTargets(
);
}

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

return { targets };
}

export async function listAllSnykTarget(
export async function listAllSnykTargets(
requestManager: requestsManager,
orgId: string,
config?: TargetFilters,
Expand Down
40 changes: 0 additions & 40 deletions src/lib/get-feature-flag-for-snyk-org.ts

This file was deleted.

11 changes: 8 additions & 3 deletions src/lib/project/compare-branches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ export async function compareAndUpdateBranches(
},
defaultBranch: string,
orgId: string,
dryRun = false,
): Promise<{ updated: boolean }> {
const {branch, projectPublicId} = project;
const { branch, projectPublicId } = project;
let updated = false
try {

if (branch != defaultBranch) {
debug(`Default branch has changed for Snyk project ${projectPublicId}`);
await updateProject(requestManager, orgId, projectPublicId, { branch: defaultBranch });
updated = true
if (!dryRun) {
await updateProject(requestManager, orgId, project.projectPublicId, {
branch: defaultBranch,
});
}
updated = true;
}

return { updated }
Expand Down
11 changes: 7 additions & 4 deletions src/lib/source-handlers/github/get-default-branch.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { Octokit } from '@octokit/rest';
import * as debugLib from 'debug';
import type { Target } from '../../types';
import { getGithubToken } from './get-github-token';
import { getGithubBaseUrl } from './github-base-url';

const debug = debugLib('snyk:get-github-defaultBranch-script');

export async function getGithubReposDefaultBranch(
RepoName: string,
target: Target,
host?: string,
): Promise<string> {
const githubToken = getGithubToken();
const baseUrl = getGithubBaseUrl(host);
const octokit: Octokit = new Octokit({ baseUrl, auth: githubToken });

debug(`fetching the default branch for repo: ${RepoName}`);

const response = await octokit.request(`GET /repos/${RepoName}`);
debug(`fetching the default branch for repo: ${target.owner}/${target.name}`);

const response = await octokit.repos.get({
owner: target.owner!,
repo: target.name!,
});
return response.data.default_branch as string;
}
4 changes: 4 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export enum SupportedIntegrationTypesImportOrgData {
BITBUCKET_CLOUD = 'bitbucket-cloud',
}

export enum SupportedIntegrationTypesUpdateProject {
GITHUB = 'github',
}

// used to generate imported targets that exist in Snyk
// when we need to grab the integrationId from Snyk
export enum SupportedIntegrationTypesToListSnykTargets {
Expand Down
10 changes: 6 additions & 4 deletions src/scripts/generate-imported-targets-from-snyk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import * as path from 'path';
import * as pMap from 'p-map';
import * as _ from 'lodash';

import {
import type {
FilePath,
SnykProject,
SupportedIntegrationTypesToListSnykTargets,
Target,
} from '../lib/types';
import {
SupportedIntegrationTypesToListSnykTargets,
} from '../lib/types';
import {
getAllOrgs,
getLoggingPath,
Expand Down Expand Up @@ -69,8 +71,8 @@ export function imageProjectToTarget(
name: project.name,
};
}

const targetGenerators = {
// TODO: move to it's own lib?
export const targetGenerators = {
[SupportedIntegrationTypesToListSnykTargets.GITHUB]: projectToTarget,
[SupportedIntegrationTypesToListSnykTargets.GITLAB]: gitlabProjectToImportLogTarget,
[SupportedIntegrationTypesToListSnykTargets.GHE]: projectToTarget,
Expand Down
Loading

0 comments on commit f33254b

Please sign in to comment.