diff --git a/setup-env/config/constants.js b/setup-env/config/constants.js index 421a479..0b9243e 100644 --- a/setup-env/config/constants.js +++ b/setup-env/config/constants.js @@ -4,7 +4,6 @@ module.exports = { ACCESS_KEY: 'access-key', BUILD_NAME: 'build-name', PROJECT_NAME: 'project-name', - GITHUB_TOKEN: 'github-token', GITHUB_APP: 'github-app', }, diff --git a/setup-env/src/actionInput/index.js b/setup-env/src/actionInput/index.js index 942a864..e6b22cf 100644 --- a/setup-env/src/actionInput/index.js +++ b/setup-env/src/actionInput/index.js @@ -1,6 +1,5 @@ const core = require('@actions/core'); const axios = require('axios'); -const github = require("@actions/github"); const InputValidator = require('./inputValidator'); const constants = require('../../config/constants'); const { BROWSERSTACK_INTEGRATIONS } = require("../../config/constants"); @@ -33,12 +32,10 @@ class ActionInput { // non-compulsory fields this.buildName = core.getInput(INPUT.BUILD_NAME); this.projectName = core.getInput(INPUT.PROJECT_NAME); - this.githubToken = core.getInput(INPUT.GITHUB_TOKEN); this.githubApp = core.getInput(INPUT.GITHUB_APP); this.rerunAttempt = process?.env?.GITHUB_RUN_ATTEMPT; this.runId = process?.env?.GITHUB_RUN_ID; - this.repository = `${github?.context?.repo?.owner}/${github?.context?.repo?.repo}`; - + this.repository = process?.env?.GITHUB_REPOSITORY; } catch (e) { throw Error(`Action input failed for reason: ${e.message}`); } @@ -51,7 +48,6 @@ class ActionInput { this.username = InputValidator.updateUsername(this.username); this.buildName = InputValidator.validateBuildName(this.buildName); this.projectName = InputValidator.validateProjectName(this.projectName); - this.githubToken = InputValidator.validateGithubToken(this.githubToken); this.githubApp = InputValidator.validateGithubAppName(this.githubApp); } @@ -82,14 +78,16 @@ class ActionInput { } async checkIfBStackReRun() { - // Using !! ensures that the function returns true or false, regardless of the input values. - if (!this.rerunAttempt || !this.rerunAttempt > 1) { + // Ensure rerunAttempt is a number and greater than 1 + if (!this.rerunAttempt || Number(this.rerunAttempt) <= 1) { return false; } - if (!this.runId || !this.runId > 1 || !this.repository || this.repository === 'none' - || !this.githubToken || this.githubToken === 'none' || !this.username || !this.accessKey) { + + // Ensure runId, repository, username, and accessKey are valid + if (!this.runId || !this.repository || this.repository === 'none' || !this.username || !this.accessKey) { return false; } + const triggeringActor = await this.identifyRunFromBStack(); core.info(`Triggering actor is - ${triggeringActor}`); return triggeringActor === this.githubApp; @@ -97,13 +95,10 @@ class ActionInput { async identifyRunFromBStack() { try { - const triggeringActorDirect = github.context.event.workflow_run.triggering_actor?.login; - core.info(`Triggering actor directly got is: ${triggeringActorDirect}`); - const runDetailsUrl = `https://api.github.com/repos/${this.repository}/actions/runs/${this.runId}`; const runDetailsResponse = await axios.get(runDetailsUrl, { headers: { - Authorization: `authorization: token ${process.env.GITHUB_TOKEN}`, + Authorization: `token ${process.env.GITHUB_TOKEN}`, Accept: 'application/vnd.github.v3+json', }, }); @@ -129,7 +124,6 @@ class ActionInput { }, headers: { 'Content-Type': 'application/json', - 'x-rs-auth': true, }, }); const variables = bsApiResponse?.data?.data?.variables; diff --git a/setup-env/src/actionInput/inputValidator.js b/setup-env/src/actionInput/inputValidator.js index a32b699..f427caa 100644 --- a/setup-env/src/actionInput/inputValidator.js +++ b/setup-env/src/actionInput/inputValidator.js @@ -115,29 +115,6 @@ class InputValidator { return inputProjectName; } - /** - * 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."); - } - /** * Validates the app name input to ensure it is a valid non-empty string. * If the input is 'none' or not provided, it returns 'browserstack[bot]'. diff --git a/setup-env/test/actionInput/index.test.js b/setup-env/test/actionInput/index.test.js index 0b64cf1..516f446 100644 --- a/setup-env/test/actionInput/index.test.js +++ b/setup-env/test/actionInput/index.test.js @@ -20,7 +20,6 @@ describe('Action Input operations for fetching all inputs, triggering validation sinon.stub(InputValidator, 'updateUsername').returns('validatedUsername'); sinon.stub(InputValidator, 'validateBuildName').returns('validatedBuildName'); sinon.stub(InputValidator, 'validateProjectName').returns('validatedProjectName'); - sinon.stub(InputValidator, 'validateGithubToken').returns('validatedToken'); sinon.stub(InputValidator, 'validateGithubAppName').returns('validatedAppName'); // Provide required inputs @@ -67,14 +66,6 @@ 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', () => { @@ -113,7 +104,6 @@ describe('Action Input operations for fetching all inputs, triggering validation sinon.stub(InputValidator, 'updateUsername').returns('validatedUsername'); sinon.stub(InputValidator, 'validateBuildName').returns('validatedBuildName'); sinon.stub(InputValidator, 'validateProjectName').returns('validatedProjectName'); - sinon.stub(InputValidator, 'validateGithubToken').returns('validatedToken'); sinon.stub(InputValidator, 'validateGithubAppName').returns('validatedAppName'); // Provide required inputs @@ -130,9 +120,8 @@ describe('Action Input operations for fetching all inputs, triggering validation }); it('Returns false if rerun attempt is less than or equal to 1', async () => { - // stubbedInput.withArgs(INPUT.GITHUB_APP).returns('someApp'); const actionInput = new ActionInput(); - actionInput.rerunAttempt = '1'; + actionInput.rerunAttempt = '1'; // This should be a string or number const result = await actionInput.checkIfBStackReRun(); // eslint-disable-next-line no-unused-expressions expect(result).to.be.false; @@ -172,7 +161,6 @@ describe('Action Input operations for fetching all inputs, triggering validation sinon.stub(InputValidator, 'updateUsername').returns('validatedUsername'); sinon.stub(InputValidator, 'validateBuildName').returns('validatedBuildName'); sinon.stub(InputValidator, 'validateProjectName').returns('validatedProjectName'); - sinon.stub(InputValidator, 'validateGithubToken').returns('validatedToken'); sinon.stub(InputValidator, 'validateGithubAppName').returns('validatedAppName'); // Provide required inputs @@ -223,7 +211,6 @@ describe('Action Input operations for fetching all inputs, triggering validation sinon.stub(InputValidator, 'updateUsername').returns('validatedUsername'); sinon.stub(InputValidator, 'validateBuildName').returns('validatedBuildName'); sinon.stub(InputValidator, 'validateProjectName').returns('validatedProjectName'); - sinon.stub(InputValidator, 'validateGithubToken').returns('validatedToken'); sinon.stub(InputValidator, 'validateGithubAppName').returns('validatedAppName'); // Provide required inputs diff --git a/setup-env/test/actionInput/inputValidator.test.js b/setup-env/test/actionInput/inputValidator.test.js index ea95914..14db306 100644 --- a/setup-env/test/actionInput/inputValidator.test.js +++ b/setup-env/test/actionInput/inputValidator.test.js @@ -153,29 +153,6 @@ describe('InputValidator class to validate individual fields of the action input }); }); - 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); - }); - }); - context('Validates GitHub App Name', () => { it("Returns 'browserstack[bot]' if the app name is not provided", () => { expect(() => InputValidator.validateGithubAppName()).to.throw("Invalid input for 'github-app'. Must be a valid string.");