UNPKG

@atomist/sdm-pack-aspect

Version:

an Atomist SDM Extension Pack for visualizing drift across an organization

151 lines 7.21 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()); }); }; var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } 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); } }; var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } }; Object.defineProperty(exports, "__esModule", { value: true }); const automation_client_1 = require("@atomist/automation-client"); const sdm_1 = require("@atomist/sdm"); const fs = require("fs-extra"); const path = require("path"); const common_1 = require("../common"); class LocalSpider { constructor(localDirectory) { this.localDirectory = localDirectory; } spider(criteria, analyzer, analysisTracking, opts) { return __awaiter(this, void 0, void 0, function* () { const go = new common_1.AnalysisRun({ howToFindRepos: () => findRepositoriesUnder(this.localDirectory), determineRepoRef: repoRefFromLocalRepo, describeFoundRepo: f => ({ description: f.replace(this.localDirectory, "") }), howToClone: (rr, fr) => automation_client_1.GitCommandGitProject.fromExistingDirectory(rr, fr), analyzer, analysisTracking, persister: opts.persister, keepExistingPersisted: opts.keepExistingPersisted, projectFilter: criteria.projectTest, }, { workspaceId: opts.workspaceId, description: "local analysis under " + this.localDirectory, maxRepos: 1000, poolSize: opts.poolSize, }); return go.run(); }); } } exports.LocalSpider = LocalSpider; function findRepositoriesUnder(dir) { return __asyncGenerator(this, arguments, function* findRepositoriesUnder_1() { var e_1, _a; try { const stat = yield __await(fs.stat(yield __await(fs.realpath(dir)))); if (!stat.isDirectory()) { // nothing interesting return yield __await(void 0); } } catch (err) { automation_client_1.logger.error("Error opening " + dir + ": " + err.message); return yield __await(void 0); } const dirContents = yield __await(fs.readdir(dir)); if (dirContents.includes(".git")) { // this is the repository you are looking for yield yield __await(dir); return yield __await(void 0); } // recurse over everything inside for (const d of dirContents) { try { for (var _b = __asyncValues(findRepositoriesUnder(path.join(dir, d))), _c; _c = yield __await(_b.next()), !_c.done;) { const dd = _c.value; yield yield __await(dd); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) yield __await(_a.call(_b)); } finally { if (e_1) throw e_1.error; } } } }); } /** * @param repoDir full path to repository */ function repoRefFromLocalRepo(repoDir) { return __awaiter(this, void 0, void 0, function* () { const repoId = yield sdm_1.execPromise("git", ["remote", "get-url", "origin"], { cwd: repoDir }) .then(execHappened => repoIdFromOriginUrl(execHappened.stdout)) .catch(() => inventRepoId(repoDir)); const sha = yield sdm_1.execPromise("git", ["rev-parse", "HEAD"], { cwd: repoDir }) .then(execHappened => execHappened.stdout.trim()) .catch(() => "unknown"); return Object.assign(Object.assign({}, repoId), { sha }); }); } function repoIdFromOriginUrl(originUrl) { const parse = /\/(?<owner>.+)\/(?<repo>.+)(.git)?$/.exec(originUrl); if (!parse) { throw new Error("Can't identify owner and repo in url: " + originUrl); } return { repo: parse.groups.repo, owner: parse.groups.owner, url: originUrl, }; } function inventRepoId(repoDir) { const { base, dir } = path.parse(repoDir); const repo = base; const owner = path.parse(dir).base || "pretendOwner"; return { repo, owner, url: "file://" + repoDir, }; } //# sourceMappingURL=LocalSpider.js.map