@atomist/sdm-core
Version:
Atomist Software Delivery Machine - Implementation
148 lines • 7.42 kB
JavaScript
/*
* 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.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const automation_client_1 = require("@atomist/automation-client");
const ApolloGraphClient_1 = require("@atomist/automation-client/lib/graph/ApolloGraphClient");
const metadata_1 = require("@atomist/automation-client/lib/internal/metadata/metadata");
const metadataReading_1 = require("@atomist/automation-client/lib/internal/metadata/metadataReading");
const AbstractRequestProcessor_1 = require("@atomist/automation-client/lib/internal/transport/AbstractRequestProcessor");
const RequestProcessor_1 = require("@atomist/automation-client/lib/internal/transport/RequestProcessor");
const constructionUtils_1 = require("@atomist/automation-client/lib/util/constructionUtils");
const cluster = require("cluster");
const _ = require("lodash");
const FulfillGoalOnRequested_1 = require("./FulfillGoalOnRequested");
class GoalExecutionAutomationEventListener extends automation_client_1.AutomationEventListenerSupport {
constructor(sdm) {
super();
this.sdm = sdm;
}
startupSuccessful(client) {
return __awaiter(this, void 0, void 0, function* () {
if (cluster.isMaster) {
const teamId = process.env.ATOMIST_GOAL_TEAM;
const teamName = process.env.ATOMIST_GOAL_TEAM_NAME || teamId;
const goalSetId = [process.env.ATOMIST_GOAL_SET_ID];
const uniqueName = [process.env.ATOMIST_GOAL_UNIQUE_NAME];
const correlationId = process.env.ATOMIST_CORRELATION_ID || automation_client_1.guid();
// Obtain goal via graphql query
const graphClient = new ApolloGraphClient_1.ApolloGraphClient(`${this.sdm.configuration.endpoints.graphql}/${teamId}`, { Authorization: `Bearer ${client.configuration.apiKey}` });
const goal = yield graphClient.query({
name: "SdmGoalsByGoalSetIdAndUniqueName",
variables: {
goalSetId,
uniqueName,
},
options: automation_client_1.QueryNoCacheOptions,
});
// Create event and run event handler
const event = {
data: _.cloneDeep(goal),
extensions: {
correlation_id: correlationId,
team_id: teamId,
team_name: teamName,
operationName: metadataReading_1.metadataFromInstance(new FulfillGoalOnRequested_1.FulfillGoalOnRequested(this.sdm.goalFulfillmentMapper, [...this.sdm.goalExecutionListeners])).name,
},
secrets: [{
uri: automation_client_1.Secrets.OrgToken,
value: "null",
}],
};
try {
yield new Promise((resolve, reject) => {
client.processEvent(event, pResults => {
pResults.then(results => {
automation_client_1.logger.debug("Processing goal completed with results %j", results);
resolve();
}, reject);
});
});
}
catch (e) {
automation_client_1.logger.error(`Processing goal failed: ${e.message}`);
}
// Exit successfully to avoid job schedulers retrying.
automation_client_1.safeExit(0);
}
});
}
}
exports.GoalExecutionAutomationEventListener = GoalExecutionAutomationEventListener;
class FilteringMetadataProcessor {
constructor(allowedCommandHandlers, allowedEventHandlers) {
this.allowedCommands = allowedCommandHandlers.map(h => metadataReading_1.metadataFromInstance(constructionUtils_1.toFactory(h)()).name);
this.allowedEvents = allowedEventHandlers.map(h => metadataReading_1.metadataFromInstance(constructionUtils_1.toFactory(h)()).name);
}
process(metadata) {
if (metadata_1.isEventHandlerMetadata(metadata) && !this.allowedEvents.includes(metadata.name)) {
metadata.expose = false;
}
else if (metadata_1.isCommandHandlerMetadata(metadata) && !this.allowedCommands.includes(metadata.name)) {
metadata.expose = false;
}
return metadata;
}
}
exports.FilteringMetadataProcessor = FilteringMetadataProcessor;
class GoalExecutionRequestProcessor extends AbstractRequestProcessor_1.AbstractRequestProcessor {
constructor(automations, configuration, listeners = []) {
super(automations, configuration, listeners);
this.automations = automations;
this.configuration = configuration;
this.listeners = listeners;
this.graphClients = configuration.graphql.client.factory;
}
createGraphClient(event) {
return this.graphClients.create(RequestProcessor_1.workspaceId(event), this.configuration);
}
createMessageClient(event, context) {
return new NoOpMessageClient();
}
sendStatusMessage(payload, ctx) {
return __awaiter(this, void 0, void 0, function* () {
// Intentionally left empty
});
}
}
exports.GoalExecutionRequestProcessor = GoalExecutionRequestProcessor;
class NoOpMessageClient {
delete(destinations, options) {
return __awaiter(this, void 0, void 0, function* () {
automation_client_1.logger.debug(`Ignoring delete message '${JSON.stringify(options)}'`);
});
}
respond(msg, options) {
return __awaiter(this, void 0, void 0, function* () {
automation_client_1.logger.debug(`Ignoring respond message '${JSON.stringify(msg)}'`);
});
}
send(msg, destinations, options) {
return __awaiter(this, void 0, void 0, function* () {
automation_client_1.logger.debug(`Ignoring send message '${JSON.stringify(msg)}'`);
});
}
}
//# sourceMappingURL=goalExecution.js.map
;