UNPKG

@atomist/sdm-pack-aspect

Version:

an Atomist SDM Extension Pack for visualizing drift across an organization

183 lines 7.69 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_1 = require("@atomist/sdm"); const array_1 = require("@atomist/sdm-core/lib/util/misc/array"); const preferences_1 = require("@atomist/sdm-pack-fingerprint/lib/adhoc/preferences"); const showTiming_1 = require("../../../util/showTiming"); /** * Analyzer implementation that captures timings that are useful during * development, but don't need to be captured during regular execution. */ class SpiderAnalyzer { constructor(aspects, virtualProjectFinder) { this.aspects = aspects; this.virtualProjectFinder = virtualProjectFinder; this.timings = {}; } analyze(p, repoTracking) { return __awaiter(this, void 0, void 0, function* () { const fingerprints = []; if (this.virtualProjectFinder) { // Seed the virtual project finder if we have one yield this.virtualProjectFinder.findVirtualProjectInfo(p); } const pili = yield fakePushImpactListenerInvocation(p); yield runExtracts(p, pili, this.aspects, fingerprints, this.timings, repoTracking); yield runConsolidates(p, pili, this.aspects.filter(aspect => !!aspect.consolidate), fingerprints, repoTracking); return { id: p.id, fingerprints, }; }); } } exports.SpiderAnalyzer = SpiderAnalyzer; function runExtracts(p, pili, aspects, fingerprints, timings, repoTracking) { return __awaiter(this, void 0, void 0, function* () { yield Promise.all(aspects .map(aspect => safeTimedExtract(aspect, p, pili, timings, repoTracking.plan(aspect, "extract")) .then(fps => fingerprints.push(...fps)))); }); } /** * This will run the consolidation aspects sequentially in order, passing in the result of previous consolidated fingerprints into * later aspects that consolidate. */ function runConsolidates(p, pili, aspects, fingerprints, repoTracking) { return __awaiter(this, void 0, void 0, function* () { for (const aspect of aspects) { const consolidatedFingerprints = yield safeConsolidate(aspect, fingerprints, p, pili, repoTracking.plan(aspect, "consolidate")); fingerprints.push(...consolidatedFingerprints); } }); } function safeTimedExtract(aspect, p, pili, timeRecorder, tracking) { return __awaiter(this, void 0, void 0, function* () { try { const timed = yield showTiming_1.time(() => __awaiter(this, void 0, void 0, function* () { const fps = array_1.toArray(yield aspect.extract(p, pili)) || []; fps.forEach(fp => { if (!fp.displayName && aspect.toDisplayableFingerprintName) { fp.displayName = aspect.toDisplayableFingerprintName(preferences_1.toName(fp.type, fp.name)); } if (!fp.displayValue && aspect.toDisplayableFingerprint) { fp.displayValue = aspect.toDisplayableFingerprint(fp); } return fp; }); return fps; })); addTiming(aspect.name, timed.millis, timeRecorder); const result = !!timed.result ? array_1.toArray(timed.result) : []; tracking.completed(result.length); return result; } catch (err) { tracking.failed(err); automation_client_1.logger.error("Please check your configuration of aspect %s.\n%s", aspect.name, err); return []; } }); } function addTiming(type, millis, timeRecorder) { let found = timeRecorder[type]; if (!found) { found = { extractions: 0, totalMillis: 0, }; timeRecorder[type] = found; } found.extractions++; found.totalMillis += millis; } function safeConsolidate(aspect, existingFingerprints, p, pili, tracking) { return __awaiter(this, void 0, void 0, function* () { try { const extracted = yield aspect.consolidate(existingFingerprints, p, pili); const result = !!extracted ? array_1.toArray(extracted) : []; tracking.completed(result.length); return result; } catch (err) { tracking.failed(err); automation_client_1.logger.error("Please check your configuration of aspect %s.\n%s", aspect.name, err); return []; } }); } function fetchChangedFiles(project) { return __awaiter(this, void 0, void 0, function* () { try { const output = yield sdm_1.execPromise("git", ["show", `--pretty=format:""`, "--name-only"], { cwd: project.baseDir }); return output.stdout.trim().split("\n"); } catch (err) { automation_client_1.logger.error("Failure getting changed files: %s", err.message); return []; } }); } /** * Make a fake push for the last commit to this project */ function fakePushImpactListenerInvocation(p) { return __awaiter(this, void 0, void 0, function* () { const project = p; const changedFiles = yield fetchChangedFiles(project); return { id: p.id, get context() { automation_client_1.logger.warn("Returning undefined context"); return undefined; }, commit: { sha: p.id.sha, }, project, push: { repo: undefined, branch: "master", }, addressChannels: () => __awaiter(this, void 0, void 0, function* () { automation_client_1.logger.warn("Cannot say anything in local mode"); }), get filesChanged() { return changedFiles; }, credentials: { token: process.env.GITHUB_TOKEN }, impactedSubProject: p, get preferences() { automation_client_1.logger.warn("Returning undefined preferences store"); return undefined; }, configuration: automation_client_1.configurationValue("", {}), }; }); } //# sourceMappingURL=SpiderAnalyzer.js.map