bananareporter
Version:
Easily generate a report from multiple sources
99 lines (98 loc) • 4.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GitCliIntegration = exports.GitCliConfig = void 0;
const base_1 = require("./base");
// import {Axios} from 'axios'
const common_1 = require("../util/common");
const zod_1 = require("zod");
const dayjs = require("dayjs");
const logger_1 = require("../util/logger");
const path = require("path");
const node_fs_1 = require("node:fs");
const shell = require("shelljs");
exports.GitCliConfig = zod_1.z.object({
// TODO support wildcard
path: zod_1.z
.string()
.min(1)
.refine(s => {
return path.resolve(s);
}),
authorUsername: zod_1.z.string().min(1),
from: common_1.zIsoString.optional(),
to: common_1.zIsoString.optional(),
});
class GitCliIntegration extends base_1.IntegrationBase {
constructor(_rawConfig, bananaReporterConfig) {
var _a, _b, _c;
super(_rawConfig, bananaReporterConfig);
logger_1.logger.debug('git-cli integration');
this.config = exports.GitCliConfig.parse(_rawConfig);
logger_1.logger.debug('git-cli integration config', this.config);
this.bananaReporterConfig = bananaReporterConfig;
this.dateRange = {
from: (_a = this.config.from) !== null && _a !== void 0 ? _a : this.bananaReporterConfig.from,
to: (_b = this.config.to) !== null && _b !== void 0 ? _b : this.bananaReporterConfig.to,
};
logger_1.logger.debug('git-cli integration dateRange', this.dateRange);
if (!((_c = (0, node_fs_1.lstatSync)(this.config.path)) === null || _c === void 0 ? void 0 : _c.isDirectory())) {
const errorMsg = `Unable to find path ${this.config.path} or not a valid directory`;
logger_1.logger.error(errorMsg);
throw new Error(errorMsg);
}
logger_1.logger.debug(`git-cli path ${this.config.path} exists`);
if (!shell.which('git')) {
throw new Error('git CLI required for git-cli source');
}
}
async fetchData() {
const fromDate = dayjs(this.dateRange.from).startOf('day');
const toDate = dayjs(this.dateRange.to).endOf('day');
// extract project name from directory
const directory = path.basename(this.config.path);
// execute git log
const gitLogCommand = shell.exec(`git -C ${this.config.path} --no-pager log --branches="*" --author="${this.config.authorUsername
// hash - author - date unix - commit message - branch
}" --reverse --no-merges --source --format='%H~~~~~%an~~~~~%at~~~~~%s~~~~~%S>%n%n' --after="${fromDate.toISOString()}" --before="${toDate.toISOString()}"`, {
silent: true,
});
if (gitLogCommand.code !== 0) {
throw new Error(`unable to execute git log to ${this.config.path}, output: ${gitLogCommand.stderr}`);
}
const gitLogJson = [];
if (gitLogCommand.stdout) {
for (const line of gitLogCommand.stdout.split('>\n\n')) {
const splitGit = line.split('~~~~~');
logger_1.logger.debug('git log', { splitGit });
if (splitGit.length !== 5) {
continue;
}
gitLogJson.push({
commit: splitGit[0].replace(/\n/g, ''),
author: splitGit[1],
dateUnix: Number(splitGit[2]),
commitMessage: splitGit[3],
branch: splitGit[4],
});
}
}
return gitLogJson.map((e, i) => {
// add project name
e.projectName = directory;
return this.toBananaReporterObj(e, i);
});
}
toBananaReporterObj(rawData, _index) {
const description = `${rawData.commitMessage} branch:${rawData.branch} git:${rawData.commit.slice(0, 7)}`;
return {
id: rawData.commit,
date: dayjs.unix(rawData.dateUnix).toISOString(),
description: description,
projectName: rawData.projectName,
type: GitCliIntegration.type,
__raw: this.bananaReporterConfig.includeRawObject ? rawData : undefined,
};
}
}
exports.GitCliIntegration = GitCliIntegration;
GitCliIntegration.type = base_1.SourceType.Enum['git-cli'];