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 // Classes
export class BuildCleaner { export class BuildCleaner {
// Constructor // 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('buildsDir', buildsDir);
assertNotMissingOrEmpty('repoSlug', repoSlug); assertNotMissingOrEmpty('repoSlug', repoSlug);
assertNotMissingOrEmpty('githubToken', githubToken);
} }
// Methods - Public // Methods - Public

View File

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

View File

@ -14,12 +14,20 @@ describe('BuildCleaner', () => {
describe('constructor()', () => { describe('constructor()', () => {
it('should throw if \'buildsDir\' is empty', () => { 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', () => { 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', () => { it('should throw if \'githubToken\' is missing or empty', () => {
const warningMessage = 'No GitHub access-token specified. Requests will be unauthenticated.'; expect(() => new GithubApi('repo/slug', '')).toThrowError('Missing or empty required parameter \'githubToken\'!');
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);
}); });
}); });

View File

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