-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add history command (#273)
- Loading branch information
Showing
18 changed files
with
452 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
/dist | ||
/coverage | ||
/.nx/cache | ||
__snapshots__ | ||
__snapshots__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import chalk from 'chalk'; | ||
import { ArgumentsCamelCase, CommandModule } from 'yargs'; | ||
import { HistoryOptions, getHashes, history } from '@code-pushup/core'; | ||
import { getCurrentBranchOrTag, safeCheckout, ui } from '@code-pushup/utils'; | ||
import { CLI_NAME } from '../constants'; | ||
import { yargsOnlyPluginsOptionsDefinition } from '../implementation/only-plugins.options'; | ||
import { HistoryCliOptions } from './history.model'; | ||
import { yargsHistoryOptionsDefinition } from './history.options'; | ||
|
||
export function yargsHistoryCommandObject() { | ||
const command = 'history'; | ||
return { | ||
command, | ||
describe: 'Collect reports for commit history', | ||
builder: yargs => { | ||
yargs.options({ | ||
...yargsHistoryOptionsDefinition(), | ||
...yargsOnlyPluginsOptionsDefinition(), | ||
}); | ||
yargs.group( | ||
Object.keys(yargsHistoryOptionsDefinition()), | ||
'History Options:', | ||
); | ||
return yargs; | ||
}, | ||
handler: async <T>(args: ArgumentsCamelCase<T>) => { | ||
ui().logger.info(chalk.bold(CLI_NAME)); | ||
ui().logger.info(chalk.gray(`Run ${command}`)); | ||
|
||
const currentBranch = await getCurrentBranchOrTag(); | ||
const { | ||
targetBranch = currentBranch, | ||
forceCleanStatus, | ||
maxCount, | ||
from, | ||
to, | ||
...restOptions | ||
} = args as unknown as HistoryCliOptions & HistoryOptions; | ||
|
||
// determine history to walk | ||
const commits: string[] = await getHashes({ maxCount, from, to }); | ||
try { | ||
// run history logic | ||
const reports = await history( | ||
{ | ||
...restOptions, | ||
targetBranch, | ||
forceCleanStatus, | ||
}, | ||
commits, | ||
); | ||
|
||
ui().logger.log(`Reports: ${reports.length}`); | ||
} finally { | ||
// go back to initial branch | ||
await safeCheckout(currentBranch); | ||
} | ||
}, | ||
} satisfies CommandModule; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { describe, expect, vi } from 'vitest'; | ||
import { type HistoryOptions, history } from '@code-pushup/core'; | ||
import { safeCheckout } from '@code-pushup/utils'; | ||
import { DEFAULT_CLI_CONFIGURATION } from '../../../mocks/constants'; | ||
import { yargsCli } from '../yargs-cli'; | ||
import { yargsHistoryCommandObject } from './history-command'; | ||
|
||
vi.mock('@code-pushup/core', async () => { | ||
const { | ||
MINIMAL_HISTORY_CONFIG_MOCK, | ||
}: typeof import('@code-pushup/test-utils') = await vi.importActual( | ||
'@code-pushup/test-utils', | ||
); | ||
const core: object = await vi.importActual('@code-pushup/core'); | ||
return { | ||
...core, | ||
history: vi | ||
.fn() | ||
.mockImplementation((options: HistoryOptions, commits: string[]) => | ||
commits.map(commit => `${commit}-report.json`), | ||
), | ||
readRcByPath: vi.fn().mockResolvedValue(MINIMAL_HISTORY_CONFIG_MOCK), | ||
}; | ||
}); | ||
|
||
vi.mock('@code-pushup/utils', async () => { | ||
const utils: object = await vi.importActual('@code-pushup/utils'); | ||
return { | ||
...utils, | ||
safeCheckout: vi.fn(), | ||
getCurrentBranchOrTag: vi.fn().mockReturnValue('main'), | ||
}; | ||
}); | ||
|
||
vi.mock('simple-git', async () => { | ||
const actual = await vi.importActual('simple-git'); | ||
return { | ||
...actual, | ||
simpleGit: () => ({ | ||
log: ({ maxCount }: { maxCount: number } = { maxCount: 1 }) => | ||
Promise.resolve({ | ||
all: [ | ||
{ hash: 'commit-6' }, | ||
{ hash: 'commit-5' }, | ||
{ hash: 'commit-4' }, | ||
{ hash: 'commit-3' }, | ||
{ hash: 'commit-2' }, | ||
{ hash: 'commit-1' }, | ||
].slice(-maxCount), | ||
}), | ||
}), | ||
}; | ||
}); | ||
|
||
describe('history-command', () => { | ||
it('should return the last 5 commits', async () => { | ||
await yargsCli(['history', '--config=/test/code-pushup.config.ts'], { | ||
...DEFAULT_CLI_CONFIGURATION, | ||
commands: [yargsHistoryCommandObject()], | ||
}).parseAsync(); | ||
|
||
expect(history).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
targetBranch: 'main', | ||
}), | ||
['commit-1', 'commit-2', 'commit-3', 'commit-4', 'commit-5'], | ||
); | ||
|
||
expect(safeCheckout).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should have 2 commits to crawl in history if maxCount is set to 2', async () => { | ||
await yargsCli( | ||
['history', '--config=/test/code-pushup.config.ts', '--maxCount=2'], | ||
{ | ||
...DEFAULT_CLI_CONFIGURATION, | ||
commands: [yargsHistoryCommandObject()], | ||
}, | ||
).parseAsync(); | ||
|
||
expect(history).toHaveBeenCalledWith(expect.any(Object), [ | ||
'commit-1', | ||
'commit-2', | ||
]); | ||
|
||
expect(safeCheckout).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { type LogOptions } from 'simple-git'; | ||
import { HistoryOnlyOptions } from '@code-pushup/core'; | ||
|
||
export type HistoryCliOptions = { | ||
targetBranch?: string; | ||
} & Pick<LogOptions, 'maxCount' | 'from' | 'to'> & | ||
HistoryOnlyOptions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Options } from 'yargs'; | ||
import { HistoryCliOptions } from './history.model'; | ||
|
||
export function yargsHistoryOptionsDefinition(): Record< | ||
keyof HistoryCliOptions, | ||
Options | ||
> { | ||
return { | ||
targetBranch: { | ||
describe: 'Branch to crawl history', | ||
type: 'string', | ||
default: 'main', | ||
}, | ||
forceCleanStatus: { | ||
describe: | ||
'If we reset the status to a clean git history forcefully or not.', | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
skipUploads: { | ||
describe: 'Upload created reports', | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
maxCount: { | ||
// https://git-scm.com/docs/git-log#Documentation/git-log.txt---max-countltnumbergt | ||
describe: 'Number of steps in history', | ||
type: 'number', | ||
// eslint-disable-next-line no-magic-numbers | ||
default: 5, | ||
}, | ||
from: { | ||
// https://git-scm.com/docs/git-log#Documentation/git-log.txt-ltrevision-rangegt | ||
describe: 'hash to first commit in history', | ||
type: 'string', | ||
}, | ||
to: { | ||
// https://git-scm.com/docs/git-log#Documentation/git-log.txt-ltrevision-rangegt | ||
describe: 'hash to last commit in history', | ||
type: 'string', | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.