Skip to content

Commit

Permalink
Adding token to input again
Browse files Browse the repository at this point in the history
  • Loading branch information
darpanLalwani committed Sep 24, 2024
1 parent c0d5032 commit 7a8d7d8
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 4 deletions.
1 change: 1 addition & 0 deletions setup-env/config/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
ACCESS_KEY: 'access-key',
BUILD_NAME: 'build-name',
PROJECT_NAME: 'project-name',
GITHUB_TOKEN: 'github-token',
GITHUB_APP: 'github-app',
},

Expand Down
31 changes: 29 additions & 2 deletions setup-env/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
ACCESS_KEY: 'access-key',
BUILD_NAME: 'build-name',
PROJECT_NAME: 'project-name',
GITHUB_TOKEN: 'github-token',
GITHUB_APP: 'github-app',
},

Expand Down Expand Up @@ -9361,6 +9362,7 @@ class ActionInput {
this.buildName = core.getInput(INPUT.BUILD_NAME);
this.projectName = core.getInput(INPUT.PROJECT_NAME);
this.githubApp = core.getInput(INPUT.GITHUB_APP);
this.githubToken = core.getInput(INPUT.GITHUB_TOKEN);
this.rerunAttempt = process?.env?.GITHUB_RUN_ATTEMPT;
this.runId = process?.env?.GITHUB_RUN_ID;
this.repository = process?.env?.GITHUB_REPOSITORY;
Expand All @@ -9377,6 +9379,7 @@ class ActionInput {
this.buildName = InputValidator.validateBuildName(this.buildName);
this.projectName = InputValidator.validateProjectName(this.projectName);
this.githubApp = InputValidator.validateGithubAppName(this.githubApp);
this.githubToken = InputValidator.validateGithubToken(this.githubToken);
}

/**
Expand Down Expand Up @@ -9412,7 +9415,8 @@ class ActionInput {
}

// Ensure runId, repository, username, and accessKey are valid
if (!this.runId || !this.repository || this.repository === 'none' || !this.username || !this.accessKey) {
if (!this.runId || !this.repository || this.repository === 'none'
|| !this.githubToken || this.githubToken === 'none' || !this.username || !this.accessKey) {
return false;
}

Expand All @@ -9426,7 +9430,7 @@ class ActionInput {
const runDetailsUrl = `https://api.github.com/repos/${this.repository}/actions/runs/${this.runId}`;
const runDetailsResponse = await axios.get(runDetailsUrl, {
headers: {
Authorization: `token ${process.env.GITHUB_TOKEN}`,
Authorization: `token ${this.githubToken}`,
Accept: 'application/vnd.github.v3+json',
},
});
Expand Down Expand Up @@ -9613,6 +9617,29 @@ class InputValidator {

throw new Error("Invalid input for 'github-app'. Must be a valid string.");
}

/**
* Validates the GitHub token input to ensure it is a valid non-empty string.
* If the input is 'none' or not provided, it returns 'none'.
* @param {string} githubToken Input for 'github-token'
* @returns {string} The validated GitHub token, or 'none' if input is 'none' or invalid
* @throws {Error} If the input is not a valid non-empty string
*/
static validateGithubToken(githubToken) {
if (typeof githubToken !== 'string') {
throw new Error("Invalid input for 'github-token'. Must be a valid non-empty string.");
}

if (githubToken.toLowerCase() === 'none') {
return 'none';
}

if (githubToken.trim().length > 0) {
return githubToken;
}

throw new Error("Invalid input for 'github-token'. Must be a valid non-empty string.");
}
}

module.exports = InputValidator;
Expand Down
7 changes: 5 additions & 2 deletions setup-env/src/actionInput/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ActionInput {
this.buildName = core.getInput(INPUT.BUILD_NAME);
this.projectName = core.getInput(INPUT.PROJECT_NAME);
this.githubApp = core.getInput(INPUT.GITHUB_APP);
this.githubToken = core.getInput(INPUT.GITHUB_TOKEN);
this.rerunAttempt = process?.env?.GITHUB_RUN_ATTEMPT;
this.runId = process?.env?.GITHUB_RUN_ID;
this.repository = process?.env?.GITHUB_REPOSITORY;
Expand All @@ -49,6 +50,7 @@ class ActionInput {
this.buildName = InputValidator.validateBuildName(this.buildName);
this.projectName = InputValidator.validateProjectName(this.projectName);
this.githubApp = InputValidator.validateGithubAppName(this.githubApp);
this.githubToken = InputValidator.validateGithubToken(this.githubToken);
}

/**
Expand Down Expand Up @@ -84,7 +86,8 @@ class ActionInput {
}

// Ensure runId, repository, username, and accessKey are valid
if (!this.runId || !this.repository || this.repository === 'none' || !this.username || !this.accessKey) {
if (!this.runId || !this.repository || this.repository === 'none'
|| !this.githubToken || this.githubToken === 'none' || !this.username || !this.accessKey) {
return false;
}

Expand All @@ -98,7 +101,7 @@ class ActionInput {
const runDetailsUrl = `https://api.github.com/repos/${this.repository}/actions/runs/${this.runId}`;
const runDetailsResponse = await axios.get(runDetailsUrl, {
headers: {
Authorization: `token ${process.env.GITHUB_TOKEN}`,
Authorization: `token ${this.githubToken}`,
Accept: 'application/vnd.github.v3+json',
},
});
Expand Down
23 changes: 23 additions & 0 deletions setup-env/src/actionInput/inputValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,29 @@ class InputValidator {

throw new Error("Invalid input for 'github-app'. Must be a valid string.");
}

/**
* Validates the GitHub token input to ensure it is a valid non-empty string.
* If the input is 'none' or not provided, it returns 'none'.
* @param {string} githubToken Input for 'github-token'
* @returns {string} The validated GitHub token, or 'none' if input is 'none' or invalid
* @throws {Error} If the input is not a valid non-empty string
*/
static validateGithubToken(githubToken) {
if (typeof githubToken !== 'string') {
throw new Error("Invalid input for 'github-token'. Must be a valid non-empty string.");
}

if (githubToken.toLowerCase() === 'none') {
return 'none';
}

if (githubToken.trim().length > 0) {
return githubToken;
}

throw new Error("Invalid input for 'github-token'. Must be a valid non-empty string.");
}
}

