Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
wip! Introduce jobs to Mirage
Browse files Browse the repository at this point in the history
  • Loading branch information
jgwhite committed May 25, 2021
1 parent 2067d2d commit 3f32c3d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
21 changes: 19 additions & 2 deletions ui/mirage/config.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand All @@ -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);
Expand All @@ -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();
}
}
3 changes: 3 additions & 0 deletions ui/mirage/factories/job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Factory } from 'miragejs';

export default Factory.extend({});
1 change: 1 addition & 0 deletions ui/mirage/models/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default Model.extend({
project: belongsTo(),
builds: hasMany(),
deployments: hasMany(),
jobs: hasMany(),

toProtobuf(): Application {
let result = new Application();
Expand Down
6 changes: 6 additions & 0 deletions ui/mirage/models/job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Model, belongsTo } from 'miragejs';

export default Model.extend({
application: belongsTo(),
workspace: belongsTo(),
});
3 changes: 3 additions & 0 deletions ui/mirage/models/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
21 changes: 21 additions & 0 deletions ui/mirage/services/job.ts
Original file line number Diff line number Diff line change
@@ -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');
}

0 comments on commit 3f32c3d

Please sign in to comment.