Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/O11y-Rerun' into O11y-Rerun-Test
Browse files Browse the repository at this point in the history
  • Loading branch information
darpanLalwani committed Sep 24, 2024
2 parents 70e23e5 + 30c3a6b commit dafb8bb
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 75 deletions.
1 change: 0 additions & 1 deletion setup-env/config/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},

Expand Down
22 changes: 8 additions & 14 deletions setup-env/src/actionInput/index.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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}`);
}
Expand All @@ -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);
}

Expand Down Expand Up @@ -82,28 +78,27 @@ 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;
}

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',
},
});
Expand All @@ -129,7 +124,6 @@ class ActionInput {
},
headers: {
'Content-Type': 'application/json',
'x-rs-auth': true,
},
});
const variables = bsApiResponse?.data?.data?.variables;
Expand Down
23 changes: 0 additions & 23 deletions setup-env/src/actionInput/inputValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]'.
Expand Down
15 changes: 1 addition & 14 deletions setup-env/test/actionInput/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
23 changes: 0 additions & 23 deletions setup-env/test/actionInput/inputValidator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down

0 comments on commit dafb8bb

Please sign in to comment.