Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename report committers args to be more clear #62

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions libs/report/committers/src/lib/committers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ jest.mock('./parse-git-log-entries');
jest.mock('./get-committer-counts');

describe('reportCommittersCommand', () => {
let defaultStartDate: string;
let defaultEndDate: string;
let defaultBeforeDate: string;
let defaultAfterDate: string;

beforeEach(() => {
defaultStartDate = format(new Date(), 'yyyy-MM-dd');
defaultEndDate = format(subMonths(new Date(), 12), 'yyyy-MM-dd');
defaultBeforeDate = format(new Date(), 'yyyy-MM-dd');
defaultAfterDate = format(subMonths(new Date(), 12), 'yyyy-MM-dd');
});

describe('metadata', () => {
Expand All @@ -37,13 +37,13 @@ describe('reportCommittersCommand', () => {
it('should set start date as today', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const builder = reportCommittersCommand.builder as any;
expect(builder.startDate.default).toEqual(defaultStartDate);
expect(builder.beforeDate.default).toEqual(defaultBeforeDate);
});

it('should set end date as 1 year ago', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const builder = reportCommittersCommand.builder as any;
expect(builder.endDate.default).toEqual(defaultEndDate);
expect(builder.afterDate.default).toEqual(defaultAfterDate);
});

it('should not have a default value for exclude', () => {
Expand All @@ -66,36 +66,42 @@ describe('reportCommittersCommand', () => {

it('should run the correct git command', () => {
const args = {
startDate: defaultStartDate,
endDate: defaultEndDate,
beforeDate: defaultBeforeDate,
afterDate: defaultAfterDate,
};

runCommandMock.mockResolvedValue('');

const { startDate, endDate } = parseDateFlags(dateFormat, defaultStartDate, defaultEndDate);
const { beforeDate, afterDate } = parseDateFlags(
dateFormat,
defaultBeforeDate,
defaultAfterDate
);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
reportCommittersCommand.handler(args as any);

const startDateEndOfDay = formatISO(addHours(addMinutes(addSeconds(startDate, 59), 59), 23));
const beforeDateEndOfDay = formatISO(
addHours(addMinutes(addSeconds(beforeDate, 59), 59), 23)
);

expect(runCommandMock).toHaveBeenCalledWith(
`git log --since "${endDate}" --until "${startDateEndOfDay}" --pretty=format:"%hΓΓΓΓ%anΓΓΓΓ%ad" `
`git log --since "${afterDate}" --until "${beforeDateEndOfDay}" --pretty=format:"%hΓΓΓΓ%anΓΓΓΓ%ad" `
);
});

it('should parse and organize the git committers', async () => {
const args = {
startDate: defaultStartDate,
endDate: defaultEndDate,
beforeDate: defaultBeforeDate,
afterDate: defaultAfterDate,
};

runCommandMock.mockResolvedValue('a\nb');
parseGitLogEntriesMock.mockReturnValue([
{
commitHash: 'hash',
committer: 'testy',
date: defaultStartDate,
date: defaultBeforeDate,
},
]);
getCommitterCountsMock.mockReturnValue([
Expand All @@ -115,7 +121,7 @@ describe('reportCommittersCommand', () => {
{
commitHash: 'hash',
committer: 'testy',
date: defaultStartDate,
date: defaultBeforeDate,
},
]);

Expand Down
29 changes: 10 additions & 19 deletions libs/report/committers/src/lib/committers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { ArgumentsCamelCase, CommandBuilder, CommandModule } from 'yargs';
import { CommitterCount } from './types';

interface Options {
startDate: string;
endDate: string;
beforeDate: string;
afterDate: string;
exclude: string[];
json: boolean;
// monthly: boolean;
Expand All @@ -20,13 +20,13 @@ export const reportCommittersCommand: CommandModule<object, Options> = {
describe: 'show git committers',
aliases: ['git'],
builder: {
startDate: {
beforeDate: {
alias: 's',
default: format(new Date(), dateFormat),
describe: `Start Date (format: ${dateFormat})`,
string: true,
},
endDate: {
afterDate: {
alias: 'e',
describe: `End Date (format: ${dateFormat})`,
required: false,
Expand Down Expand Up @@ -56,29 +56,20 @@ export const reportCommittersCommand: CommandModule<object, Options> = {
};

async function run(args: ArgumentsCamelCase<Options>): Promise<void> {
const { startDate, endDate } = parseDateFlags(dateFormat, args.startDate, args.endDate);
const startDateEndOfDay = formatISO(addHours(addMinutes(addSeconds(startDate, 59), 59), 23));
const { beforeDate, afterDate } = parseDateFlags(dateFormat, args.beforeDate, args.afterDate);
const beforeDateEndOfDay = formatISO(addHours(addMinutes(addSeconds(beforeDate, 59), 59), 23));

const ignores = args.exclude && args.exclude.length ? `-- . "!(${args.exclude.join('|')})"` : '';

const gitCommand = `git log --since "${endDate}" --until "${startDateEndOfDay}" --pretty=format:${gitOutputFormat} ${ignores}`;
const gitCommand = `git log --since "${afterDate}" --until "${beforeDateEndOfDay}" --pretty=format:${gitOutputFormat} ${ignores}`;

const result = await runCommand(gitCommand);

const rawEntries = (result as string).split('\n');

// const { committers, monthly } = collapseAndSortCommitterInfo(startDate, endDate, rawEntries);
// if( args.monthly ){
// printMonthly(monthly);
// return;
// }else{
// printCommitters(committers);
// return;
// }
if (rawEntries.length === 1 && rawEntries[0] === '') {
const startDateStr = format(startDate, 'yyyy-MM-dd');
const endDateStr = format(endDate, 'yyyy-MM-dd');
console.log(`No commits found between ${endDateStr} and ${startDateStr}`);
const beforeDateStr = format(beforeDate, 'yyyy-MM-dd');
const afterDateStr = format(afterDate, 'yyyy-MM-dd');
console.log(`No commits found between ${afterDateStr} and ${beforeDateStr}`);
return;
}
const entries = parseGitLogEntries(rawEntries);
Expand Down
10 changes: 3 additions & 7 deletions libs/report/committers/src/lib/parse-date-flags.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { parse } from 'date-fns';

export function parseDateFlags(
dateFormat: string,
startDate: string,
endDate: string
) {
export function parseDateFlags(dateFormat: string, beforeDate: string, afterDate: string) {
return {
endDate: parse(endDate, dateFormat, new Date()),
startDate: parse(startDate, dateFormat, new Date()),
afterDate: parse(afterDate, dateFormat, new Date()),
beforeDate: parse(beforeDate, dateFormat, new Date()),
};
}