Skip to content

Commit

Permalink
feat: sync support for github enterprise
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Nov 14, 2022
1 parent a7dd842 commit 55e8c3f
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 79 deletions.
22 changes: 22 additions & 0 deletions docs/sync.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
- [2. Download & run](#2-download--run)
- [Examples](#examples)
- [Github.com](#githubcom)
- [GitHub Enterprise Server](#github-enterprise-server)
- [GitHub Enterprise Cloud](#github-enterprise-cloud)
- [Known limitations](#known-limitations)

## Prerequisites
Expand Down Expand Up @@ -60,6 +62,26 @@ In dry-run mode:
Live mode:
`DEBUG=*snyk* SNYK_TOKEN=xxxx snyk-api-import sync --orgPublicId=<snyk_org_public_id> --source=github`


### GitHub Enterprise Server

In dry-run mode:
`DEBUG=*snyk* SNYK_TOKEN=xxxx snyk-api-import sync --orgPublicId=<snyk_org_public_id> --source=github-enterprise --sourceUrl=https://custom.ghe.com --dryRun=true`

Live mode:
`DEBUG=*snyk* SNYK_TOKEN=xxxx snyk-api-import sync --orgPublicId=<snyk_org_public_id> --source=github-enterprise --sourceUrl=https://custom.ghe.com`



### GitHub Enterprise Cloud

In dry-run mode:
`DEBUG=*snyk* SNYK_TOKEN=xxxx snyk-api-import sync --orgPublicId=<snyk_org_public_id> --source=github-enterprise --dryRun=true`

Live mode:
`DEBUG=*snyk* SNYK_TOKEN=xxxx snyk-api-import sync --orgPublicId=<snyk_org_public_id> --source=github-enterprise`


# Known limitations
- Any organizations using a custom branch feature are currently not supported, `sync` will not continue.
- ANy organizations that previously used the custom feature flag should ideally delete all existing projects & re-import to restore the project names to standard format (do not include a branch in the project name). `sync` will work regardless but may cause confusion as the project name will reference a branch that is not likely to be the actual branch being tested.
1 change: 0 additions & 1 deletion src/cmds/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const builder = {
default: undefined,
desc: 'Custom base url for the source API that can list organizations (e.g. Github Enterprise url)',
},
// TODO: needs integration Type for GHE<> Github setup
source: {
required: true,
default: SupportedIntegrationTypesUpdateProject.GITHUB,
Expand Down
7 changes: 1 addition & 6 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export enum SupportedIntegrationTypesImportOrgData {

export enum SupportedIntegrationTypesUpdateProject {
GITHUB = 'github',
GHE = 'github-enterprise',
}

// used to generate imported targets that exist in Snyk
Expand All @@ -107,12 +108,6 @@ export enum SupportedIntegrationTypesToListSnykTargets {
AZURE_REPOS = 'azure-repos',
BITBUCKET_SERVER = 'bitbucket-server',
}
interface ImportingUser {
id: string;
name: string;
username: string;
email: string;
}

export interface CommandResult {
fileName: string | undefined;
Expand Down
9 changes: 5 additions & 4 deletions src/scripts/sync/sync-org-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function isSourceConfigured(
): () => void {
const getDefaultBranchGenerators = {
[SupportedIntegrationTypesUpdateProject.GITHUB]: isGithubConfigured,
[SupportedIntegrationTypesUpdateProject.GHE]: isGithubConfigured,
};
return getDefaultBranchGenerators[origin];
}
Expand All @@ -34,7 +35,7 @@ export async function updateOrgTargets(
publicOrgId: string,
sources: SupportedIntegrationTypesUpdateProject[],
dryRun = false,
host?: string,
sourceUrl?: string,
): Promise<{
fileName: string;
failedFileName: string;
Expand Down Expand Up @@ -124,7 +125,7 @@ export async function updateOrgTargets(
publicOrgId,
targets,
dryRun,
host,
sourceUrl,
);
res.processedTargets += response.processedTargets;
res.meta.projects.updated.push(...response.meta.projects.updated);
Expand Down Expand Up @@ -156,7 +157,7 @@ export async function updateTargets(
orgId: string,
targets: SnykTarget[],
dryRun = false,
host?: string,
sourceUrl?: string,
): Promise<{
processedTargets: number;
meta: {
Expand All @@ -181,7 +182,7 @@ export async function updateTargets(
orgId,
target,
dryRun,
host,
sourceUrl,
);
updatedProjects.push(...updated);
failedProjects.push(...failed);
Expand Down
5 changes: 3 additions & 2 deletions src/scripts/sync/sync-projects-per-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function getBranchGenerator(
const getDefaultBranchGenerators = {
[SupportedIntegrationTypesUpdateProject.GITHUB]:
getGithubReposDefaultBranch,
[SupportedIntegrationTypesUpdateProject.GHE]: getGithubReposDefaultBranch,
};
return getDefaultBranchGenerators[origin];
}
Expand Down Expand Up @@ -72,7 +73,7 @@ export async function bulkUpdateProjectsBranch(
orgId: string,
projects: SnykProject[],
dryRun = false,
host?: string,
sourceUrl?: string,
): Promise<{ updated: ProjectUpdate[]; failed: ProjectUpdateFailure[] }> {
const updatedProjects: ProjectUpdate[] = [];
const failedProjects: ProjectUpdateFailure[] = [];
Expand All @@ -82,7 +83,7 @@ export async function bulkUpdateProjectsBranch(
try {
const target = targetGenerators[origin](projects[0]);
debug(`Getting default branch via ${origin} for ${projects[0].name}`);
defaultBranch = await getBranchGenerator(origin)(target, host);
defaultBranch = await getBranchGenerator(origin)(target, sourceUrl);
} catch (e) {
debug(e);
const error = `Getting default branch via ${origin} API failed with error: ${e.message}`;
Expand Down
Loading

0 comments on commit 55e8c3f

Please sign in to comment.