UNPKG

@atomist/sdm-pack-aspect

Version:

an Atomist SDM Extension Pack for visualizing drift across an organization

160 lines 7.13 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_pack_fingerprint_1 = require("@atomist/sdm-pack-fingerprint"); const Score_1 = require("../../scorer/Score"); const commonBands_1 = require("../../util/commonBands"); const _ = require("lodash"); function isScoredAspectFingerprint(fp) { const maybe = fp; return !!maybe && !!maybe.data && !!maybe.data.weightedScore; } exports.isScoredAspectFingerprint = isScoredAspectFingerprint; /** * Default properties to configure ScoredAspect */ exports.ScoredAspectDefaults = { stats: { defaultStatStatus: { entropy: false, }, basicStatsPath: "score", }, toDisplayableFingerprint: fp => commonBands_1.starBand(fp.data.weightedScore), }; function isRepositoryScorer(s) { const maybe = s; return !!maybe && !!maybe.scoreFingerprints; } exports.isRepositoryScorer = isRepositoryScorer; function isPushScorer(scorer) { const maybe = scorer; return !!maybe && !!maybe.scorePush; } exports.isPushScorer = isPushScorer; function isPushOrProjectScorer(scorer) { const maybe = scorer; return !!maybe && !!maybe.scoreProject || isPushScorer(scorer); } exports.isPushOrProjectScorer = isPushOrProjectScorer; /** * Score this aspect based on projects, from low to high. * Requires no other fingerprints */ function scoringAspect(opts) { const pushScorers = opts.scorers.filter(isPushOrProjectScorer); const repositoryScorers = opts.scorers.filter(isRepositoryScorer); return Object.assign(Object.assign({ extract: (p, pili) => __awaiter(this, void 0, void 0, function* () { // Just save these scores. They'll go into consolidate const scores = yield pushAndProjectScoresFor(pushScorers, pili); pili.scores = scores; return []; }), consolidate: (fingerprints, p, pili) => __awaiter(this, void 0, void 0, function* () { const emittedFingerprints = []; const repoToScore = { analysis: { id: p.id, fingerprints } }; const distinctNonRootPaths = _.uniq(repoToScore.analysis.fingerprints .map(fp => fp.path) .filter(p => !["", ".", undefined].includes(p))); for (const path of distinctNonRootPaths) { const scores = yield fingerprintScoresFor(repositoryScorers.filter(rs => !rs.baseOnly), withFingerprintsOnlyUnderPath(repoToScore, path)); const scored = { scores }; const weightedScore = Score_1.weightedCompositeScore(scored, opts.scoreWeightings); emittedFingerprints.push(toFingerprint(opts.name, weightedScore, path)); } // Score under root const additionalScores = Object.assign(Object.assign(Object.assign(Object.assign({}, yield fingerprintScoresFor(repositoryScorers, withFingerprintsOnlyUnderPath(repoToScore, ""))), yield fingerprintScoresFor(repositoryScorers, withFingerprintsOnlyUnderPath(repoToScore, "."))), yield fingerprintScoresFor(repositoryScorers, withFingerprintsOnlyUnderPath(repoToScore, undefined))), yield fingerprintScoresFor(repositoryScorers.filter(rs => rs.scoreAll), repoToScore)); const scores = Object.assign(Object.assign({}, additionalScores), pili.scores); // Add rollup of subprojects emittedFingerprints.forEach(ef => { scores[ef.path + "_" + ef.name] = { name: ef.path + "_" + ef.name, score: ef.data.weightedScore, }; }); const scored = { scores }; const weightedScore = Score_1.weightedCompositeScore(scored, opts.scoreWeightings); emittedFingerprints.push(toFingerprint(opts.name, weightedScore)); return emittedFingerprints; }) }, exports.ScoredAspectDefaults), opts); } function toFingerprint(type, data, path) { return { type, name: type, path, data, sha: sdm_pack_fingerprint_1.sha256(JSON.stringify(data.weightedScore)), }; } function withFingerprintsOnlyUnderPath(rts, path) { return { analysis: { id: Object.assign(Object.assign({}, rts.analysis.id), { path }), fingerprints: rts.analysis.fingerprints.filter(fp => fp.path === path), }, }; } function fingerprintScoresFor(repositoryScorers, toScore) { return __awaiter(this, void 0, void 0, function* () { const scores = {}; for (const scorer of repositoryScorers) { const sr = yield scorer.scoreFingerprints(toScore); if (sr) { const score = Object.assign(Object.assign({}, sr), { name: scorer.name, category: scorer.category }); scores[score.name] = score; } } return scores; }); } exports.fingerprintScoresFor = fingerprintScoresFor; function pushAndProjectScoresFor(pushScorers, toScore) { return __awaiter(this, void 0, void 0, function* () { const scores = {}; for (const scorer of pushScorers) { const sr = isPushScorer(scorer) ? yield scorer.scorePush(toScore) : yield scorer.scoreProject(toScore.project); if (sr) { const score = Object.assign(Object.assign({}, sr), { name: scorer.name, category: scorer.category }); scores[score.name] = score; } } return scores; }); } function emitScoringAspect(name, scorers, scoreWeightings) { return scorers.length > 0 ? scoringAspect({ name, displayName: `Scores for ${name}`, scorers, scoreWeightings, }) : undefined; } exports.emitScoringAspect = emitScoringAspect; //# sourceMappingURL=ScoredAspect.js.map