Skip to content

Commit 7a8d7d8

Browse files
committed
Adding token to input again
1 parent c0d5032 commit 7a8d7d8

File tree

6 files changed

+93
-4
lines changed

6 files changed

+93
-4
lines changed

setup-env/config/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = {
44
ACCESS_KEY: 'access-key',
55
BUILD_NAME: 'build-name',
66
PROJECT_NAME: 'project-name',
7+
GITHUB_TOKEN: 'github-token',
78
GITHUB_APP: 'github-app',
89
},
910

setup-env/dist/index.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010
ACCESS_KEY: 'access-key',
1111
BUILD_NAME: 'build-name',
1212
PROJECT_NAME: 'project-name',
13+
GITHUB_TOKEN: 'github-token',
1314
GITHUB_APP: 'github-app',
1415
},
1516

@@ -9361,6 +9362,7 @@ class ActionInput {
93619362
this.buildName = core.getInput(INPUT.BUILD_NAME);
93629363
this.projectName = core.getInput(INPUT.PROJECT_NAME);
93639364
this.githubApp = core.getInput(INPUT.GITHUB_APP);
9365+
this.githubToken = core.getInput(INPUT.GITHUB_TOKEN);
93649366
this.rerunAttempt = process?.env?.GITHUB_RUN_ATTEMPT;
93659367
this.runId = process?.env?.GITHUB_RUN_ID;
93669368
this.repository = process?.env?.GITHUB_REPOSITORY;
@@ -9377,6 +9379,7 @@ class ActionInput {
93779379
this.buildName = InputValidator.validateBuildName(this.buildName);
93789380
this.projectName = InputValidator.validateProjectName(this.projectName);
93799381
this.githubApp = InputValidator.validateGithubAppName(this.githubApp);
9382+
this.githubToken = InputValidator.validateGithubToken(this.githubToken);
93809383
}
93819384

93829385
/**
@@ -9412,7 +9415,8 @@ class ActionInput {
94129415
}
94139416

94149417
// Ensure runId, repository, username, and accessKey are valid
9415-
if (!this.runId || !this.repository || this.repository === 'none' || !this.username || !this.accessKey) {
9418+
if (!this.runId || !this.repository || this.repository === 'none'
9419+
|| !this.githubToken || this.githubToken === 'none' || !this.username || !this.accessKey) {
94169420
return false;
94179421
}
94189422

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

96149618
throw new Error("Invalid input for 'github-app'. Must be a valid string.");
96159619
}
9620+
9621+
/**
9622+
* Validates the GitHub token input to ensure it is a valid non-empty string.
9623+
* If the input is 'none' or not provided, it returns 'none'.
9624+
* @param {string} githubToken Input for 'github-token'
9625+
* @returns {string} The validated GitHub token, or 'none' if input is 'none' or invalid
9626+
* @throws {Error} If the input is not a valid non-empty string
9627+
*/
9628+
static validateGithubToken(githubToken) {
9629+
if (typeof githubToken !== 'string') {
9630+
throw new Error("Invalid input for 'github-token'. Must be a valid non-empty string.");
9631+
}
9632+
9633+
if (githubToken.toLowerCase() === 'none') {
9634+
return 'none';
9635+
}
9636+
9637+
if (githubToken.trim().length > 0) {
9638+
return githubToken;
9639+
}
9640+
9641+
throw new Error("Invalid input for 'github-token'. Must be a valid non-empty string.");
9642+
}
96169643
}
96179644

96189645
module.exports = InputValidator;

