-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { assertEquals } from "$std/assert/assert_equals.ts" | ||
import { outdent } from "$dax/src/deps.ts" | ||
import { getSections, parseCommits } from "./mod.ts" | ||
import { renderBBCode } from "./render_bbcode.ts" | ||
|
||
export const exampleCommits = parseCommits([ | ||
"feat: asdf (#1234)", | ||
"feat!: test (#123)", | ||
"fix(L10n): language stuff (#415)", | ||
]) | ||
const exampleSections = getSections(exampleCommits) | ||
|
||
Deno.test("parseCommits() correctly parse commits", () => { | ||
assertEquals(exampleCommits, [ | ||
{ | ||
type: "feat", | ||
scopes: undefined, | ||
breaking: undefined, | ||
subject: "asdf", | ||
pr: "1234", | ||
}, | ||
{ | ||
type: "feat", | ||
scopes: undefined, | ||
breaking: "!", | ||
subject: "test", | ||
pr: "123", | ||
}, | ||
{ | ||
type: "fix", | ||
scopes: "L10n", | ||
breaking: undefined, | ||
subject: "language stuff", | ||
pr: "415", | ||
}, | ||
]) | ||
}) | ||
|
||
Deno.test("renderBBCode() outputs identical text", () => | ||
assertEquals( | ||
renderBBCode({ version: "v1.0.0", date: "2024-03-12", sections: exampleSections }), | ||
outdent` | ||
[h1]v1.0.0 (2024-03-12)[/h1] | ||
[h2]Breaking Changes[/h2] | ||
[list] | ||
[*] test ([url=https://github.com/scarf005/marisa/pull/123]#123[/url]) | ||
[/list] | ||
[h2]New Features[/h2] | ||
[list] | ||
[*] asdf ([url=https://github.com/scarf005/marisa/pull/1234]#1234[/url]) | ||
[*] test ([url=https://github.com/scarf005/marisa/pull/123]#123[/url]) | ||
[/list] | ||
[h2]Fixes[/h2] | ||
[list] | ||
[*] language stuff ([url=https://github.com/scarf005/marisa/pull/415]#415[/url]) | ||
[/list] | ||
`, | ||
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import $ from "$dax/mod.ts" | ||
import { typedRegEx } from "https://deno.land/x/typed_regex@0.2.0/mod.ts" | ||
import type { RegExCaptureResult } from "https://deno.land/x/typed_regex@0.2.0/type_parser.ts" | ||
|
||
import { renderBBCode } from "./render_bbcode.ts" | ||
|
||
export type Commit = RegExCaptureResult<typeof re> | ||
export type Sections = Record<string, Commit[]> | ||
|
||
export const getTags = () => $`git tag --sort=-v:refname`.lines() | ||
|
||
type CommitRange = { from: string; to: string } | ||
export const getCommits = ({ from, to }: CommitRange) => | ||
$`git log ${from}..${to} --pretty=format:"%s"`.lines() | ||
|
||
const re = | ||
"^(?<type>\\w+)(\\((?<scopes>.*)?\\))?(?<breaking>!)?:\\s*(?<subject>.*?)\\s*\\(#(?<pr>\\d+)\\)$" | ||
|
||
const commitParser = typedRegEx(re) | ||
|
||
export const parseCommits = (xs: string[]) => | ||
xs | ||
.map(commitParser.captures) | ||
.filter((x): x is Commit => x !== undefined) | ||
.filter((x) => x.breaking || ["fix", "feat"].includes(x.type)) | ||
|
||
export const getSections = (commits: Commit[]): Sections => ({ | ||
"Breaking Changes": commits.filter((x) => x.breaking), | ||
"New Features": commits.filter((x) => x.type === "feat"), | ||
Fixes: commits.filter((x) => x.type === "fix"), | ||
}) | ||
|
||
if (import.meta.main) { | ||
const [latestTag] = await getTags() | ||
const commits = await getCommits({ from: latestTag, to: "main" }).then(parseCommits) | ||
|
||
const sections: Sections = getSections(commits) | ||
|
||
console.log(latestTag) | ||
console.log(sections) | ||
console.log(commits) | ||
console.log( | ||
renderBBCode({ | ||
version: "v1.0.0", | ||
date: new Date().toISOString().split("T")[0], | ||
sections, | ||
}), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Commit, Sections } from "./mod.ts" | ||
|
||
export type ChangelogRenderer = (option: { | ||
version: string | ||
date: string | ||
sections: Sections | ||
}) => string | ||
|
||
export type SectionFormatter = (tup: [section: string, commits: Commit[]]) => string | ||
export type SectionsFormatter = { fmtSection: SectionFormatter; sections: Sections } | ||
|
||
export const renderSections = ( | ||
{ fmtSection, sections }: SectionsFormatter, | ||
) => | ||
Object.entries(sections) | ||
.filter(([, commits]) => commits.length > 0) | ||
.map(fmtSection) | ||
.join("\n\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { outdent } from "$dax/src/deps.ts" | ||
import { Commit } from "./mod.ts" | ||
import { ChangelogRenderer, renderSections, SectionFormatter } from "./render.ts" | ||
|
||
const fmtCommit = (x: Commit) => | ||
` [*] ${x.subject} ([url=https://github.com/scarf005/marisa/pull/${x.pr}]#${x.pr}[/url])` | ||
|
||
const fmtSection: SectionFormatter = ([section, commits]) => | ||
`[h2]${section}[/h2]\n` + "[list]\n" + commits.map(fmtCommit).join("\n") + "\n[/list]" | ||
|
||
export const renderBBCode: ChangelogRenderer = ({ version, date, sections }) => | ||
outdent` | ||
[h1]${version} (${date})[/h1] | ||
${renderSections({ fmtSection, sections })} | ||
` |