diff --git a/github-actions/post-approval-changes/lib/main.ts b/github-actions/post-approval-changes/lib/main.ts index de015f434..b1d32e7a5 100644 --- a/github-actions/post-approval-changes/lib/main.ts +++ b/github-actions/post-approval-changes/lib/main.ts @@ -8,23 +8,38 @@ import {ANGULAR_ROBOT, getAuthTokenFor, revokeActiveInstallationToken} from '../ const googleOwnedRobots = ['angular-robot']; async function main() { - let installationClient: Octokit | null = null; + let repoClient: Octokit | null = null; + let googlersOrgClient: Octokit | null = null; try { // Use the `.github` repo from googlers to get an installation that has access to the googlers // user membership. - const token = await getAuthTokenFor(ANGULAR_ROBOT, {owner: 'googlers', repo: '.github'}); - installationClient = new Octokit({auth: token}); + const googlersOrgToken = await getAuthTokenFor(ANGULAR_ROBOT, { + owner: 'googlers', + repo: '.github', + }); + googlersOrgClient = new Octokit({auth: googlersOrgToken}); - await runPostApprovalChangesAction(installationClient); + // Use the `.github` repo from googlers to get an installation that has access to the googlers + // user membership. + const repoToken = await getAuthTokenFor(ANGULAR_ROBOT, context.repo); + repoClient = new Octokit({auth: repoToken}); + + await runPostApprovalChangesAction(googlersOrgClient, repoClient); } finally { - if (installationClient !== null) { - await revokeActiveInstallationToken(installationClient); + if (googlersOrgClient !== null) { + await revokeActiveInstallationToken(googlersOrgClient); + } + if (repoClient !== null) { + await revokeActiveInstallationToken(repoClient); } } } -async function runPostApprovalChangesAction(client: Octokit): Promise { +async function runPostApprovalChangesAction( + googlersOrgClient: Octokit, + repoClient: Octokit, +): Promise { if (context.eventName !== 'pull_request_target') { throw Error('This action can only run for with pull_request_target events'); } @@ -32,7 +47,7 @@ async function runPostApprovalChangesAction(client: Octokit): Promise { const actionUser = context.actor; - if (await isGooglerOrgMember(client, actionUser)) { + if (await isGooglerOrgMember(googlersOrgClient, actionUser)) { core.info( 'Action performed by an account in the Googler Github Org, skipping as post approval changes are allowed.', ); @@ -60,7 +75,11 @@ async function runPostApprovalChangesAction(client: Octokit): Promise { const pull_number = context.issue.number; /** List of reviews for the pull request. */ - const allReviews = await client.paginate(client.pulls.listReviews, {owner, pull_number, repo}); + const allReviews = await repoClient.paginate(repoClient.pulls.listReviews, { + owner, + pull_number, + repo, + }); /** Set of reviewers whose latest review has already been processed. */ const knownReviewers = new Set(); /** The latest approving reviews for each reviewer on the pull request. */ @@ -74,7 +93,7 @@ async function runPostApprovalChangesAction(client: Octokit): Promise { continue; } // Only consider reviews by Googlers for this check. - if (!(await isGooglerOrgMember(client, user))) { + if (!(await isGooglerOrgMember(googlersOrgClient, user))) { continue; } knownReviewers.add(user); @@ -104,7 +123,7 @@ async function runPostApprovalChangesAction(client: Octokit): Promise { const reviewToRerequest = reviews[0]; core.info(`Requesting a new review from ${reviewToRerequest.user!.login}`); - await client.pulls.requestReviewers({ + await repoClient.pulls.requestReviewers({ owner, pull_number, repo, diff --git a/github-actions/post-approval-changes/main.js b/github-actions/post-approval-changes/main.js index 20d4439cb..b820eee96 100644 --- a/github-actions/post-approval-changes/main.js +++ b/github-actions/post-approval-changes/main.js @@ -23368,25 +23368,34 @@ async function revokeActiveInstallationToken(githubOrToken) { // var googleOwnedRobots = ["angular-robot"]; async function main() { - let installationClient = null; + let repoClient = null; + let googlersOrgClient = null; try { - const token = await getAuthTokenFor(ANGULAR_ROBOT, { owner: "googlers", repo: ".github" }); - installationClient = new import_rest2.Octokit({ auth: token }); - await runPostApprovalChangesAction(installationClient); + const googlersOrgToken = await getAuthTokenFor(ANGULAR_ROBOT, { + owner: "googlers", + repo: ".github" + }); + googlersOrgClient = new import_rest2.Octokit({ auth: googlersOrgToken }); + const repoToken = await getAuthTokenFor(ANGULAR_ROBOT, import_github2.context.repo); + repoClient = new import_rest2.Octokit({ auth: repoToken }); + await runPostApprovalChangesAction(googlersOrgClient, repoClient); } finally { - if (installationClient !== null) { - await revokeActiveInstallationToken(installationClient); + if (googlersOrgClient !== null) { + await revokeActiveInstallationToken(googlersOrgClient); + } + if (repoClient !== null) { + await revokeActiveInstallationToken(repoClient); } } } -async function runPostApprovalChangesAction(client) { +async function runPostApprovalChangesAction(googlersOrgClient, repoClient) { var _a; if (import_github2.context.eventName !== "pull_request_target") { throw Error("This action can only run for with pull_request_target events"); } const { pull_request: pr } = import_github2.context.payload; const actionUser = import_github2.context.actor; - if (await isGooglerOrgMember(client, actionUser)) { + if (await isGooglerOrgMember(googlersOrgClient, actionUser)) { core.info("Action performed by an account in the Googler Github Org, skipping as post approval changes are allowed."); return; } @@ -23402,7 +23411,11 @@ async function runPostApprovalChangesAction(client) { } const { repo, owner } = import_github2.context.issue; const pull_number = import_github2.context.issue.number; - const allReviews = await client.paginate(client.pulls.listReviews, { owner, pull_number, repo }); + const allReviews = await repoClient.paginate(repoClient.pulls.listReviews, { + owner, + pull_number, + repo + }); const knownReviewers = /* @__PURE__ */ new Set(); const reviews = []; for (let review of allReviews.concat().reverse()) { @@ -23410,7 +23423,7 @@ async function runPostApprovalChangesAction(client) { if (knownReviewers.has(user)) { continue; } - if (!await isGooglerOrgMember(client, user)) { + if (!await isGooglerOrgMember(googlersOrgClient, user)) { continue; } knownReviewers.add(user); @@ -23435,7 +23448,7 @@ async function runPostApprovalChangesAction(client) { } const reviewToRerequest = reviews[0]; core.info(`Requesting a new review from ${reviewToRerequest.user.login}`); - await client.pulls.requestReviewers({ + await repoClient.pulls.requestReviewers({ owner, pull_number, repo,