UNPKG

@atomist/sdm-pack-aspect

Version:

an Atomist SDM Extension Pack for visualizing drift across an organization

130 lines 5.54 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 automation_client_1 = require("@atomist/automation-client"); const sdm_pack_fingerprint_1 = require("@atomist/sdm-pack-fingerprint"); const classificationAspect_1 = require("../compose/classificationAspect"); /** * Emit fingerprint aspect, count aspect and classification aspect for the given review comment. * If a terminator CodeTransform is provided, it will try to delete all instances of the fingerprint */ function reviewerAspects(opts) { const aspects = [ reviewCommentAspect(opts), reviewCommentCountAspect(opts), ]; if (opts.emitClassifier || opts.tag) { aspects.push(reviewCommentClassificationAspect(opts)); } return aspects; } exports.reviewerAspects = reviewerAspects; function isReviewCommentFingerprint(fp) { const maybe = fp.data; return !!maybe && !!maybe.subcategory && !!maybe.detail; } exports.isReviewCommentFingerprint = isReviewCommentFingerprint; /** * Create fingerprints from the output of this reviewer. * Every fingerprint is unique */ function reviewCommentAspect(opts) { const inspection = isReviewerRegistration(opts.reviewer) ? opts.reviewer.inspection : opts.reviewer; const type = reviewCommentAspectName(opts.name); return Object.assign(Object.assign({}, opts), { name: type, extract: (p, pli) => __awaiter(this, void 0, void 0, function* () { const result = yield inspection(p, Object.assign(Object.assign({}, pli), { push: pli })); if (!result) { return []; } return result.comments.map(data => { return sdm_pack_fingerprint_1.fingerprintOf({ type, data, }); }); }) }); } function reviewCommentAspectName(name) { return "instance_" + name; } function reviewCommentClassificationAspect(opts) { const requiredType = reviewCommentAspectName(opts.name); return classificationAspect_1.projectClassificationAspect({ name: `has_${opts.name}`, displayName: opts.displayName, }, { tags: opts.tag || `has-${opts.name}`, reason: `Has review comment ${opts.name}`, testFingerprints: (fps) => __awaiter(this, void 0, void 0, function* () { return fps.some(fp => isReviewCommentFingerprint(fp) && fp.type === requiredType); }), }); } /** * Count the problematic usage and delete it if necessary * @param {ReviewerAspectOptions} opts * @return {CountAspect} */ function reviewCommentCountAspect(opts) { const requiredType = reviewCommentAspectName(opts.name); const type = countFingerprintTypeFor(opts.name); return { name: type, displayName: opts.displayName, extract: () => __awaiter(this, void 0, void 0, function* () { return []; }), consolidate: (fps) => __awaiter(this, void 0, void 0, function* () { const count = fps.filter(fp => isReviewCommentFingerprint(fp) && fp.type === requiredType).length; return sdm_pack_fingerprint_1.fingerprintOf({ type, data: { count }, }); }), apply: opts.terminator ? terminateWithExtremePrejudice(opts) : undefined, }; } exports.reviewCommentCountAspect = reviewCommentCountAspect; function countFingerprintTypeFor(name) { return `count_${name}`; } function findReviewCommentCountFingerprint(name, fps) { const type = countFingerprintTypeFor(name); return fps.find(fp => fp.type === type); } exports.findReviewCommentCountFingerprint = findReviewCommentCountFingerprint; function terminateWithExtremePrejudice(opts) { return (p, pi) => __awaiter(this, void 0, void 0, function* () { const to = pi.parameters.fp; if (to.data.count !== 0) { const msg = `Doesn't make sense to keep a non-zero number of fingerprints in ${opts.name}`; automation_client_1.logger.warn(msg); return { target: p, success: false, error: new Error(msg) }; } return opts.terminator(p, pi); }); } function isReviewerRegistration(er) { const maybe = er; return !!maybe.inspection; } //# sourceMappingURL=reviewerAspect.js.map