Skip to content

Commit

Permalink
refactor: check for existing repo in send-to-sns
Browse files Browse the repository at this point in the history
  • Loading branch information
tjsilver committed Nov 26, 2024
1 parent 1ff180c commit cc7fd61
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 44 deletions.
67 changes: 27 additions & 40 deletions packages/dependency-graph-integrator/src/pull-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
1 change: 1 addition & 0 deletions packages/repocop/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export async function main() {
productionWorkflowUsages,
repoOwners,
dependencyGraphIntegratorRepoCount,
octokit,
);

await writeEvaluationTable(repocopRules, prisma);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -128,6 +130,7 @@ export async function sendReposToDependencyGraphIntegrator(
productionWorkflowUsages: guardian_github_actions_usage[],
repoOwners: view_repo_ownership[],
repoCount: number,
octokit: Octokit,
): Promise<void> {
const reposRequiringDepGraphIntegration: RepositoryWithDepGraphLanguage[] =
getReposWithoutWorkflows(
Expand All @@ -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);
Expand Down

0 comments on commit cc7fd61

Please sign in to comment.