Skip to content

Commit 1092dd1

Browse files
Merge pull request #87 from rounak610/fix/sdk-7461-do-not-override-unset-build-project-name
[SDK-7461] fix(setup-env): do not export a build/project name the workflow never set
2 parents 1ab56d9 + d2e8a43 commit 1092dd1

4 files changed

Lines changed: 110 additions & 15 deletions

File tree

‎setup-env/README.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
This action sets up the following environment variables in the runner environment. These environment variables shall be used in the tests for BrowserStack:
44

5-
1. `BROWSERSTACK_BUILD_NAME`: This environment variable is set on the basis of the input to `build-name` field. By default, the value will be decided based on the event, i.e. push, pull_request etc for the workflow:
5+
1. `BROWSERSTACK_BUILD_NAME`: This environment variable is set **only when you pass the `build-name` input**. If you do not pass it, the variable is left unset so that the build name configured in your own test setup (e.g. `browserstack.json` / `browserstack.yml`) is used. Pass the `BUILD_INFO` keyword to get a name generated from the event, i.e. push, pull_request etc for the workflow:
66
1. `push` event: `[<Branch-Name>] Commit <commit-sha>: <commit-message> [Workflow: <Workflow-number>]`
77
2. `pull_request` event: `[<Branch-Name>] PR <PR-number>: <PR-title> [Workflow: <Workflow-number>]`
88
3. `release` event: `[<Branch-Name>] Release <Release-tag>: <Release-name> [Workflow: <Workflow-number>]`
99
4. Other events: `<Event-Name> [Workflow: <Workflow-number>]`
1010

11-
2. `BROWSERSTACK_PROJECT_NAME`: This environment variable is set on the basis of the input to `project-name` field. By default, i.e. if any input is not provided, the value will be set as the Repository Name.
11+
2. `BROWSERSTACK_PROJECT_NAME`: This environment variable is set **only when you pass the `project-name` input**. If you do not pass it, the variable is left unset so that the project name configured in your own test setup is used. Pass the `REPO_NAME` keyword to use the Repository Name.
1212
3. `BROWSERSTACK_USERNAME`: This environment variable's value is taken from the input to `username` field. Ideal way would be to pass the GitHub Secret as the input, i.e. `username: ${{ secrets.BROWSERSTACK_USERNAME }}`.
1313
4. `BROWSERSTACK_ACCESS_KEY`: This environment variable's value is taken from the input to `access-key` field. Ideal way would be to pass the GitHub Secret as the input, i.e. `access-key: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}`.
1414

@@ -46,10 +46,11 @@ or
4646
* `build-name: BUILD_INFO - My String at the end`
4747
* `build-name: String at the Beginning - BUILD_INFO - String at the end`
4848
* The keyword `BUILD_INFO` will be replaced by the information based on the event of the workflow as described above for `BROWSERSTACK_BUILD_NAME` environment variable.
49+
* If you omit this input entirely, `BROWSERSTACK_BUILD_NAME` is **not** exported and your own configured build name is left untouched.
4950
* `project-name`: (**Optional**)
5051
* You can pass any string that you want to set as the `BROWSERSTACK_PROJECT_NAME`. E.g. `project-name: My Project Name Goes Here`.
5152
* You can also pass the keyword `REPO_NAME` as the input. This will set the Repository Name for the `BROWSERSTACK_PROJECT_NAME` environment variable.
52-
* If no input is provided, `REPO_NAME` will be considered as the default input.
53+
* If you omit this input entirely, `BROWSERSTACK_PROJECT_NAME` is **not** exported and your own configured project name is left untouched.
5354

5455
---
5556
**NOTE**

