UNPKG

@atomist/sdm-pack-aspect

Version:

an Atomist SDM Extension Pack for visualizing drift across an organization

163 lines 7.42 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 _ = require("lodash"); const scoring_1 = require("../scorer/scoring"); const types_1 = require("../typings/types"); const showTiming_1 = require("../util/showTiming"); const AspectRegistry_1 = require("./AspectRegistry"); const ProblemStore_1 = require("./ProblemStore"); class DefaultAspectRegistry { constructor(opts) { this.opts = opts; this.taggers = []; opts.aspects.forEach(f => { if (!f) { throw new Error("A null aspect was passed in"); } }); } /** * Add a tagger that will work on all repositories. */ withTaggers(...taggers) { this.taggers.push(...taggers); return this; } scoreWorkspace(workspaceId, workspaceToScore) { return __awaiter(this, void 0, void 0, function* () { return scoring_1.scoreOrg(this.opts.workspaceScorers || [], workspaceToScore, this.opts.scoreWeightings); }); } tagAndScoreRepos(workspaceId, repos, tsOpts) { return __awaiter(this, void 0, void 0, function* () { const tagged = yield showTiming_1.showTiming(`Tag ${repos.length} repos with ${this.taggers.length} taggers`, () => __awaiter(this, void 0, void 0, function* () { return this.tagRepos({ repoCount: repos.length, // TODO fix this averageFingerprintCount: -1, workspaceId, aspectRegistry: this, }, repos); })); const scored = yield showTiming_1.showTiming(`Score ${repos.length} repos with ${this.scorers.length} scorers`, () => __awaiter(this, void 0, void 0, function* () { return scoring_1.scoreRepos(this.scorers, tagged, this.opts.scoreWeightings, tsOpts); })); return scored; }); } get availableTags() { return _.uniqBy(this.taggers, tag => tag.name); } get aspects() { return this.opts.aspects; } aspectOf(type) { return type ? this.aspects.find(f => f.name === type) : undefined; } reportDetailsOf(typeOrAspect, workspaceId) { return __awaiter(this, void 0, void 0, function* () { const type = typeof typeOrAspect === "string" ? typeOrAspect : typeOrAspect.name; const aspect = this.aspectOf(type); if (!!aspect) { return aspect.details; } if (!sdm_core_1.isInLocalMode() && !!_.get(this.opts.configuration, "graphql.client.factory")) { const aspectRegistrations = yield this.opts.configuration.graphql.client.factory.create(workspaceId, this.opts.configuration) .query({ name: "AspectRegistrations", variables: { state: [types_1.AspectRegistrationState.Enabled], }, }); const aspectRegistration = (_.get(aspectRegistrations, "AspectRegistration") || []) .filter(a => a.name === type); if (!!aspectRegistration && aspectRegistration.length > 0) { return aspectRegistration[0]; } } return undefined; }); } undesirableUsageCheckerFor(workspaceId) { return __awaiter(this, void 0, void 0, function* () { // TODO going for check functions is inelegant if (this.opts.undesirableUsageChecker) { return ProblemStore_1.chainUndesirableUsageCheckers((yield ProblemStore_1.problemStoreBackedUndesirableUsageCheckerFor(this.problemStore, workspaceId)).check, this.opts.undesirableUsageChecker.check); } return undefined; }); } 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 => taggerFrom(td, 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(Object.assign({}, repo), { tags: yield tagsFor(repo, tagContext, taggers) }); }); } } 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(rts, tagContext, taggers) { return __awaiter(this, void 0, void 0, function* () { const tags = yield Promise.all(taggers .map(tagger => tagger.test(rts) .then(yes => (Object.assign(Object.assign({}, tagger), { tag: yes ? tagger.name : undefined }))))); return _.uniqBy(tags.filter(t => !!t.tag), tag => tag.name); }); } function taggerFrom(wst, workspaceId, ar) { return __awaiter(this, void 0, void 0, function* () { return Object.assign(Object.assign({}, wst), { test: yield wst.createTest(workspaceId, ar) }); }); } //# sourceMappingURL=DefaultAspectRegistry.js.map