UNPKG

@atomist/org-visualizer

Version:

Organization Visualizer using Atomist project scanning

149 lines 5.3 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) { 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 }); const automation_client_1 = require("@atomist/automation-client"); const sdm_pack_fingerprints_1 = require("@atomist/sdm-pack-fingerprints"); const child_process = require("child_process"); const util = require("util"); const bands_1 = require("../../util/bands"); const dateUtils_1 = require("./dateUtils"); const exec = util.promisify(child_process.exec); const gitLastCommitCommand = "git log -1 --format=%cd --date=short"; const gitRecencyExtractor = (p) => __awaiter(this, void 0, void 0, function* () { const r = yield exec(gitLastCommitCommand, { cwd: p.baseDir }); if (!r.stdout) { return undefined; } const data = new Date(r.stdout.trim()); return { type: "git-recency", name: "git-recency", data, sha: sdm_pack_fingerprints_1.sha256(JSON.stringify(data)), }; }); /** * Classify since last commit */ exports.GitRecency = { name: "git-recency", displayName: "Recency of git activity", extract: gitRecencyExtractor, toDisplayableFingerprintName: () => "Recency of git activity", toDisplayableFingerprint: fp => { const date = new Date(fp.data); return lastDateToActivityBand(date); }, stats: { defaultStatStatus: { entropy: false, }, }, }; function committersCommands(commitDepth) { return [ `git fetch --depth=${commitDepth}`, `git shortlog -s -n --all --max-count ${commitDepth}`, ]; } function activeCommittersExtractor(commitDepth) { return (p) => __awaiter(this, void 0, void 0, function* () { const cwd = p.baseDir; const cmds = committersCommands(commitDepth); automation_client_1.logger.debug("Running commands %s in %s", cwd, cmds); yield exec(cmds[0], { cwd }); const r = yield exec(cmds[1], { cwd }); if (!r.stdout) { return undefined; } const count = r.stdout.trim().split("\n").length; const data = { count }; return { type: "git-actives", name: "git-actives", data, sha: sdm_pack_fingerprints_1.sha256(JSON.stringify(data)), }; }); } /** * Active committers. This is expensive as it requires cloning the * last commitDepth commits */ function gitActiveCommitters(commitDepth) { return { name: "git-actives", displayName: "Active git committers", extract: activeCommittersExtractor(commitDepth), toDisplayableFingerprintName: () => "Active git committers", toDisplayableFingerprint: fp => { return bands_1.bandFor({ low: { upTo: 4 }, medium: { upTo: 12 }, high: bands_1.Default, }, fp.data.count, { includeNumber: true }); }, stats: { defaultStatStatus: { entropy: false, }, basicStatsPath: "count", }, }; } exports.gitActiveCommitters = gitActiveCommitters; // export const gitActivityExtractor: ExtractFingerprint = // async p => { // // TODO make this reusable so we can see for default branch and all others // const r = await exec(sinceDays(7), { cwd: (p as LocalProject).baseDir }); // if (!r.stdout) { // return undefined; // } // const last7 = parseInt(r.stdout.trim(), 10); // // return { // type: "git-activity" // name: "gitActivity", // last7, // }; // }; /** * Return a command * @param {number} days * @return {string} */ function sinceDays(days) { return `git log --all --since=${days}.days --pretty=oneline | wc -l`; } function lastDateToActivityBand(date) { const days = dateUtils_1.daysSince(date); return bands_1.bandFor({ current: { upTo: 30 }, recent: { upTo: 200 }, ancient: { upTo: 500 }, prehistoric: bands_1.Default, }, days, { includeNumber: true }); } //# sourceMappingURL=gitActivity.js.map