From 92dc4d40951376c98d0fec401499454381b656f2 Mon Sep 17 00:00:00 2001 From: TJ Silver Date: Thu, 9 Jan 2025 09:27:53 +0000 Subject: [PATCH] refactor: update/add tests for exempted repos --- .../send-to-sns.test.ts | 55 ++++++++++++++++++- .../send-to-sns.ts | 6 -- packages/repocop/src/types.ts | 3 +- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/packages/repocop/src/remediation/dependency_graph-integrator/send-to-sns.test.ts b/packages/repocop/src/remediation/dependency_graph-integrator/send-to-sns.test.ts index def888dcd..a19229c4b 100644 --- a/packages/repocop/src/remediation/dependency_graph-integrator/send-to-sns.test.ts +++ b/packages/repocop/src/remediation/dependency_graph-integrator/send-to-sns.test.ts @@ -1,5 +1,6 @@ import type { github_languages, + github_repository_custom_properties, guardian_github_actions_usage, view_repo_ownership, } from '@prisma/client'; @@ -119,6 +120,32 @@ describe('When trying to find repos using Scala', () => { }); }); +function exemptedCustomProperty(): github_repository_custom_properties { + return { + cq_sync_time: null, + cq_source_name: null, + cq_id: 'id1', + cq_parent_id: null, + org: 'guardian', + property_name: 'gu_dependency_graph_integrator_ignore', + repository_id: BigInt(1), + value: 'Scala', + }; +} + +function nonExemptedCustomProperty(): github_repository_custom_properties { + return { + cq_sync_time: null, + cq_source_name: null, + cq_id: 'id1', + cq_parent_id: null, + org: 'guardian', + property_name: 'gu_dependency_graph_integrator_ignore', + repository_id: BigInt(12345), + value: 'Scala', + }; +} + describe('When checking a repo for an existing dependency submission workflow', () => { test('return true if repo workflow is present', () => { const result = doesRepoHaveDepSubmissionWorkflowForLanguage( @@ -138,12 +165,13 @@ describe('When checking a repo for an existing dependency submission workflow', }); }); -describe('When getting suitable events to send to SNS', () => { +describe('When getting suitable repos to send to SNS', () => { test('return the repo when a Scala repo is found without an existing workflow', () => { const result = getSuitableReposWithoutWorkflows( [repoWithTargetLanguage(fullName)], [repository(fullName)], [repoWithoutWorkflow(fullName)], + [], ); const expected = [repositoryWithDepGraphLanguage(fullName, 'Scala')]; @@ -154,6 +182,7 @@ describe('When getting suitable events to send to SNS', () => { [repoWithTargetLanguage(fullName)], [repository(fullName)], [repoWithDepSubmissionWorkflow(fullName)], + [], ); expect(result).toEqual([]); }); @@ -162,14 +191,16 @@ describe('When getting suitable events to send to SNS', () => { [repoWithoutTargetLanguage(fullName)], [repository(fullName)], [repoWithoutWorkflow(fullName)], + [], ); expect(result).toEqual([]); }); - test('return 2 events when 2 Scala repos are found without an existing workflow', () => { + test('return both repos when 2 Scala repos are found without an existing workflow', () => { const result = getSuitableReposWithoutWorkflows( [repoWithTargetLanguage(fullName), repoWithTargetLanguage(fullName2)], [repository(fullName), repository(fullName2)], [repoWithoutWorkflow(fullName), repoWithoutWorkflow(fullName2)], + [], ); const expected = [ repositoryWithDepGraphLanguage(fullName, 'Scala'), @@ -178,6 +209,26 @@ describe('When getting suitable events to send to SNS', () => { expect(result).toEqual(expected); }); + test('return the repo when a Scala repo is found without an existing workflow and repo is not exempt', () => { + const result = getSuitableReposWithoutWorkflows( + [repoWithTargetLanguage(fullName)], + [repository(fullName)], + [repoWithoutWorkflow(fullName)], + [nonExemptedCustomProperty()], + ); + const expected = [repositoryWithDepGraphLanguage(fullName, 'Scala')]; + + expect(result).toEqual(expected); + }); + test('return empty repo array when a Scala repo is found without an existing workflow but is exempt', () => { + const result = getSuitableReposWithoutWorkflows( + [repoWithTargetLanguage(fullName)], + [repository(fullName)], + [repoWithoutWorkflow(fullName)], + [exemptedCustomProperty()], + ); + expect(result).toEqual([]); + }); const ownershipRecord1: view_repo_ownership = { full_repo_name: fullName, diff --git a/packages/repocop/src/remediation/dependency_graph-integrator/send-to-sns.ts b/packages/repocop/src/remediation/dependency_graph-integrator/send-to-sns.ts index 3c223d36a..c09d21367 100644 --- a/packages/repocop/src/remediation/dependency_graph-integrator/send-to-sns.ts +++ b/packages/repocop/src/remediation/dependency_graph-integrator/send-to-sns.ts @@ -202,8 +202,6 @@ export async function sendReposToDependencyGraphIntegrator( const selectedRepos: RepositoryWithDepGraphLanguage[] = []; - let reposWithPrs = 0; - while (selectedRepos.length < repoCount && shuffledRepos.length > 0) { const repo = shuffledRepos.pop(); if (repo) { @@ -221,14 +219,10 @@ export async function sendReposToDependencyGraphIntegrator( ); if (!existingPr) { selectedRepos.push(repo); - } else { - reposWithPrs++; } } } - console.log(`Found ${reposWithPrs} repos with existing PRs`); - const eventsToSend: DependencyGraphIntegratorEvent[] = createSnsEventsForDependencyGraphIntegration(selectedRepos, repoOwners); diff --git a/packages/repocop/src/types.ts b/packages/repocop/src/types.ts index c07065372..261d4fc84 100644 --- a/packages/repocop/src/types.ts +++ b/packages/repocop/src/types.ts @@ -2,7 +2,6 @@ import type { Action } from '@guardian/anghammarad'; import type { Endpoints } from '@octokit/types'; import type { aws_cloudformation_stacks, - github_repository_custom_properties, github_teams, repocop_github_repository_rules, } from '@prisma/client'; @@ -150,4 +149,4 @@ export interface VulnerabilityDigest { subject: string; message: string; actions: Action[]; -} \ No newline at end of file +}