UNPKG

@atomist/sdm-pack-aspect

Version:

an Atomist SDM Extension Pack for visualizing drift across an organization

149 lines 7.72 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 __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 __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } 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 Octokit = require("@octokit/rest"); const common_1 = require("../common"); /** * Spider GitHub. Ensure that GITHUB_TOKEN environment variable is set. */ class GitHubSpider { constructor(cloner, queryFunction = queryByCriteria) { this.cloner = cloner; this.queryFunction = queryFunction; } spider(criteria, analyzer, analysisTracking, opts) { return __awaiter(this, void 0, void 0, function* () { const run = new common_1.AnalysisRun({ howToFindRepos: () => this.queryFunction(process.env.GITHUB_TOKEN, criteria), determineRepoRef: sourceData => Promise.resolve({ owner: sourceData.owner.login, repo: sourceData.name, url: sourceData.url, }), describeFoundRepo: sourceData => ({ description: sourceData.owner.login + "/" + sourceData.name, url: sourceData.html_url }), howToClone: (rr, sourceData) => __awaiter(this, void 0, void 0, function* () { const p = yield this.cloner.clone(sourceData); rr.sha = p.id.sha; // very sneaky. We don't have it sooner. Hopefully this is soon enough. return p; }), analyzer, analysisTracking, persister: opts.persister, keepExistingPersisted: opts.keepExistingPersisted, projectFilter: criteria.projectTest, }, { workspaceId: opts.workspaceId, description: "querying GitHub: " + (criteria.githubQueries || []).join(" and "), maxRepos: 1000, poolSize: opts.poolSize || common_1.DefaultPoolSize, }); return run.run(); }); } } exports.GitHubSpider = GitHubSpider; function dropIrrelevantFields(sourceData) { return { owner: { login: sourceData.owner.login }, name: sourceData.name, url: sourceData.url, html_url: sourceData.html_url, timestamp: sourceData.timestamp, query: sourceData.query, }; } function queryByCriteria(token, criteria) { return __asyncGenerator(this, arguments, function* queryByCriteria_1() { var e_1, _a; const octokit = new Octokit({ auth: token ? "token " + token : undefined, baseUrl: "https://api.github.com", }); let results = []; let retrieved = 0; for (const q of criteria.githubQueries) { automation_client_1.logger.debug("Running query " + q + "..."); const options = octokit.search.repos.endpoint.merge({ q }); try { try { for (var _b = __asyncValues(octokit.paginate.iterator(options)), _c; _c = yield __await(_b.next()), !_c.done;) { const response = _c.value; retrieved += response.data.length; const newResults = response.data .filter((r) => !results.some(existing => existing.full_name === r.full_name)); newResults.forEach((r) => { r.query = q; r.timestamp = new Date(); }); for (const newResult of newResults) { yield yield __await(dropIrrelevantFields(newResult)); } automation_client_1.logger.debug(`Looked at ${retrieved} repos of max ${criteria.maxRetrieved}...`); if (retrieved > criteria.maxRetrieved) { break; } if (results.length > criteria.maxReturned) { results = results.slice(0, criteria.maxReturned); break; } } } 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; } } } catch (error) { automation_client_1.logger.error("Error querying: ", error); return yield __await(void 0); } } }); } //# sourceMappingURL=GitHubSpider.js.map