Skip to content

Commit cd56180

Browse files
committed
feat: throw an error in case of invalid repository and no token
1 parent c3dd265 commit cd56180

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

src/GithubHelper.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ class GithubHelper {
99
* @param {Object} repo - personal access token
1010
*/
1111
constructor(pkg, config) {
12-
if (!isValidRepositoryUrl(pkg) || !hasGithubToken(config.token)) {
13-
throw new Error();
14-
}
12+
isValidRepositoryUrl(pkg);
13+
hasGithubToken(config.token);
1514

1615
this.repo = this._getRepo(pkg, config);
1716
this.config = config;

src/utils.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ function getRepositoryUrl(pkg) {
1717
}
1818

1919
if (!GIT_REPO_REGEXP.test(repositoryUrl)) {
20-
console.error('Invalid repository url on package.json');
21-
repositoryUrl = '';
20+
throw new Error('Invalid repository url on package.json');
2221
}
2322

2423
return repositoryUrl;
@@ -41,7 +40,7 @@ function isValidRepositoryUrl(pkg) {
4140
function hasGithubToken(token) {
4241
const isValidToken = typeof token === 'string' && token.length > 0;
4342
if (!isValidToken) {
44-
console.error('Missing TUI_GITHUB_TOKEN environment variable');
43+
throw new Error('Missing Github access token');
4544
}
4645

4746
return isValidToken;

test/utils.spec.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,32 @@ describe('utils.isValidRepositoryUrl()', () => {
2121
})
2222
).toBe(true);
2323

24-
expect(
25-
isValidRepositoryUrl({
24+
expect(() => {
25+
return isValidRepositoryUrl({
2626
repository: 'https://github.com/user-name/repository-name'
27-
})
28-
).toBe(false);
27+
});
28+
}).toThrow(new Error('Invalid repository url on package.json'));
2929

30-
expect(
31-
isValidRepositoryUrl({
30+
expect(() => {
31+
return isValidRepositoryUrl({
3232
repository: ''
33-
})
34-
).toBe(false);
33+
});
34+
}).toThrow(new Error('Invalid repository url on package.json'));
3535

36-
expect(
37-
isValidRepositoryUrl({
36+
expect(() => {
37+
return isValidRepositoryUrl({
3838
repository: {
3939
url: 'https://github.com/user-name/repository-name'
4040
}
41-
})
42-
).toBe(false);
41+
});
42+
}).toThrow(new Error('Invalid repository url on package.json'));
4343
});
4444
});
4545

4646
describe('utils.hasGithubToken()', () => {
4747
it('should determine a token is valid or not.', () => {
4848
expect(hasGithubToken('test_token')).toBe(true);
49-
expect(hasGithubToken(123123)).toBe(false);
49+
expect(() => hasGithubToken(123123)).toThrow(new Error('Missing Github access token'));
5050
});
5151
});
5252

0 commit comments

Comments
 (0)