-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add adapt functionality from decorator handlers
[changelog:added]
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |