UNPKG

@atomist/sdm-pack-aspect

Version:

an Atomist SDM Extension Pack for visualizing drift across an organization

131 lines 4.9 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 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 showTiming_1 = require("../../util/showTiming"); const dateUtils_1 = require("./dateUtils"); const exec = util.promisify(child_process.exec); const gitLastCommitCommand = "git log -1 --format=%cd --date=short"; exports.GitRecencyType = "git-recency"; 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 = { lastCommitTime: new Date(r.stdout.trim()).getTime() }; return { type: exports.GitRecencyType, name: exports.GitRecencyType, data, sha: sdm_pack_fingerprints_1.sha256(JSON.stringify(data)), }; }); /** * Classify since last commit */ exports.GitRecency = { name: exports.GitRecencyType, displayName: "Recency of git activity", baseOnly: true, extract: gitRecencyExtractor, toDisplayableFingerprintName: () => "Recency of git activity", toDisplayableFingerprint: fp => { const date = new Date(fp.data.lastCommitTime); return lastDateToActivityBand(date); }, stats: { defaultStatStatus: { entropy: false, }, }, }; function committersCommands(commitDepth) { return [ `git fetch --depth=${commitDepth}`, `git shortlog -s -n --all --max-count ${commitDepth}`, ]; } exports.GitActivesType = "git-actives"; function activeCommittersExtractor(commitDepth) { return (p) => __awaiter(this, void 0, void 0, function* () { const cwd = p.baseDir; const cmds = committersCommands(commitDepth); const r = yield showTiming_1.showTiming(`commands ${cmds} in ${cwd}`, () => __awaiter(this, void 0, void 0, function* () { exec(cmds[0], { cwd }); return exec(cmds[1], { cwd }); })); if (!r.stdout) { return undefined; } const count = r.stdout.trim().split("\n").length; const data = { count }; return { type: exports.GitActivesType, name: exports.GitActivesType, 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: exports.GitActivesType, displayName: "Active git committers", baseOnly: true, extract: activeCommittersExtractor(commitDepth), toDisplayableFingerprintName: () => `Active git committers to ${commitDepth} commits`, 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; 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