Skip to content

Commit

Permalink
feat(synchronizer): introduce method to toggle suppressions
Browse files Browse the repository at this point in the history
  • Loading branch information
f1ames committed Feb 7, 2024
1 parent 71dd95b commit f1b9029
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
42 changes: 42 additions & 0 deletions packages/synchronizer/src/handlers/apiHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,19 @@ const getSuppressionsQuery = `
data {
id
fingerprint
description
location
status
justification
expiresAt
updatedAt
createdAt
isUnderReview
isAccepted
isRejected
isExpired
isDeleted
repositoryId
}
}
}
Expand All @@ -158,6 +165,30 @@ const getRepoIdQuery = `
}
`;

const toggleSuppressionMutation = `
mutation toggleSuppression($fingerprint: String!, $repoId: ID!, $description: String!) {
toggleSuppression(
input: {fingerprint: $fingerprint, repository: $repoId, description: $description, skipReview: true}
) {
id
fingerprint
description
location
status
justification
expiresAt
updatedAt
createdAt
isUnderReview
isAccepted
isRejected
isExpired
isDeleted
repositoryId
}
}
`;

export type ApiUserProjectRepo = {
id: string;
projectId: number;
Expand Down Expand Up @@ -211,12 +242,19 @@ export type ApiPolicyData = {
export type ApiSuppression = {
id: string;
fingerprint: string;
description: string;
locations: string;
status: SuppressionStatus;
justification: string;
expiresAt: string;
updatedAt: string;
createdAt: string;
isUnderReview: boolean;
isAccepted: boolean;
isRejected: boolean;
isExpired: boolean;
isDeleted: boolean;
repositoryId: string;
};

export type ApiSuppressionsData = {
Expand Down Expand Up @@ -364,6 +402,10 @@ export class ApiHandler {
return this.queryApi(getRepoIdQuery, tokenInfo, {projectSlug, repoOwner, repoName});
}

async toggleSuppression(fingerprint: string, repoId: string, description: string, tokenInfo: TokenInfo) {
return this.queryApi<ApiSuppression>(toggleSuppressionMutation, tokenInfo, {fingerprint, repoId, description});
}

generateDeepLink(path: string) {
let appUrl = this._originConfig?.origin;

Expand Down
52 changes: 46 additions & 6 deletions packages/synchronizer/src/utils/projectSynchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ export class ProjectSynchronizer extends EventEmitter {
return this._dataCache[this.getCacheId(rootPath, projectSlug)]?.permissions;
}

getRepositorySuppressions(rootPath: string, projectSlug?: string) {
return this._dataCache[this.getCacheId(rootPath, projectSlug)]?.suppressions ?? [];
}

getProjectPolicy(rootPath: string, projectSlug?: string) {
const cacheId = this.getCacheId(rootPath, projectSlug);
const cached = this._dataCache[cacheId];
Expand All @@ -74,9 +70,53 @@ export class ProjectSynchronizer extends EventEmitter {
};
}

async toggleSuppression(fingerprint: string, repoId: string, description: string) {}
getRepositoryData(rootPath: string, projectSlug?: string) {
return this._dataCache[this.getCacheId(rootPath, projectSlug)]?.repository ?? undefined;
}

getRepositorySuppressions(rootPath: string, projectSlug?: string) {
return this._dataCache[this.getCacheId(rootPath, projectSlug)]?.suppressions ?? [];
}

async toggleSuppression(tokenInfo: TokenInfo, fingerprint: string, description: string, rootPath: string, projectSlug?: string) {
if (!tokenInfo?.accessToken?.length) {
throw new Error('Cannot use suppressions without access token.');
}

const repoData = await this.getRootGitData(rootPath);

let {id} = this.getRepositoryData(rootPath, projectSlug);
let {slug} = this.getProjectInfo(rootPath, projectSlug);
if (!id || !slug) {
const ownerProject = await this.getMatchingProject(repoData, tokenInfo);
if (!ownerProject) {
// This error would mostly be caused by incorrect integration. Integrator should
// make sure that suppressions are only used for repos with owner project.
throw new Error('Trying to suppress annotation for repo without owner project!');
}

const repoIdData = await this._apiHandler.getRepoId(ownerProject.slug, repoData.owner, repoData.name, tokenInfo);

async reviewSuppression(suppressionId: string, accepted: boolean) {}
id = repoIdData?.data.getProject.repository.id ?? '';
slug = ownerProject.slug;
}

if (!id || !slug) {
throw new Error('Cannot suppress due to missing repository id or project slug!');
}

const suppressionResult = await this._apiHandler.toggleSuppression(fingerprint, id, description, tokenInfo);
if (suppressionResult) {
const existingSuppressions = await this.readSuppressions(repoData);
const allSuppressions = this.mergeSuppressions(existingSuppressions, [suppressionResult]);
await this.storeSuppressions(allSuppressions, repoData);

const cacheId = this.getCacheId(rootPath, projectSlug);
if (this._dataCache[cacheId]) {
this._dataCache[cacheId].suppressions = allSuppressions;
}
}
}

async synchronize(tokenInfo: TokenInfo, rootPath: string, projectSlug?: string): Promise<void> {
if (!tokenInfo?.accessToken?.length) {
Expand Down

0 comments on commit f1b9029

Please sign in to comment.