|
| 1 | +// --------------------------------------------------------------------------- |
| 2 | +// JSDoc types |
| 3 | +// --------------------------------------------------------------------------- |
| 4 | + |
| 5 | +/** @typedef {import('conventional-commits-parser').Commit} Commit */ |
| 6 | +/** @typedef {import('conventional-changelog-writer').Context} WriterContext */ |
| 7 | + |
| 8 | +// --------------------------------------------------------------------------- |
| 9 | +// Constants |
| 10 | +// --------------------------------------------------------------------------- |
| 11 | + |
| 12 | +const ADO_BASE = "https://dev.azure.com/NJInnovation/Business%20First%20Stop/_workitems/edit"; |
| 13 | + |
| 14 | +// --------------------------------------------------------------------------- |
| 15 | +// Pure helper functions |
| 16 | +// --------------------------------------------------------------------------- |
| 17 | + |
| 18 | +/** |
| 19 | + * Returns true if the commit should be omitted from the changelog. |
| 20 | + * Commits with breaking-change notes are always kept; otherwise only |
| 21 | + * feat / fix / perf / revert are included. |
| 22 | + * |
| 23 | + * @param {Commit} commit |
| 24 | + * @returns {boolean} |
| 25 | + */ |
| 26 | +const shouldDiscard = (commit) => { |
| 27 | + if (commit.notes.length > 0) return false; |
| 28 | + const releasable = ["feat", "fix", "perf", "revert"]; |
| 29 | + return !releasable.includes(commit.type) && !commit.revert; |
| 30 | +}; |
| 31 | + |
| 32 | +/** |
| 33 | + * Maps a conventional-commit type token to its changelog section label. |
| 34 | + * Preserves the original Angular preset's if-else order so that the |
| 35 | + * `revert || commit.revert` edge case is handled identically. |
| 36 | + * |
| 37 | + * @param {Commit} commit |
| 38 | + * @returns {string | null} |
| 39 | + */ |
| 40 | +const resolveType = (commit) => { |
| 41 | + if (commit.type === "feat") return "Features"; |
| 42 | + if (commit.type === "fix") return "Bug Fixes"; |
| 43 | + if (commit.type === "perf") return "Performance Improvements"; |
| 44 | + if (commit.type === "revert" || commit.revert) return "Reverts"; |
| 45 | + if (commit.type === "docs") return "Documentation"; |
| 46 | + if (commit.type === "style") return "Styles"; |
| 47 | + if (commit.type === "refactor") return "Code Refactoring"; |
| 48 | + if (commit.type === "test") return "Tests"; |
| 49 | + if (commit.type === "build") return "Build System"; |
| 50 | + if (commit.type === "ci") return "Continuous Integration"; |
| 51 | + return commit.type; |
| 52 | +}; |
| 53 | + |
| 54 | +/** |
| 55 | + * Replaces `[AB#N]` with an Azure DevOps work-item link and bare `#N` with a |
| 56 | + * GitHub issue link in a single regex pass (preventing the ADO-embedded `#N` |
| 57 | + * from being re-matched by the GitHub branch of the alternation). |
| 58 | + * |
| 59 | + * @param {string} subject |
| 60 | + * @param {string | undefined} repoBase e.g. "https://github.com/org/repo" |
| 61 | + * @returns {{ subject: string, issues: string[] }} |
| 62 | + */ |
| 63 | +const linkifyIssues = (subject, repoBase) => { |
| 64 | + const issues = []; |
| 65 | + const result = subject.replace(/\[AB#(\d+)\]|#([0-9]+)/g, (match, adoIssue, ghIssue) => { |
| 66 | + if (adoIssue !== undefined) { |
| 67 | + issues.push(adoIssue); |
| 68 | + return `[AB#${adoIssue}](${ADO_BASE}/${adoIssue})`; |
| 69 | + } |
| 70 | + if (ghIssue !== undefined && repoBase) { |
| 71 | + issues.push(ghIssue); |
| 72 | + return `[#${ghIssue}](${repoBase}/issues/${ghIssue})`; |
| 73 | + } |
| 74 | + return match; |
| 75 | + }); |
| 76 | + return { subject: result, issues }; |
| 77 | +}; |
| 78 | + |
| 79 | +/** |
| 80 | + * Replaces `@username` mentions with Markdown profile links. |
| 81 | + * Org-scoped handles (`@org/team`) are left as plain text. |
| 82 | + * |
| 83 | + * @param {string} subject |
| 84 | + * @param {string} host e.g. "https://github.com" |
| 85 | + * @returns {string} |
| 86 | + */ |
| 87 | +const linkifyMentions = (subject, host) => |
| 88 | + subject.replace(/\B@([a-z0-9](?:-?[a-z0-9/]){0,38})/g, (_, username) => |
| 89 | + username.includes("/") ? `@${username}` : `[@${username}](${host}/${username})`, |
| 90 | + ); |
| 91 | + |
| 92 | +// --------------------------------------------------------------------------- |
| 93 | +// Transform |
| 94 | +// --------------------------------------------------------------------------- |
| 95 | + |
| 96 | +/** |
| 97 | + * Transform function for `conventional-changelog-writer`. |
| 98 | + * Replicates the Angular preset's transform with ADO work-item links |
| 99 | + * replacing GitHub issue links for `[AB#N]` references. |
| 100 | + * |
| 101 | + * @param {Commit} commit |
| 102 | + * @param {WriterContext} context |
| 103 | + * @returns {Commit | undefined} |
| 104 | + */ |
| 105 | +const adoTransform = (commit, context) => { |
| 106 | + commit.notes.forEach((note) => { |
| 107 | + note.title = "BREAKING CHANGES"; |
| 108 | + }); |
| 109 | + |
| 110 | + if (shouldDiscard(commit)) return; |
| 111 | + |
| 112 | + commit.type = resolveType(commit); |
| 113 | + if (commit.scope === "*") commit.scope = ""; |
| 114 | + if (typeof commit.hash === "string") commit.shortHash = commit.hash.substring(0, 7); |
| 115 | + |
| 116 | + if (typeof commit.subject === "string") { |
| 117 | + const repoBase = context.repository |
| 118 | + ? `${context.host}/${context.owner}/${context.repository}` |
| 119 | + : context.repoUrl; |
| 120 | + |
| 121 | + const { subject, issues } = linkifyIssues(commit.subject, repoBase); |
| 122 | + commit.subject = context.host ? linkifyMentions(subject, context.host) : subject; |
| 123 | + commit.references = commit.references.filter((ref) => !issues.includes(ref.issue)); |
| 124 | + } |
| 125 | + |
| 126 | + return commit; |
| 127 | +}; |
| 128 | + |
| 129 | +// --------------------------------------------------------------------------- |
| 130 | +// Config |
| 131 | +// --------------------------------------------------------------------------- |
| 132 | + |
| 133 | +module.exports = { |
| 134 | + branches: ["main"], |
| 135 | + plugins: [ |
| 136 | + [ |
| 137 | + "@semantic-release/commit-analyzer", |
| 138 | + { |
| 139 | + preset: "angular", |
| 140 | + parserOpts: { noteKeywords: ["BREAKING CHANGE", "BREAKING CHANGES"] }, |
| 141 | + }, |
| 142 | + ], |
| 143 | + [ |
| 144 | + "@semantic-release/release-notes-generator", |
| 145 | + { |
| 146 | + preset: "angular", |
| 147 | + writerOpts: { transform: adoTransform }, |
| 148 | + }, |
| 149 | + ], |
| 150 | + "@semantic-release/npm", |
| 151 | + "@semantic-release/changelog", |
| 152 | + ["@semantic-release/github", { successComment: false }], |
| 153 | + "@semantic-release/git", |
| 154 | + ], |
| 155 | +}; |
0 commit comments