UNPKG

@atomist/sdm-pack-aspect

Version:

an Atomist SDM Extension Pack for visualizing drift across an organization

146 lines 6.2 kB
"use strict"; /* * 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 sdm_core_1 = require("@atomist/sdm-core"); const array_1 = require("@atomist/sdm-core/lib/util/misc/array"); const sdm_pack_fingerprint_1 = require("@atomist/sdm-pack-fingerprint"); const _ = require("lodash"); function isProjectClassifier(c) { const maybe = c; return !!maybe.test && !!maybe.reason && !!maybe.tags; } function isDerivedClassifier(c) { const maybe = c; return !!maybe.testFingerprints; } function isTagger(c) { const maybe = c; return !!maybe.test && !!maybe.name; } function isClassificationDataFingerprint(fp) { const maybe = fp; return !!maybe.data && !!maybe.data.reason; } exports.isClassificationDataFingerprint = isClassificationDataFingerprint; function isClassificationAspect(a) { const maybe = a; return !!maybe.classifierMetadata; } exports.isClassificationAspect = isClassificationAspect; /** * Classify the project uniquely or otherwise * undefined to return no fingerprint * @param opts: Whether to allow multiple tags and whether to compute a fingerprint in all cases * @param classifiers classifier functions */ function projectClassificationAspect(opts, ...classifiers) { const projectClassifiers = classifiers.filter(isProjectClassifier); const derivedClassifiers = [ ...classifiers.filter(isDerivedClassifier), ...classifiers.filter(isTagger).map(toDerivedClassifier), ]; return Object.assign({ classifierMetadata: _.flatten([...projectClassifiers, ...derivedClassifiers].map(c => ({ reason: c.reason, tags: c.tags, }))), extract: (p, pili) => __awaiter(this, void 0, void 0, function* () { const emitter = emitFingerprints(projectClassifiers, opts); return emitter([], p, pili); }), consolidate: (fps, p, pili) => __awaiter(this, void 0, void 0, function* () { const test = emitFingerprints(derivedClassifiers, opts); return test(fps, p, pili); }), toDisplayableFingerprint: fp => fp.name }, opts); } exports.projectClassificationAspect = projectClassificationAspect; function toDerivedClassifier(tagger) { return { tags: [tagger.name], reason: tagger.description, testFingerprints: (fingerprints, p) => __awaiter(this, void 0, void 0, function* () { const rts = { analysis: { id: p.id, fingerprints, }, }; return tagger.test(rts); }), }; } function emitFingerprints(classifiers, opts) { return (fps, p, pili) => __awaiter(this, void 0, void 0, function* () { const found = []; function recordFingerprint(name, reason) { if (!found.some(fp => fp.name === name)) { found.push({ type: opts.name, name, data: { description: opts.displayName, reason }, sha: sdm_pack_fingerprint_1.sha256({ present: true }), }); } } // Don't re-evaluate if we've already seen the tag const classifierMatches = (classifier, fps, p, pili) => __awaiter(this, void 0, void 0, function* () { return !_.includes(found.map(f => f.name), classifier.tags) && isProjectClassifier(classifier) ? classifier.test(p, pili) : classifier.testFingerprints(fps, p, pili); }); if (opts.stopAtFirst || !sdm_core_1.isInLocalMode()) { // Ordering is important. Execute in series and stop when we find a match. // Also team mode requires serial execution for (const classifier of classifiers) { if (yield classifierMatches(classifier, fps, p, pili)) { for (const name of array_1.toArray(classifier.tags)) { recordFingerprint(name, classifier.reason); } if (opts.stopAtFirst) { break; } } } } else { // Ordering is not important. We can run in parallel yield Promise.all(classifiers.map(classifier => { return classifierMatches(classifier, fps, p, pili) .then(result => result ? ({ tags: array_1.toArray(classifier.tags), reason: classifier.reason, }) : undefined) .then(st => { if (st) { for (const name of st.tags) { recordFingerprint(name, st.reason); } } }); })); } return found; }); } //# sourceMappingURL=classificationAspect.js.map