fix(aio): do not hardcode the domain in preview link comments

This commit is contained in:
Georgios Kalpakas 2017-03-02 00:04:03 +02:00 committed by Chuck Jazdzewski
parent d28ea80db8
commit 03a5fd01c9
4 changed files with 25 additions and 8 deletions

View File

@ -7,6 +7,7 @@ import {uploadServerFactory} from './upload-server-factory';
// Constants // Constants
const AIO_BUILDS_DIR = getEnvVar('AIO_BUILDS_DIR'); const AIO_BUILDS_DIR = getEnvVar('AIO_BUILDS_DIR');
const AIO_DOMAIN_NAME = getEnvVar('AIO_DOMAIN_NAME');
const AIO_GITHUB_ORGANIZATION = getEnvVar('AIO_GITHUB_ORGANIZATION'); const AIO_GITHUB_ORGANIZATION = getEnvVar('AIO_GITHUB_ORGANIZATION');
const AIO_GITHUB_TEAM_SLUGS = getEnvVar('AIO_GITHUB_TEAM_SLUGS'); const AIO_GITHUB_TEAM_SLUGS = getEnvVar('AIO_GITHUB_TEAM_SLUGS');
const AIO_GITHUB_TOKEN = getEnvVar('AIO_GITHUB_TOKEN'); const AIO_GITHUB_TOKEN = getEnvVar('AIO_GITHUB_TOKEN');
@ -23,6 +24,7 @@ function _main() {
uploadServerFactory. uploadServerFactory.
create({ create({
buildsDir: AIO_BUILDS_DIR, buildsDir: AIO_BUILDS_DIR,
domainName: AIO_DOMAIN_NAME,
githubOrganization: AIO_GITHUB_ORGANIZATION, githubOrganization: AIO_GITHUB_ORGANIZATION,
githubTeamSlugs: AIO_GITHUB_TEAM_SLUGS.split(','), githubTeamSlugs: AIO_GITHUB_TEAM_SLUGS.split(','),
githubToken: AIO_GITHUB_TOKEN, githubToken: AIO_GITHUB_TOKEN,

View File

@ -2,6 +2,7 @@
import * as express from 'express'; import * as express from 'express';
import * as http from 'http'; import * as http from 'http';
import {GithubPullRequests} from '../common/github-pull-requests'; import {GithubPullRequests} from '../common/github-pull-requests';
import {assertNotMissingOrEmpty} from '../common/utils';
import {BuildCreator} from './build-creator'; import {BuildCreator} from './build-creator';
import {CreatedBuildEvent} from './build-events'; import {CreatedBuildEvent} from './build-events';
import {BuildVerifier} from './build-verifier'; import {BuildVerifier} from './build-verifier';
@ -14,6 +15,7 @@ const X_FILE_HEADER = 'X-FILE';
// Interfaces - Types // Interfaces - Types
interface UploadServerConfig { interface UploadServerConfig {
buildsDir: string; buildsDir: string;
domainName: string;
githubOrganization: string; githubOrganization: string;
githubTeamSlugs: string[]; githubTeamSlugs: string[];
githubToken: string; githubToken: string;
@ -26,14 +28,17 @@ class UploadServerFactory {
// Methods - Public // Methods - Public
public create({ public create({
buildsDir, buildsDir,
domainName,
githubOrganization, githubOrganization,
githubTeamSlugs, githubTeamSlugs,
githubToken, githubToken,
repoSlug, repoSlug,
secret, secret,
}: UploadServerConfig): http.Server { }: UploadServerConfig): http.Server {
assertNotMissingOrEmpty('domainName', domainName);
const buildVerifier = new BuildVerifier(secret, githubToken, repoSlug, githubOrganization, githubTeamSlugs); const buildVerifier = new BuildVerifier(secret, githubToken, repoSlug, githubOrganization, githubTeamSlugs);
const buildCreator = this.createBuildCreator(buildsDir, githubToken, repoSlug); const buildCreator = this.createBuildCreator(buildsDir, githubToken, repoSlug, domainName);
const middleware = this.createMiddleware(buildVerifier, buildCreator); const middleware = this.createMiddleware(buildVerifier, buildCreator);
const httpServer = http.createServer(middleware); const httpServer = http.createServer(middleware);
@ -47,13 +52,14 @@ class UploadServerFactory {
} }
// Methods - Protected // Methods - Protected
protected createBuildCreator(buildsDir: string, githubToken: string, repoSlug: string): BuildCreator { protected createBuildCreator(buildsDir: string, githubToken: string, repoSlug: string,
domainName: string): BuildCreator {
const buildCreator = new BuildCreator(buildsDir); const buildCreator = new BuildCreator(buildsDir);
const githubPullRequests = new GithubPullRequests(githubToken, repoSlug); const githubPullRequests = new GithubPullRequests(githubToken, repoSlug);
buildCreator.on(CreatedBuildEvent.type, ({pr, sha}: CreatedBuildEvent) => { buildCreator.on(CreatedBuildEvent.type, ({pr, sha}: CreatedBuildEvent) => {
const body = `The angular.io preview for ${sha.slice(0, 7)} is available [here][1].\n\n` + const body = `The angular.io preview for ${sha.slice(0, 7)} is available [here][1].\n\n` +
`[1]: https://pr${pr}-${sha}.ngbuilds.io/`; `[1]: https://pr${pr}-${sha}.${domainName}/`;
githubPullRequests.addComment(pr, body); githubPullRequests.addComment(pr, body);
}); });

View File

@ -12,6 +12,7 @@ import {uploadServerFactory as usf} from '../../lib/upload-server/upload-server-
describe('uploadServerFactory', () => { describe('uploadServerFactory', () => {
const defaultConfig = { const defaultConfig = {
buildsDir: 'builds/dir', buildsDir: 'builds/dir',
domainName: 'domain.name',
githubOrganization: 'organization', githubOrganization: 'organization',
githubTeamSlugs: ['team1', 'team2'], githubTeamSlugs: ['team1', 'team2'],
githubToken: '12345', githubToken: '12345',
@ -32,9 +33,15 @@ describe('uploadServerFactory', () => {
}); });
it('should throw if \'secret\' is missing or empty', () => { it('should throw if \'buildsDir\' is missing or empty', () => {
expect(() => createUploadServer({secret: ''})). expect(() => createUploadServer({buildsDir: ''})).
toThrowError('Missing or empty required parameter \'secret\'!'); toThrowError('Missing or empty required parameter \'buildsDir\'!');
});
it('should throw if \'domainName\' is missing or empty', () => {
expect(() => createUploadServer({domainName: ''})).
toThrowError('Missing or empty required parameter \'domainName\'!');
}); });
@ -83,7 +90,7 @@ describe('uploadServerFactory', () => {
const buildCreator: BuildCreator = usfCreateBuildCreatorSpy.calls.mostRecent().returnValue; const buildCreator: BuildCreator = usfCreateBuildCreatorSpy.calls.mostRecent().returnValue;
expect(usfCreateMiddlewareSpy).toHaveBeenCalledWith(jasmine.any(BuildVerifier), buildCreator); expect(usfCreateMiddlewareSpy).toHaveBeenCalledWith(jasmine.any(BuildVerifier), buildCreator);
expect(usfCreateBuildCreatorSpy).toHaveBeenCalledWith('builds/dir', '12345', 'repo/slug'); expect(usfCreateBuildCreatorSpy).toHaveBeenCalledWith('builds/dir', '12345', 'repo/slug', 'domain.name');
}); });
@ -124,6 +131,7 @@ describe('uploadServerFactory', () => {
defaultConfig.buildsDir, defaultConfig.buildsDir,
defaultConfig.githubToken, defaultConfig.githubToken,
defaultConfig.repoSlug, defaultConfig.repoSlug,
defaultConfig.domainName,
); );
}); });
@ -136,7 +144,7 @@ describe('uploadServerFactory', () => {
it('should post a comment on GitHub on \'build.created\'', () => { it('should post a comment on GitHub on \'build.created\'', () => {
const prsAddCommentSpy = spyOn(GithubPullRequests.prototype, 'addComment'); const prsAddCommentSpy = spyOn(GithubPullRequests.prototype, 'addComment');
const commentBody = 'The angular.io preview for 1234567 is available [here][1].\n\n' + const commentBody = 'The angular.io preview for 1234567 is available [here][1].\n\n' +
'[1]: https://pr42-1234567890.ngbuilds.io/'; '[1]: https://pr42-1234567890.domain.name/';
buildCreator.emit(CreatedBuildEvent.type, {pr: 42, sha: '1234567890'}); buildCreator.emit(CreatedBuildEvent.type, {pr: 42, sha: '1234567890'});

View File

@ -3,6 +3,7 @@ set -e -o pipefail
# Set up env variables for testing # Set up env variables for testing
export AIO_BUILDS_DIR=$TEST_AIO_BUILDS_DIR export AIO_BUILDS_DIR=$TEST_AIO_BUILDS_DIR
export AIO_DOMAIN_NAME=$TEST_AIO_DOMAIN_NAME
export AIO_GITHUB_ORGANIZATION=$TEST_AIO_GITHUB_ORGANIZATION export AIO_GITHUB_ORGANIZATION=$TEST_AIO_GITHUB_ORGANIZATION
export AIO_GITHUB_TEAM_SLUGS=$TEST_AIO_GITHUB_TEAM_SLUGS export AIO_GITHUB_TEAM_SLUGS=$TEST_AIO_GITHUB_TEAM_SLUGS
export AIO_PREVIEW_DEPLOYMENT_TOKEN=$TEST_AIO_PREVIEW_DEPLOYMENT_TOKEN export AIO_PREVIEW_DEPLOYMENT_TOKEN=$TEST_AIO_PREVIEW_DEPLOYMENT_TOKEN