Skip to content

Commit

Permalink
refactor!: update to new conventional-changelog preset interface (#133)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: now requires conventional-changelog >= 6.0.0
  • Loading branch information
isn-renovate[bot] authored Jun 8, 2024
1 parent 78a74ed commit 92430f8
Show file tree
Hide file tree
Showing 5 changed files with 498 additions and 455 deletions.
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import commitGroupsSort from './lib/commit-groups-compare.js';
import transform from './lib/commit-transform.js';

async function createPreset() {
return conventionalChangelogConventionalCommits().then((preset) =>
_.merge(preset, {
writerOpts: { transform, commitGroupsSort, groupBy: 'groupType' },
})
);
return conventionalChangelogConventionalCommits().then((preset) => {
return _.merge(preset, {
writer: { transform, commitGroupsSort, groupBy: 'groupType' },
});
});
}

export default createPreset;
67 changes: 34 additions & 33 deletions lib/commit-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,28 @@ const COMMIT_HASH_LENGTH = 7;
* @return {Object} the transformed commit.
*/
export default (commit, context) => {
if (commit.notes) {
commit.notes.forEach((note) => {
note.title = 'Breaking changes';
});
}
const entry = types[commit.type];

if (
types[commit.type] &&
(types[commit.type].changelog || (commit.notes && commit.notes.length > 0))
) {
commit.groupType = `${
types[commit.type].emoji ? `${types[commit.type].emoji} ` : ''
}${types[commit.type].title}`;
} else {
return null;
}
const notes = commit.notes.map((note) => {
return {
...note,
title: 'Breaking changes',
};
});

if (commit.scope === '*') {
commit.scope = '';
}
if (!entry || (!entry.changelog && notes.length == 0)) return null;

if (typeof commit.hash === 'string') {
commit.shortHash = commit.hash.slice(0, COMMIT_HASH_LENGTH);
}
const groupType = `${entry.emoji ? `${entry.emoji} ` : ''}${entry.title}`;

const references = [];
const scope = commit.scope === '*' ? '' : commit.scope;

const shortHash =
typeof commit.hash === 'string'
? commit.hash.substring(0, COMMIT_HASH_LENGTH)
: commit.shortHash;

let subject = commit.subject;
const subjectReferences = [];

if (typeof commit.subject === 'string') {
let url = context.repository
Expand All @@ -45,31 +41,36 @@ export default (commit, context) => {
if (url) {
url += '/issues/';
// Issue URLs.
commit.subject = commit.subject.replace(/#(\d+)/g, (_, issue) => {
references.push(issue);
subject = subject.replace(/#(\d+)/g, (_, issue) => {
subjectReferences.push(issue);
return `[#${issue}](${url}${issue})`;
});
}

if (context.host) {
// User URLs.
commit.subject = commit.subject.replace(
subject = subject.replace(
/\B@([a-z0-9](?:-?[a-z0-9]){0,38})/g,
`[@$1](${context.host}/$1)`
);
}
}

let references;
if (commit.references) {
// Remove references that already appear in the subject
commit.references = commit.references.filter((reference) => {
if (!references.includes(reference.issue)) {
return true;
}

return false;
});
references = commit.references.filter(
(reference) => !subjectReferences.includes(reference.issue)
);
}

return commit;
return {
notes,
groupType,
type: commit.type,
scope,
shortHash,
subject,
references,
};
};
Loading

0 comments on commit 92430f8

Please sign in to comment.