diff --git a/.env.example b/.env.example index ee78008..521453b 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,4 @@ # Replace ghp_xxx with your GitHub PAT token -GH_TOKEN=ghp_xxx +GH_TOKEN_1=ghp_xxx +GH_TOKEN_2=ghp_xxx +GH_TOKEN_3=ghp_xxx diff --git a/README.md b/README.md index 5b7af55..c05549c 100644 --- a/README.md +++ b/README.md @@ -328,9 +328,10 @@ Step-by-step guide on setting up your own Vercel instance. 7. To import a project, click the `Add New...` or `+` button and select the `Project` option. 8. Click the Continue with GitHub button, search for the required Git Repository and import it by clicking the `Import` button. Alternatively, you can import a Third-Party Git Repository using the `Import Third-Party Git Repository` link at the bottom of the page. 9. Create a personal access token (PAT) [here](https://github.com/settings/tokens/new) and enable the `repo` and `user` permissions (this allows access to see private repo and user stats). -10. Add the PAT as an environment variable named `GH_TOKEN`. -11. In build and output settings, you set install command toggle and add command `npm install typescript`. -12. Click deploy, and you're good to go. See your domains to use the API! +10. Add the PAT as an environment variable named `GH_TOKEN_1`. +11. Repeat step number 9 and add PAT up to the `GH_TOKEN_3` environment variable. +12. In build and output settings, you set install command toggle and add command `npm install typescript`. +13. Click deploy, and you're good to go. See your domains to use the API! [![Deploy](https://raw.githubusercontent.com/FajarKim/FajarKim/master/images/deploy-on-vercel.svg)](https://vercel.com/import/project?template=https://github.com/FajarKim/github-readme-profile) diff --git a/src/getToken.ts b/src/getToken.ts index e84e2f7..0be12a3 100644 --- a/src/getToken.ts +++ b/src/getToken.ts @@ -4,7 +4,7 @@ import { getInput } from "@actions/core"; dotenv.config(); /** - * Retrieves the GitHub token from the environment variables or GitHub Actions inputs. + * Retrieves the GitHub token 1 from the environment variables or GitHub Actions inputs. * * @param {boolean} bearerHeader - Flag indicating whether to return the token with 'Bearer' prefix. * @returns {string} - The GitHub token. @@ -12,17 +12,17 @@ dotenv.config(); function getToken(bearerHeader: boolean): string { const getEnvirontment: any = process.env; let getGHEnvirontment: any = Object.keys(getEnvirontment).filter((key) => - key.startsWith("GH_") + key.startsWith("GH_TOKEN_1") ); getGHEnvirontment = getGHEnvirontment.map((key: string) => getEnvirontment[key]); // Select a random GitHub environment variable - const getGHToken: string = + let getGHToken: string = getGHEnvirontment[Math.floor(Math.random() * getGHEnvirontment.length)]; // If no GitHub environment variable is found, get the token from GitHub Actions inputs if (!getGHToken) { - const getGHToken = getInput("github_token"); + getGHToken = getInput("github_token"); if (!getGHToken) { throw new Error("Could not find github token"); @@ -36,4 +36,67 @@ const getEnvirontment: any = process.env; return getGHToken; } +/** + * Retrieves the GitHub token 2 from the environment variables or GitHub Actions inputs. + * + * @param {boolean} bearerHeader - Flag indicating whether to return the token with 'Bearer' prefix. + * @returns {string} - The GitHub token. + */ +function getToken2(bearerHeader: boolean): string { +const getEnvirontment: any = process.env; + let getGHEnvirontment: any = Object.keys(getEnvirontment).filter((key) => + key.startsWith("GH_TOKEN_2") + ); + getGHEnvirontment = getGHEnvirontment.map((key: string) => getEnvirontment[key]); + + // Select a random GitHub environment variable + let getGHToken: string = + getGHEnvirontment[Math.floor(Math.random() * getGHEnvirontment.length)]; + + // If no GitHub environment variable is found, get the token from GitHub Actions inputs + if (!getGHToken) { + getGHToken = getToken(false); + } + + if (bearerHeader) { + return `Bearer ${getGHToken}`; + } + + return getGHToken; +} + +/** + * Retrieves the GitHub token 3 from the environment variables or GitHub Actions inputs. + * + * @param {boolean} bearerHeader - Flag indicating whether to return the token with 'Bearer' prefix. + * @returns {string} - The GitHub token. + */ +function getToken3(bearerHeader: boolean): string { +const getEnvirontment: any = process.env; + let getGHEnvirontment: any = Object.keys(getEnvirontment).filter((key) => + key.startsWith("GH_TOKEN_3") + ); + getGHEnvirontment = getGHEnvirontment.map((key: string) => getEnvirontment[key]); + + // Select a random GitHub environment variable + let getGHToken: string = + getGHEnvirontment[Math.floor(Math.random() * getGHEnvirontment.length)]; + + // If no GitHub environment variable is found, get the token from GitHub Actions inputs + if (!getGHToken) { + getGHToken = getToken(false); + } + + if (bearerHeader) { + return `Bearer ${getGHToken}`; + } + + return getGHToken; +} + +export { + getToken, + getToken2, + getToken3 +}; export default getToken; diff --git a/tests/getToken.test.ts b/tests/getToken.test.ts index 23cf956..a2eb8e7 100644 --- a/tests/getToken.test.ts +++ b/tests/getToken.test.ts @@ -1,4 +1,4 @@ -import getToken from "../src/getToken"; +import { getToken, getToken2, getToken3 } from "../src/getToken"; import * as core from "@actions/core"; jest.mock("dotenv"); @@ -9,7 +9,7 @@ type MockedGetInput = jest.MockedFunction; const mockedGetInput = core.getInput as MockedGetInput; -describe("getToken function", () => { +describe("Test getToken function", () => { const originalEnv = process.env; beforeEach(() => { process.env = { ...originalEnv }; @@ -18,28 +18,92 @@ describe("getToken function", () => { process.env = originalEnv; }); - it("should return an individual token without Bearer prefix", () => { - process.env.GH_TOKEN = "ghp_token"; + it("should return an personal token 1 without Bearer prefix", () => { + process.env.GH_TOKEN_1 = "ghp_token"; const token = getToken(false); expect(token).toEqual("ghp_token"); }); - it("should return an individual token with Bearer prefix", () => { - process.env.GH_TOKEN = "ghp_token"; + it("should return an personal token 1 with Bearer prefix", () => { + process.env.GH_TOKEN_1 = "ghp_token"; const token = getToken(true); expect(token).toEqual("Bearer ghp_token"); }); + it("should return an personal token 2 without Bearer prefix", () => { + process.env.GH_TOKEN_2 = "ghp_token2"; + + const token = getToken2(false); + + expect(token).toEqual("ghp_token2"); + }); + + it("should return an personal token 2 with Bearer prefix", () => { + process.env.GH_TOKEN_2 = "ghp_token2"; + + const token = getToken2(true); + + expect(token).toEqual("Bearer ghp_token2"); + }); + + it("should return an personal token 2 if no token 2 available without Bearer prefix", () => { + process.env.GH_TOKEN_1 = "ghp_token"; + + const token = getToken2(false); + + expect(token).toEqual("ghp_token"); + }); + + it("should return an personal token 2 if no token 2 available with Bearer prefix", () => { + process.env.GH_TOKEN_1 = "ghp_token"; + + const token = getToken2(true); + + expect(token).toEqual("Bearer ghp_token"); + }); + + it("should return an personal token 3 without Bearer prefix", () => { + process.env.GH_TOKEN_3 = "ghp_token3"; + + const token = getToken3(false); + + expect(token).toEqual("ghp_token3"); + }); + + it("should return an personal token 3 with Bearer prefix", () => { + process.env.GH_TOKEN_3 = "ghp_token3"; + + const token = getToken3(true); + + expect(token).toEqual("Bearer ghp_token3"); + }); + + it("should return an personal token 3 if no token 3 available without Bearer prefix", () => { + process.env.GH_TOKEN_1 = "ghp_token"; + + const token = getToken3(false); + + expect(token).toEqual("ghp_token"); + }); + + it("should return an personal token 3 if no token 3 available with Bearer prefix", () => { + process.env.GH_TOKEN_1 = "ghp_token"; + + const token = getToken3(true); + + expect(token).toEqual("Bearer ghp_token"); + }); + it("should return a GitHub Actions bot token without Bearer prefix", () => { mockedGetInput.mockReturnValue("GitHubActionsBotToken"); const token = getToken(false); - expect(token).not.toEqual("GitHubActionsBotToken"); + expect(token).toEqual("GitHubActionsBotToken"); }); it("should return a GitHub Actions bot token with Bearer prefix", () => { @@ -47,7 +111,7 @@ describe("getToken function", () => { const token = getToken(true); - expect(token).not.toEqual("Bearer GitHubActionsBotToken"); + expect(token).toEqual("Bearer GitHubActionsBotToken"); }); it("should throw an error if no tokens are available", () => {