Skip to content

Commit

Permalink
chore: added environment variable tokens 2 and 3 (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
FajarKim authored Jan 6, 2024
1 parent db59240 commit e06891c
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
71 changes: 67 additions & 4 deletions src/getToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ 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.
*/
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");
Expand All @@ -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;
80 changes: 72 additions & 8 deletions tests/getToken.test.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -9,7 +9,7 @@ type MockedGetInput = jest.MockedFunction<typeof core.getInput>;
const mockedGetInput = core.getInput as MockedGetInput;


describe("getToken function", () => {
describe("Test getToken function", () => {
const originalEnv = process.env;
beforeEach(() => {
process.env = { ...originalEnv };
Expand All @@ -18,36 +18,100 @@ 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", () => {
mockedGetInput.mockReturnValue("GitHubActionsBotToken");

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", () => {
Expand Down

0 comments on commit e06891c

Please sign in to comment.