setup-env/src/actionInput/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class ActionInput {
3333
this.buildName = core.getInput(INPUT.BUILD_NAME);
3434
this.projectName = core.getInput(INPUT.PROJECT_NAME);
3535
this.githubApp = core.getInput(INPUT.GITHUB_APP);
36+
this.githubToken = core.getInput(INPUT.GITHUB_TOKEN);
3637
this.rerunAttempt = process?.env?.GITHUB_RUN_ATTEMPT;
3738
this.runId = process?.env?.GITHUB_RUN_ID;
3839
this.repository = process?.env?.GITHUB_REPOSITORY;
@@ -49,6 +50,7 @@ class ActionInput {
4950
this.buildName = InputValidator.validateBuildName(this.buildName);
5051
this.projectName = InputValidator.validateProjectName(this.projectName);
5152
this.githubApp = InputValidator.validateGithubAppName(this.githubApp);
53+
this.githubToken = InputValidator.validateGithubToken(this.githubToken);
5254
}
5355

5456
/**
@@ -84,7 +86,8 @@ class ActionInput {
8486
}
8587

8688
// Ensure runId, repository, username, and accessKey are valid
87-
if (!this.runId || !this.repository || this.repository === 'none' || !this.username || !this.accessKey) {
89+
if (!this.runId || !this.repository || this.repository === 'none'
90+
|| !this.githubToken || this.githubToken === 'none' || !this.username || !this.accessKey) {
8891
return false;
8992
}
9093

@@ -98,7 +101,7 @@ class ActionInput {
98101
const runDetailsUrl = `https://api.github.com/repos/${this.repository}/actions/runs/${this.runId}`;
99102
const runDetailsResponse = await axios.get(runDetailsUrl, {
100103
headers: {
101-
Authorization: `token ${process.env.GITHUB_TOKEN}`,
104+
Authorization: `token ${this.githubToken}`,
102105
Accept: 'application/vnd.github.v3+json',
103106
},
104107
});

setup-env/src/actionInput/inputValidator.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,29 @@ class InputValidator {
137137

138138
throw new Error("Invalid input for 'github-app'. Must be a valid string.");
139139
}
140+
141+
/**
142+
* Validates the GitHub token input to ensure it is a valid non-empty string.
143+
* If the input is 'none' or not provided, it returns 'none'.
144+
* @param {string} githubToken Input for 'github-token'
145+
* @returns {string} The validated GitHub token, or 'none' if input is 'none' or invalid
146+
* @throws {Error} If the input is not a valid non-empty string
147+
*/
148+
static validateGithubToken(githubToken) {
149+
if (typeof githubToken !== 'string') {
150+
throw new Error("Invalid input for 'github-token'. Must be a valid non-empty string.");
151+
}
152+
153+
if (githubToken.toLowerCase() === 'none') {
154+
return 'none';
155+
}
156+
157+
if (githubToken.trim().length > 0) {
158+
return githubToken;
159+
}
160+
161+
throw new Error("Invalid input for 'github-token'. Must be a valid non-empty string.");
162+
}
140163
}
141164

142165
module.exports = InputValidator;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('Action Input operations for fetching all inputs, triggering validation
2121
sinon.stub(InputValidator, 'validateBuildName').returns('validatedBuildName');
2222
sinon.stub(InputValidator, 'validateProjectName').returns('validatedProjectName');
2323
sinon.stub(InputValidator, 'validateGithubAppName').returns('validatedAppName');
24+
sinon.stub(InputValidator, 'validateGithubToken').returns('validatedToken');
2425

2526
// Provide required inputs
2627
stubbedInput.withArgs(INPUT.USERNAME, { required: true }).returns('someUsername');
@@ -66,6 +67,14 @@ describe('Action Input operations for fetching all inputs, triggering validation
6667
expect(e.message).to.eq('Action input failed for reason: Access Key Required');
6768
}
6869
});
70+
71+
it('Takes input and validates GitHub token and app name successfully', () => {
72+
stubbedInput.withArgs(INPUT.GITHUB_TOKEN).returns('someToken');
73+
stubbedInput.withArgs(INPUT.GITHUB_APP).returns('someApp');
74+
const actionInput = new ActionInput();
75+
expect(actionInput.githubToken).to.eq('validatedToken');
76+
expect(actionInput.githubApp).to.eq('validatedAppName');
77+
});
6978
});
7079

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

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

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

231243
// Provide required inputs
232244
stubbedInput.withArgs(INPUT.USERNAME, { required: true }).returns('someUsername');

setup-env/test/actionInput/inputValidator.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,29 @@ describe('InputValidator class to validate individual fields of the action input
175175
expect(InputValidator.validateGithubAppName(validAppName)).to.eq(validAppName);
176176
});
177177
});
178+
179+
context('Validates GitHub Token', () => {
180+
it("Returns 'none' if the token is not provided", () => {
181+
expect(() => InputValidator.validateGithubToken()).to.throw("Invalid input for 'github-token'. Must be a valid non-empty string.");
182+
});
183+
184+
it("Returns 'none' if the token is 'none' (case insensitive)", () => {
185+
expect(InputValidator.validateGithubToken('None')).to.eq('none');
186+
});
187+
188+
it('Throws an error if the token is an empty string', () => {
189+
expect(() => InputValidator.validateGithubToken('')).to.throw("Invalid input for 'github-token'. Must be a valid non-empty string.");
190+
});
191+
192+
it('Throws an error if the token is not a valid string', () => {
193+
expect(() => InputValidator.validateGithubToken(123)).to.throw("Invalid input for 'github-token'. Must be a valid non-empty string.");
194+
});
195+
196+
it('Returns the token if it is a valid non-empty string and not "none"', () => {
197+
const validToken = 'someValidToken';
198+
expect(InputValidator.validateGithubToken(validToken)).to.eq(validToken);
199+
});
200+
});
178201
});
179202
});
180203
});

0 commit comments

Comments
 (0)