@atomist/sdm-pack-fingerprints
Version:
an Atomist SDM Extension Pack for fingerprinting code
174 lines • 7.4 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 automation_client_1 = require("@atomist/automation-client");
const clj_editors_1 = require("@atomist/clj-editors");
const _ = require("lodash");
const fingerprints_1 = require("../adhoc/fingerprints");
const preferences_1 = require("../adhoc/preferences");
const callbacks_1 = require("../checktarget/callbacks");
const messageMaker_1 = require("../checktarget/messageMaker");
const fingerprintSupport_1 = require("./fingerprintSupport");
/**
* Give each Aspect the opportunity to evaluate the current FP, the previous FP, and any target FP
*
* @param fp current Fingerprint
* @param previous Fingeprint from Push.before (could be nil)
* @param info missing info
* @param handlers deprecated handlers
* @param aspect parent Aspect for this Fingerprint
* @param i PushImpactListenerInvocation
*/
function handleDiffs(fp, previous, info, handlers, aspect, i) {
return __awaiter(this, void 0, void 0, function* () {
const diff = Object.assign({}, info, { from: previous, to: fp, branch: i.push.branch, owner: i.push.repo.owner, repo: i.push.repo.name, sha: i.push.after.sha, data: {
from: [],
to: [],
} });
let diffVotes = [];
if (previous && fp.sha !== previous.sha) {
diffVotes = yield Promise.all(handlers
.filter(h => h.diffHandler)
.filter(h => h.selector(fp))
.map(h => h.diffHandler(i, diff, aspect)));
}
const currentVotes = yield Promise.all(handlers
.filter(h => h.handler)
.filter(h => h.selector(fp))
.map(h => h.handler(i, diff, aspect)));
let aspectVotes = [];
if (aspect.workflows) {
aspectVotes = yield Promise.all(aspect.workflows.map(h => h(i, diff, aspect)));
}
return [].concat(diffVotes, currentVotes, aspectVotes);
});
}
function lastFingerprints(sha, graphClient) {
return __awaiter(this, void 0, void 0, function* () {
// TODO what about empty queries, and missing fingerprints on previous commit
const results = yield graphClient.query({
name: "GetAllFpsOnSha",
options: automation_client_1.QueryNoCacheOptions,
variables: {
sha,
},
});
return results.Commit[0].analysis.reduce((record, fp) => {
if (fp.name) {
record[fp.name] = {
sha: fp.sha,
data: JSON.parse(fp.data),
name: fp.name,
type: fp.type,
version: "1.0",
abbreviation: "abbrev",
};
}
return record;
}, {});
});
}
function missingInfo(i) {
return __awaiter(this, void 0, void 0, function* () {
const info = {
providerId: _.get(i, "push.repo.org.provider.providerId"),
channel: _.get(i, "push.repo.channels[0].name"),
};
if (!!info.providerId && !!i.push.id) {
try {
const targets = yield preferences_1.getFPTargets(i.context.graphClient);
return Object.assign({}, info, { targets });
}
catch (e) {
return Object.assign({}, info, { targets: { TeamConfiguration: [] } });
}
}
else {
throw new Error(`PushImpactListenerInvocation missing providerId or push id. Info not available.`);
}
});
}
exports.computeFingerprints = (fingerprinters, p) => __awaiter(this, void 0, void 0, function* () {
const allFps = (yield Promise.all(fingerprinters.map(x => x.extract(p)))).reduce((acc, fps) => {
if (fps && !(fps instanceof Array)) {
acc.push(fps);
return acc;
}
else if (fps) {
// TODO does concat return the larger array?
return acc.concat(fps);
}
else {
return acc;
}
}, []);
return allFps;
});
/**
* Construct our FingerprintRunner for the current registrations
*/
function fingerprintRunner(fingerprinters, handlers, computer, options = {
aspects: [],
transformPresentation: fingerprintSupport_1.DefaultTransformPresentation,
messageMaker: messageMaker_1.messageMaker,
}) {
const targetDiffBallot = callbacks_1.votes(options);
const tallyVotes = (vts, fingerprintHandlers, i, info) => __awaiter(this, void 0, void 0, function* () {
const coordinate = {
owner: i.push.repo.owner,
repo: i.push.repo.name,
sha: i.push.after.sha,
providerId: info.providerId,
branch: i.push.branch,
};
return targetDiffBallot(i.context, vts, coordinate, info.channel);
});
return (i) => __awaiter(this, void 0, void 0, function* () {
const p = i.project;
let previous = {};
if (!!i.push.before) {
previous = yield lastFingerprints(i.push.before.sha, i.context.graphClient);
}
automation_client_1.logger.info(`Found ${Object.keys(previous).length} fingerprints`);
const allFps = yield computer(fingerprinters, p);
automation_client_1.logger.debug(clj_editors_1.renderData(allFps));
yield fingerprints_1.sendFingerprintToAtomist(i, allFps, previous);
try {
const info = yield missingInfo(i);
const allVotes = (yield Promise.all(allFps.map(fp => {
const fpAspect = fingerprinters.find(aspects => aspects.name === (fp.type || fp.name));
return handleDiffs(fp, previous[fp.name], info, handlers, fpAspect, i);
}))).reduce((acc, vts) => acc.concat(vts), []);
automation_client_1.logger.debug(`Votes: ${clj_editors_1.renderData(allVotes)}`);
yield tallyVotes(allVotes, handlers, i, info);
}
catch (e) {
automation_client_1.logger.warn(`Not handling diffs (${e.message})`);
}
return allFps;
});
}
exports.fingerprintRunner = fingerprintRunner;
//# sourceMappingURL=runner.js.map