Skip to content

Commit

Permalink
feat(semver): ignore merge commits by default (#583)
Browse files Browse the repository at this point in the history
* test: refactor test code

* refactor(semver): reformat code

* feat(semver): ignore merge commits by default
  • Loading branch information
cakeinpanic authored Sep 5, 2022
1 parent eafe502 commit b86f5de
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ nx run workspace:version [...options]
6. Pushes the version to the remote repository.
7. Runs post-targets hook to publish the version on NPM, GitHub or GitLab.

Important: merge commits messages are ignored by the tool when calculating next version to bump.

#### Available options

| name | type | default | description |
Expand Down
97 changes: 94 additions & 3 deletions packages/semver/src/executors/version/index.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('@jscutlery/semver:version', () => {
skipRootChangelog: false,
syncVersions: false,
skipCommitTypes: [],

postTargets: [],
preset: 'angular',
commitMessageFormat: 'chore(${projectName}): release version ${version}',
Expand Down Expand Up @@ -1317,6 +1318,73 @@ $`)
});
});

describe('ignoring merge commits', () => {
beforeEach(async () => {
testingWorkspace = setupTestingWorkspace(new Map(commonWorkspaceFiles));

/* Commit changes. */
initGit();

execSync(
`
git add .
git commit -m "🐣"
echo a > packages/a/a.txt
git add .
git commit -m "docs(a): 🚀 new feature"
echo b > packages/b/b.txt
git add .
git commit -m "fix(b): 🐞 fix emptiness"
`
);
createMergeCommit();
});

afterEach(() => testingWorkspace.tearDown());

it('should not create a version if all commits are of skipCommitTypes or merge commits', async () => {
result = await version(
{
...defaultBuilderOptions,
skipCommitTypes: ['docs'],
},
createFakeContext({
project: 'a',
projectRoot: resolve(testingWorkspace.root, 'packages/a'),
workspaceRoot: testingWorkspace.root,
})
);

expect(commitMessage()).toBe("Merge branch 'another-branch'");
expect(uncommitedChanges()).toHaveLength(0);
});

it('should create correct version if last tag was put on merge commit', async () => {
execSync(`
git tag b-5.0.0
echo b > packages/b/b-1.txt
git add .
git commit -m "fix(b): 🐞 fix emptiness"
`);
result = await version(
{
...defaultBuilderOptions,
skipCommitTypes: ['docs'],
},
createFakeContext({
project: 'b',
projectRoot: resolve(testingWorkspace.root, 'packages/b'),
workspaceRoot: testingWorkspace.root,
})
);

expect(commitMessage()).toBe('chore(b): release version 5.0.1');
expect(uncommitedChanges()).toHaveLength(0);
});
});

// The testing workspace isn't really configured for
// executors, perhaps using the `new FSTree()` from
// and `new Workspace()` @nrwl/toa would give a
Expand Down Expand Up @@ -1364,8 +1432,7 @@ $`)
it.todo('should pass in only the new lines from the changelog as ${notes}');
});
});

