Skip to content

Commit 77b2017

Browse files
authored
fix: only request org installation for googlers organization (#2730)
It seems that we no longer have an installation on a specific repository, but checking via the still-active organization installation should be sufficient. This commit attempts this.
1 parent cddc66a commit 77b2017

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

github-actions/post-approval-changes/lib/main.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,17 @@ async function main() {
1212
let googlersOrgClient: Octokit | null = null;
1313

1414
try {
15-
// Use the `.github` repo from googlers to get an installation that has access to the googlers
16-
// user membership.
17-
const googlersOrgToken = await getAuthTokenFor(ANGULAR_ROBOT, {
18-
owner: 'googlers',
19-
repo: '.github',
20-
});
21-
googlersOrgClient = new Octokit({auth: googlersOrgToken});
22-
23-
// Use the `.github` repo from googlers to get an installation that has access to the googlers
24-
// user membership.
2515
const repoToken = await getAuthTokenFor(ANGULAR_ROBOT, context.repo);
16+
const googlersOrgToken = await getGooglersOrgInstallationToken();
17+
2618
// TODO: remove once GHA supports node18 as a target runner for Javascript action
2719
repoClient = new Octokit({auth: repoToken, request: {fetch}});
2820

29-
await runPostApprovalChangesAction(googlersOrgClient, repoClient);
21+
if (googlersOrgToken !== null) {
22+
googlersOrgClient = new Octokit({auth: googlersOrgToken, request: {fetch}});
23+
}
24+
25+
await runPostApprovalChangesAction(googlersOrgClient ?? repoClient, repoClient);
3026
} finally {
3127
if (googlersOrgClient !== null) {
3228
await revokeActiveInstallationToken(googlersOrgClient);
@@ -37,8 +33,23 @@ async function main() {
3733
}
3834
}
3935

36+
async function getGooglersOrgInstallationToken(): Promise<string | null> {
37+
try {
38+
// Use the `.github` repo from googlers to get an installation that has access to the googlers
39+
// user membership.
40+
return await getAuthTokenFor(ANGULAR_ROBOT, {
41+
org: 'googlers',
42+
});
43+
} catch (e) {
44+
console.error('Could not retrieve installation token for `googlers` org.');
45+
console.error(e);
46+
}
47+
48+
return null;
49+
}
50+
4051
async function runPostApprovalChangesAction(
41-
googlersOrgClient: Octokit,
52+
membershipCheckClient: Octokit,
4253
repoClient: Octokit,
4354
): Promise<void> {
4455
if (context.eventName !== 'pull_request_target') {
@@ -48,7 +59,7 @@ async function runPostApprovalChangesAction(
4859

4960
const actionUser = context.actor;
5061

51-
if (await isGooglerOrgMember(googlersOrgClient, actionUser)) {
62+
if (await isGooglerOrgMember(membershipCheckClient, actionUser)) {
5263
core.info(
5364
'Action performed by an account in the Googler Github Org, skipping as post approval changes are allowed.',
5465
);
@@ -94,7 +105,7 @@ async function runPostApprovalChangesAction(
94105
continue;
95106
}
96107
// Only consider reviews by Googlers for this check.
97-
if (!(await isGooglerOrgMember(googlersOrgClient, user))) {
108+
if (!(await isGooglerOrgMember(membershipCheckClient, user))) {
98109
continue;
99110
}
100111
knownReviewers.add(user);
@@ -157,6 +168,7 @@ async function isGooglerOrgMember(client: Octokit, username: string): Promise<bo
157168
if (context.repo.owner === 'angular') {
158169
main().catch((e: Error) => {
159170
console.error(e);
171+
console.error(e.stack);
160172
core.setFailed(e.message);
161173
});
162174
} else {

github-actions/post-approval-changes/main.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47842,14 +47842,13 @@ async function main() {
4784247842
let repoClient = null;
4784347843
let googlersOrgClient = null;
4784447844
try {
47845-
const googlersOrgToken = await getAuthTokenFor(ANGULAR_ROBOT, {
47846-
owner: "googlers",
47847-
repo: ".github"
47848-
});
47849-
googlersOrgClient = new Octokit2({ auth: googlersOrgToken });
4785047845
const repoToken = await getAuthTokenFor(ANGULAR_ROBOT, import_github2.context.repo);
47846+
const googlersOrgToken = await getGooglersOrgInstallationToken();
4785147847
repoClient = new Octokit2({ auth: repoToken, request: { fetch } });
47852-
await runPostApprovalChangesAction(googlersOrgClient, repoClient);
47848+
if (googlersOrgToken !== null) {
47849+
googlersOrgClient = new Octokit2({ auth: googlersOrgToken, request: { fetch } });
47850+
}
47851+
await runPostApprovalChangesAction(googlersOrgClient ?? repoClient, repoClient);
4785347852
} finally {
4785447853
if (googlersOrgClient !== null) {
4785547854
await revokeActiveInstallationToken(googlersOrgClient);
@@ -47859,14 +47858,25 @@ async function main() {
4785947858
}
4786047859
}
4786147860
}
47862-
async function runPostApprovalChangesAction(googlersOrgClient, repoClient) {
47861+
async function getGooglersOrgInstallationToken() {
47862+
try {
47863+
return await getAuthTokenFor(ANGULAR_ROBOT, {
47864+
org: "googlers"
47865+
});
47866+
} catch (e) {
47867+
console.error("Could not retrieve installation token for `googlers` org.");
47868+
console.error(e);
47869+
}
47870+
return null;
47871+
}
47872+
async function runPostApprovalChangesAction(membershipCheckClient, repoClient) {
4786347873
var _a;
4786447874
if (import_github2.context.eventName !== "pull_request_target") {
4786547875
throw Error("This action can only run for with pull_request_target events");
4786647876
}
4786747877
const { pull_request: pr } = import_github2.context.payload;
4786847878
const actionUser = import_github2.context.actor;
47869-
if (await isGooglerOrgMember(googlersOrgClient, actionUser)) {
47879+
if (await isGooglerOrgMember(membershipCheckClient, actionUser)) {
4787047880
core.info("Action performed by an account in the Googler Github Org, skipping as post approval changes are allowed.");
4787147881
return;
4787247882
}
@@ -47894,7 +47904,7 @@ async function runPostApprovalChangesAction(googlersOrgClient, repoClient) {
4789447904
if (knownReviewers.has(user)) {
4789547905
continue;
4789647906
}
47897-
if (!await isGooglerOrgMember(googlersOrgClient, user)) {
47907+
if (!await isGooglerOrgMember(membershipCheckClient, user)) {
4789847908
continue;
4789947909
}
4790047910
knownReviewers.add(user);
@@ -47939,6 +47949,7 @@ async function isGooglerOrgMember(client, username) {
4793947949
if (import_github2.context.repo.owner === "angular") {
4794047950
main().catch((e) => {
4794147951
console.error(e);
47952+
console.error(e.stack);
4794247953
core.setFailed(e.message);
4794347954
});
4794447955
} else {

0 commit comments

Comments
 (0)