Skip to content

Commit

Permalink
fix: duplicate generation of changesets (#26) (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Black Ritter committed Mar 20, 2024
1 parent b825ee5 commit e526cfb
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 3 deletions.
47 changes: 47 additions & 0 deletions .changeset/wet-adults-taste.md
@@ -0,0 +1,47 @@
---
'changeset-conventional-commits': patch
---

fix: duplicate generation of changesets (#26)

The same changesets were generated again, because the duplicate detection failed on trailing line breaks (`\n`) it got from `git`.

<details>
<summary>Details</summary>
Imagine this data it holds while duplicate checking:

`const changesets = ...:`

```ts
// Data from Commits
[
{
releases: [[Object], [Object]],
summary: 'chore(root): add two test packages\n',
packagesChanged: [[Object], [Object]],
},
];
```

`const currentChangesets = ...:`

```ts
// Data from Changesets
[
{
releases: [[Object], [Object]],
summary: 'chore(root): add two test packages',
packagesChanged: [[Object], [Object]],
},
];
```

Truncating the linebreak at [line 165](https://github.com/iamchathu/changeset-conventional-commits/blob/a4d324693eca549b0d016a162442eef49477ec75/src/utils/index.ts#L165) of `src/utils/index.ts` fixed it:

```ts
const compareChangeSet = (a: Changeset, b: Changeset): boolean => {
// return a.summary === b.summary && JSON.stringify(a.releases) == JSON.stringify(b.releases);
return a.summary.replace(/\n$/, '') === b.summary && JSON.stringify(a.releases) == JSON.stringify(b.releases);
};
```
</details>
10 changes: 10 additions & 0 deletions src/types/index.ts
@@ -1,3 +1,5 @@
import { Changeset } from '@changesets/types';

export interface PkgJson {
name?: string;
version?: string;
Expand Down Expand Up @@ -30,3 +32,11 @@ export interface ManyPkgPackages {
packages: ManyPkgPackage[];
root: ManyPkgPackage;
}

export type ChangesetConventionalCommit = Changeset & {
packagesChanged: {
dir: string;
relativeDir: string;
packageJson: PkgJson;
}[];
};
68 changes: 68 additions & 0 deletions src/utils/index.spec.ts
@@ -1,6 +1,8 @@
import { describe, expect, it } from '@jest/globals';
import { ChangesetConventionalCommit } from '../types';
import {
associateCommitsToConventionalCommitMessages,
difference,
getRepoRoot,
gitFetch,
isBreakingChange,
Expand Down Expand Up @@ -201,6 +203,72 @@ describe('associate-commits-to-conventional-commit-messages', () => {
});
});

describe('difference', () => {
const changesets = [
{
releases: [
{ name: 'changeset-cc-test-01', type: 'minor' },
{ name: 'changeset-cc-test-02', type: 'minor' },
],
summary: 'feat: add cli helper and flags',
packagesChanged: [
{
dir: '/z/merge-requests/changeset-conventional-commits/packages/test-01',
relativeDir: 'packages/test-01',
packageJson: {},
},
{
dir: '/z/merge-requests/changeset-conventional-commits/packages/test-02',
relativeDir: 'packages/test-02',
packageJson: {},
},
],
},
{
releases: [{ name: 'changeset-cc-test-01', type: 'minor' }],
summary: 'docs(changeset-cc-test-01): add update #2 and #3',
packagesChanged: [
{
dir: '/z/merge-requests/changeset-conventional-commits/packages/test-01',
relativeDir: 'packages/test-01',
packageJson: {},
},
],
},
] as ChangesetConventionalCommit[];

const currentChangesets = [
{
releases: [
{ name: 'changeset-cc-test-01', type: 'minor' },
{ name: 'changeset-cc-test-02', type: 'minor' },
],
summary: 'feat: add cli helper and flags',
packagesChanged: [
{
dir: '/z/merge-requests/changeset-conventional-commits/packages/test-01',
relativeDir: 'packages/test-01',
packageJson: {},
},
{
dir: '/z/merge-requests/changeset-conventional-commits/packages/test-02',
relativeDir: 'packages/test-02',
packageJson: {},
},
],
},
] as ChangesetConventionalCommit[];

it('correctly detects equal changesets *without* trailing new line/line break within `summary`', () => {
expect(difference(changesets, currentChangesets)).toEqual([changesets[1]]);
});

it('correctly detects equal changesets *with* trailing new line/line break within `summary`', () => {
changesets[0].summary += '\n';
expect(difference(changesets, currentChangesets)).toEqual([changesets[1]]);
});
});

describe('get-repo-root', () => {
it('correctly gets the repo root', () => {
expect(getRepoRoot().endsWith('changeset-conventional-commits')).toBe(true);
Expand Down
6 changes: 3 additions & 3 deletions src/utils/index.ts
Expand Up @@ -119,8 +119,8 @@ export const conventionalMessagesWithCommitsToChangesets = (
type: isBreakingChange(entry.changelogMessage)
? 'major'
: entry.changelogMessage.startsWith('feat')
? 'minor'
: 'patch',
? 'minor'
: 'patch',
};
}),
summary: entry.changelogMessage,
Expand Down Expand Up @@ -162,7 +162,7 @@ export const getCommitsSinceRef = (branch: string) => {
};

const compareChangeSet = (a: Changeset, b: Changeset): boolean => {
return a.summary === b.summary && JSON.stringify(a.releases) == JSON.stringify(b.releases);
return a.summary.replace(/\n$/, '') === b.summary && JSON.stringify(a.releases) == JSON.stringify(b.releases);
};

export const difference = (a: Changeset[], b: Changeset[]): Changeset[] => {
Expand Down

0 comments on commit e526cfb

Please sign in to comment.