Skip to content

Commit

Permalink
feat: replace auto versioning with put
Browse files Browse the repository at this point in the history
  • Loading branch information
grutt committed Jan 27, 2024
1 parent c13ff1d commit a318ed0
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 277 deletions.
170 changes: 1 addition & 169 deletions typescript-sdk/hatchet/clients/admin/admin-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,102 +40,6 @@ describe('AdminClient', () => {
);
});

describe('should_put', () => {
let workflow: CreateWorkflowVersionOpts;

beforeAll(() => {
workflow = {
name: 'workflow1',
version: '',
description: 'description1',
eventTriggers: [],
cronTriggers: [],
scheduledTriggers: [],
jobs: [],
};
});

it('shouldPut:F and vX; if auto:F', () => {
const existing: any = {
versions: [
{
version: 'v0.0.1',
},
],
};
const [shouldPut, version] = AdminClient.should_put(
{
...workflow,
version: 'vX',
},
existing,
{
autoVersion: false,
}
);
expect(shouldPut).toEqual(false);
expect(version).toEqual('vX');
});

it('shouldPut:T and v0.0.1 if auto:T, no version, and no Existing', () => {
const existing: any = undefined;
const [shouldPut, version] = AdminClient.should_put(
{
...workflow,
version: '',
},
existing,
{
autoVersion: true,
}
);
expect(shouldPut).toEqual(true);
expect(version).toEqual('v0.1.0');
});

it('shouldPut:T and bump version if auto:T, no version, and has Existing', () => {
const existing: any = {
versions: [
{
version: 'v0.0.1',
},
],
};
const [shouldPut, version] = AdminClient.should_put(
{
...workflow,
version: '',
},
existing,
{
autoVersion: true,
}
);

expect(shouldPut).toEqual(true);
expect(version).toEqual('v0.1.0');
});

it('shouldPut:T and keep existing version if auto:T, with a version, and no Existing', () => {
const existing: any = {
versions: [],
};
const [shouldPut, version] = AdminClient.should_put(
{
...workflow,
version: 'vKEEP',
},
existing,
{
autoVersion: true,
}
);

expect(shouldPut).toEqual(true);
expect(version).toEqual('vKEEP');
});
});

describe('put_workflow', () => {
it('should throw an error if no version and not auto version', async () => {
const workflow: CreateWorkflowVersionOpts = {
Expand All @@ -153,7 +57,7 @@ describe('AdminClient', () => {
);
});

it('should check if an existing workflow exists, if not it should put', async () => {
it('should attempt to put the workflow', async () => {
const workflow: CreateWorkflowVersionOpts = {
name: 'workflow1',
version: 'v0.0.1',
Expand All @@ -164,12 +68,6 @@ describe('AdminClient', () => {
jobs: [],
};

const existingSpy = jest
.spyOn(client.client, 'getWorkflowByName')
.mockRejectedValue(new ServerError(Status.NOT_FOUND, 'not found'));

const shouldPutSpy = jest.spyOn(AdminClient, 'should_put').mockReturnValue([true, 'v0.1.0']);

const putSpy = jest.spyOn(client.client, 'putWorkflow').mockResolvedValue({
id: 'workflow1',
version: 'v0.1.0',
Expand All @@ -183,74 +81,8 @@ describe('AdminClient', () => {

await client.put_workflow(workflow);

expect(existingSpy).toHaveBeenCalledWith({
name: 'workflow1',
});

expect(shouldPutSpy).toHaveBeenCalledWith(workflow, undefined, undefined);

expect(putSpy).toHaveBeenCalled();
});

it('should check if an existing workflow exists, if not it should put', async () => {
const workflow: CreateWorkflowVersionOpts = {
name: 'workflow1',
version: 'v0.0.1',
description: 'description1',
eventTriggers: [],
cronTriggers: [],
scheduledTriggers: [],
jobs: [],
};

const mockExisting: Workflow = {
id: 'workflow1',
name: 'workflow1',
tenantId: 'TENANT_ID',
description: 'description1',
versions: [
{
id: 'workflow1',
version: 'v0.0.1',
order: 1,
workflowId: 'workflow1',
jobs: [],
createdAt: undefined,
updatedAt: undefined,
triggers: undefined,
},
],
createdAt: undefined,
updatedAt: undefined,
};

const existingSpy = jest
.spyOn(client.client, 'getWorkflowByName')
.mockResolvedValue(mockExisting);

const shouldPutSpy = jest.spyOn(AdminClient, 'should_put').mockReturnValue([false, 'v0.2.0']);

const putSpy = jest.spyOn(client.client, 'putWorkflow').mockResolvedValue({
id: 'workflow1',
version: 'v0.2.0',
order: 1,
workflowId: 'workflow1',
jobs: [],
createdAt: undefined,
updatedAt: undefined,
triggers: undefined,
});

await client.put_workflow(workflow, { autoVersion: false });

expect(existingSpy).toHaveBeenCalledWith({
name: 'workflow1',
});

expect(shouldPutSpy).toHaveBeenCalledWith(workflow, mockExisting, { autoVersion: false });

expect(putSpy).not.toHaveBeenCalled();
});
});

describe('schedule_workflow', () => {
Expand Down
48 changes: 1 addition & 47 deletions typescript-sdk/hatchet/clients/admin/admin-client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Channel, Status, createClient } from 'nice-grpc';
import { Channel, createClient } from 'nice-grpc';
import {
CreateWorkflowVersionOpts,
Workflow,
WorkflowServiceClient,
WorkflowServiceDefinition,
} from '@protoc/workflows';
import HatchetError from '@util/errors/hatchet-error';
import { ClientConfig } from '@clients/hatchet-client/client-config';
import * as semver from '@util/semver/semver';

export class AdminClient {
config: ClientConfig;
Expand All @@ -18,55 +16,11 @@ export class AdminClient {
this.client = createClient(WorkflowServiceDefinition, channel);
}

static should_put(
workflow: CreateWorkflowVersionOpts,
existing?: Workflow,
options?: { autoVersion?: boolean }
): [boolean, string] {
// TODO verify this function before merge #122
if (!options?.autoVersion) {
return [false, workflow.version];
}

if (workflow.version === '') {
return [true, 'v0.1.0'];
}

if (existing && existing.versions.length > 0) {
const newVersion = semver.bumpMinorVersion(existing.versions[0].version);
const shouldPut = newVersion !== workflow.version;
return [shouldPut, newVersion];
}

return [true, workflow.version];
}

async put_workflow(workflow: CreateWorkflowVersionOpts, options?: { autoVersion?: boolean }) {
if (workflow.version === '' && !options?.autoVersion) {
throw new HatchetError('PutWorkflow error: workflow version is required, or use autoVersion');
}

let existing: Workflow | undefined;

try {
existing = await this.client.getWorkflowByName({
name: workflow.name,
});
} catch (e: any) {
if (e.code === Status.NOT_FOUND) {
existing = undefined;
} else {
throw new HatchetError(e.message);
}
}

const [shouldPut, version] = AdminClient.should_put(workflow, existing, options);

// eslint-disable-next-line no-param-reassign
workflow.version = version;

if (!shouldPut) return;

try {
await this.client.putWorkflow({
opts: workflow,
Expand Down
38 changes: 0 additions & 38 deletions typescript-sdk/hatchet/util/semver/semver.test.ts

This file was deleted.

23 changes: 0 additions & 23 deletions typescript-sdk/hatchet/util/semver/semver.ts

This file was deleted.

0 comments on commit a318ed0

Please sign in to comment.