Skip to content

Commit d86d8e2

Browse files
authored
feat(init): Catch GitHub 403 errors and provide a more useful error message (firebase#3089)
1 parent 7f99ac5 commit d86d8e2

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

src/init/features/hosting/github.ts

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import { addServiceAccountToRoles, firebaseRoles } from "../../../gcp/resourceManager";
1818
import { logger } from "../../../logger";
1919
import { prompt } from "../../../prompt";
20-
import { logBullet, logLabeledBullet, logSuccess, reject } from "../../../utils";
20+
import { logBullet, logLabeledBullet, logSuccess, logWarning, reject } from "../../../utils";
2121
import { githubApiOrigin, githubClientId } from "../../../api";
2222
import { Client } from "../../../apiv2";
2323

@@ -401,16 +401,39 @@ async function promptForRepo(
401401
message:
402402
"For which GitHub repository would you like to set up a GitHub workflow? (format: user/repository)",
403403
validate: async (repo: string) => {
404-
// eslint-disable-next-line camelcase
405-
const { body } = await githubApiClient.get<{ key: string; key_id: string }>(
406-
`/repos/${repo}/actions/secrets/public-key`,
407-
{
408-
headers: { Authorization: `token ${ghAccessToken}`, "User-Agent": "Firebase CLI" },
409-
queryParams: { type: "owner" },
404+
try {
405+
// eslint-disable-next-line camelcase
406+
const { body } = await githubApiClient.get<{ key: string; key_id: string }>(
407+
`/repos/${repo}/actions/secrets/public-key`,
408+
{
409+
headers: { Authorization: `token ${ghAccessToken}`, "User-Agent": "Firebase CLI" },
410+
queryParams: { type: "owner" },
411+
}
412+
);
413+
key = body.key;
414+
keyId = body.key_id;
415+
} catch (e) {
416+
if (e.status === 403) {
417+
logger.info();
418+
logger.info();
419+
logWarning(
420+
"The provided authorization cannot be used with this repository. If this repository is in an organization, did you remember to grant access?",
421+
"error"
422+
);
423+
logger.info();
424+
logLabeledBullet(
425+
"Action required",
426+
`Visit this URL to ensure access has been granted to the appropriate organization(s) for the Firebase CLI GitHub OAuth App:`
427+
);
428+
logger.info(
429+
bold.underline(
430+
`https://github.com/settings/connections/applications/${githubClientId}`
431+
)
432+
);
433+
logger.info();
410434
}
411-
);
412-
key = body.key;
413-
keyId = body.key_id;
435+
return false;
436+
}
414437
return true;
415438
},
416439
},

0 commit comments

Comments
 (0)