diff --git a/dist/main.cjs b/dist/main.cjs index 1c7250f..463e9ad 100644 --- a/dist/main.cjs +++ b/dist/main.cjs @@ -23305,12 +23305,12 @@ async function getTokenFromEnterprise(request2, auth5, enterprise, permissions) } }); } catch (error2) { - if (error2.status !== 404) { - throw error2; + if (error2.status === 404) { + throw new Error( + `No enterprise installation found matching the name ${enterprise}.` + ); } - throw new Error( - `No enterprise installation found matching the name ${enterprise}.` - ); + throw error2; } return createInstallationAuthResult(auth5, response.data, permissions); } diff --git a/lib/main.js b/lib/main.js index b05b761..c9fc320 100644 --- a/lib/main.js +++ b/lib/main.js @@ -220,14 +220,13 @@ async function getTokenFromEnterprise(request, auth, enterprise, permissions) { }, }); } catch (error) { - /* c8 ignore next 3 */ - if (error.status !== 404) { - throw error; + if (error.status === 404) { + throw new Error( + `No enterprise installation found matching the name ${enterprise}.` + ); } - throw new Error( - `No enterprise installation found matching the name ${enterprise}.` - ); + throw error; } // Get token for the enterprise installation diff --git a/tests/index.js.snapshot b/tests/index.js.snapshot index 2b250a8..be443b9 100644 --- a/tests/index.js.snapshot +++ b/tests/index.js.snapshot @@ -17,6 +17,25 @@ POST /api/v3/app/installations/123456/access_tokens {"repositories":["create-github-app-token"]} `; +exports[`main-enterprise-fail-response.test.js > stdout 1`] = ` +Creating enterprise installation token for enterprise "test-enterprise". +Failed to create token for enterprise "test-enterprise" (attempt 1): GitHub API not available +::add-mask::ghs_16C7e42F292c6912E7710c838347Ae178B4a + +::set-output name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a + +::set-output name=installation-id::123456 + +::set-output name=app-slug::github-actions +::save-state name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a +::save-state name=expiresAt::2016-07-11T22:14:10Z +--- REQUESTS --- +GET /enterprises/test-enterprise/installation +GET /enterprises/test-enterprise/installation +POST /app/installations/123456/access_tokens +null +`; + exports[`main-enterprise-installation-not-found.test.js > stderr 1`] = ` Error: No enterprise installation found matching the name test-enterprise. at getTokenFromEnterprise (file:///lib/main.js::) diff --git a/tests/main-enterprise-fail-response.test.js b/tests/main-enterprise-fail-response.test.js new file mode 100644 index 0000000..068e2cb --- /dev/null +++ b/tests/main-enterprise-fail-response.test.js @@ -0,0 +1,39 @@ +import { test } from "./main.js"; + +// Verify enterprise installation lookup retries when the GitHub API returns a 500 error. +await test((mockPool) => { + process.env.INPUT_ENTERPRISE = "test-enterprise"; + delete process.env.INPUT_OWNER; + delete process.env.INPUT_REPOSITORIES; + + const mockInstallationId = "123456"; + const mockAppSlug = "github-actions"; + + mockPool + .intercept({ + path: "/enterprises/test-enterprise/installation", + method: "GET", + headers: { + accept: "application/vnd.github.v3+json", + "user-agent": "actions/create-github-app-token", + // Intentionally omitting the `authorization` header, since JWT creation is not idempotent. + }, + }) + .reply(500, "GitHub API not available"); + + mockPool + .intercept({ + path: "/enterprises/test-enterprise/installation", + method: "GET", + headers: { + accept: "application/vnd.github.v3+json", + "user-agent": "actions/create-github-app-token", + // Intentionally omitting the `authorization` header, since JWT creation is not idempotent. + }, + }) + .reply( + 200, + { id: mockInstallationId, app_slug: mockAppSlug }, + { headers: { "content-type": "application/json" } }, + ); +});