module.exports = InputValidator;
12 changes: 12 additions & 0 deletions setup-env/test/actionInput/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('Action Input operations for fetching all inputs, triggering validation
sinon.stub(InputValidator, 'validateBuildName').returns('validatedBuildName');
sinon.stub(InputValidator, 'validateProjectName').returns('validatedProjectName');
sinon.stub(InputValidator, 'validateGithubAppName').returns('validatedAppName');
sinon.stub(InputValidator, 'validateGithubToken').returns('validatedToken');

// Provide required inputs
stubbedInput.withArgs(INPUT.USERNAME, { required: true }).returns('someUsername');
Expand Down Expand Up @@ -66,6 +67,14 @@ describe('Action Input operations for fetching all inputs, triggering validation
expect(e.message).to.eq('Action input failed for reason: Access Key Required');
}
});

it('Takes input and validates GitHub token and app name successfully', () => {
stubbedInput.withArgs(INPUT.GITHUB_TOKEN).returns('someToken');
stubbedInput.withArgs(INPUT.GITHUB_APP).returns('someApp');
const actionInput = new ActionInput();
expect(actionInput.githubToken).to.eq('validatedToken');
expect(actionInput.githubApp).to.eq('validatedAppName');
});
});

context('Set Environment Variables', () => {
Expand Down Expand Up @@ -120,6 +129,7 @@ describe('Action Input operations for fetching all inputs, triggering validation
sinon.stub(InputValidator, 'validateBuildName').returns('validatedBuildName');
sinon.stub(InputValidator, 'validateProjectName').returns('validatedProjectName');
sinon.stub(InputValidator, 'validateGithubAppName').returns('validatedAppName');
sinon.stub(InputValidator, 'validateGithubToken').returns('validatedToken');

// Provide required inputs
stubbedInput.withArgs(INPUT.USERNAME, { required: true }).returns('someUsername');
Expand Down Expand Up @@ -177,6 +187,7 @@ describe('Action Input operations for fetching all inputs, triggering validation
sinon.stub(InputValidator, 'validateBuildName').returns('validatedBuildName');
sinon.stub(InputValidator, 'validateProjectName').returns('validatedProjectName');
sinon.stub(InputValidator, 'validateGithubAppName').returns('validatedAppName');
sinon.stub(InputValidator, 'validateGithubToken').returns('validatedToken');

// Provide required inputs
stubbedInput.withArgs(INPUT.USERNAME, { required: true }).returns('someUsername');
Expand Down Expand Up @@ -227,6 +238,7 @@ describe('Action Input operations for fetching all inputs, triggering validation
sinon.stub(InputValidator, 'validateBuildName').returns('validatedBuildName');
sinon.stub(InputValidator, 'validateProjectName').returns('validatedProjectName');
sinon.stub(InputValidator, 'validateGithubAppName').returns('validatedAppName');
sinon.stub(InputValidator, 'validateGithubToken').returns('validatedToken');

// Provide required inputs
stubbedInput.withArgs(INPUT.USERNAME, { required: true }).returns('someUsername');
Expand Down
23 changes: 23 additions & 0 deletions setup-env/test/actionInput/inputValidator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,29 @@ describe('InputValidator class to validate individual fields of the action input
expect(InputValidator.validateGithubAppName(validAppName)).to.eq(validAppName);
});
});

context('Validates GitHub Token', () => {
it("Returns 'none' if the token is not provided", () => {
expect(() => InputValidator.validateGithubToken()).to.throw("Invalid input for 'github-token'. Must be a valid non-empty string.");
});

it("Returns 'none' if the token is 'none' (case insensitive)", () => {
expect(InputValidator.validateGithubToken('None')).to.eq('none');
});

it('Throws an error if the token is an empty string', () => {
expect(() => InputValidator.validateGithubToken('')).to.throw("Invalid input for 'github-token'. Must be a valid non-empty string.");
});

it('Throws an error if the token is not a valid string', () => {
expect(() => InputValidator.validateGithubToken(123)).to.throw("Invalid input for 'github-token'. Must be a valid non-empty string.");
});

it('Returns the token if it is a valid non-empty string and not "none"', () => {
const validToken = 'someValidToken';
expect(InputValidator.validateGithubToken(validToken)).to.eq(validToken);
});
});
});
});
});

0 comments on commit 7a8d7d8

Please sign in to comment.