@atomist/sdm-pack-fingerprints
Version:
an Atomist SDM Extension Pack for fingerprinting code
174 lines • 8.35 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) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const editModes_1 = require("@atomist/automation-client/lib/operations/edit/editModes");
const sdm_1 = require("@atomist/sdm");
const array_1 = require("@atomist/sdm-core/lib/util/misc/array");
const _ = require("lodash");
const callbacks_1 = require("../checktarget/callbacks");
const messageMaker_1 = require("../checktarget/messageMaker");
const applyFingerprint_1 = require("../handlers/commands/applyFingerprint");
const broadcast_1 = require("../handlers/commands/broadcast");
const fingerprints_1 = require("../handlers/commands/fingerprints");
const list_1 = require("../handlers/commands/list");
const showTargets_1 = require("../handlers/commands/showTargets");
const updateTarget_1 = require("../handlers/commands/updateTarget");
const runner_1 = require("./runner");
function forFingerprints(...s) {
return fp => {
return s.map(n => (fp.type === n) || (fp.name === n))
.reduce((acc, v) => acc || v);
};
}
exports.forFingerprints = forFingerprints;
exports.DefaultTargetDiffHandler = (ctx, diff, aspect) => __awaiter(this, void 0, void 0, function* () {
const v = yield callbacks_1.checkFingerprintTarget(ctx.context, diff, aspect, () => __awaiter(this, void 0, void 0, function* () {
return diff.targets;
}));
return v;
});
/**
* wrap a FingerprintDiffHandler to only check if the shas have changed
*
* @param handler the FingerprintDiffHandler to wrap
*/
function diffOnlyHandler(handler) {
return (context, diff, aspect) => __awaiter(this, void 0, void 0, function* () {
if (diff.from && diff.to.sha !== diff.from.sha) {
return handler(context, diff, aspect);
}
else {
return {
abstain: true,
};
}
});
}
exports.diffOnlyHandler = diffOnlyHandler;
exports.DefaultTransformPresentation = createPullRequestTransformPresentation();
/**
* Creates the default TransformPresentation for raising PullRequests
*/
function createPullRequestTransformPresentation(options = {}) {
return (ci, p) => new LazyPullRequest(options, ci.parameters, p);
}
exports.createPullRequestTransformPresentation = createPullRequestTransformPresentation;
/**
* Lazy implementation of PullRequest to defer creation of title and body etc to when they
* are actually needed
*
* This allows us to better format the properties of the PullRequest as we have access to the
* parameters instance.
*/
class LazyPullRequest {
constructor(options, parameters, project) {
this.options = options;
this.parameters = parameters;
this.project = project;
this.fingerprint = (this.parameters.fingerprint || this.parameters.targetfingerprint || this.parameters.type);
if (!this.fingerprint) {
this.fingerprint = this.parameters.fingerprints;
if (!!this.fingerprint) {
this.fingerprint = this.fingerprint.split(",").map(f => f.trim()).join(", ");
}
}
this.branchName = `${this.options.branchPrefix || "apply-target-fingerprint"}-${sdm_1.formatDate()}`;
}
get branch() {
return this.branchName;
}
get title() {
return this.options.title || this.parameters.title || `Apply target fingerprint (${this.fingerprint})`;
}
get body() {
return `${this.options.body || this.parameters.body || this.title}\n\n[atomist:generated]`;
}
get message() {
return this.options.message || this.title;
}
get targetBranch() {
return this.project.id.branch;
}
get autoMerge() {
const autoMerge = _.get(this.options, "autoMerge") || {};
return {
method: autoMerge.method || editModes_1.AutoMergeMethod.Squash,
mode: autoMerge.mode || editModes_1.AutoMergeMode.ApprovedReview,
};
}
}
/**
* Install and configure the fingerprint support in this SDM
*/
function fingerprintSupport(options) {
return Object.assign({}, sdm_1.metadata(), { configure: (sdm) => {
const fingerprints = array_1.toArray(options.aspects);
// const handlerRegistrations: RegisterFingerprintImpactHandler[]
// = Array.isArray(options.handlers) ? options.handlers : [options.handlers];
// const handlers: FingerprintHandler[] = handlerRegistrations.map(h => h(sdm, fingerprints));
const handlerRegistrations = [];
const handlers = [];
const runner = runner_1.fingerprintRunner(fingerprints, handlers, runner_1.computeFingerprints, Object.assign({ messageMaker: messageMaker_1.messageMaker, transformPresentation: exports.DefaultTransformPresentation }, options));
// tslint:disable:deprecation
if (!!options.fingerprintGoal) {
options.fingerprintGoal.with({
name: `${options.fingerprintGoal.uniqueName}-fingerprinter`,
action: (i) => __awaiter(this, void 0, void 0, function* () {
yield runner(i);
return [];
}),
});
}
if (!!options.pushImpactGoal) {
options.pushImpactGoal.withListener(runner);
}
configure(sdm, handlerRegistrations, fingerprints, options.transformPresentation || exports.DefaultTransformPresentation);
} });
}
exports.fingerprintSupport = fingerprintSupport;
function configure(sdm, handlers, aspects, editModeMaker) {
sdm.addCommand(list_1.listFingerprints(sdm));
sdm.addCommand(list_1.listFingerprint(sdm));
// set a target given using the entire JSON fingerprint payload in a parameter
sdm.addCommand(updateTarget_1.setTargetFingerprint(aspects));
// set a different target after noticing that a fingerprint is different from current target
sdm.addCommand(updateTarget_1.updateTargetFingerprint(sdm, aspects));
// Bootstrap a fingerprint target by selecting one from current project
sdm.addCommand(updateTarget_1.selectTargetFingerprintFromCurrentProject(sdm));
// Bootstrap a fingerprint target from project by name
sdm.addCommand(updateTarget_1.setTargetFingerprintFromLatestMaster(sdm, aspects));
sdm.addCommand(updateTarget_1.deleteTargetFingerprint(sdm));
// standard actionable message embedding ApplyTargetFingerprint
sdm.addCommand(broadcast_1.broadcastFingerprintNudge(aspects));
sdm.addCommand(messageMaker_1.ignoreCommand(aspects));
sdm.addCommand(showTargets_1.listFingerprintTargets(sdm));
sdm.addCommand(showTargets_1.listOneFingerprintTarget(sdm));
sdm.addCommand(fingerprints_1.FingerprintMenu);
sdm.addCodeTransformCommand(applyFingerprint_1.applyTarget(sdm, aspects, editModeMaker));
sdm.addCodeTransformCommand(applyFingerprint_1.applyTargets(sdm, aspects, editModeMaker));
sdm.addCodeTransformCommand(applyFingerprint_1.applyTargetBySha(sdm, aspects, editModeMaker));
sdm.addCommand(applyFingerprint_1.broadcastFingerprintMandate(sdm, aspects));
}
//# sourceMappingURL=fingerprintSupport.js.map