‎setup-env/dist/index.js‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34599,6 +34599,10 @@ class ActionInput {
3459934599
// non-compulsory fields
3460034600
this.buildName = core.getInput(INPUT.BUILD_NAME);
3460134601
this.projectName = core.getInput(INPUT.PROJECT_NAME);
34602+
34603+
// Capture before _validateInput() replaces blanks with generated defaults.
34604+
this.buildNameProvided = Boolean(this.buildName && this.buildName.trim());
34605+
this.projectNameProvided = Boolean(this.projectName && this.projectName.trim());
3460234606
this.githubApp = core.getInput(INPUT.GITHUB_APP);
3460334607
this.githubToken = core.getInput(INPUT.GITHUB_TOKEN);
3460434608
this.rerunAttempt = process?.env?.GITHUB_RUN_ATTEMPT;
@@ -34632,13 +34636,23 @@ class ActionInput {
3463234636
core.exportVariable(ENV_VARS.BROWSERSTACK_ACCESS_KEY, this.accessKey);
3463334637
core.info(`Use ${ENV_VARS.BROWSERSTACK_ACCESS_KEY} environment variable for your access key in your tests\n`);
3463434638

34635-
core.exportVariable(ENV_VARS.BROWSERSTACK_PROJECT_NAME, this.projectName);
34636-
core.info(`${ENV_VARS.BROWSERSTACK_PROJECT_NAME} environment variable set as: ${this.projectName}`);
34637-
core.info(`Use ${ENV_VARS.BROWSERSTACK_PROJECT_NAME} environment variable for your project name capability in your tests\n`);
34639+
// Export only when supplied: an env var outranks the user's browserstack.json,
34640+
// so a generated default would silently replace it. BUILD_INFO / REPO_NAME opt in.
34641+
if (this.projectNameProvided) {
34642+
core.exportVariable(ENV_VARS.BROWSERSTACK_PROJECT_NAME, this.projectName);
34643+
core.info(`${ENV_VARS.BROWSERSTACK_PROJECT_NAME} environment variable set as: ${this.projectName}`);
34644+
core.info(`Use ${ENV_VARS.BROWSERSTACK_PROJECT_NAME} environment variable for your project name capability in your tests\n`);
34645+
} else {
34646+
core.info(`No project-name input given, so ${ENV_VARS.BROWSERSTACK_PROJECT_NAME} was left unset and your own configuration will be used. Pass project-name (or the REPO_NAME token) to set it here.\n`);
34647+
}
3463834648

34639-
core.exportVariable(ENV_VARS.BROWSERSTACK_BUILD_NAME, this.buildName);
34640-
core.info(`${ENV_VARS.BROWSERSTACK_BUILD_NAME} environment variable set as: ${this.buildName}`);
34641-
core.info(`Use ${ENV_VARS.BROWSERSTACK_BUILD_NAME} environment variable for your build name capability in your tests\n`);
34649+
if (this.buildNameProvided) {
34650+
core.exportVariable(ENV_VARS.BROWSERSTACK_BUILD_NAME, this.buildName);
34651+
core.info(`${ENV_VARS.BROWSERSTACK_BUILD_NAME} environment variable set as: ${this.buildName}`);
34652+
core.info(`Use ${ENV_VARS.BROWSERSTACK_BUILD_NAME} environment variable for your build name capability in your tests\n`);
34653+
} else {
34654+
core.info(`No build-name input given, so ${ENV_VARS.BROWSERSTACK_BUILD_NAME} was left unset and your own configuration will be used. Pass build-name (or the BUILD_INFO token) to set it here.\n`);
34655+
}
3464234656

3464334657
if (await this.checkIfBStackReRun()) {
3464434658
await this.setBStackRerunEnvVars();

‎setup-env/src/actionInput/index.js‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class ActionInput {
3232
// non-compulsory fields
3333
this.buildName = core.getInput(INPUT.BUILD_NAME);
3434
this.projectName = core.getInput(INPUT.PROJECT_NAME);
35+
36+
// Capture before _validateInput() replaces blanks with generated defaults.
37+
this.buildNameProvided = Boolean(this.buildName && this.buildName.trim());
38+
this.projectNameProvided = Boolean(this.projectName && this.projectName.trim());
3539
this.githubApp = core.getInput(INPUT.GITHUB_APP);
3640
this.githubToken = core.getInput(INPUT.GITHUB_TOKEN);
3741
this.rerunAttempt = process?.env?.GITHUB_RUN_ATTEMPT;
@@ -65,13 +69,23 @@ class ActionInput {
6569
core.exportVariable(ENV_VARS.BROWSERSTACK_ACCESS_KEY, this.accessKey);
6670
core.info(`Use ${ENV_VARS.BROWSERSTACK_ACCESS_KEY} environment variable for your access key in your tests\n`);
6771

68-
core.exportVariable(ENV_VARS.BROWSERSTACK_PROJECT_NAME, this.projectName);
69-
core.info(`${ENV_VARS.BROWSERSTACK_PROJECT_NAME} environment variable set as: ${this.projectName}`);
70-
core.info(`Use ${ENV_VARS.BROWSERSTACK_PROJECT_NAME} environment variable for your project name capability in your tests\n`);
72+
// Export only when supplied: an env var outranks the user's browserstack.json,
73+
// so a generated default would silently replace it. BUILD_INFO / REPO_NAME opt in.
74+
if (this.projectNameProvided) {
75+
core.exportVariable(ENV_VARS.BROWSERSTACK_PROJECT_NAME, this.projectName);
76+
core.info(`${ENV_VARS.BROWSERSTACK_PROJECT_NAME} environment variable set as: ${this.projectName}`);
77+
core.info(`Use ${ENV_VARS.BROWSERSTACK_PROJECT_NAME} environment variable for your project name capability in your tests\n`);
78+
} else {
79+
core.info(`No project-name input given, so ${ENV_VARS.BROWSERSTACK_PROJECT_NAME} was left unset and your own configuration will be used. Pass project-name (or the REPO_NAME token) to set it here.\n`);
80+
}
7181

72-
core.exportVariable(ENV_VARS.BROWSERSTACK_BUILD_NAME, this.buildName);
73-
core.info(`${ENV_VARS.BROWSERSTACK_BUILD_NAME} environment variable set as: ${this.buildName}`);
74-
core.info(`Use ${ENV_VARS.BROWSERSTACK_BUILD_NAME} environment variable for your build name capability in your tests\n`);
82+
if (this.buildNameProvided) {
83+
core.exportVariable(ENV_VARS.BROWSERSTACK_BUILD_NAME, this.buildName);
84+
core.info(`${ENV_VARS.BROWSERSTACK_BUILD_NAME} environment variable set as: ${this.buildName}`);
85+
core.info(`Use ${ENV_VARS.BROWSERSTACK_BUILD_NAME} environment variable for your build name capability in your tests\n`);
86+
} else {
87+
core.info(`No build-name input given, so ${ENV_VARS.BROWSERSTACK_BUILD_NAME} was left unset and your own configuration will be used. Pass build-name (or the BUILD_INFO token) to set it here.\n`);
88+
}
7589

7690
if (await this.checkIfBStackReRun()) {
7791
await this.setBStackRerunEnvVars();

‎setup-env/test/actionInput/index.test.js‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,36 @@ describe('Action Input operations for fetching all inputs, triggering validation
4848
expect(actionInput.projectName).to.eq('validatedProjectName');
4949
});
5050

51+
it('Records that build-name and project-name were supplied', () => {
52+
stubbedInput.withArgs(INPUT.BUILD_NAME).returns('someBuildName');
53+
stubbedInput.withArgs(INPUT.PROJECT_NAME).returns('someProjectName');
54+
const actionInput = new ActionInput();
55+
// eslint-disable-next-line no-unused-expressions
56+
expect(actionInput.buildNameProvided).to.be.true;
57+
// eslint-disable-next-line no-unused-expressions
58+
expect(actionInput.projectNameProvided).to.be.true;
59+
});
60+
61+
it('Records that build-name and project-name were NOT supplied when absent', () => {
62+
stubbedInput.withArgs(INPUT.BUILD_NAME).returns('');
63+
stubbedInput.withArgs(INPUT.PROJECT_NAME).returns('');
64+
const actionInput = new ActionInput();
65+
// eslint-disable-next-line no-unused-expressions
66+
expect(actionInput.buildNameProvided).to.be.false;
67+
// eslint-disable-next-line no-unused-expressions
68+
expect(actionInput.projectNameProvided).to.be.false;
69+
});
70+
71+
it('Treats a whitespace-only name input as not supplied', () => {
72+
stubbedInput.withArgs(INPUT.BUILD_NAME).returns(' ');
73+
stubbedInput.withArgs(INPUT.PROJECT_NAME).returns('\t ');
74+
const actionInput = new ActionInput();
75+
// eslint-disable-next-line no-unused-expressions
76+
expect(actionInput.buildNameProvided).to.be.false;
77+
// eslint-disable-next-line no-unused-expressions
78+
expect(actionInput.projectNameProvided).to.be.false;
79+
});
80+
5181
it('Takes input and throws error if username is not provided in input', () => {
5282
stubbedInput.withArgs(INPUT.USERNAME, { required: true }).throws(Error('Username Required'));
5383
try {
@@ -94,6 +124,9 @@ describe('Action Input operations for fetching all inputs, triggering validation
94124
actionInput.accessKey = 'someAccessKey';
95125
actionInput.buildName = 'someBuildName';
96126
actionInput.projectName = 'someProjectName';
127+
// _fetchAllInput is stubbed out above, so these flags have to be set by hand.
128+
actionInput.buildNameProvided = true;
129+
actionInput.projectNameProvided = true;
97130

98131
// Stub checkIfBStackReRun to return true
99132
sinon.stub(actionInput, 'checkIfBStackReRun').returns(Promise.resolve(true));
@@ -111,6 +144,39 @@ describe('Action Input operations for fetching all inputs, triggering validation
111144
sinon.assert.calledWith(core.exportVariable, ENV_VARS.BROWSERSTACK_BUILD_NAME, 'someBuildName');
112145
});
113146

147+
it('Does not export BROWSERSTACK_PROJECT_NAME when no project-name input was given', () => {
148+
actionInput.projectNameProvided = false;
149+
actionInput.setEnvVariables();
150+
sinon.assert.neverCalledWith(
151+
core.exportVariable, ENV_VARS.BROWSERSTACK_PROJECT_NAME, sinon.match.any,
152+
);
153+
// the other variables are unaffected
154+
sinon.assert.calledWith(core.exportVariable, ENV_VARS.BROWSERSTACK_BUILD_NAME, 'someBuildName');
155+
sinon.assert.calledWith(core.exportVariable, ENV_VARS.BROWSERSTACK_USERNAME, 'someUsername');
156+
});
157+
158+
it('Does not export BROWSERSTACK_BUILD_NAME when no build-name input was given', () => {
159+
actionInput.buildNameProvided = false;
160+
actionInput.setEnvVariables();
161+
sinon.assert.neverCalledWith(
162+
core.exportVariable, ENV_VARS.BROWSERSTACK_BUILD_NAME, sinon.match.any,
163+
);
164+
sinon.assert.calledWith(core.exportVariable, ENV_VARS.BROWSERSTACK_PROJECT_NAME, 'someProjectName');
165+
sinon.assert.calledWith(core.exportVariable, ENV_VARS.BROWSERSTACK_ACCESS_KEY, 'someAccessKey');
166+
});
167+
168+
it('Exports neither name when neither input was given, leaving the user config to win', () => {
169+
actionInput.buildNameProvided = false;
170+
actionInput.projectNameProvided = false;
171+
actionInput.setEnvVariables();
172+
sinon.assert.neverCalledWith(
173+
core.exportVariable, ENV_VARS.BROWSERSTACK_BUILD_NAME, sinon.match.any,
174+
);
175+
sinon.assert.neverCalledWith(
176+
core.exportVariable, ENV_VARS.BROWSERSTACK_PROJECT_NAME, sinon.match.any,
177+
);
178+
});
179+
114180
it('Calls setBStackRerunEnvVars when checkIfBStackReRun returns true', async () => {
115181
const setBStackRerunEnvVarsStub = sinon.stub(actionInput, 'setBStackRerunEnvVars').resolves();
116182
await actionInput.setEnvVariables();

0 commit comments

Comments
 (0)