@atomist/sdm
Version:
Atomist Software Delivery Machine SDK
116 lines • 4.36 kB
JavaScript
;
/*
* Copyright © 2020 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.wrapTest = exports.wrapPredicateMapping = exports.notGoalOrOutputTest = exports.goalTest = exports.matchStringOrRegexp = exports.isGoal = void 0;
const RequestProcessor_1 = require("@atomist/automation-client/lib/internal/transport/RequestProcessor");
const _ = require("lodash");
const sdmGoal_1 = require("../../api-helper/goal/sdmGoal");
const commonPushTests_1 = require("./support/commonPushTests");
function isGoal(options = {}) {
return goalTest(`is goal ${JSON.stringify(options)}`, async (g) => {
if (!!options.name &&
!matchStringOrRegexp(options.name, `${g.registration}/${g.name}`) &&
!matchStringOrRegexp(options.name, `${g.registration}/${g.uniqueName}`)) {
return false;
}
if (!!options.state && options.state !== g.state) {
return false;
}
if (!!options.output) {
const data = sdmGoal_1.goalData(g);
const outputs = data["@atomist/sdm/output"];
if (!outputs) {
return false;
}
else if (!outputs.some(o => matchStringOrRegexp(options.output, o.classifier))) {
return false;
}
}
if (!!options.data && !matchStringOrRegexp(options.data, g.data)) {
return false;
}
return true;
}, options.pushTest);
}
exports.isGoal = isGoal;
function matchStringOrRegexp(pattern, toMatch) {
if (typeof pattern === "string") {
return pattern === toMatch;
}
else {
return pattern.test(toMatch);
}
}
exports.matchStringOrRegexp = matchStringOrRegexp;
function goalTest(name, goalMapping, pushTest = commonPushTests_1.AnyPush) {
return {
name,
mapping: async (pli) => {
var _a;
const trigger = (_a = pli === null || pli === void 0 ? void 0 : pli.context) === null || _a === void 0 ? void 0 : _a.trigger;
if (!!trigger && RequestProcessor_1.isEventIncoming(trigger)) {
const goal = _.get(trigger, "data.SdmGoal[0]");
if (!!goal) {
const match = await goalMapping(goal, pli);
if (!!match) {
if (!pli.project) {
return true;
}
else {
return pushTest.mapping(pli);
}
}
}
}
return false;
},
pushTest,
};
}
exports.goalTest = goalTest;
/**
* Wrap a PushTest to make sure it doesn't get the chance to match on goal planning
* based on goal events
*/
function notGoalOrOutputTest(pushTest) {
return Object.assign(Object.assign({}, pushTest), { mapping: async (pli) => {
var _a;
const trigger = (_a = pli === null || pli === void 0 ? void 0 : pli.context) === null || _a === void 0 ? void 0 : _a.trigger;
if (!!trigger && RequestProcessor_1.isEventIncoming(trigger)) {
const goal = _.get(trigger, "data.SdmGoal[0]") || _.get(trigger, "data.SkillOutput[0]");
if (!!goal) {
return false;
}
}
return pushTest.mapping(pli);
} });
}
exports.notGoalOrOutputTest = notGoalOrOutputTest;
function wrapPredicateMapping(guards) {
return wrapTest(guards);
}
exports.wrapPredicateMapping = wrapPredicateMapping;
function wrapTest(test) {
if (!!test.pushTest) {
return test;
}
else {
return notGoalOrOutputTest(test);
}
}
exports.wrapTest = wrapTest;
//# sourceMappingURL=goalTest.js.map