diff --git a/packages/dependency-graph-integrator/src/pull-requests.ts b/packages/dependency-graph-integrator/src/pull-requests.ts index a75b888fa..1158bdd29 100644 --- a/packages/dependency-graph-integrator/src/pull-requests.ts +++ b/packages/dependency-graph-integrator/src/pull-requests.ts @@ -139,53 +139,40 @@ export async function createPrAndAddToProject( ) { if (stage === 'PROD') { const ghClient = octokit ?? (await stageAwareOctokit(stage)); - const existingPullRequest = await getExistingPullRequest( - ghClient, + + const pullRequestResponse = await createPullRequest(ghClient, { repoName, owner, - `${author}[bot]`, - ); + title: prTitle, + body: prBody, + branchName: branch, + changes: [ + { + commitMessage, + files: { + [fileName]: fileContents, + }, + }, + ], + admins, + }); - if (!existingPullRequest) { - const pullRequestResponse = await createPullRequest(ghClient, { + if (pullRequestResponse?.html_url && pullRequestResponse.number) { + console.log( + 'Pull request successfully created:', + pullRequestResponse.html_url, + ); + + await requestTeamReview( + ghClient, repoName, owner, - title: prTitle, - body: prBody, - branchName: branch, - changes: [ - { - commitMessage, - files: { - [fileName]: fileContents, - }, - }, - ], + pullRequestResponse.number, admins, - }); - - if (pullRequestResponse?.html_url && pullRequestResponse.number) { - console.log( - 'Pull request successfully created:', - pullRequestResponse.html_url, - ); - - await requestTeamReview( - ghClient, - repoName, - owner, - pullRequestResponse.number, - admins, - ); - - await addPrToProject(stage, repoName, boardNumber, author); - console.log('Updated project board'); - } - } else { - console.log( - `Existing pull request found. Skipping creating a new one.`, - existingPullRequest.html_url, ); + + await addPrToProject(stage, repoName, boardNumber, author); + console.log('Updated project board'); } } else { console.log(`Testing generation of ${fileName} for ${repoName}`); diff --git a/packages/repocop/src/index.ts b/packages/repocop/src/index.ts index 80c902283..4cda3c3ae 100644 --- a/packages/repocop/src/index.ts +++ b/packages/repocop/src/index.ts @@ -169,6 +169,7 @@ export async function main() { productionWorkflowUsages, repoOwners, dependencyGraphIntegratorRepoCount, + octokit, ); await writeEvaluationTable(repocopRules, prisma); 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 25d1c4295..e8d759eb1 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 @@ -12,6 +12,8 @@ import type { Repository, RepositoryWithDepGraphLanguage, } from 'common/src/types'; +import { getExistingPullRequest } from 'dependency-graph-integrator/src/pull-requests'; +import type { Octokit } from 'octokit'; import type { Config } from '../../config'; import { findContactableOwners, removeRepoOwner } from '../shared-utilities'; @@ -128,6 +130,7 @@ export async function sendReposToDependencyGraphIntegrator( productionWorkflowUsages: guardian_github_actions_usage[], repoOwners: view_repo_ownership[], repoCount: number, + octokit: Octokit, ): Promise { const reposRequiringDepGraphIntegration: RepositoryWithDepGraphLanguage[] = getReposWithoutWorkflows( @@ -141,10 +144,30 @@ export async function sendReposToDependencyGraphIntegrator( `Found ${reposRequiringDepGraphIntegration.length} repos requiring dependency graph integration`, ); - const selectedRepos = shuffle(reposRequiringDepGraphIntegration).slice( - 0, - repoCount, - ); + const shuffledRepos = shuffle(reposRequiringDepGraphIntegration); + + const selectedRepos: RepositoryWithDepGraphLanguage[] = []; + + while (selectedRepos.length < repoCount && shuffledRepos.length > 0) { + const repo = shuffledRepos.pop(); + if (repo) { + console.log('Checking for existing PR for', repo.name); + const existingPr = await getExistingPullRequest( + octokit, + repo.name, + 'guardian', + 'gu-dependency-graph-integrator[bot]', + ); + console.log( + existingPr + ? `Existing PR found for ${repo.name}` + : `PR not found for ${repo.name}`, + ); + if (!existingPr) { + selectedRepos.push(repo); + } + } + } const eventsToSend: DependencyGraphIntegratorEvent[] = createSnsEventsForDependencyGraphIntegration(selectedRepos, repoOwners);