feat(aio): make `githubToken` mandatory for `GithubApi`

This commit is contained in:
Georgios Kalpakas 2017-02-27 22:40:13 +02:00 committed by Chuck Jazdzewski
parent c5644e5a0d
commit 37348989f0
5 changed files with 26 additions and 33 deletions

View File

@ -8,9 +8,10 @@ import {assertNotMissingOrEmpty} from '../common/utils';
// Classes
export class BuildCleaner {
// Constructor
constructor(protected buildsDir: string, protected repoSlug: string, protected githubToken?: string) {
constructor(protected buildsDir: string, protected repoSlug: string, protected githubToken: string) {
assertNotMissingOrEmpty('buildsDir', buildsDir);
assertNotMissingOrEmpty('repoSlug', repoSlug);
assertNotMissingOrEmpty('githubToken', githubToken);
}
// Methods - Public

View File

@ -18,16 +18,14 @@ export class GithubApi {
protected requestHeaders: {[key: string]: string};
// Constructor
constructor(protected repoSlug: string, githubToken?: string) {
constructor(protected repoSlug: string, githubToken: string) {
assertNotMissingOrEmpty('repoSlug', repoSlug);
if (!githubToken) {
console.warn('No GitHub access-token specified. Requests will be unauthenticated.');
}
assertNotMissingOrEmpty('githubToken', githubToken);
this.requestHeaders = {'User-Agent': `Node/${process.versions.node}`};
if (githubToken) {
this.requestHeaders.Authorization = `token ${githubToken}`;
}
this.requestHeaders = {
'Authorization': `token ${githubToken}`,
'User-Agent': `Node/${process.versions.node}`,
};
}
// Methods - Public

View File

@ -14,12 +14,20 @@ describe('BuildCleaner', () => {
describe('constructor()', () => {
it('should throw if \'buildsDir\' is empty', () => {
expect(() => new BuildCleaner('', '/baz/qux')).toThrowError('Missing or empty required parameter \'buildsDir\'!');
expect(() => new BuildCleaner('', '/baz/qux', '12345')).
toThrowError('Missing or empty required parameter \'buildsDir\'!');
});
it('should throw if \'repoSlug\' is empty', () => {
expect(() => new BuildCleaner('/foo/bar', '')).toThrowError('Missing or empty required parameter \'repoSlug\'!');
expect(() => new BuildCleaner('/foo/bar', '', '12345')).
toThrowError('Missing or empty required parameter \'repoSlug\'!');
});
it('should throw if \'githubToken\' is empty', () => {
expect(() => new BuildCleaner('/foo/bar', 'baz/qux', '')).
toThrowError('Missing or empty required parameter \'githubToken\'!');
});
});

View File

@ -18,18 +18,8 @@ describe('GithubApi', () => {
});
it('should log a warning if \'githubToken\' is not defined or empty', () => {
const warningMessage = 'No GitHub access-token specified. Requests will be unauthenticated.';
const consoleWarnSpy = spyOn(console, 'warn');
/* tslint:disable: no-unused-new */
new GithubApi('repo/slug');
new GithubApi('repo/slug', '');
/* tslint:enable: no-unused-new */
expect(consoleWarnSpy).toHaveBeenCalledTimes(2);
expect(consoleWarnSpy.calls.argsFor(0)[0]).toBe(warningMessage);
expect(consoleWarnSpy.calls.argsFor(1)[0]).toBe(warningMessage);
it('should throw if \'githubToken\' is missing or empty', () => {
expect(() => new GithubApi('repo/slug', '')).toThrowError('Missing or empty required parameter \'githubToken\'!');
});
});

View File

@ -6,19 +6,15 @@ describe('GithubPullRequests', () => {
describe('constructor()', () => {
it('should log a warning if \'githubToken\' is not defined', () => {
const warningMessage = 'No GitHub access-token specified. Requests will be unauthenticated.';
const consoleWarnSpy = spyOn(console, 'warn');
// tslint:disable-next-line: no-unused-new
new GithubPullRequests('repo/slug');
expect(consoleWarnSpy).toHaveBeenCalledWith(warningMessage);
it('should throw if \'repoSlug\' is missing or empty', () => {
expect(() => new GithubPullRequests('', '12345')).
toThrowError('Missing or empty required parameter \'repoSlug\'!');
});
it('should throw if \'repoSlug\' is not defined', () => {
expect(() => new GithubPullRequests('', '12345')).toThrowError('Missing required parameter \'repoSlug\'!');
it('should throw if \'githubToken\' is missing or empty', () => {
expect(() => new GithubPullRequests('foo/bar', '')).
toThrowError('Missing or empty required parameter \'githubToken\'!');
});
});