Skip to content
Permalink
Browse files

Add invokeCommand method

[changelog:added]
  • Loading branch information...
cdupuis committed Jun 21, 2019
1 parent 8f058a5 commit 6581460df84ba5b415071752ea0aa8078aba532d
Showing with 126 additions and 58 deletions.
  1. +3 −1 index.ts
  2. +4 −57 lib/pack/job/executeTask.ts
  3. +61 −0 lib/pack/job/helpers.ts
  4. +58 −0 lib/pack/job/invokeCommand.ts
@@ -102,7 +102,6 @@ export {
SdmVersionForCommit,
} from "./lib/typings/types";
import * as github from "./lib/util/github/ghub";

export {
gitHubGoalStatus,
githubGoalStatusSupport,
@@ -121,6 +120,9 @@ export {
export {
createJob,
} from "./lib/pack/job/createJob";
export {
invokeCommand,
} from "./lib/pack/job/invokeCommand";
export { github };
export {
tagRepo,
@@ -16,21 +16,12 @@

import {
automationClientInstance,
CommandInvocation,
GraphQL,
HandlerContext,
logger,
MessageOptions,
OnEvent,
ParameterType,
SourceDestination,
Success,
} from "@atomist/automation-client";
import {
isCommandIncoming,
isEventIncoming,
} from "@atomist/automation-client/lib/internal/transport/RequestProcessor";
import { CommandHandlerMetadata } from "@atomist/automation-client/lib/metadata/automationMetadata";
import { redact } from "@atomist/automation-client/lib/util/redact";
import {
EventHandlerRegistration,
@@ -45,6 +36,10 @@ import {
JobTask,
JobTaskType,
} from "./createJob";
import {
prepareCommandInvocation,
prepareHandlerContext,
} from "./helpers";

/**
* Execute an incoming job task event
@@ -155,51 +150,3 @@ async function updateJobTaskState(id: string,
},
});
}

/**
* Prepare the CommandInvocation instance to be sent for execution
*
* This pieces apart provided values form the parameters into the command's parameter, mapped parameter
* and secret structures.
*/
function prepareCommandInvocation(md: CommandHandlerMetadata, parameters: ParameterType = {}): CommandInvocation {
const ci: CommandInvocation = {
name: md.name,
args: md.parameters.filter(p => !!parameters[p.name]).map(p => ({
name: p.name,
value: parameters[p.name] as any,
})),
mappedParameters: md.mapped_parameters.filter(p => !!parameters[p.name]).map(p => ({
name: p.name,
value: parameters[p.name] as any,
})),
secrets: md.secrets.map(p => ({
uri: p.uri,
value: parameters[p.name] as any || "null",
})),
};
return ci;
}

/**
* Decorate the HandlerContext to support response messages for this event handler invocation.
*
* Task execution happens is rooted in an event handler executing; this would prevent response
* messages to work out of the box which is why this function adds the respond function to the
* MessageClient if possible.
*/
function prepareHandlerContext(ctx: HandlerContext, trigger: any): HandlerContext {
if (isCommandIncoming(trigger)) {
const source = trigger.source;
if (!!source) {
ctx.messageClient.respond = (msg: any, options?: MessageOptions) => {
return ctx.messageClient.send(msg, new SourceDestination(source, source.user_agent), options);
};
}
} else if (isEventIncoming(trigger)) {
ctx.messageClient.respond = async () => {
return;
};
}
return ctx;
}
@@ -0,0 +1,61 @@

import {
CommandInvocation,
HandlerContext,
MessageOptions,
ParameterType,
SourceDestination,
} from "@atomist/automation-client";
import {
isCommandIncoming,
isEventIncoming,
} from "@atomist/automation-client/lib/internal/transport/RequestProcessor";
import { CommandHandlerMetadata } from "@atomist/automation-client/lib/metadata/automationMetadata";

/**
* Prepare the CommandInvocation instance to be sent for execution
*
* This pieces apart provided values form the parameters into the command's parameter, mapped parameter
* and secret structures.
*/
export function prepareCommandInvocation(md: CommandHandlerMetadata, parameters: ParameterType = {}): CommandInvocation {
const ci: CommandInvocation = {
name: md.name,
args: md.parameters.filter(p => !!parameters[p.name]).map(p => ({
name: p.name,
value: parameters[p.name] as any,
})),
mappedParameters: md.mapped_parameters.filter(p => !!parameters[p.name]).map(p => ({
name: p.name,
value: parameters[p.name] as any,
})),
secrets: md.secrets.map(p => ({
uri: p.uri,
value: parameters[p.name] as any || "null",
})),
};
return ci;
}

/**
* Decorate the HandlerContext to support response messages for this event handler invocation.
*
* Task execution happens is rooted in an event handler executing; this would prevent response
* messages to work out of the box which is why this function adds the respond function to the
* MessageClient if possible.
*/
export function prepareHandlerContext(ctx: HandlerContext, trigger: any): HandlerContext {
if (isCommandIncoming(trigger)) {
const source = trigger.source;
if (!!source) {
ctx.messageClient.respond = (msg: any, options?: MessageOptions) => {
return ctx.messageClient.send(msg, new SourceDestination(source, source.user_agent), options);
};
}
} else if (isEventIncoming(trigger)) {
ctx.messageClient.respond = async () => {
return;
}
}
return ctx;
}
@@ -0,0 +1,58 @@
/*
* Copyright © 2019 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
automationClientInstance,
AutomationContextAware,
HandlerContext,
HandlerResult,
ParameterType,
} from "@atomist/automation-client";
import { CommandRegistration } from "@atomist/sdm";
import {
prepareCommandInvocation,
prepareHandlerContext,
} from "./helpers";

/**
* Invoke any registered command programmatically in this SDM instance
*
* @param command name of the CommandRegistration or actual CommandRegistration instance to run
* @param parameters parameters to be passed to the command
* @param ctx HandlerContext instance
*/
export async function invokeCommand<T extends ParameterType>(command: string | CommandRegistration<T>,
parameters: T,
ctx: HandlerContext): Promise<HandlerResult> {
const name = typeof command === "string" ? command : command.name;
const trigger = (ctx as any as AutomationContextAware).trigger;

const md = automationClientInstance().automationServer.automations.commands
.find(c => c.name === name);

if (!md) {
return {
code: 1,
message: `Command '${name}' could not be found`,
};
} else {
// Invoke the command
return await automationClientInstance().automationServer.invokeCommand(
prepareCommandInvocation(md, parameters),
prepareHandlerContext(ctx, trigger),
);
}
}

0 comments on commit 6581460

Please sign in to comment.
You can’t perform that action at this time.