Skip to content

Commit

Permalink
feat: parse commits
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Mar 12, 2024
1 parent a7e58d2 commit 96b8d6e
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
3 changes: 3 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions scripts/changelog/changelog_test.ts
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]
`,
))
49 changes: 49 additions & 0 deletions scripts/changelog/mod.ts
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,
}),
)
}
18 changes: 18 additions & 0 deletions scripts/changelog/render.ts
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")
16 changes: 16 additions & 0 deletions scripts/changelog/render_bbcode.ts
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 })}
`

0 comments on commit 96b8d6e

Please sign in to comment.