@atomist/sdm
Version:
Atomist Software Delivery Machine SDK
75 lines • 2.58 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PushRules = void 0;
const logger_1 = require("@atomist/automation-client/lib/util/logger");
const GoalSetter_1 = require("../GoalSetter");
/**
* Use to execute a rule set for any push to resolve to an object.
* The value from the first matching rule will be used.
*/
class PushRules {
/**
* Return all possible values
* @param {string} name
* @param {Array<PushMapping<V>>} rules Array of rules.
* Passing an empty array will result in an instance that always maps to undefined,
* and is not an error.
*/
constructor(name, rules = []) {
this.name = name;
this.rules = rules;
this.choices = [];
if (!name) {
throw new Error("PushRule name must be specified");
}
this.add(rules);
}
get structure() {
return {
components: this.rules,
compositionStyle: GoalSetter_1.GoalSettingCompositionStyle.FirstMatch,
};
}
/**
* Return a PushRules with a subset of the rules of this one
* @param {(p: PushMapping<V>) => boolean} predicate
* @return {PushRules<V>}
*/
filter(predicate) {
return new PushRules("name-", this.choices.filter(predicate));
}
add(rules) {
this.choices = this.choices.concat(rules);
}
async mapping(pi) {
for (const pc of this.choices) {
const found = await pc.mapping(pi);
logger_1.logger.debug("Eligible PushRule named %s returned choice %j", pc.name, found);
if (found) {
logger_1.logger.debug("PushRules '%s': Value for push on %j is %j", this.name, pi.id, found);
return found;
}
else if (found === null) {
return undefined;
}
}
return undefined;
}
}
exports.PushRules = PushRules;
//# sourceMappingURL=PushRules.js.map