Skip to content

Commit

Permalink
fix(version): get oldest commit data for changelog include commit login
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Aug 5, 2022
1 parent 29a76bb commit 5d7464b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
Expand Up @@ -4,7 +4,7 @@ jest.mock('../get-github-commits');

import { getGithubCommits } from '../get-github-commits';
import { describeRefSync } from '../../utils/describe-ref';
import { getCommitsSinceLastRelease, getLastTagDetails } from '../get-commits-since-last-release';
import { getCommitsSinceLastRelease, getOldestCommitSinceLastTag } from '../get-commits-since-last-release';
import { execSync } from '../../child-process';

(execSync as jest.Mock).mockReturnValue('"deadbeef 2022-07-01T00:01:02-04:00"');
Expand Down Expand Up @@ -45,7 +45,7 @@ describe('getCommitsSinceLastRelease', () => {
});
});

describe('getLastTagDetails', () => {
describe('getOldestCommitSinceLastTag', () => {
const execOpts = { cwd: '/test' };

describe('with existing tag', () => {
Expand All @@ -54,11 +54,11 @@ describe('getLastTagDetails', () => {
});

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

expect(execSpy).toHaveBeenCalledWith('git', ['log', '-1', '--format="%h %cI"', 'v1.0.0'], execOpts);
expect(result).toEqual({ tagDate: '2022-07-01T00:01:02-04:00', tagHash: 'deadbeef', tagRefCount: '1' });
expect(execSpy).toHaveBeenCalledWith('git', ['log', 'v1.0.0..HEAD', '--format="%h %cI"', '--reverse'], execOpts);
expect(result).toEqual({ commitDate: '2022-07-01T00:01:02-04:00', commitHash: 'deadbeef' });
});
});

Expand All @@ -75,14 +75,14 @@ describe('getLastTagDetails', () => {

it('should return first commit date when describeRefSync() did not return a tag date', async () => {
const execSpy = (execSync as jest.Mock).mockReturnValue('"abcbeef 2022-07-01T00:01:02-04:00"');
const result = await getLastTagDetails(execOpts);
const result = await getOldestCommitSinceLastTag(execOpts);

expect(execSpy).toHaveBeenCalledWith(
'git',
['log', '--oneline', '--format="%h %cI"', '--reverse', '--max-parents=0', 'HEAD'],
execOpts
);
expect(result).toEqual({ tagDate: '2022-07-01T00:01:02-04:00', tagHash: 'abcbeef', tagRefCount: '1' });
expect(result).toEqual({ commitDate: '2022-07-01T00:01:02-04:00', commitHash: 'abcbeef' });
});
});
});
Expand Up @@ -12,21 +12,21 @@ import { ValidationError } from '../validation-error';
* @param {RemoteClientType} client
* @param {String} gitRemote
* @param {String} branchName
* @param {ExecOpts} execOpts
* @param {ExecOpts} [execOpts]
* @returns {Promise<RemoteCommit[]>}
*/
export async function getCommitsSinceLastRelease(
client: RemoteClientType,
gitRemote: string,
branchName: string,
execOpts: ExecOpts
execOpts?: ExecOpts
): Promise<RemoteCommit[]> {
// get the last release tag date or the first commit date if no release tag found
const { tagDate } = getLastTagDetails(execOpts, false);
const { commitDate } = getOldestCommitSinceLastTag(execOpts, false);

switch (client) {
case 'github':
return getGithubCommits(gitRemote, branchName, tagDate, execOpts);
return getGithubCommits(gitRemote, branchName, commitDate, execOpts);
case 'gitlab':
default:
throw new ValidationError(
Expand All @@ -37,26 +37,29 @@ export async function getCommitsSinceLastRelease(
}

/**
* From the current branch, we will return the last tag date, sha and ref count
* or else return it from the first commit of the current branch
* 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]
* @returns {*} - oldest commit detail (hash, date)
*/
export function getLastTagDetails(execOpts: ExecOpts, includeMergedTags?: boolean) {
let execResult = '';
export function getOldestCommitSinceLastTag(execOpts?: ExecOpts, includeMergedTags?: boolean) {
let commitResult = '';
const describeOptions: DescribeRefOptions = { ...execOpts };
const { refCount, lastTagName } = describeRefSync(describeOptions, includeMergedTags);
const { lastTagName } = describeRefSync(describeOptions, includeMergedTags);

if (lastTagName) {
log.silly('git', 'getCurrentBranchLastTagDateSha');
execResult = execSync('git', ['log', '-1', '--format="%h %cI"', lastTagName], execOpts);
log.silly('git', 'getCurrentBranchOldestCommitSinceLastTag');
const stdout = execSync('git', ['log', `${lastTagName}..HEAD`, '--format="%h %cI"', '--reverse'], execOpts);
[commitResult] = stdout.split('\n');
} else {
log.silly('git', 'getCurrentBranchFirstCommitDateSha');
execResult = execSync(
log.silly('git', 'getCurrentBranchFirstCommit');
commitResult = execSync(
'git',
['log', '--oneline', '--format="%h %cI"', '--reverse', '--max-parents=0', 'HEAD'],
execOpts
);
}

const [_, tagHash, tagDate] = /^"?([0-9a-f]+)\s([0-9\-T\:]*)"?$/.exec(execResult) || [];
return { tagHash, tagDate, tagRefCount: refCount };
const [, commitHash, commitDate] = /^"?([0-9a-f]+)\s([0-9\-T\:]*)"?$/.exec(commitResult) || [];
return { commitHash, commitDate };
}
Expand Up @@ -19,7 +19,7 @@ export async function getGithubCommits(
gitRemote: string,
branchName: string,
sinceDate: string,
execOpts: ExecOpts
execOpts?: ExecOpts
): Promise<RemoteCommit[]> {
const repo = parseGitRepo(gitRemote, execOpts);
const octokit = createGitHubClient();
Expand Down

0 comments on commit 5d7464b

Please sign in to comment.