function commitChanges() {
function initGit() {
execSync(
`
git init --quiet
Expand All @@ -1375,7 +1442,12 @@ function commitChanges() {
git config user.name "Test Bot"
git config commit.gpgsign false
`
);
}
function createAndCommitFiles() {
execSync(
`
git add .
git commit -m "🐣"
Expand All @@ -1393,6 +1465,25 @@ function commitChanges() {
`
);
}
function commitChanges() {
initGit();
createAndCommitFiles();
}

function createMergeCommit() {
execSync(
`
git checkout HEAD~2
git checkout -b "another-branch"
echo a > packages/a/a-merge.txt
git add .
git commit -m "docs(a): merge 🐣"
git checkout master
git merge another-branch
`
);
}

function uncommitedChanges() {
return (
Expand Down
26 changes: 20 additions & 6 deletions packages/semver/src/executors/version/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ export function getCommits({
projectRoot: string;
since?: string;
}): Observable<string[]> {
return getFormattedCommits({ since, projectRoot, format: '%B' });
return getFormattedCommits({
since,
projectRoot,
ignoreMergeCommits: true,
format: '%B',
});
}
/**
* Return hash of last commit of a project
Expand All @@ -24,26 +29,35 @@ export function getLastCommitHash({
}: {
projectRoot: string;
}): Observable<string> {
return getFormattedCommits({ projectRoot, format: '%H' }).pipe(
map(([commit]) => commit.trim())
);
return getFormattedCommits({
projectRoot,
ignoreMergeCommits: false,
format: '%H',
}).pipe(map(([commit]) => commit.trim()));
}

function getFormattedCommits({
projectRoot,
format,
ignoreMergeCommits,
since = '',
}: {
projectRoot: string;
format: string;
ignoreMergeCommits: boolean;
since?: string;
}): Observable<string[]> {
return new Observable<string>((observer) => {
gitRawCommits({
const params: any = {
from: since,
format,
path: projectRoot,
})
'full-history': true,
};
if (ignoreMergeCommits) {
params['no-merges'] = ignoreMergeCommits;
}
gitRawCommits(params)
.on('data', (data: string) => observer.next(data))
.on('error', (error: Error) => observer.error(error))
.on('close', () => observer.complete())
Expand Down
14 changes: 14 additions & 0 deletions packages/semver/src/executors/version/utils/try-bump.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe('tryBump', () => {
releaseType: undefined,
preid: undefined,
skipCommitTypes: [],

projectName: '',
})
);
Expand Down Expand Up @@ -118,6 +119,7 @@ describe('tryBump', () => {
tagPrefix: 'v',
syncVersions: true,
skipCommitTypes: [],

projectName: '',
})
);
Expand All @@ -127,6 +129,7 @@ describe('tryBump', () => {
expect(mockGetCommits).toBeCalledTimes(3);
expect(mockGetCommits).toBeCalledWith({
projectRoot: '/libs/demo',

since: 'v2.1.0',
});
expect(mockGetCommits).toBeCalledWith({
Expand Down Expand Up @@ -161,6 +164,7 @@ describe('tryBump', () => {
releaseType: 'premajor',
skipCommitTypes: [],
preid: 'alpha',

projectName: '',
})
);
Expand Down Expand Up @@ -193,6 +197,7 @@ describe('tryBump', () => {
tagPrefix: 'v',
releaseType: 'major',
skipCommitTypes: [],

projectName: '',
})
);
Expand Down Expand Up @@ -225,6 +230,7 @@ describe('tryBump', () => {
tagPrefix: 'v',
releaseType: 'patch',
projectName: '',

skipCommitTypes: [],
})
);
Expand Down Expand Up @@ -257,6 +263,7 @@ describe('tryBump', () => {
tagPrefix: 'v',
releaseType: 'minor',
projectName: '',

skipCommitTypes: [],
})
);
Expand All @@ -283,6 +290,7 @@ describe('tryBump', () => {
tagPrefix: 'v',
releaseType: 'patch',
projectName: '',

skipCommitTypes: [],
})
);
Expand Down Expand Up @@ -311,6 +319,7 @@ describe('tryBump', () => {
projectRoot: '/libs/demo',
tagPrefix: 'v',
projectName: '',

skipCommitTypes: [],
})
);
Expand Down Expand Up @@ -342,6 +351,7 @@ describe('tryBump', () => {
projectRoot: '/libs/demo',
tagPrefix: 'v',
projectName: '',

skipCommitTypes: [],
})
);
Expand Down Expand Up @@ -371,6 +381,7 @@ describe('tryBump', () => {
tagPrefix: 'v',
allowEmptyRelease: true,
projectName: '',

skipCommitTypes: [],
})
);
Expand Down Expand Up @@ -400,6 +411,7 @@ describe('tryBump', () => {
skipCommitTypes: ['docs', 'refactor'],
projectRoot: '/libs/demo',
tagPrefix: 'v',

projectName: '',
})
);
Expand All @@ -423,6 +435,7 @@ describe('tryBump', () => {
skipCommitTypes: ['docs', 'refactor'],
projectRoot: '/libs/demo',
tagPrefix: 'v',

projectName: '',
})
);
Expand Down Expand Up @@ -455,6 +468,7 @@ describe('tryBump', () => {
dependencyRoots: [{ name: 'dep1', path: '/libs/dep1' }],
tagPrefix: 'v',
skipCommitTypes: ['docs', 'refactor'],

syncVersions: true,
projectName: '',
})
Expand Down
2 changes: 1 addition & 1 deletion packages/semver/src/executors/version/utils/try-bump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export function tryBump({
);
}

const filteredCommits = commits.filter((commit) =>
const filteredCommits = commits.filter((commit: string) =>
shouldCommitBeCalculated({ commit, skipCommitTypes })
);

Expand Down
6 changes: 4 additions & 2 deletions packages/semver/src/executors/version/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { forkJoin, Observable, of } from 'rxjs';
import { concatMap, map } from 'rxjs/operators';
import {
insertChangelogDependencyUpdates,
updateChangelog
updateChangelog,
} from './utils/changelog';
import { commit } from './utils/commit';
import { addToStage, createTag, getLastCommitHash } from './utils/git';
Expand Down Expand Up @@ -129,7 +129,9 @@ export function versionProject({
skipCommit,
tag,
...options
}: { projectRoot: string } & CommonVersionOptions) {
}: {
projectRoot: string;
} & CommonVersionOptions) {
return _generateChangelogs({
projectName,
projectRoots: [projectRoot],
Expand Down

0 comments on commit b86f5de

Please sign in to comment.