@atomist/org-visualizer
Version:
Organization Visualizer using Atomist project scanning
175 lines • 7.68 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) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
// tslint:disable
/**
* Main entry point script. Added as a binary in package.json
*/
const automation_client_1 = require("@atomist/automation-client");
const configuration_1 = require("@atomist/automation-client/lib/configuration");
const _ = require("lodash");
const path = require("path");
const yargs = require("yargs");
const fileNamesSubprojectFinder_1 = require("../analysis/fileNamesSubprojectFinder");
const PostgresProjectAnalysisResultStore_1 = require("../analysis/offline/persist/PostgresProjectAnalysisResultStore");
const GitHubSpider_1 = require("../analysis/offline/spider/github/GitHubSpider");
const LocalSpider_1 = require("../analysis/offline/spider/local/LocalSpider");
const subprojectFinder_1 = require("../analysis/subprojectFinder");
const machine_1 = require("../machine/machine");
const analytics_1 = require("../analysis/offline/spider/analytics");
// Ensure we see console logging, and send info to the console
automation_client_1.configureLogging(automation_client_1.PlainLogging);
process.on('uncaughtException', function (err) {
console.log(err);
console.log(err.stack);
process.exit(1);
});
/**
* Spider a GitHub.com org
*/
function spider(params) {
return __awaiter(this, void 0, void 0, function* () {
const analyzer = machine_1.createAnalyzer(undefined);
const org = params.owner;
const searchInRepoName = search ? ` ${search} in:name` : "";
const spider = params.source === "GitHub" ? new GitHubSpider_1.GitHubSpider() : new LocalSpider_1.LocalSpider(params.localDirectory);
const persister = new PostgresProjectAnalysisResultStore_1.PostgresProjectAnalysisResultStore(machine_1.sdmConfigClientFactory(configuration_1.loadUserConfiguration()));
const query = params.query || `org:${org}` + searchInRepoName;
const criteria = {
// See the GitHub search API documentation at
// https://developer.github.com/v3/search/
// You can query for many other things here, beyond org
githubQueries: [query],
maxRetrieved: 1500,
maxReturned: 1500,
projectTest: (p) => __awaiter(this, void 0, void 0, function* () {
// You can also perform a computation here to return false if a project should not
// be analyzed and persisted, based on its contents. For example,
// this enables you to analyze only projects containing a particular file
// through calling getFile()
return true;
}),
subprojectFinder: subprojectFinder_1.firstSubprojectFinderOf(fileNamesSubprojectFinder_1.fileNamesSubprojectFinder("pom.xml", "build.gradle", "package.json", "requirements.txt")),
};
const arr = new Array(JSON.stringify(criteria).length + 20);
_.fill(arr, "-");
const sep = arr.join("");
automation_client_1.logger.info("%s\nOptions: %j\nSpider criteria: %j\n%s\n", sep, params, criteria, sep);
const spiderResult = yield spider.spider(criteria, analyzer, {
persister,
keepExistingPersisted: (existing) => __awaiter(this, void 0, void 0, function* () {
// Perform a computation here to return true if an existing analysis seems valid
const keep = !params.update;
automation_client_1.logger.info(keep ?
`Retaining existing analysis for ${existing.analysis.id.url}:${existing.analysis.id.sha}` :
`Recomputing analysis for ${existing.analysis.id.url}:${existing.analysis.id.sha}`);
return keep;
}),
// Controls promise usage inNode
poolSize: 40,
workspaceId,
});
console.log("Computing analytics over all fingerprints...");
yield analytics_1.computeAnalytics(persister, workspaceId);
return spiderResult;
});
}
yargs
.option("owner", {
required: false,
alias: 'o',
requiresArg: true,
description: "GitHub user or organization",
})
.option("search", {
required: false,
alias: 's',
requiresArg: true,
description: "Search within repository names"
})
.option("query", {
required: false,
alias: 'q',
requiresArg: true,
description: "GitHub query"
})
.option("workspace", {
required: false,
requiresArg: true,
alias: 'w',
description: "Name of Atomist workspace to store results under"
})
.option("localDirectory", {
required: false,
alias: "l",
requiresArg: true,
description: "local directory to search for repositories (instead of GitHub)",
})
.option("update", {
type: "boolean",
required: false,
default: false,
alias: "u",
description: "always update existing analyses for same commit sha",
})
.strict()
.usage("spider <GitHub criteria: supply owner or query>\nspider --localDirectory <directory containing repositories>");
const commandLineParameters = yargs.argv;
const owner = commandLineParameters.owner;
const search = commandLineParameters.search;
const query = commandLineParameters.query;
const workspaceId = commandLineParameters.workspace || "local";
const source = commandLineParameters.localDirectory ? "local" : "GitHub";
const localDirectory = commandLineParameters.localDirectory ? path.resolve(commandLineParameters.localDirectory) : "";
if (!owner && !query && !localDirectory) {
console.log(`Please specify owner, query, or local directory`);
process.exit(1);
}
if (localDirectory) {
console.log(`Spidering repositories under ${localDirectory}...`);
}
else {
if (search) {
console.log(`Spidering GitHub repositories in organization ${owner} with '${search}' in the name...`);
}
if (query) {
console.log(`Running GitHub query '${query}' for workspace '${workspaceId}'...`);
}
else {
console.log(`Spidering GitHub organization ${owner} for workspace '${workspaceId}'...`);
}
}
const params = {
owner, search, query, workspaceId, source,
localDirectory, update: commandLineParameters.update
};
spider(params).then(r => {
console.log(`Successfully analyzed ${JSON.stringify(params)}. result is `
+ JSON.stringify(r, null, 2));
}, err => {
console.log("Failure: " + err.message);
});
//# sourceMappingURL=spider.js.map