Skip to content

Commit

Permalink
fix(core): use match pattern to get last tag date with independent mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Aug 19, 2022
1 parent 59f4847 commit cebcecf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
Expand Up @@ -31,17 +31,27 @@ describe('getCommitsSinceLastRelease', () => {
});

it('throws an error if used with a remote client other than "github"', async () => {
await expect(getCommitsSinceLastRelease('gitlab', 'durable', 'main', execOpts)).rejects.toThrow(
await expect(getCommitsSinceLastRelease('gitlab', 'durable', 'main', false, execOpts)).rejects.toThrow(
'Invalid remote client type, "github" is currently the only supported client with the option --changelog-include-commits-client-login.'
);
});

it('should expect commits returned when using "github" when a valid tag is returned', async () => {
(getGithubCommits as jest.Mock).mockResolvedValue(commitsStub);
const result = await getCommitsSinceLastRelease('github', 'durable', 'main', execOpts);
const isIndependent = false;
const result = await getCommitsSinceLastRelease('github', 'durable', 'main', isIndependent, execOpts);

expect(result).toEqual(commitsStub);
});

it('should expect commits returned when using "github" when a valid tag in independent mode is returned', async () => {
(getGithubCommits as jest.Mock).mockResolvedValue(commitsStub);
const isIndependent = true;
const result = await getCommitsSinceLastRelease('github', 'durable', 'main', isIndependent, execOpts);

expect(describeRefSync).toHaveBeenCalledWith({ cwd: '/test', match: '*@*' }, false);
expect(result).toEqual(commitsStub);
});
});

describe('getOldestCommitSinceLastTag', () => {
Expand Down Expand Up @@ -75,7 +85,7 @@ describe('getOldestCommitSinceLastTag', () => {
(describeRefSync as jest.Mock).mockReturnValue(tagStub);
});

it('should expect a result with a tag date, hash and ref count', async () => {
it('should return first commit date and hash when last tag is not found', async () => {
const execSpy = (execSync as jest.Mock)
.mockReturnValueOnce('')
.mockReturnValueOnce('"deedbeaf 2022-07-01T00:01:02-04:00"');
Expand All @@ -87,12 +97,37 @@ describe('getOldestCommitSinceLastTag', () => {
expect(result).toEqual({ commitDate: '2022-07-01T00:01:02-04:00', commitHash: 'deedbeaf' });
});

it('should expect a result with a tag date, hash and ref count', async () => {
const result = await getOldestCommitSinceLastTag(execOpts);
it('should expect a result with a tag date, hash and ref count when last tag is found', async () => {
const result = await getOldestCommitSinceLastTag(execOpts, false, false);
const execSpy = (execSync as jest.Mock).mockReturnValueOnce('"deadbeef 2022-07-01T00:01:02-04:00"');

expect(describeRefSync).toHaveBeenCalledWith({ cwd: '/test' }, false);
expect(execSpy).toHaveBeenCalledWith('git', ['log', 'v1.0.0..HEAD', '--format="%h %aI"', '--reverse'], execOpts);
expect(result).toEqual({ commitDate: '2022-07-01T00:01:02-04:00', commitHash: 'deadbeef' });
});
});

describe('with existing tag in independent mode', () => {
beforeEach(() => {
(describeRefSync as jest.Mock).mockReturnValue({
...tagStub,
lastTagName: '@my-workspace/pkg-a@2.0.3',
lastVersion: '2.0.3',
});
});

it('should expect a tag date & hash but queried with a particular tag match pattern when using independent mode', async () => {
const isIndependent = true;
const result = await getOldestCommitSinceLastTag(execOpts, isIndependent, false);
const execSpy = (execSync as jest.Mock).mockReturnValueOnce('"deadbeef 2022-07-01T00:01:02-04:00"');

expect(describeRefSync).toHaveBeenCalledWith({ cwd: '/test', match: '*@*' }, false);
expect(execSpy).toHaveBeenCalledWith(
'git',
['log', '@my-workspace/pkg-a@2.0.3..HEAD', '--format="%h %aI"', '--reverse'],
execOpts
);
expect(result).toEqual({ commitDate: '2022-07-01T00:01:02-04:00', commitHash: 'deadbeef' });
});
});
});
Expand Up @@ -12,17 +12,19 @@ import { ValidationError } from '../validation-error';
* @param {RemoteClientType} client
* @param {String} gitRemote
* @param {String} branchName
* @param {Boolean} [isIndependent]
* @param {ExecOpts} [execOpts]
* @returns {Promise<RemoteCommit[]>}
*/
export async function getCommitsSinceLastRelease(
client: RemoteClientType,
gitRemote: string,
branchName: string,
isIndependent = false,
execOpts?: ExecOpts
): Promise<RemoteCommit[]> {
// get the last release tag date or the first commit date if no release tag found
const { commitDate } = getOldestCommitSinceLastTag(execOpts, false);
const { commitDate } = getOldestCommitSinceLastTag(execOpts, isIndependent, false);

switch (client) {
case 'github':
Expand All @@ -40,11 +42,15 @@ export async function getCommitsSinceLastRelease(
* Find the oldest commit details since the last release tag or else if not tag exists then return first commit info
* @param {ExecOpts} [execOpts]
* @param {Boolean} [includeMergedTags]
* @param {Boolean} [isIndependent]
* @returns {*} - oldest commit detail (hash, date)
*/
export function getOldestCommitSinceLastTag(execOpts?: ExecOpts, includeMergedTags?: boolean) {
export function getOldestCommitSinceLastTag(execOpts?: ExecOpts, isIndependent?: boolean, includeMergedTags?: boolean) {
let commitResult = '';
const describeOptions: DescribeRefOptions = { ...execOpts };
if (isIndependent) {
describeOptions.match = '*@*'; // independent tag pattern
}
const { lastTagName } = describeRefSync(describeOptions, includeMergedTags);

if (lastTagName) {
Expand Down
1 change: 1 addition & 0 deletions packages/version/src/version-command.ts
Expand Up @@ -282,6 +282,7 @@ export class VersionCommand extends Command<VersionCommandOption> {
remoteClient,
this.options.gitRemote,
this.currentBranch,
isIndependent,
this.execOpts
);
}
Expand Down

0 comments on commit cebcecf

Please sign in to comment.