Skip to content

Commit

Permalink
Merge pull request #403 from snyk-tech-services/feat/dedupe-projects-…
Browse files Browse the repository at this point in the history
…before-logging

fix: dedupe projects before logging
  • Loading branch information
lili2311 authored Dec 14, 2022
2 parents 22df49f + 46751ae commit fb7607d
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/scripts/sync/sync-projects-per-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ export async function syncProjectsForTarget(
entitlements: SnykProductEntitlement[] = ['openSource'],
manifestTypes?: string[],
): Promise<{ updated: ProjectUpdate[]; failed: ProjectUpdateFailure[] }> {
const failed: ProjectUpdateFailure[] = [];
const updated: ProjectUpdate[] = [];
const updated = new Set<ProjectUpdate>();
const failed = new Set<ProjectUpdateFailure>();

debug(`Listing projects for target ${target.attributes.displayName}`);
const { projects } = await listProjects(requestManager, orgId, {
targetId: target.id,
Expand All @@ -51,8 +52,8 @@ export async function syncProjectsForTarget(
// TODO: if target is empty, try to import it and stop here
if (projects.length < 1) {
return {
updated,
failed,
updated: Array.from(updated),
failed: Array.from(failed),
};
}
debug(`Syncing projects for target ${target.attributes.displayName}`);
Expand All @@ -66,7 +67,7 @@ export async function syncProjectsForTarget(
debug(e);
const error = `Getting default branch via ${origin} API failed with error: ${e.message}`;
projects.map((project) => {
failed.push({
failed.add({
errorMessage: error,
projectPublicId: project.id,
type: ProjectUpdateType.BRANCH,
Expand Down Expand Up @@ -97,7 +98,7 @@ export async function syncProjectsForTarget(
debug(e);
const error = `Cloning and analysing the repo to deactivate projects failed with error: ${e.message}`;
projects.map((project) => {
failed.push({
failed.add({
errorMessage: error,
projectPublicId: project.id,
from: 'active',
Expand Down Expand Up @@ -130,14 +131,24 @@ export async function syncProjectsForTarget(
];

const [branchUpdate, deactivatedProjects] = await Promise.all(actions);
updated.push(...branchUpdate.updated.map((t) => ({ ...t, target })));
failed.push(...branchUpdate.failed.map((t) => ({ ...t, target })));
updated.push(...deactivatedProjects.updated.map((t) => ({ ...t, target })));
failed.push(...deactivatedProjects.failed.map((t) => ({ ...t, target })));

branchUpdate.updated
.map((t) => ({ ...t, target }))
.forEach((i) => updated.add(i));
branchUpdate.failed
.map((t) => ({ ...t, target }))
.forEach((i) => failed.add(i));

deactivatedProjects.updated
.map((t) => ({ ...t, target }))
.forEach((i) => updated.add(i));
deactivatedProjects.failed
.map((t) => ({ ...t, target }))
.forEach((i) => failed.add(i));
// TODO: add target info for logs
return {
updated,
failed,
updated: Array.from(updated),
failed: Array.from(failed),
};
}

Expand Down

0 comments on commit fb7607d

Please sign in to comment.