Skip to content

Golang package for changelog 🔖 creation/parsing

License

Notifications You must be signed in to change notification settings

anton-yurchenko/go-changelog

Repository files navigation

go-changelog

Go Reference Code Coverage Go Report Card Release License

Golang package for changelog file creation/parsing

Features

Manual

  • Install with go get -u github.com/anton-yurchenko/go-changelog

Examples

Create a changelog file

package main

import (
    changelog "github.com/anton-yurchenko/go-changelog"
    "github.com/spf13/afero"
)

func main() {
    c := changelog.NewChangelog()
    c.SetTitle("Changelog")
    c.SetDescription("This file contains changes of all releases")

    c.AddUnreleasedChange("fixed", []string{"Bug"})
    c.AddUnreleasedChange("added", []string{"Feature"})

    r, err := c.CreateReleaseFromUnreleasedWithURL("1.0.0", "2021-05-31","https://github.com/anton-yurchenko/go-changelog/releases/tag/v1.0.0")
    if err != nil {
        panic(err)
    }

    if err := r.AddChange("changed", "User API"); err != nil {
        panic(err)
    }
    r.AddNotice("**This release contains breaking changes**")

    if err := c.SaveToFile(afero.NewOsFs(), "./CHANGELOG.md"); err != nil {
        panic(err)
    }
}

Parse an existing changelog file

package main

import (
    "fmt"
    changelog "github.com/anton-yurchenko/go-changelog"
)

func main() {
    p, err := changelog.NewParser("./CHANGELOG.md")
    if err != nil {
        panic(err)
    }

    c, err := p.Parse()
    if err != nil {
        panic(err)
    }

    fmt.Printf("Changelog contains %v releases", c.Releases.Len())
}

Update an existing changelog file

Click to expand
package main

import (
    changelog "github.com/anton-yurchenko/go-changelog"
    "github.com/spf13/afero"
)

func main() {
    p, err := changelog.NewParser("./CHANGELOG.md")
    if err != nil {
        panic(err)
    }

    c, err := p.Parse()
    if err != nil {
        panic(err)
    }

    r := c.GetRelease("1.2.1")
    if r == nil {
        panic("Release does not exists")
    }

    r.Yanked = true

    c.SaveToFile(afero.NewOsFs(), "./CHANGELOG.md")
    if err != nil {
        panic(err)
    }
}

Notes

  • Releases are sorted by their Semantic Version
  • Scopes are sorted by their importance
  • SaveToFile will overwrite the existing file, and anything that does not match the changelog format will be omitted

License

MIT © 2021-present Anton Yurchenko