|
| 1 | +// @ts-check |
| 2 | +import { MockAgent, setGlobalDispatcher } from "undici"; |
| 3 | + |
| 4 | +// Set required environment variables and inputs |
| 5 | +process.env.GITHUB_REPOSITORY = "actions/create-github-app-token"; |
| 6 | +// inputs are set as environment variables with the prefix INPUT_ |
| 7 | +// https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs |
| 8 | +process.env.INPUT_APP_ID = "123456"; |
| 9 | +process.env.INPUT_PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY----- |
| 10 | +MIIEowIBAAKCAQEA280nfuUM9w00Ib9E2rvZJ6Qu3Ua3IqR34ZlK53vn/Iobn2EL |
| 11 | +Z9puc5Q/nFBU15NKwHyQNb+OG2hTCkjd1Xi9XPzEOH1r42YQmTGq8YCkUSkk6KZA |
| 12 | +5dnhLwN9pFquT9fQgrf4r1D5GJj3rqvj8JDr1sBmunArqY5u4gziSrIohcjLIZV0 |
| 13 | +cIMz/RUIMe/EAsNeiwzEteHAtf/WpMs+OfF94SIUrDlkPr0H0H3DER8l1HZAvE0e |
| 14 | +eD3ZJ6njrF6UHQWDVrekSTB0clpVTTU9TMpe+gs2nnFww9G8As+WsW8xHVjVipJy |
| 15 | +AwqBhiR+s7wlcbh2i0NQqt8GL9/jIFTmleiwsQIDAQABAoIBAHNyP8pgl/yyzKzk |
| 16 | +/0871wUBMTQ7zji91dGCaFtJM0HrcDK4D/uOOPEv7nE1qDpKPLr5Me1pHUS7+NGw |
| 17 | +EAPtlNhgUtew2JfppdIwyi5qeOPADoi7ud6AH8xHsxg+IMwC+JuP8WhzyUHoJj9y |
| 18 | +PRi/pX94Mvy9qdE25HqKddjx1mLdaHhxqPkr16/em23uYZqm1lORsCPD3vTlthj7 |
| 19 | +WiEbBSqmpYvjj8iFP4yFk2N+LvuWgSilRzq1Af3qE7PUp4xhjmcOPs78Sag1T7nl |
| 20 | +ww/pgqCegISABHik7j++/5T+UlI5cLsyp/XENU9zAd4kCIczwNKpun2bn+djJdft |
| 21 | +ravyX4ECgYEA+k2mHfi1zwKF3wT+cJbf30+uXrJczK2yZ33//4RKnhBaq7nSbQAI |
| 22 | +nhWz2JESBK0TEo0+L7yYYq3HnT9vcES5R1NxzruH9wXgxssSx3JUj6w1raXYPh3B |
| 23 | ++1XpYQsa6/bo2LmBELEx47F8FQbNgD5dmTJ4jBrf6MV4rRh9h6Bs7UkCgYEA4M3K |
| 24 | +eAw52c2MNMIxH/LxdOQtEBq5GMu3AQC8I64DSSRrAoiypfEgyTV6S4gWJ5TKgYfD |
| 25 | +zclnOVJF+tITe3neO9wHoZp8iCjCnoijcT6p2cJ4IaW68LEHPOokWBk0EpLjF4p2 |
| 26 | +7sFi9+lUpXYXfCN7aMJ77QpGzB7dNsBf0KUxMCkCgYEAjw/mjGbk82bLwUaHby6s |
| 27 | +0mQmk7V6WPpGZ+Sadx7TzzglutVAslA8nK5m1rdEBywtJINaMcqnhm8xEm15cj+1 |
| 28 | +blEBUVnaQpQ3fyf+mcR9FIknPRL3X7l+b/sQowjH4GqFd6m/XR0KGMwO0a3Lsyry |
| 29 | +MGeqgtmxdMe5S6YdyXEmERECgYAgQsgklDSVIh9Vzux31kh6auhgoEUh3tJDbZSS |
| 30 | +Vj2YeIZ21aE1mTYISglj34K2aW7qSc56sMWEf18VkKJFHQccdgYOVfo7HAZZ8+fo |
| 31 | +r4J2gqb0xTDfq7gLMNrIXc2QQM4gKbnJp60JQM3p9NmH8huavBZGvSvNzTwXyGG3 |
| 32 | +so0tiQKBgGQXZaxaXhYUcxYHuCkQ3V4Vsj3ezlM92xXlP32SGFm3KgFhYy9kATxw |
| 33 | +Cax1ytZzvlrKLQyQFVK1COs2rHt7W4cJ7op7C8zXfsigXCiejnS664oAuX8sQZID |
| 34 | +x3WQZRiXlWejSMUAHuMwXrhGlltF3lw83+xAjnqsVp75kGS6OH61 |
| 35 | +-----END RSA PRIVATE KEY-----`; // This key is invalidated. It’s from https://github.com/octokit/auth-app.js/issues/465#issuecomment-1564998327. |
| 36 | + |
| 37 | +// Set up mocking |
| 38 | +const mockAgent = new MockAgent(); |
| 39 | +mockAgent.disableNetConnect(); |
| 40 | +setGlobalDispatcher(mockAgent); |
| 41 | +const mockPool = mockAgent.get("https://api.github.com"); |
| 42 | + |
| 43 | +// Calling `auth({ type: "app" })` to obtain a JWT doesn’t make network requests, so no need to intercept. |
| 44 | + |
| 45 | +// Mock installation id request |
| 46 | +const mockInstallationId = "123456"; |
| 47 | +mockPool |
| 48 | + .intercept({ |
| 49 | + path: `/repos/${process.env.GITHUB_REPOSITORY}/installation`, |
| 50 | + method: "GET", |
| 51 | + headers: { |
| 52 | + accept: "application/vnd.github.v3+json", |
| 53 | + "user-agent": "actions/create-github-app-token", |
| 54 | + // Intentionally omitting the `authorization` header, since JWT creation is not idempotent. |
| 55 | + }, |
| 56 | + }) |
| 57 | + .reply( |
| 58 | + 200, |
| 59 | + { id: mockInstallationId }, |
| 60 | + { headers: { "content-type": "application/json" } } |
| 61 | + ); |
| 62 | + |
| 63 | +// Mock installation access token request |
| 64 | +const mockInstallationAccessToken = "ghs_16C7e42F292c6912E7710c838347Ae178B4a"; // This token is invalidated. It’s from https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#create-an-installation-access-token-for-an-app. |
| 65 | +mockPool |
| 66 | + .intercept({ |
| 67 | + path: `/app/installations/${mockInstallationId}/access_tokens`, |
| 68 | + method: "POST", |
| 69 | + headers: { |
| 70 | + accept: "application/vnd.github.v3+json", |
| 71 | + "user-agent": "actions/create-github-app-token", |
| 72 | + // Note: Intentionally omitting the `authorization` header, since JWT creation is not idempotent. |
| 73 | + }, |
| 74 | + }) |
| 75 | + .reply(201, { token: mockInstallationAccessToken }); |
| 76 | + |
| 77 | +// Run the main script |
| 78 | +await import("../main.js"); |
0 commit comments