Skip to content

Commit

Permalink
Add adapt functionality from decorator handlers
Browse files Browse the repository at this point in the history
[changelog:added]
  • Loading branch information
cdupuis committed May 15, 2019
1 parent 07ca015 commit 8068ee5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/api-helper/machine/adaptHandleCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Maker } from "@atomist/automation-client";
import { HandleCommand } from "@atomist/automation-client/lib/HandleCommand";
import { metadataFromInstance } from "@atomist/automation-client/lib/internal/metadata/metadataReading";
import { CommandHandlerMetadata } from "@atomist/automation-client/lib/metadata/automationMetadata";
import { toFactory } from "@atomist/automation-client/lib/util/constructionUtils";
import * as _ from "lodash";
import { CommandHandlerRegistration } from "../../api/registration/CommandHandlerRegistration";

/**
* Convert an decorator-style HandleCommand to a SDM-style CommandHandlerRegistration
*/
export function adaptHandleCommand(maker: Maker<HandleCommand<any>>): CommandHandlerRegistration {
const md = metadataFromInstance(toFactory(maker)()) as CommandHandlerMetadata;

return {
name: md.name,
intent: md.intent,
description: md.description,
tags: (md.tags || []).map(t => t.name),
autoSubmit: md.auto_submit,
paramsMaker: maker,
listener: async ci => {
const h = toFactory(maker)() as HandleCommand;
_.forEach(ci.parameters, (v, k) => h[k] = v);
return h.handle(ci.context, ci.parameters);
},
};
}
54 changes: 54 additions & 0 deletions test/api-helper/machine/adaptHandleCommand.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
HandlerContext,
HandlerResult,
MappedParameter,
MappedParameters,
Parameter,
Secret,
Secrets,
Success,
Tags,
} from "@atomist/automation-client";
import { ConfigurableCommandHandler } from "@atomist/automation-client/lib/decorators";
import { HandleCommand } from "@atomist/automation-client/lib/HandleCommand";
import { metadataFromInstance } from "@atomist/automation-client/lib/internal/metadata/metadataReading";
import { CommandHandlerMetadata } from "@atomist/automation-client/lib/metadata/automationMetadata";
import { toFactory } from "@atomist/automation-client/lib/util/constructionUtils";
import * as assert from "power-assert";
import { adaptHandleCommand } from "../../../lib/api-helper/machine/adaptHandleCommand";

describe("adaptHandleCommand", () => {

it("should adapt simple HelloWorld", async () => {
const cd = adaptHandleCommand(HelloWorld);
assert.strictEqual(cd.name, "HelloWorld");
assert.strictEqual(cd.description, "desc");
assert.deepStrictEqual(cd.intent, ["intent"]);
assert.strictEqual(cd.autoSubmit, true);

const md = metadataFromInstance(toFactory(cd.paramsMaker)()) as CommandHandlerMetadata;
assert.strictEqual(md.parameters[0].description, "test");
assert.strictEqual(md.secrets[0].uri, Secrets.userToken("repo"));
assert.strictEqual(md.mapped_parameters[0].uri, MappedParameters.SlackUser);
});

});

@ConfigurableCommandHandler("desc", { autoSubmit: true, intent: "intent" })
@Tags("test1", "test2")
class HelloWorld implements HandleCommand {

@Parameter({ description: "test" })
public test: string;

@Secret(Secrets.userToken("repo"))
public token: string;

@MappedParameter(MappedParameters.SlackUser)
public userId: string;

public async handle(ctx: HandlerContext): Promise<HandlerResult> {
return Success;
}

}

0 comments on commit 8068ee5

Please sign in to comment.