UNPKG

@atomist/sdm

Version:

Atomist Software Delivery Machine SDK

96 lines 4.2 kB
"use strict"; /* * 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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.createJob = exports.JobTaskType = void 0; const configuration_1 = require("@atomist/automation-client/lib/configuration"); const GraphClient_1 = require("@atomist/automation-client/lib/spi/graph/GraphClient"); const slack_messages_1 = require("@atomist/slack-messages"); const _ = require("lodash"); const preferenceStore_1 = require("../../../api/context/preferenceStore"); var JobTaskType; (function (JobTaskType) { JobTaskType["Command"] = "command"; })(JobTaskType = exports.JobTaskType || (exports.JobTaskType = {})); /** * Create a Job in the backend with the provided name and tasks * * A job can execute any registered command in the same or other connected SDM: * * command: name of the CommandRegistration or the registration instance itself * registration: optional name of the SDM this job should be send to; defaults to the current SDM * parameters: Record type with all required parameters for this command * name: optional name of the job * description: optional description of the job used to display to the user in chat or web */ async function createJob(details, ctx) { const { command, parameters, name, description, registration } = details; const cmd = typeof command === "string" ? command : command.name; const nameToUse = !!name ? name : cmd; const owner = registration || configuration_1.configurationValue("name"); const preferenceStoreFactory = configuration_1.configurationValue("sdm.preferenceStoreFactory", () => preferenceStore_1.NoPreferenceStore); const concurrentTasks = await preferenceStoreFactory(ctx).get(`@atomist/job/${owner}/concurrentTasks`, { defaultValue: (details.concurrentTasks || 1).toString() }); const data = _.cloneDeep(_.get(ctx, "trigger") || {}); data.secrets = []; const params = (Array.isArray(parameters) ? parameters : [parameters]).filter(p => !!p); const paramsChunks = _.chunk(params, 250); const result = await ctx.graphClient.mutate({ name: "CreateJob", variables: { name: nameToUse, description: !!description ? description : `Executing ${slack_messages_1.codeLine(cmd)}`, owner, data: JSON.stringify(data), tasks: (paramsChunks[0] || []).map(p => ({ name: cmd, data: JSON.stringify({ type: JobTaskType.Command, parameters: p, }), })), concurrentTasks: +concurrentTasks, }, options: GraphClient_1.MutationNoCacheOptions, }); if (paramsChunks.length > 1) { for (const paramChunk of paramsChunks.slice(1)) { await ctx.graphClient.mutate({ name: "AddJobTasks", variables: { id: result.createAtmJob.id, tasks: paramChunk.map(p => ({ name: cmd, data: JSON.stringify({ type: JobTaskType.Command, parameters: p, }), })), }, options: GraphClient_1.MutationNoCacheOptions, }); } } await ctx.graphClient.mutate({ name: "ResumeJob", variables: { id: result.createAtmJob.id, }, options: GraphClient_1.MutationNoCacheOptions, }); return { id: result.createAtmJob.id }; } exports.createJob = createJob; //# sourceMappingURL=createJob.js.map