From 3f32c3d18ef514a57b70530996b5ea722105bf47 Mon Sep 17 00:00:00 2001 From: Jamie White Date: Tue, 25 May 2021 23:33:39 +0200 Subject: [PATCH] wip! Introduce jobs to Mirage --- ui/mirage/config.ts | 21 +++++++++++++++++++-- ui/mirage/factories/job.ts | 3 +++ ui/mirage/models/application.ts | 1 + ui/mirage/models/job.ts | 6 ++++++ ui/mirage/models/workspace.ts | 3 +++ ui/mirage/services/job.ts | 21 +++++++++++++++++++++ 6 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 ui/mirage/factories/job.ts create mode 100644 ui/mirage/models/job.ts create mode 100644 ui/mirage/services/job.ts diff --git a/ui/mirage/config.ts b/ui/mirage/config.ts index 961f2aa92dd..c1e3361553b 100644 --- a/ui/mirage/config.ts +++ b/ui/mirage/config.ts @@ -1,5 +1,7 @@ +import Ember from 'ember'; import { logRequestConsole } from './utils'; import { Server } from 'miragejs'; +import * as QUnit from 'qunit'; import * as build from './services/build'; import * as project from './services/project'; @@ -8,6 +10,7 @@ import * as token from './services/token'; import * as inviteToken from './services/invite-token'; import * as release from './services/release'; import * as versionInfo from './services/version-info'; +import * as job from './services/job'; export default function (this: Server) { this.namespace = 'hashicorp.waypoint.Waypoint'; @@ -21,6 +24,17 @@ export default function (this: Server) { }; this.pretender.handledRequest = logRequestConsole; + this.pretender.unhandledRequest = (verb, path, request) => { + let result = false; + let message = `There is no Mirage handler for ${verb} ${path}. Please define one in ui/mirage/config.ts.`; + // Technically it is possible to get the real stack using `new + // Error().stack` but honestly it’s pretty opaque and not terribly useful + // for debugging. Easier to bring folks here so they can add a breakpoint + // and dig around. + let source = 'ui/mirage/config.ts:35'; + + QUnit.config.current.assert.pushResult({ result, message, source }); + }; this.post('/ListBuilds', build.list); this.post('/GetBuild', build.get); @@ -34,7 +48,10 @@ export default function (this: Server) { this.post('/ListReleases', release.list); this.post('/GetRelease', release.get); this.post('/GetVersionInfo', versionInfo.get); + this.post('/QueueJob', job.queue); - // Pass through all other requests - this.passthrough(); + if (!Ember.testing) { + // Pass through all other requests + this.passthrough(); + } } diff --git a/ui/mirage/factories/job.ts b/ui/mirage/factories/job.ts new file mode 100644 index 00000000000..b733171e87c --- /dev/null +++ b/ui/mirage/factories/job.ts @@ -0,0 +1,3 @@ +import { Factory } from 'miragejs'; + +export default Factory.extend({}); diff --git a/ui/mirage/models/application.ts b/ui/mirage/models/application.ts index 92d23c7d27f..00a714db3c2 100644 --- a/ui/mirage/models/application.ts +++ b/ui/mirage/models/application.ts @@ -5,6 +5,7 @@ export default Model.extend({ project: belongsTo(), builds: hasMany(), deployments: hasMany(), + jobs: hasMany(), toProtobuf(): Application { let result = new Application(); diff --git a/ui/mirage/models/job.ts b/ui/mirage/models/job.ts new file mode 100644 index 00000000000..c6449d7db31 --- /dev/null +++ b/ui/mirage/models/job.ts @@ -0,0 +1,6 @@ +import { Model, belongsTo } from 'miragejs'; + +export default Model.extend({ + application: belongsTo(), + workspace: belongsTo(), +}); diff --git a/ui/mirage/models/workspace.ts b/ui/mirage/models/workspace.ts index f9b16a857e2..bdabab93997 100644 --- a/ui/mirage/models/workspace.ts +++ b/ui/mirage/models/workspace.ts @@ -3,6 +3,9 @@ import { Ref, Workspace } from 'waypoint-pb'; export default Model.extend({ builds: hasMany(), + deployments: hasMany(), + releases: hasMany(), + jobs: hasMany(), toProtobuf(): Workspace { let result = new Workspace(); diff --git a/ui/mirage/services/job.ts b/ui/mirage/services/job.ts new file mode 100644 index 00000000000..da9624fefbe --- /dev/null +++ b/ui/mirage/services/job.ts @@ -0,0 +1,21 @@ +import { QueueJobRequest, QueueJobResponse } from 'waypoint-pb'; +import { Request, Response } from 'miragejs'; +import { decode } from '../helpers/protobufs'; + +export function queue(schema: any, { requestBody }: Request): Response { + let requestMsg = decode(QueueJobRequest, requestBody); + let job = requestMsg.getJob(); + let projectName = job.getApplication().getProject(); + let applicationName = job.getApplication().getApplication(); + let workspaceName = job.getWorkspace().getWorkspace(); + let project = schema.projects.findBy({ name: projectName }); + let application = schema.applications.findBy({ name: applicationName, projectId: project.id }); + let workspace = schema.workspaces.findBy({ name: workspaceName }); + let result = new QueueJobResponse(); + // TODO: pull more details from the request + let model = schema.jobs.create({ application, workspace }); + + result.setJobId(model.id); + + return this.serialize(result, 'application'); +}