@atomist/sdm-pack-aspect
Version:
an Atomist SDM Extension Pack for visualizing drift across an organization
197 lines • 8.4 kB
JavaScript
;
/*
* 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());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
const automation_client_1 = require("@atomist/automation-client");
const analytics_1 = require("./analytics");
/**
* A reasonable number of repositories to analyze at a time.
* The bucketing mechanism will wait for this many to complete, then
* start another batch. If this is too big, the web interface can't respond.
*/
exports.DefaultPoolSize = 6;
/**
* This class knows how to execute an analysis run,
* given some functions that are specific to the source
* of the repositories.
*/
class AnalysisRun {
constructor(world, params) {
this.world = world;
this.params = params;
if (!this.params.maxRepos) {
this.params.maxRepos = 1000;
}
if (!this.params.poolSize) {
this.params.poolSize = exports.DefaultPoolSize;
}
}
run() {
return __awaiter(this, void 0, void 0, function* () {
const analysisBeingTracked = this.world.analysisTracking.startAnalysis({
description: this.params.description,
});
try {
const plannedRepos = yield takeFromIterator(this.params.maxRepos, this.world.howToFindRepos());
const trackedRepos = plannedRepos.map(pr => ({
tracking: analysisBeingTracked.plan(this.world.describeFoundRepo(pr)),
foundRepo: pr,
}));
// run poolSize at the same time
const chewThroughThese = trackedRepos.slice();
while (chewThroughThese.length > 0) {
const promises = chewThroughThese.splice(0, this.params.poolSize)
.map(trackedRepo => analyzeOneRepo(this.world, Object.assign(Object.assign({}, trackedRepo), { workspaceId: this.params.workspaceId })));
yield Promise.all(promises);
}
automation_client_1.logger.debug("Computing analytics over all fingerprints...");
// Question for Rod: should this run intermittently or only at the end?
// Answer from Rod: intermitently.
yield analytics_1.computeAnalytics(this.world.persister, this.params.workspaceId);
const finalResult = trackedRepos.map(tr => tr.tracking.spiderResult()).reduce(combineSpiderResults, emptySpiderResult);
analysisBeingTracked.stop();
return finalResult;
}
catch (error) {
analysisBeingTracked.failed(error);
return emptySpiderResult;
}
});
}
}
exports.AnalysisRun = AnalysisRun;
function analyzeOneRepo(world, params) {
return __awaiter(this, void 0, void 0, function* () {
automation_client_1.logger.info("Now analyzing: " + JSON.stringify(params.foundRepo));
const { tracking, workspaceId, foundRepo } = params;
tracking.beganAnalysis();
const repoRef = yield world.determineRepoRef(foundRepo);
tracking.setRepoRef(repoRef);
// we might choose to skip this one
if (yield existingRecordShouldBeKept(world, repoRef)) {
// enhancement: record timestamp of kept record
tracking.keptExisting();
return;
}
// clone
let project;
try {
project = yield world.howToClone(repoRef, foundRepo);
}
catch (error) {
tracking.failed({ whileTryingTo: "clone", error });
return;
}
// we might choose to skip this one (is this used anywhere?)
if (world.projectFilter && !(yield world.projectFilter(project))) {
tracking.skipped("projectFilter returned false");
return;
}
// analyze !
let analysis;
try {
analysis = yield world.analyzer.analyze(project, tracking);
}
catch (error) {
tracking.failed({ whileTryingTo: "analyze", error });
return;
}
// save :-)
const persistResult = yield world.persister.persist({
workspaceId,
repoRef,
analysis: Object.assign(Object.assign({}, analysis), { id: repoRef }),
timestamp: new Date(),
});
persistResult.failedFingerprints.forEach(f => {
tracking.failFingerprint(f.failedFingerprint, f.error);
});
if (persistResult.failed.length === 1) {
tracking.failed(persistResult.failed[0]);
}
else if (persistResult.succeeded.length === 1) {
tracking.persisted(persistResult.succeeded[0]);
}
else {
throw new Error("Unexpected condition in persistResult: " + JSON.stringify(persistResult));
}
});
}
function existingRecordShouldBeKept(opts, repoId) {
return __awaiter(this, void 0, void 0, function* () {
const found = yield opts.persister.loadByRepoRef(repoId, true);
if (!found || !found.analysis) {
return false;
}
return opts.keepExistingPersisted(found);
});
}
function takeFromIterator(max, iter) {
var iter_1, iter_1_1;
var e_1, _a;
return __awaiter(this, void 0, void 0, function* () {
let i = 0;
const result = [];
try {
for (iter_1 = __asyncValues(iter); iter_1_1 = yield iter_1.next(), !iter_1_1.done;) {
const t = iter_1_1.value;
if (++i > max) {
return result;
}
result.push(t);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (iter_1_1 && !iter_1_1.done && (_a = iter_1.return)) yield _a.call(iter_1);
}
finally { if (e_1) throw e_1.error; }
}
return result;
});
}
function combineSpiderResults(r1, r2) {
return {
repositoriesDetected: r1.repositoriesDetected + r2.repositoriesDetected,
failed: [...r1.failed, ...r2.failed],
keptExisting: [...r1.keptExisting, ...r2.keptExisting],
persistedAnalyses: [...r1.persistedAnalyses, ...r2.persistedAnalyses],
};
}
const emptySpiderResult = {
repositoriesDetected: 0,
failed: [],
keptExisting: [],
persistedAnalyses: [],
};
//# sourceMappingURL=common.js.map