UNPKG

@atomist/sdm-pack-aspect

Version:

an Atomist SDM Extension Pack for visualizing drift across an organization

135 lines 5.95 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) { 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 AspectRegistry_1 = require("./AspectRegistry"); const _ = require("lodash"); const error_1 = require("tslint/lib/error"); const scoring_1 = require("../scorer/scoring"); const showTiming_1 = require("../util/showTiming"); const ProblemStore_1 = require("./ProblemStore"); class DefaultAspectRegistry { constructor(opts) { this.opts = opts; this.taggers = []; this.combinationTaggers = []; opts.aspects.forEach(f => { if (!f) { throw new error_1.Error("A null aspect was passed in"); } }); } /** * Add a tagger that will work on all repositories. */ withTaggers(...taggers) { this.taggers.push(...taggers); return this; } withCombinationTaggers(...taggers) { this.combinationTaggers.push(...taggers); return this; } combinationTagsFor(fps, id, tagContext) { return _.uniqBy(this.combinationTaggers .map(tagger => (Object.assign({}, tagger, { tag: tagger.test(fps, id, tagContext) }))) .filter(t => !!t.tag), tag => tag.name); } tagAndScoreRepos(workspaceId, repos) { return __awaiter(this, void 0, void 0, function* () { const scored = yield showTiming_1.showTiming(`Tag and score ${repos.length} repos`, () => __awaiter(this, void 0, void 0, function* () { return scoring_1.scoreRepos(this.scorers, yield this.tagRepos({ repoCount: repos.length, // TODO fix this averageFingerprintCount: -1, workspaceId, aspectRegistry: this, }, repos), this.opts.scoreWeightings); })); return scored; }); } get availableTags() { return _.uniqBy([...this.taggers, ...this.combinationTaggers], tag => tag.name); } get aspects() { return this.opts.aspects; } aspectOf(type) { return type ? this.aspects.find(f => f.name === type) : undefined; } undesirableUsageCheckerFor(workspaceId) { return __awaiter(this, void 0, void 0, function* () { // TODO going for check functions is inelegant return ProblemStore_1.chainUndesirableUsageCheckers((yield ProblemStore_1.problemStoreBackedUndesirableUsageCheckerFor(this.problemStore, workspaceId)).check, this.opts.undesirableUsageChecker.check); }); } get idealStore() { return this.opts.idealStore; } get problemStore() { return this.opts.problemStore; } get scorers() { return this.opts.scorers || []; } tagRepos(tagContext, repos) { return __awaiter(this, void 0, void 0, function* () { const simpleTaggers = this.taggers.filter(AspectRegistry_1.isTagger); const workspaceSpecificTaggers = yield Promise.all(this.taggers .filter(td => !AspectRegistry_1.isTagger(td)) // TODO why is this cast needed? .map(td => td.create(tagContext.workspaceId, this))); const taggersToUse = [...simpleTaggers, ...workspaceSpecificTaggers]; return Promise.all(repos.map(repo => this.tagRepo(tagContext, repo, taggersToUse))); }); } tagRepo(tagContext, repo, taggers) { return __awaiter(this, void 0, void 0, function* () { return Object.assign({}, repo, { tags: (yield tagsIn(repo.analysis.fingerprints, repo.repoRef, tagContext, taggers)) .concat(this.combinationTagsFor(repo.analysis.fingerprints, repo.repoRef, tagContext)) }); }); } } exports.DefaultAspectRegistry = DefaultAspectRegistry; function defaultedToDisplayableFingerprintName(aspect) { return (aspect && aspect.toDisplayableFingerprintName) || (name => name); } exports.defaultedToDisplayableFingerprintName = defaultedToDisplayableFingerprintName; function defaultedToDisplayableFingerprint(aspect) { return (aspect && aspect.toDisplayableFingerprint) || (fp => fp && fp.data); } exports.defaultedToDisplayableFingerprint = defaultedToDisplayableFingerprint; function tagsFor(fp, id, tagContext, taggers) { return _.uniqBy(taggers .map(tagger => (Object.assign({}, tagger, { tag: tagger.test(fp, id, tagContext) }))) .filter(t => !!t.tag), tag => tag.name); } function tagsIn(fps, id, tagContext, taggersToUse) { return __awaiter(this, void 0, void 0, function* () { return _.uniqBy(_.flatten(fps.map(fp => tagsFor(fp, id, tagContext, taggersToUse))), tag => tag.name) .sort(); }); } //# sourceMappingURL=DefaultAspectRegistry.js.map