refactored BuildService

This commit is contained in:
alecmerdler 2017-03-05 20:47:23 -08:00
parent 16ccc946f3
commit 7b35c0c0d5
6 changed files with 183 additions and 62 deletions

View file

@ -12,6 +12,7 @@ import { LinearWorkflowComponent } from './directives/ui/linear-workflow/linear-
import { LinearWorkflowSectionComponent } from './directives/ui/linear-workflow/linear-workflow-section.component';
import { QuayConfig } from './quay-config.module';
import { QuayRun } from './quay-run.module';
import { BuildServiceImpl } from './services/build/build.service.impl';
/**
@ -33,6 +34,7 @@ import { QuayRun } from './quay-run.module';
],
providers: [
ViewArrayImpl,
BuildServiceImpl,
],
})
export class quay {

View file

@ -1,62 +0,0 @@
/**
* Service which provides helper methods for reasoning about builds.
*/
angular.module('quay').factory('BuildService', [function() {
var buildService = {};
buildService.isActive = function(build) {
return build.phase != 'complete' && build.phase != 'error' && build.phase != 'expired' && build.phase != 'cancelled';
};
buildService.getBuildMessage = function(phase) {
switch (phase) {
case 'cannot_load':
return 'Cannot load build status';
case 'starting':
case 'initializing':
return 'Starting Dockerfile build';
case 'waiting':
return 'Waiting for available build worker';
case 'unpacking':
return 'Unpacking build package';
case 'pulling':
return 'Pulling base image';
case 'building':
return 'Building image from Dockerfile';
case 'checking-cache':
return 'Looking up cached images';
case 'priming-cache':
return 'Priming cache for build';
case 'build-scheduled':
return 'Preparing build node';
case 'pushing':
return 'Pushing image built from Dockerfile';
case 'complete':
return 'Dockerfile build completed and pushed';
case 'error':
return 'Dockerfile build failed';
case 'expired':
return 'Build did not complete after 3 attempts. Re-submit this build to try again.';
case 'internalerror':
return 'An internal system error occurred while building; the build will be retried in the next few minutes.';
case 'cancelled':
return 'This build was previously cancelled.';
}
};
return buildService;
}]);

View file

@ -0,0 +1,76 @@
import { BuildServiceImpl } from './build.service.impl';
describe("BuildServiceImpl", () => {
var buildServiceImpl: BuildServiceImpl;
var build: {phase: string};
beforeEach(() => {
buildServiceImpl = new BuildServiceImpl();
build = {phase: ""};
});
describe("isActive", () => {
var phases: string[];
beforeEach(() => {
phases = ['complete', 'error', 'expired', 'cancelled'];
});
it("returns false if given build's phase matches an inactive phase", () => {
phases.forEach((phase: string) => {
build.phase = phase;
expect(buildServiceImpl.isActive(build)).toBe(false);
});
});
it("returns true if given build's phase does not match inactive phases", () => {
build.phase = 'initializing';
expect(buildServiceImpl.isActive(build)).toBe(true);
});
});
describe("getBuildMessage", () => {
var buildMessages: {phase: string, message: string}[];
beforeEach(() => {
buildMessages = [
{phase: 'cannot_load', message: 'Cannot load build status'},
{phase: 'starting', message: 'Starting Dockerfile build'},
{phase: 'initializing', message: 'Starting Dockerfile build'},
{phase: 'waiting', message: 'Waiting for available build worker'},
{phase: 'unpacking', message: 'Unpacking build package'},
{phase: 'pulling', message: 'Pulling base image'},
{phase: 'building', message: 'Building image from Dockerfile'},
{phase: 'checking-cache', message: 'Looking up cached images'},
{phase: 'priming-cache', message: 'Priming cache for build'},
{phase: 'build-scheduled', message: 'Preparing build node'},
{phase: 'pushing', message: 'Pushing image built from Dockerfile'},
{phase: 'complete', message: 'Dockerfile build completed and pushed'},
{phase: 'error', message: 'Dockerfile build failed'},
{phase: 'expired', message: 'Build did not complete after 3 attempts. Re-submit this build to try again.'},
{phase: 'internalerror', message: 'An internal system error occurred while building; the build will be retried in the next few minutes.'},
{phase: 'cancelled', message: 'This build was previously cancelled.'},
];
});
it("returns the correct message for the given phase", () => {
buildMessages.forEach((buildMessage) => {
expect(buildServiceImpl.getBuildMessage(buildMessage.phase)).toEqual(buildMessage.message, buildMessage);
});
});
it("throws an error if given phase is not supported", () => {
var phase: string = "not-a-phase";
try {
buildServiceImpl.getBuildMessage(phase);
fail("Should throw error");
} catch (error) {
expect(error.message).toEqual("Invalid build phase");
}
});
});
});

View file

@ -0,0 +1,84 @@
import { BuildService } from './build.service';
import { Injectable } from 'angular-ts-decorators';
@Injectable(BuildService.name)
export class BuildServiceImpl implements BuildService {
private inactivePhases: string[] = ['complete', 'error', 'expired', 'cancelled'];
public isActive(build: {phase: string}): boolean {
return this.inactivePhases.indexOf(build.phase) == -1;
}
public getBuildMessage(phase: string): string {
var message: string;
switch (phase) {
case 'cannot_load':
message = 'Cannot load build status';
break;
case 'starting':
case 'initializing':
message = 'Starting Dockerfile build';
break;
case 'waiting':
message = 'Waiting for available build worker';
break;
case 'unpacking':
message = 'Unpacking build package';
break;
case 'pulling':
message = 'Pulling base image';
break;
case 'building':
message = 'Building image from Dockerfile';
break;
case 'checking-cache':
message = 'Looking up cached images';
break;
case 'priming-cache':
message = 'Priming cache for build';
break;
case 'build-scheduled':
message = 'Preparing build node';
break;
case 'pushing':
message = 'Pushing image built from Dockerfile';
break;
case 'complete':
message = 'Dockerfile build completed and pushed';
break;
case 'error':
message = 'Dockerfile build failed';
break;
case 'expired':
message = 'Build did not complete after 3 attempts. Re-submit this build to try again.';
break;
case 'internalerror':
message = 'An internal system error occurred while building; the build will be retried in the next few minutes.';
break;
case 'cancelled':
message = 'This build was previously cancelled.';
break;
default:
throw new Error("Invalid build phase");
}
return message;
}
}

View file

@ -0,0 +1,19 @@
/**
* Service which provides helper methods for reasoning about builds.
*/
export abstract class BuildService {
/**
* Determine if the given build is active.
* @param build The build object.
* @return isActive If the given build is active.
*/
public abstract isActive(build: {phase: string}): boolean;
/**
* Generate a message based on a given phase.
* @param phase The phase type.
* @return buildMessage The message associated with the given phase.
*/
public abstract getBuildMessage(phase: string): string;
}

View file

@ -1,4 +1,6 @@
import { ViewArrayImpl } from "static/js/services/view-array/view-array.impl";
/**
* Specialized wrapper around array which provides a toggle() method for viewing the contents of the
* array in a manner that is asynchronously filled in over a short time period. This prevents long