mirror of
https://github.com/apricote/releaser-pleaser.git
synced 2026-02-11 04:07:05 +00:00
Compare commits
2 commits
1c7381307e
...
8269592303
| Author | SHA1 | Date | |
|---|---|---|---|
| 8269592303 | |||
|
|
96e01b3f52 |
32 changed files with 892 additions and 923 deletions
|
|
@ -1,12 +1,15 @@
|
||||||
package changelog
|
package rp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/commitparser"
|
const (
|
||||||
|
ChangelogFile = "CHANGELOG.md"
|
||||||
|
ChangelogHeader = "# Changelog"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -24,9 +27,9 @@ func init() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewChangelogEntry(commits []commitparser.AnalyzedCommit, version, link, prefix, suffix string) (string, error) {
|
func NewChangelogEntry(commits []AnalyzedCommit, version, link, prefix, suffix string) (string, error) {
|
||||||
features := make([]commitparser.AnalyzedCommit, 0)
|
features := make([]AnalyzedCommit, 0)
|
||||||
fixes := make([]commitparser.AnalyzedCommit, 0)
|
fixes := make([]AnalyzedCommit, 0)
|
||||||
|
|
||||||
for _, commit := range commits {
|
for _, commit := range commits {
|
||||||
switch commit.Type {
|
switch commit.Type {
|
||||||
|
|
@ -51,4 +54,5 @@ func NewChangelogEntry(commits []commitparser.AnalyzedCommit, version, link, pre
|
||||||
}
|
}
|
||||||
|
|
||||||
return changelog.String(), nil
|
return changelog.String(), nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,12 +1,9 @@
|
||||||
package changelog
|
package rp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/commitparser"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/git"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func ptr[T any](input T) *T {
|
func ptr[T any](input T) *T {
|
||||||
|
|
@ -15,7 +12,7 @@ func ptr[T any](input T) *T {
|
||||||
|
|
||||||
func Test_NewChangelogEntry(t *testing.T) {
|
func Test_NewChangelogEntry(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
analyzedCommits []commitparser.AnalyzedCommit
|
analyzedCommits []AnalyzedCommit
|
||||||
version string
|
version string
|
||||||
link string
|
link string
|
||||||
prefix string
|
prefix string
|
||||||
|
|
@ -30,7 +27,7 @@ func Test_NewChangelogEntry(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "empty",
|
name: "empty",
|
||||||
args: args{
|
args: args{
|
||||||
analyzedCommits: []commitparser.AnalyzedCommit{},
|
analyzedCommits: []AnalyzedCommit{},
|
||||||
version: "1.0.0",
|
version: "1.0.0",
|
||||||
link: "https://example.com/1.0.0",
|
link: "https://example.com/1.0.0",
|
||||||
},
|
},
|
||||||
|
|
@ -40,9 +37,9 @@ func Test_NewChangelogEntry(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "single feature",
|
name: "single feature",
|
||||||
args: args{
|
args: args{
|
||||||
analyzedCommits: []commitparser.AnalyzedCommit{
|
analyzedCommits: []AnalyzedCommit{
|
||||||
{
|
{
|
||||||
Commit: git.Commit{},
|
Commit: Commit{},
|
||||||
Type: "feat",
|
Type: "feat",
|
||||||
Description: "Foobar!",
|
Description: "Foobar!",
|
||||||
},
|
},
|
||||||
|
|
@ -56,9 +53,9 @@ func Test_NewChangelogEntry(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "single fix",
|
name: "single fix",
|
||||||
args: args{
|
args: args{
|
||||||
analyzedCommits: []commitparser.AnalyzedCommit{
|
analyzedCommits: []AnalyzedCommit{
|
||||||
{
|
{
|
||||||
Commit: git.Commit{},
|
Commit: Commit{},
|
||||||
Type: "fix",
|
Type: "fix",
|
||||||
Description: "Foobar!",
|
Description: "Foobar!",
|
||||||
},
|
},
|
||||||
|
|
@ -72,25 +69,25 @@ func Test_NewChangelogEntry(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "multiple commits with scopes",
|
name: "multiple commits with scopes",
|
||||||
args: args{
|
args: args{
|
||||||
analyzedCommits: []commitparser.AnalyzedCommit{
|
analyzedCommits: []AnalyzedCommit{
|
||||||
{
|
{
|
||||||
Commit: git.Commit{},
|
Commit: Commit{},
|
||||||
Type: "feat",
|
Type: "feat",
|
||||||
Description: "Blabla!",
|
Description: "Blabla!",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Commit: git.Commit{},
|
Commit: Commit{},
|
||||||
Type: "feat",
|
Type: "feat",
|
||||||
Description: "So awesome!",
|
Description: "So awesome!",
|
||||||
Scope: ptr("awesome"),
|
Scope: ptr("awesome"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Commit: git.Commit{},
|
Commit: Commit{},
|
||||||
Type: "fix",
|
Type: "fix",
|
||||||
Description: "Foobar!",
|
Description: "Foobar!",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Commit: git.Commit{},
|
Commit: Commit{},
|
||||||
Type: "fix",
|
Type: "fix",
|
||||||
Description: "So sad!",
|
Description: "So sad!",
|
||||||
Scope: ptr("sad"),
|
Scope: ptr("sad"),
|
||||||
|
|
@ -115,9 +112,9 @@ func Test_NewChangelogEntry(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "prefix",
|
name: "prefix",
|
||||||
args: args{
|
args: args{
|
||||||
analyzedCommits: []commitparser.AnalyzedCommit{
|
analyzedCommits: []AnalyzedCommit{
|
||||||
{
|
{
|
||||||
Commit: git.Commit{},
|
Commit: Commit{},
|
||||||
Type: "fix",
|
Type: "fix",
|
||||||
Description: "Foobar!",
|
Description: "Foobar!",
|
||||||
},
|
},
|
||||||
|
|
@ -138,9 +135,9 @@ func Test_NewChangelogEntry(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "suffix",
|
name: "suffix",
|
||||||
args: args{
|
args: args{
|
||||||
analyzedCommits: []commitparser.AnalyzedCommit{
|
analyzedCommits: []AnalyzedCommit{
|
||||||
{
|
{
|
||||||
Commit: git.Commit{},
|
Commit: Commit{},
|
||||||
Type: "fix",
|
Type: "fix",
|
||||||
Description: "Foobar!",
|
Description: "Foobar!",
|
||||||
},
|
},
|
||||||
|
|
@ -6,11 +6,6 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
rp "github.com/apricote/releaser-pleaser"
|
rp "github.com/apricote/releaser-pleaser"
|
||||||
"github.com/apricote/releaser-pleaser/internal/commitparser/conventionalcommits"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/forge"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/forge/github"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/updater"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/versioning"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var runCmd = &cobra.Command{
|
var runCmd = &cobra.Command{
|
||||||
|
|
@ -46,9 +41,9 @@ func run(cmd *cobra.Command, _ []string) error {
|
||||||
"repo", flagRepo,
|
"repo", flagRepo,
|
||||||
)
|
)
|
||||||
|
|
||||||
var f forge.Forge
|
var forge rp.Forge
|
||||||
|
|
||||||
forgeOptions := forge.Options{
|
forgeOptions := rp.ForgeOptions{
|
||||||
Repository: flagRepo,
|
Repository: flagRepo,
|
||||||
BaseBranch: flagBranch,
|
BaseBranch: flagBranch,
|
||||||
}
|
}
|
||||||
|
|
@ -58,23 +53,23 @@ func run(cmd *cobra.Command, _ []string) error {
|
||||||
// f = rp.NewGitLab(forgeOptions)
|
// f = rp.NewGitLab(forgeOptions)
|
||||||
case "github":
|
case "github":
|
||||||
logger.DebugContext(ctx, "using forge GitHub")
|
logger.DebugContext(ctx, "using forge GitHub")
|
||||||
f = github.New(logger, &github.Options{
|
forge = rp.NewGitHub(logger, &rp.GitHubOptions{
|
||||||
Options: forgeOptions,
|
ForgeOptions: forgeOptions,
|
||||||
Owner: flagOwner,
|
Owner: flagOwner,
|
||||||
Repo: flagRepo,
|
Repo: flagRepo,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
extraFiles := parseExtraFiles(flagExtraFiles)
|
extraFiles := parseExtraFiles(flagExtraFiles)
|
||||||
|
|
||||||
releaserPleaser := rp.New(
|
releaserPleaser := rp.New(
|
||||||
f,
|
forge,
|
||||||
logger,
|
logger,
|
||||||
flagBranch,
|
flagBranch,
|
||||||
conventionalcommits.NewParser(),
|
rp.NewConventionalCommitsParser(),
|
||||||
versioning.SemVerNextVersion,
|
rp.SemVerNextVersion,
|
||||||
extraFiles,
|
extraFiles,
|
||||||
[]updater.NewUpdater{updater.Generic},
|
[]rp.Updater{&rp.GenericUpdater{}},
|
||||||
)
|
)
|
||||||
|
|
||||||
return releaserPleaser.Run(ctx)
|
return releaserPleaser.Run(ctx)
|
||||||
|
|
|
||||||
|
|
@ -1,32 +1,54 @@
|
||||||
package conventionalcommits
|
package rp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/leodido/go-conventionalcommits"
|
"github.com/leodido/go-conventionalcommits"
|
||||||
"github.com/leodido/go-conventionalcommits/parser"
|
"github.com/leodido/go-conventionalcommits/parser"
|
||||||
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/commitparser"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/git"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Parser struct {
|
type Commit struct {
|
||||||
|
Hash string
|
||||||
|
Message string
|
||||||
|
|
||||||
|
PullRequest *PullRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
type PullRequest struct {
|
||||||
|
ID int
|
||||||
|
Title string
|
||||||
|
Description string
|
||||||
|
}
|
||||||
|
|
||||||
|
type AnalyzedCommit struct {
|
||||||
|
Commit
|
||||||
|
Type string
|
||||||
|
Description string
|
||||||
|
Scope *string
|
||||||
|
BreakingChange bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type CommitParser interface {
|
||||||
|
Analyze(commits []Commit) ([]AnalyzedCommit, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ConventionalCommitsParser struct {
|
||||||
machine conventionalcommits.Machine
|
machine conventionalcommits.Machine
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewParser() *Parser {
|
func NewConventionalCommitsParser() *ConventionalCommitsParser {
|
||||||
parserMachine := parser.NewMachine(
|
parserMachine := parser.NewMachine(
|
||||||
parser.WithBestEffort(),
|
parser.WithBestEffort(),
|
||||||
parser.WithTypes(conventionalcommits.TypesConventional),
|
parser.WithTypes(conventionalcommits.TypesConventional),
|
||||||
)
|
)
|
||||||
|
|
||||||
return &Parser{
|
return &ConventionalCommitsParser{
|
||||||
machine: parserMachine,
|
machine: parserMachine,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Parser) Analyze(commits []git.Commit) ([]commitparser.AnalyzedCommit, error) {
|
func (c *ConventionalCommitsParser) Analyze(commits []Commit) ([]AnalyzedCommit, error) {
|
||||||
analyzedCommits := make([]commitparser.AnalyzedCommit, 0, len(commits))
|
analyzedCommits := make([]AnalyzedCommit, 0, len(commits))
|
||||||
|
|
||||||
for _, commit := range commits {
|
for _, commit := range commits {
|
||||||
msg, err := c.machine.Parse([]byte(commit.Message))
|
msg, err := c.machine.Parse([]byte(commit.Message))
|
||||||
|
|
@ -41,7 +63,7 @@ func (c *Parser) Analyze(commits []git.Commit) ([]commitparser.AnalyzedCommit, e
|
||||||
commitVersionBump := conventionalCommit.VersionBump(conventionalcommits.DefaultStrategy)
|
commitVersionBump := conventionalCommit.VersionBump(conventionalcommits.DefaultStrategy)
|
||||||
if commitVersionBump > conventionalcommits.UnknownVersion {
|
if commitVersionBump > conventionalcommits.UnknownVersion {
|
||||||
// We only care about releasable commits
|
// We only care about releasable commits
|
||||||
analyzedCommits = append(analyzedCommits, commitparser.AnalyzedCommit{
|
analyzedCommits = append(analyzedCommits, AnalyzedCommit{
|
||||||
Commit: commit,
|
Commit: commit,
|
||||||
Type: conventionalCommit.Type,
|
Type: conventionalCommit.Type,
|
||||||
Description: conventionalCommit.Description,
|
Description: conventionalCommit.Description,
|
||||||
|
|
@ -1,30 +1,27 @@
|
||||||
package conventionalcommits
|
package rp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/commitparser"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/git"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAnalyzeCommits(t *testing.T) {
|
func TestAnalyzeCommits(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
commits []git.Commit
|
commits []Commit
|
||||||
expectedCommits []commitparser.AnalyzedCommit
|
expectedCommits []AnalyzedCommit
|
||||||
wantErr assert.ErrorAssertionFunc
|
wantErr assert.ErrorAssertionFunc
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "empty commits",
|
name: "empty commits",
|
||||||
commits: []git.Commit{},
|
commits: []Commit{},
|
||||||
expectedCommits: []commitparser.AnalyzedCommit{},
|
expectedCommits: []AnalyzedCommit{},
|
||||||
wantErr: assert.NoError,
|
wantErr: assert.NoError,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "malformed commit message",
|
name: "malformed commit message",
|
||||||
commits: []git.Commit{
|
commits: []Commit{
|
||||||
{
|
{
|
||||||
Message: "aksdjaklsdjka",
|
Message: "aksdjaklsdjka",
|
||||||
},
|
},
|
||||||
|
|
@ -34,17 +31,17 @@ func TestAnalyzeCommits(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "drops unreleasable",
|
name: "drops unreleasable",
|
||||||
commits: []git.Commit{
|
commits: []Commit{
|
||||||
{
|
{
|
||||||
Message: "chore: foobar",
|
Message: "chore: foobar",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedCommits: []commitparser.AnalyzedCommit{},
|
expectedCommits: []AnalyzedCommit{},
|
||||||
wantErr: assert.NoError,
|
wantErr: assert.NoError,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "highest bump (patch)",
|
name: "highest bump (patch)",
|
||||||
commits: []git.Commit{
|
commits: []Commit{
|
||||||
{
|
{
|
||||||
Message: "chore: foobar",
|
Message: "chore: foobar",
|
||||||
},
|
},
|
||||||
|
|
@ -52,9 +49,9 @@ func TestAnalyzeCommits(t *testing.T) {
|
||||||
Message: "fix: blabla",
|
Message: "fix: blabla",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedCommits: []commitparser.AnalyzedCommit{
|
expectedCommits: []AnalyzedCommit{
|
||||||
{
|
{
|
||||||
Commit: git.Commit{Message: "fix: blabla"},
|
Commit: Commit{Message: "fix: blabla"},
|
||||||
Type: "fix",
|
Type: "fix",
|
||||||
Description: "blabla",
|
Description: "blabla",
|
||||||
},
|
},
|
||||||
|
|
@ -63,7 +60,7 @@ func TestAnalyzeCommits(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "highest bump (minor)",
|
name: "highest bump (minor)",
|
||||||
commits: []git.Commit{
|
commits: []Commit{
|
||||||
{
|
{
|
||||||
Message: "fix: blabla",
|
Message: "fix: blabla",
|
||||||
},
|
},
|
||||||
|
|
@ -71,14 +68,14 @@ func TestAnalyzeCommits(t *testing.T) {
|
||||||
Message: "feat: foobar",
|
Message: "feat: foobar",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedCommits: []commitparser.AnalyzedCommit{
|
expectedCommits: []AnalyzedCommit{
|
||||||
{
|
{
|
||||||
Commit: git.Commit{Message: "fix: blabla"},
|
Commit: Commit{Message: "fix: blabla"},
|
||||||
Type: "fix",
|
Type: "fix",
|
||||||
Description: "blabla",
|
Description: "blabla",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Commit: git.Commit{Message: "feat: foobar"},
|
Commit: Commit{Message: "feat: foobar"},
|
||||||
Type: "feat",
|
Type: "feat",
|
||||||
Description: "foobar",
|
Description: "foobar",
|
||||||
},
|
},
|
||||||
|
|
@ -88,7 +85,7 @@ func TestAnalyzeCommits(t *testing.T) {
|
||||||
|
|
||||||
{
|
{
|
||||||
name: "highest bump (major)",
|
name: "highest bump (major)",
|
||||||
commits: []git.Commit{
|
commits: []Commit{
|
||||||
{
|
{
|
||||||
Message: "fix: blabla",
|
Message: "fix: blabla",
|
||||||
},
|
},
|
||||||
|
|
@ -96,14 +93,14 @@ func TestAnalyzeCommits(t *testing.T) {
|
||||||
Message: "feat!: foobar",
|
Message: "feat!: foobar",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedCommits: []commitparser.AnalyzedCommit{
|
expectedCommits: []AnalyzedCommit{
|
||||||
{
|
{
|
||||||
Commit: git.Commit{Message: "fix: blabla"},
|
Commit: Commit{Message: "fix: blabla"},
|
||||||
Type: "fix",
|
Type: "fix",
|
||||||
Description: "blabla",
|
Description: "blabla",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Commit: git.Commit{Message: "feat!: foobar"},
|
Commit: Commit{Message: "feat!: foobar"},
|
||||||
Type: "feat",
|
Type: "feat",
|
||||||
Description: "foobar",
|
Description: "foobar",
|
||||||
BreakingChange: true,
|
BreakingChange: true,
|
||||||
|
|
@ -114,7 +111,7 @@ func TestAnalyzeCommits(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
analyzedCommits, err := NewParser().Analyze(tt.commits)
|
analyzedCommits, err := NewConventionalCommitsParser().Analyze(tt.commits)
|
||||||
if !tt.wantErr(t, err) {
|
if !tt.wantErr(t, err) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package github
|
package rp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
@ -13,27 +13,75 @@ import (
|
||||||
"github.com/go-git/go-git/v5/plumbing/transport"
|
"github.com/go-git/go-git/v5/plumbing/transport"
|
||||||
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||||
"github.com/google/go-github/v63/github"
|
"github.com/google/go-github/v63/github"
|
||||||
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/forge"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/git"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/pointer"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/releasepr"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
PerPageMax = 100
|
GitHubPerPageMax = 100
|
||||||
PRStateOpen = "open"
|
GitHubPRStateOpen = "open"
|
||||||
PRStateClosed = "closed"
|
GitHubPRStateClosed = "closed"
|
||||||
EnvAPIToken = "GITHUB_TOKEN" // nolint:gosec // Not actually a hardcoded credential
|
GitHubEnvAPIToken = "GITHUB_TOKEN" // nolint:gosec // Not actually a hardcoded credential
|
||||||
EnvUsername = "GITHUB_USER"
|
GitHubEnvUsername = "GITHUB_USER"
|
||||||
EnvRepository = "GITHUB_REPOSITORY"
|
GitHubEnvRepository = "GITHUB_REPOSITORY"
|
||||||
LabelColor = "dedede"
|
GitHubLabelColor = "dedede"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ forge.Forge = &GitHub{}
|
type Forge interface {
|
||||||
|
RepoURL() string
|
||||||
|
CloneURL() string
|
||||||
|
ReleaseURL(version string) string
|
||||||
|
|
||||||
|
GitAuth() transport.AuthMethod
|
||||||
|
|
||||||
|
// LatestTags returns the last stable tag created on the main branch. If there is a more recent pre-release tag,
|
||||||
|
// that is also returned. If no tag is found, it returns nil.
|
||||||
|
LatestTags(context.Context) (Releases, error)
|
||||||
|
|
||||||
|
// CommitsSince returns all commits to main branch after the Tag. The tag can be `nil`, in which case this
|
||||||
|
// function should return all commits.
|
||||||
|
CommitsSince(context.Context, *Tag) ([]Commit, error)
|
||||||
|
|
||||||
|
// EnsureLabelsExist verifies that all desired labels are available on the repository. If labels are missing, they
|
||||||
|
// are created them.
|
||||||
|
EnsureLabelsExist(context.Context, []Label) error
|
||||||
|
|
||||||
|
// PullRequestForBranch returns the open pull request between the branch and ForgeOptions.BaseBranch. If no open PR
|
||||||
|
// exists, it returns nil.
|
||||||
|
PullRequestForBranch(context.Context, string) (*ReleasePullRequest, error)
|
||||||
|
|
||||||
|
// CreatePullRequest opens a new pull/merge request for the ReleasePullRequest.
|
||||||
|
CreatePullRequest(context.Context, *ReleasePullRequest) error
|
||||||
|
|
||||||
|
// UpdatePullRequest updates the pull/merge request identified through the ID of
|
||||||
|
// the ReleasePullRequest to the current description and title.
|
||||||
|
UpdatePullRequest(context.Context, *ReleasePullRequest) error
|
||||||
|
|
||||||
|
// SetPullRequestLabels updates the pull/merge request identified through the ID of
|
||||||
|
// the ReleasePullRequest to the current labels.
|
||||||
|
SetPullRequestLabels(ctx context.Context, pr *ReleasePullRequest, remove, add []Label) error
|
||||||
|
|
||||||
|
// ClosePullRequest closes the pull/merge request identified through the ID of
|
||||||
|
// the ReleasePullRequest, as it is no longer required.
|
||||||
|
ClosePullRequest(context.Context, *ReleasePullRequest) error
|
||||||
|
|
||||||
|
// PendingReleases returns a list of ReleasePullRequest. The list should contain all pull/merge requests that are
|
||||||
|
// merged and have the matching label.
|
||||||
|
PendingReleases(context.Context, Label) ([]*ReleasePullRequest, error)
|
||||||
|
|
||||||
|
// CreateRelease creates a release on the Forge, pointing at the commit with the passed in details.
|
||||||
|
CreateRelease(ctx context.Context, commit Commit, title, changelog string, prerelease, latest bool) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type ForgeOptions struct {
|
||||||
|
Repository string
|
||||||
|
BaseBranch string
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ Forge = &GitHub{}
|
||||||
|
|
||||||
|
// var _ Forge = &GitLab{}
|
||||||
|
|
||||||
type GitHub struct {
|
type GitHub struct {
|
||||||
options *Options
|
options *GitHubOptions
|
||||||
|
|
||||||
client *github.Client
|
client *github.Client
|
||||||
log *slog.Logger
|
log *slog.Logger
|
||||||
|
|
@ -58,24 +106,24 @@ func (g *GitHub) GitAuth() transport.AuthMethod {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitHub) LatestTags(ctx context.Context) (git.Releases, error) {
|
func (g *GitHub) LatestTags(ctx context.Context) (Releases, error) {
|
||||||
g.log.DebugContext(ctx, "listing all tags in github repository")
|
g.log.DebugContext(ctx, "listing all tags in github repository")
|
||||||
|
|
||||||
page := 1
|
page := 1
|
||||||
|
|
||||||
var releases git.Releases
|
var releases Releases
|
||||||
|
|
||||||
for {
|
for {
|
||||||
tags, resp, err := g.client.Repositories.ListTags(
|
tags, resp, err := g.client.Repositories.ListTags(
|
||||||
ctx, g.options.Owner, g.options.Repo,
|
ctx, g.options.Owner, g.options.Repo,
|
||||||
&github.ListOptions{Page: page, PerPage: PerPageMax},
|
&github.ListOptions{Page: page, PerPage: GitHubPerPageMax},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return git.Releases{}, err
|
return Releases{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, ghTag := range tags {
|
for _, ghTag := range tags {
|
||||||
tag := &git.Tag{
|
tag := &Tag{
|
||||||
Hash: ghTag.GetCommit().GetSHA(),
|
Hash: ghTag.GetCommit().GetSHA(),
|
||||||
Name: ghTag.GetName(),
|
Name: ghTag.GetName(),
|
||||||
}
|
}
|
||||||
|
|
@ -112,7 +160,7 @@ func (g *GitHub) LatestTags(ctx context.Context) (git.Releases, error) {
|
||||||
return releases, nil
|
return releases, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitHub) CommitsSince(ctx context.Context, tag *git.Tag) ([]git.Commit, error) {
|
func (g *GitHub) CommitsSince(ctx context.Context, tag *Tag) ([]Commit, error) {
|
||||||
var repositoryCommits []*github.RepositoryCommit
|
var repositoryCommits []*github.RepositoryCommit
|
||||||
var err error
|
var err error
|
||||||
if tag != nil {
|
if tag != nil {
|
||||||
|
|
@ -125,9 +173,9 @@ func (g *GitHub) CommitsSince(ctx context.Context, tag *git.Tag) ([]git.Commit,
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var commits = make([]git.Commit, 0, len(repositoryCommits))
|
var commits = make([]Commit, 0, len(repositoryCommits))
|
||||||
for _, ghCommit := range repositoryCommits {
|
for _, ghCommit := range repositoryCommits {
|
||||||
commit := git.Commit{
|
commit := Commit{
|
||||||
Hash: ghCommit.GetSHA(),
|
Hash: ghCommit.GetSHA(),
|
||||||
Message: ghCommit.GetCommit().GetMessage(),
|
Message: ghCommit.GetCommit().GetMessage(),
|
||||||
}
|
}
|
||||||
|
|
@ -142,7 +190,7 @@ func (g *GitHub) CommitsSince(ctx context.Context, tag *git.Tag) ([]git.Commit,
|
||||||
return commits, nil
|
return commits, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitHub) commitsSinceTag(ctx context.Context, tag *git.Tag) ([]*github.RepositoryCommit, error) {
|
func (g *GitHub) commitsSinceTag(ctx context.Context, tag *Tag) ([]*github.RepositoryCommit, error) {
|
||||||
head := g.options.BaseBranch
|
head := g.options.BaseBranch
|
||||||
log := g.log.With("base", tag.Hash, "head", head)
|
log := g.log.With("base", tag.Hash, "head", head)
|
||||||
log.Debug("comparing commits", "base", tag.Hash, "head", head)
|
log.Debug("comparing commits", "base", tag.Hash, "head", head)
|
||||||
|
|
@ -156,7 +204,7 @@ func (g *GitHub) commitsSinceTag(ctx context.Context, tag *git.Tag) ([]*github.R
|
||||||
ctx, g.options.Owner, g.options.Repo,
|
ctx, g.options.Owner, g.options.Repo,
|
||||||
tag.Hash, head, &github.ListOptions{
|
tag.Hash, head, &github.ListOptions{
|
||||||
Page: page,
|
Page: page,
|
||||||
PerPage: PerPageMax,
|
PerPage: GitHubPerPageMax,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -196,7 +244,7 @@ func (g *GitHub) commitsSinceInit(ctx context.Context) ([]*github.RepositoryComm
|
||||||
SHA: head,
|
SHA: head,
|
||||||
ListOptions: github.ListOptions{
|
ListOptions: github.ListOptions{
|
||||||
Page: page,
|
Page: page,
|
||||||
PerPage: PerPageMax,
|
PerPage: GitHubPerPageMax,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -206,7 +254,7 @@ func (g *GitHub) commitsSinceInit(ctx context.Context) ([]*github.RepositoryComm
|
||||||
if repositoryCommits == nil && resp.LastPage > 0 {
|
if repositoryCommits == nil && resp.LastPage > 0 {
|
||||||
// Pre-initialize slice on first request
|
// Pre-initialize slice on first request
|
||||||
log.Debug("found commits", "pages", resp.LastPage)
|
log.Debug("found commits", "pages", resp.LastPage)
|
||||||
repositoryCommits = make([]*github.RepositoryCommit, 0, resp.LastPage*PerPageMax)
|
repositoryCommits = make([]*github.RepositoryCommit, 0, resp.LastPage*GitHubPerPageMax)
|
||||||
}
|
}
|
||||||
|
|
||||||
repositoryCommits = append(repositoryCommits, commits...)
|
repositoryCommits = append(repositoryCommits, commits...)
|
||||||
|
|
@ -221,7 +269,7 @@ func (g *GitHub) commitsSinceInit(ctx context.Context) ([]*github.RepositoryComm
|
||||||
return repositoryCommits, nil
|
return repositoryCommits, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitHub) prForCommit(ctx context.Context, commit git.Commit) (*git.PullRequest, error) {
|
func (g *GitHub) prForCommit(ctx context.Context, commit Commit) (*PullRequest, error) {
|
||||||
// We naively look up the associated PR for each commit through the "List pull requests associated with a commit"
|
// We naively look up the associated PR for each commit through the "List pull requests associated with a commit"
|
||||||
// endpoint. This requires len(commits) requests.
|
// endpoint. This requires len(commits) requests.
|
||||||
// Using the "List pull requests" endpoint might be faster, as it allows us to fetch 100 arbitrary PRs per request,
|
// Using the "List pull requests" endpoint might be faster, as it allows us to fetch 100 arbitrary PRs per request,
|
||||||
|
|
@ -237,7 +285,7 @@ func (g *GitHub) prForCommit(ctx context.Context, commit git.Commit) (*git.PullR
|
||||||
ctx, g.options.Owner, g.options.Repo,
|
ctx, g.options.Owner, g.options.Repo,
|
||||||
commit.Hash, &github.ListOptions{
|
commit.Hash, &github.ListOptions{
|
||||||
Page: page,
|
Page: page,
|
||||||
PerPage: PerPageMax,
|
PerPage: GitHubPerPageMax,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -266,7 +314,7 @@ func (g *GitHub) prForCommit(ctx context.Context, commit git.Commit) (*git.PullR
|
||||||
return gitHubPRToPullRequest(pullrequest), nil
|
return gitHubPRToPullRequest(pullrequest), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitHub) EnsureLabelsExist(ctx context.Context, labels []releasepr.Label) error {
|
func (g *GitHub) EnsureLabelsExist(ctx context.Context, labels []Label) error {
|
||||||
existingLabels := make([]string, 0, len(labels))
|
existingLabels := make([]string, 0, len(labels))
|
||||||
|
|
||||||
page := 1
|
page := 1
|
||||||
|
|
@ -277,7 +325,7 @@ func (g *GitHub) EnsureLabelsExist(ctx context.Context, labels []releasepr.Label
|
||||||
ctx, g.options.Owner, g.options.Repo,
|
ctx, g.options.Owner, g.options.Repo,
|
||||||
&github.ListOptions{
|
&github.ListOptions{
|
||||||
Page: page,
|
Page: page,
|
||||||
PerPage: PerPageMax,
|
PerPage: GitHubPerPageMax,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -299,8 +347,8 @@ func (g *GitHub) EnsureLabelsExist(ctx context.Context, labels []releasepr.Label
|
||||||
_, _, err := g.client.Issues.CreateLabel(
|
_, _, err := g.client.Issues.CreateLabel(
|
||||||
ctx, g.options.Owner, g.options.Repo,
|
ctx, g.options.Owner, g.options.Repo,
|
||||||
&github.Label{
|
&github.Label{
|
||||||
Name: pointer.Pointer(string(label)),
|
Name: Pointer(string(label)),
|
||||||
Color: pointer.Pointer(LabelColor),
|
Color: Pointer(GitHubLabelColor),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -312,13 +360,13 @@ func (g *GitHub) EnsureLabelsExist(ctx context.Context, labels []releasepr.Label
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitHub) PullRequestForBranch(ctx context.Context, branch string) (*releasepr.ReleasePullRequest, error) {
|
func (g *GitHub) PullRequestForBranch(ctx context.Context, branch string) (*ReleasePullRequest, error) {
|
||||||
page := 1
|
page := 1
|
||||||
|
|
||||||
for {
|
for {
|
||||||
prs, resp, err := g.client.PullRequests.ListPullRequestsWithCommit(ctx, g.options.Owner, g.options.Repo, branch, &github.ListOptions{
|
prs, resp, err := g.client.PullRequests.ListPullRequestsWithCommit(ctx, g.options.Owner, g.options.Repo, branch, &github.ListOptions{
|
||||||
Page: page,
|
Page: page,
|
||||||
PerPage: PerPageMax,
|
PerPage: GitHubPerPageMax,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var ghErr *github.ErrorResponse
|
var ghErr *github.ErrorResponse
|
||||||
|
|
@ -331,7 +379,7 @@ func (g *GitHub) PullRequestForBranch(ctx context.Context, branch string) (*rele
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, pr := range prs {
|
for _, pr := range prs {
|
||||||
if pr.GetBase().GetRef() == g.options.BaseBranch && pr.GetHead().GetRef() == branch && pr.GetState() == PRStateOpen {
|
if pr.GetBase().GetRef() == g.options.BaseBranch && pr.GetHead().GetRef() == branch && pr.GetState() == GitHubPRStateOpen {
|
||||||
return gitHubPRToReleasePullRequest(pr), nil
|
return gitHubPRToReleasePullRequest(pr), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -345,7 +393,7 @@ func (g *GitHub) PullRequestForBranch(ctx context.Context, branch string) (*rele
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitHub) CreatePullRequest(ctx context.Context, pr *releasepr.ReleasePullRequest) error {
|
func (g *GitHub) CreatePullRequest(ctx context.Context, pr *ReleasePullRequest) error {
|
||||||
ghPR, _, err := g.client.PullRequests.Create(
|
ghPR, _, err := g.client.PullRequests.Create(
|
||||||
ctx, g.options.Owner, g.options.Repo,
|
ctx, g.options.Owner, g.options.Repo,
|
||||||
&github.NewPullRequest{
|
&github.NewPullRequest{
|
||||||
|
|
@ -362,7 +410,7 @@ func (g *GitHub) CreatePullRequest(ctx context.Context, pr *releasepr.ReleasePul
|
||||||
// TODO: String ID?
|
// TODO: String ID?
|
||||||
pr.ID = ghPR.GetNumber()
|
pr.ID = ghPR.GetNumber()
|
||||||
|
|
||||||
err = g.SetPullRequestLabels(ctx, pr, []releasepr.Label{}, pr.Labels)
|
err = g.SetPullRequestLabels(ctx, pr, []Label{}, pr.Labels)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -370,7 +418,7 @@ func (g *GitHub) CreatePullRequest(ctx context.Context, pr *releasepr.ReleasePul
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitHub) UpdatePullRequest(ctx context.Context, pr *releasepr.ReleasePullRequest) error {
|
func (g *GitHub) UpdatePullRequest(ctx context.Context, pr *ReleasePullRequest) error {
|
||||||
_, _, err := g.client.PullRequests.Edit(
|
_, _, err := g.client.PullRequests.Edit(
|
||||||
ctx, g.options.Owner, g.options.Repo,
|
ctx, g.options.Owner, g.options.Repo,
|
||||||
pr.ID, &github.PullRequest{
|
pr.ID, &github.PullRequest{
|
||||||
|
|
@ -385,7 +433,7 @@ func (g *GitHub) UpdatePullRequest(ctx context.Context, pr *releasepr.ReleasePul
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitHub) SetPullRequestLabels(ctx context.Context, pr *releasepr.ReleasePullRequest, remove, add []releasepr.Label) error {
|
func (g *GitHub) SetPullRequestLabels(ctx context.Context, pr *ReleasePullRequest, remove, add []Label) error {
|
||||||
for _, label := range remove {
|
for _, label := range remove {
|
||||||
_, err := g.client.Issues.RemoveLabelForIssue(
|
_, err := g.client.Issues.RemoveLabelForIssue(
|
||||||
ctx, g.options.Owner, g.options.Repo,
|
ctx, g.options.Owner, g.options.Repo,
|
||||||
|
|
@ -412,11 +460,11 @@ func (g *GitHub) SetPullRequestLabels(ctx context.Context, pr *releasepr.Release
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitHub) ClosePullRequest(ctx context.Context, pr *releasepr.ReleasePullRequest) error {
|
func (g *GitHub) ClosePullRequest(ctx context.Context, pr *ReleasePullRequest) error {
|
||||||
_, _, err := g.client.PullRequests.Edit(
|
_, _, err := g.client.PullRequests.Edit(
|
||||||
ctx, g.options.Owner, g.options.Repo,
|
ctx, g.options.Owner, g.options.Repo,
|
||||||
pr.ID, &github.PullRequest{
|
pr.ID, &github.PullRequest{
|
||||||
State: pointer.Pointer(PRStateClosed),
|
State: Pointer(GitHubPRStateClosed),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -426,20 +474,20 @@ func (g *GitHub) ClosePullRequest(ctx context.Context, pr *releasepr.ReleasePull
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitHub) PendingReleases(ctx context.Context, pendingLabel releasepr.Label) ([]*releasepr.ReleasePullRequest, error) {
|
func (g *GitHub) PendingReleases(ctx context.Context, pendingLabel Label) ([]*ReleasePullRequest, error) {
|
||||||
page := 1
|
page := 1
|
||||||
|
|
||||||
var prs []*releasepr.ReleasePullRequest
|
var prs []*ReleasePullRequest
|
||||||
|
|
||||||
for {
|
for {
|
||||||
ghPRs, resp, err := g.client.PullRequests.List(
|
ghPRs, resp, err := g.client.PullRequests.List(
|
||||||
ctx, g.options.Owner, g.options.Repo,
|
ctx, g.options.Owner, g.options.Repo,
|
||||||
&github.PullRequestListOptions{
|
&github.PullRequestListOptions{
|
||||||
State: PRStateClosed,
|
State: GitHubPRStateClosed,
|
||||||
Base: g.options.BaseBranch,
|
Base: g.options.BaseBranch,
|
||||||
ListOptions: github.ListOptions{
|
ListOptions: github.ListOptions{
|
||||||
Page: page,
|
Page: page,
|
||||||
PerPage: PerPageMax,
|
PerPage: GitHubPerPageMax,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -449,7 +497,7 @@ func (g *GitHub) PendingReleases(ctx context.Context, pendingLabel releasepr.Lab
|
||||||
if prs == nil && resp.LastPage > 0 {
|
if prs == nil && resp.LastPage > 0 {
|
||||||
// Pre-initialize slice on first request
|
// Pre-initialize slice on first request
|
||||||
g.log.Debug("found pending releases", "pages", resp.LastPage)
|
g.log.Debug("found pending releases", "pages", resp.LastPage)
|
||||||
prs = make([]*releasepr.ReleasePullRequest, 0, (resp.LastPage-1)*PerPageMax)
|
prs = make([]*ReleasePullRequest, 0, (resp.LastPage-1)*GitHubPerPageMax)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, pr := range ghPRs {
|
for _, pr := range ghPRs {
|
||||||
|
|
@ -478,7 +526,7 @@ func (g *GitHub) PendingReleases(ctx context.Context, pendingLabel releasepr.Lab
|
||||||
return prs, nil
|
return prs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitHub) CreateRelease(ctx context.Context, commit git.Commit, title, changelog string, preRelease, latest bool) error {
|
func (g *GitHub) CreateRelease(ctx context.Context, commit Commit, title, changelog string, preRelease, latest bool) error {
|
||||||
makeLatest := ""
|
makeLatest := ""
|
||||||
if latest {
|
if latest {
|
||||||
makeLatest = "true"
|
makeLatest = "true"
|
||||||
|
|
@ -503,29 +551,29 @@ func (g *GitHub) CreateRelease(ctx context.Context, commit git.Commit, title, ch
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func gitHubPRToPullRequest(pr *github.PullRequest) *git.PullRequest {
|
func gitHubPRToPullRequest(pr *github.PullRequest) *PullRequest {
|
||||||
return &git.PullRequest{
|
return &PullRequest{
|
||||||
ID: pr.GetNumber(),
|
ID: pr.GetNumber(),
|
||||||
Title: pr.GetTitle(),
|
Title: pr.GetTitle(),
|
||||||
Description: pr.GetBody(),
|
Description: pr.GetBody(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func gitHubPRToReleasePullRequest(pr *github.PullRequest) *releasepr.ReleasePullRequest {
|
func gitHubPRToReleasePullRequest(pr *github.PullRequest) *ReleasePullRequest {
|
||||||
labels := make([]releasepr.Label, 0, len(pr.Labels))
|
labels := make([]Label, 0, len(pr.Labels))
|
||||||
for _, label := range pr.Labels {
|
for _, label := range pr.Labels {
|
||||||
labelName := releasepr.Label(label.GetName())
|
labelName := Label(label.GetName())
|
||||||
if slices.Contains(releasepr.KnownLabels, releasepr.Label(label.GetName())) {
|
if slices.Contains(KnownLabels, Label(label.GetName())) {
|
||||||
labels = append(labels, labelName)
|
labels = append(labels, labelName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var releaseCommit *git.Commit
|
var releaseCommit *Commit
|
||||||
if pr.MergeCommitSHA != nil {
|
if pr.MergeCommitSHA != nil {
|
||||||
releaseCommit = &git.Commit{Hash: pr.GetMergeCommitSHA()}
|
releaseCommit = &Commit{Hash: pr.GetMergeCommitSHA()}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &releasepr.ReleasePullRequest{
|
return &ReleasePullRequest{
|
||||||
ID: pr.GetNumber(),
|
ID: pr.GetNumber(),
|
||||||
Title: pr.GetTitle(),
|
Title: pr.GetTitle(),
|
||||||
Description: pr.GetBody(),
|
Description: pr.GetBody(),
|
||||||
|
|
@ -536,16 +584,16 @@ func gitHubPRToReleasePullRequest(pr *github.PullRequest) *releasepr.ReleasePull
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Options) autodiscover() {
|
func (g *GitHubOptions) autodiscover() {
|
||||||
if apiToken := os.Getenv(EnvAPIToken); apiToken != "" {
|
if apiToken := os.Getenv(GitHubEnvAPIToken); apiToken != "" {
|
||||||
g.APIToken = apiToken
|
g.APIToken = apiToken
|
||||||
}
|
}
|
||||||
// TODO: Check if there is a better solution for cloning/pushing locally
|
// TODO: Check if there is a better solution for cloning/pushing locally
|
||||||
if username := os.Getenv(EnvUsername); username != "" {
|
if username := os.Getenv(GitHubEnvUsername); username != "" {
|
||||||
g.Username = username
|
g.Username = username
|
||||||
}
|
}
|
||||||
|
|
||||||
if envRepository := os.Getenv(EnvRepository); envRepository != "" {
|
if envRepository := os.Getenv(GitHubEnvRepository); envRepository != "" {
|
||||||
// GITHUB_REPOSITORY=apricote/releaser-pleaser
|
// GITHUB_REPOSITORY=apricote/releaser-pleaser
|
||||||
parts := strings.Split(envRepository, "/")
|
parts := strings.Split(envRepository, "/")
|
||||||
if len(parts) == 2 {
|
if len(parts) == 2 {
|
||||||
|
|
@ -556,8 +604,8 @@ func (g *Options) autodiscover() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Options struct {
|
type GitHubOptions struct {
|
||||||
forge.Options
|
ForgeOptions
|
||||||
|
|
||||||
Owner string
|
Owner string
|
||||||
Repo string
|
Repo string
|
||||||
|
|
@ -566,7 +614,7 @@ type Options struct {
|
||||||
Username string
|
Username string
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(log *slog.Logger, options *Options) *GitHub {
|
func NewGitHub(log *slog.Logger, options *GitHubOptions) *GitHub {
|
||||||
options.autodiscover()
|
options.autodiscover()
|
||||||
|
|
||||||
client := github.NewClient(nil)
|
client := github.NewClient(nil)
|
||||||
|
|
@ -583,3 +631,29 @@ func New(log *slog.Logger, options *Options) *GitHub {
|
||||||
|
|
||||||
return gh
|
return gh
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GitLab struct {
|
||||||
|
options ForgeOptions
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GitLab) autodiscover() {
|
||||||
|
// Read settings from GitLab-CI env vars
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGitLab(options ForgeOptions) *GitLab {
|
||||||
|
gl := &GitLab{
|
||||||
|
options: options,
|
||||||
|
}
|
||||||
|
|
||||||
|
gl.autodiscover()
|
||||||
|
|
||||||
|
return gl
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GitLab) RepoURL() string {
|
||||||
|
return fmt.Sprintf("https://gitlab.com/%s", g.options.Repository)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Pointer[T any](value T) *T {
|
||||||
|
return &value
|
||||||
|
}
|
||||||
52
git.go
Normal file
52
git.go
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
package rp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-git/go-git/v5"
|
||||||
|
"github.com/go-git/go-git/v5/plumbing"
|
||||||
|
"github.com/go-git/go-git/v5/plumbing/object"
|
||||||
|
"github.com/go-git/go-git/v5/plumbing/transport"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
GitRemoteName = "origin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Tag struct {
|
||||||
|
Hash string
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func CloneRepo(ctx context.Context, cloneURL, branch string, auth transport.AuthMethod) (*git.Repository, error) {
|
||||||
|
dir, err := os.MkdirTemp("", "releaser-pleaser.*")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create temporary directory for repo clone: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Log tmpdir
|
||||||
|
fmt.Printf("Clone tmpdir: %s\n", dir)
|
||||||
|
repo, err := git.PlainCloneContext(ctx, dir, false, &git.CloneOptions{
|
||||||
|
URL: cloneURL,
|
||||||
|
RemoteName: GitRemoteName,
|
||||||
|
ReferenceName: plumbing.NewBranchReferenceName(branch),
|
||||||
|
SingleBranch: false,
|
||||||
|
Auth: auth,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to clone repository: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return repo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GitSignature() *object.Signature {
|
||||||
|
return &object.Signature{
|
||||||
|
Name: "releaser-pleaser",
|
||||||
|
Email: "",
|
||||||
|
When: time.Now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
1
git_test.go
Normal file
1
git_test.go
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
package rp
|
||||||
2
go.mod
2
go.mod
|
|
@ -4,6 +4,7 @@ go 1.23.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/blang/semver/v4 v4.0.0
|
github.com/blang/semver/v4 v4.0.0
|
||||||
|
github.com/go-git/go-billy/v5 v5.5.0
|
||||||
github.com/go-git/go-git/v5 v5.12.0
|
github.com/go-git/go-git/v5 v5.12.0
|
||||||
github.com/google/go-github/v63 v63.0.0
|
github.com/google/go-github/v63 v63.0.0
|
||||||
github.com/leodido/go-conventionalcommits v0.12.0
|
github.com/leodido/go-conventionalcommits v0.12.0
|
||||||
|
|
@ -21,7 +22,6 @@ require (
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/emirpasic/gods v1.18.1 // indirect
|
github.com/emirpasic/gods v1.18.1 // indirect
|
||||||
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
|
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
|
||||||
github.com/go-git/go-billy/v5 v5.5.0 // indirect
|
|
||||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||||
github.com/google/go-querystring v1.1.0 // indirect
|
github.com/google/go-querystring v1.1.0 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
|
|
|
||||||
|
|
@ -1,17 +0,0 @@
|
||||||
package commitparser
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/git"
|
|
||||||
)
|
|
||||||
|
|
||||||
type CommitParser interface {
|
|
||||||
Analyze(commits []git.Commit) ([]AnalyzedCommit, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type AnalyzedCommit struct {
|
|
||||||
git.Commit
|
|
||||||
Type string
|
|
||||||
Description string
|
|
||||||
Scope *string
|
|
||||||
BreakingChange bool
|
|
||||||
}
|
|
||||||
|
|
@ -1,61 +0,0 @@
|
||||||
package forge
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/transport"
|
|
||||||
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/git"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/releasepr"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Forge interface {
|
|
||||||
RepoURL() string
|
|
||||||
CloneURL() string
|
|
||||||
ReleaseURL(version string) string
|
|
||||||
|
|
||||||
GitAuth() transport.AuthMethod
|
|
||||||
|
|
||||||
// LatestTags returns the last stable tag created on the main branch. If there is a more recent pre-release tag,
|
|
||||||
// that is also returned. If no tag is found, it returns nil.
|
|
||||||
LatestTags(context.Context) (git.Releases, error)
|
|
||||||
|
|
||||||
// CommitsSince returns all commits to main branch after the Tag. The tag can be `nil`, in which case this
|
|
||||||
// function should return all commits.
|
|
||||||
CommitsSince(context.Context, *git.Tag) ([]git.Commit, error)
|
|
||||||
|
|
||||||
// EnsureLabelsExist verifies that all desired labels are available on the repository. If labels are missing, they
|
|
||||||
// are created them.
|
|
||||||
EnsureLabelsExist(context.Context, []releasepr.Label) error
|
|
||||||
|
|
||||||
// PullRequestForBranch returns the open pull request between the branch and Options.BaseBranch. If no open PR
|
|
||||||
// exists, it returns nil.
|
|
||||||
PullRequestForBranch(context.Context, string) (*releasepr.ReleasePullRequest, error)
|
|
||||||
|
|
||||||
// CreatePullRequest opens a new pull/merge request for the ReleasePullRequest.
|
|
||||||
CreatePullRequest(context.Context, *releasepr.ReleasePullRequest) error
|
|
||||||
|
|
||||||
// UpdatePullRequest updates the pull/merge request identified through the ID of
|
|
||||||
// the ReleasePullRequest to the current description and title.
|
|
||||||
UpdatePullRequest(context.Context, *releasepr.ReleasePullRequest) error
|
|
||||||
|
|
||||||
// SetPullRequestLabels updates the pull/merge request identified through the ID of
|
|
||||||
// the ReleasePullRequest to the current labels.
|
|
||||||
SetPullRequestLabels(ctx context.Context, pr *releasepr.ReleasePullRequest, remove, add []releasepr.Label) error
|
|
||||||
|
|
||||||
// ClosePullRequest closes the pull/merge request identified through the ID of
|
|
||||||
// the ReleasePullRequest, as it is no longer required.
|
|
||||||
ClosePullRequest(context.Context, *releasepr.ReleasePullRequest) error
|
|
||||||
|
|
||||||
// PendingReleases returns a list of ReleasePullRequest. The list should contain all pull/merge requests that are
|
|
||||||
// merged and have the matching label.
|
|
||||||
PendingReleases(context.Context, releasepr.Label) ([]*releasepr.ReleasePullRequest, error)
|
|
||||||
|
|
||||||
// CreateRelease creates a release on the Forge, pointing at the commit with the passed in details.
|
|
||||||
CreateRelease(ctx context.Context, commit git.Commit, title, changelog string, prerelease, latest bool) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type Options struct {
|
|
||||||
Repository string
|
|
||||||
BaseBranch string
|
|
||||||
}
|
|
||||||
|
|
@ -1,31 +0,0 @@
|
||||||
package gitlab
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/forge"
|
|
||||||
)
|
|
||||||
|
|
||||||
// var _ forge.Forge = &GitLab{}
|
|
||||||
|
|
||||||
type GitLab struct {
|
|
||||||
options forge.Options
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GitLab) autodiscover() {
|
|
||||||
// Read settings from GitLab-CI env vars
|
|
||||||
}
|
|
||||||
|
|
||||||
func New(options forge.Options) *GitLab {
|
|
||||||
gl := &GitLab{
|
|
||||||
options: options,
|
|
||||||
}
|
|
||||||
|
|
||||||
gl.autodiscover()
|
|
||||||
|
|
||||||
return gl
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GitLab) RepoURL() string {
|
|
||||||
return fmt.Sprintf("https://gitlab.com/%s", g.options.Repository)
|
|
||||||
}
|
|
||||||
|
|
@ -1,227 +0,0 @@
|
||||||
package git
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5"
|
|
||||||
"github.com/go-git/go-git/v5/config"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/object"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/transport"
|
|
||||||
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/updater"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
remoteName = "origin"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Commit struct {
|
|
||||||
Hash string
|
|
||||||
Message string
|
|
||||||
|
|
||||||
PullRequest *PullRequest
|
|
||||||
}
|
|
||||||
|
|
||||||
type PullRequest struct {
|
|
||||||
ID int
|
|
||||||
Title string
|
|
||||||
Description string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Tag struct {
|
|
||||||
Hash string
|
|
||||||
Name string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Releases struct {
|
|
||||||
Latest *Tag
|
|
||||||
Stable *Tag
|
|
||||||
}
|
|
||||||
|
|
||||||
func CloneRepo(ctx context.Context, logger *slog.Logger, cloneURL, branch string, auth transport.AuthMethod) (*Repository, error) {
|
|
||||||
dir, err := os.MkdirTemp("", "releaser-pleaser.*")
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to create temporary directory for repo clone: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
repo, err := git.PlainCloneContext(ctx, dir, false, &git.CloneOptions{
|
|
||||||
URL: cloneURL,
|
|
||||||
RemoteName: remoteName,
|
|
||||||
ReferenceName: plumbing.NewBranchReferenceName(branch),
|
|
||||||
SingleBranch: false,
|
|
||||||
Auth: auth,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to clone repository: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Repository{r: repo, logger: logger, auth: auth}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Repository struct {
|
|
||||||
r *git.Repository
|
|
||||||
logger *slog.Logger
|
|
||||||
auth transport.AuthMethod
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Repository) DeleteBranch(ctx context.Context, branch string) error {
|
|
||||||
if b, _ := r.r.Branch(branch); b != nil {
|
|
||||||
r.logger.DebugContext(ctx, "deleting local branch", "branch.name", branch)
|
|
||||||
if err := r.r.DeleteBranch(branch); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Repository) Checkout(_ context.Context, branch string) error {
|
|
||||||
worktree, err := r.r.Worktree()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = worktree.Checkout(&git.CheckoutOptions{
|
|
||||||
Branch: plumbing.NewBranchReferenceName(branch),
|
|
||||||
Create: true,
|
|
||||||
}); err != nil {
|
|
||||||
return fmt.Errorf("failed to check out branch: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Repository) UpdateFile(_ context.Context, path string, updaters []updater.Updater) error {
|
|
||||||
worktree, err := r.r.Worktree()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := worktree.Filesystem.OpenFile(path, os.O_RDWR, 0)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
content, err := io.ReadAll(file)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
updatedContent := string(content)
|
|
||||||
|
|
||||||
for _, update := range updaters {
|
|
||||||
updatedContent, err = update(updatedContent)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to run updater on file %s", path)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = file.Truncate(0)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to replace file content: %w", err)
|
|
||||||
}
|
|
||||||
_, err = file.Seek(0, 0)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to replace file content: %w", err)
|
|
||||||
}
|
|
||||||
_, err = file.Write([]byte(updatedContent))
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to replace file content: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = worktree.Add(path)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to add updated file to git worktree: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Repository) Commit(_ context.Context, message string) (Commit, error) {
|
|
||||||
worktree, err := r.r.Worktree()
|
|
||||||
if err != nil {
|
|
||||||
return Commit{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
releaseCommitHash, err := worktree.Commit(message, &git.CommitOptions{
|
|
||||||
Author: signature(),
|
|
||||||
Committer: signature(),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return Commit{}, fmt.Errorf("failed to commit changes: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return Commit{
|
|
||||||
Hash: releaseCommitHash.String(),
|
|
||||||
Message: message,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Repository) HasChangesWithRemote(ctx context.Context, branch string) (bool, error) {
|
|
||||||
remoteRef, err := r.r.Reference(plumbing.NewRemoteReferenceName(remoteName, branch), false)
|
|
||||||
if err != nil {
|
|
||||||
if err.Error() == "reference not found" {
|
|
||||||
// No remote branch means that there are changes
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
remoteCommit, err := r.r.CommitObject(remoteRef.Hash())
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
localRef, err := r.r.Reference(plumbing.NewBranchReferenceName(branch), false)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
localCommit, err := r.r.CommitObject(localRef.Hash())
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
diff, err := localCommit.PatchContext(ctx, remoteCommit)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
hasChanges := len(diff.FilePatches()) > 0
|
|
||||||
|
|
||||||
return hasChanges, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Repository) ForcePush(ctx context.Context, branch string) error {
|
|
||||||
pushRefSpec := config.RefSpec(fmt.Sprintf(
|
|
||||||
"+%s:%s",
|
|
||||||
plumbing.NewBranchReferenceName(branch),
|
|
||||||
// This needs to be the local branch name, not the remotes/origin ref
|
|
||||||
// See https://stackoverflow.com/a/75727620
|
|
||||||
plumbing.NewBranchReferenceName(branch),
|
|
||||||
))
|
|
||||||
|
|
||||||
r.logger.DebugContext(ctx, "pushing branch", "branch.name", branch, "refspec", pushRefSpec.String())
|
|
||||||
return r.r.PushContext(ctx, &git.PushOptions{
|
|
||||||
RemoteName: remoteName,
|
|
||||||
RefSpecs: []config.RefSpec{pushRefSpec},
|
|
||||||
Force: true,
|
|
||||||
Auth: r.auth,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func signature() *object.Signature {
|
|
||||||
return &object.Signature{
|
|
||||||
Name: "releaser-pleaser",
|
|
||||||
Email: "",
|
|
||||||
When: time.Now(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,5 +0,0 @@
|
||||||
package pointer
|
|
||||||
|
|
||||||
func Pointer[T any](value T) *T {
|
|
||||||
return &value
|
|
||||||
}
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
package releasepr
|
|
||||||
126
internal/testutils/git.go
Normal file
126
internal/testutils/git.go
Normal file
|
|
@ -0,0 +1,126 @@
|
||||||
|
package testutils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-git/go-billy/v5/memfs"
|
||||||
|
"github.com/go-git/go-git/v5"
|
||||||
|
"github.com/go-git/go-git/v5/plumbing/object"
|
||||||
|
"github.com/go-git/go-git/v5/storage/memory"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
var author = &object.Signature{
|
||||||
|
Name: "releaser-pleaser",
|
||||||
|
When: time.Date(2020, 01, 01, 01, 01, 01, 01, time.UTC),
|
||||||
|
}
|
||||||
|
|
||||||
|
type CommitOption func(*commitOptions)
|
||||||
|
|
||||||
|
type commitOptions struct {
|
||||||
|
cleanFiles bool
|
||||||
|
files []commitFile
|
||||||
|
tags []string
|
||||||
|
}
|
||||||
|
|
||||||
|
type commitFile struct {
|
||||||
|
path string
|
||||||
|
content string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Commit func(*testing.T, *git.Repository) error
|
||||||
|
|
||||||
|
type Repo func(*testing.T) *git.Repository
|
||||||
|
|
||||||
|
func WithCommit(message string, options ...CommitOption) Commit {
|
||||||
|
return func(t *testing.T, repo *git.Repository) error {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
require.NotEmpty(t, message, "commit message is required")
|
||||||
|
|
||||||
|
opts := &commitOptions{}
|
||||||
|
for _, opt := range options {
|
||||||
|
opt(opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
wt, err := repo.Worktree()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Yeet all files
|
||||||
|
if opts.cleanFiles {
|
||||||
|
files, err := wt.Filesystem.ReadDir(".")
|
||||||
|
require.NoError(t, err, "failed to get current files")
|
||||||
|
|
||||||
|
for _, fileInfo := range files {
|
||||||
|
err = wt.Filesystem.Remove(fileInfo.Name())
|
||||||
|
require.NoError(t, err, "failed to remove file %q", fileInfo.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new files
|
||||||
|
for _, fileInfo := range opts.files {
|
||||||
|
file, err := wt.Filesystem.Create(fileInfo.path)
|
||||||
|
require.NoError(t, err, "failed to create file %q", fileInfo.path)
|
||||||
|
|
||||||
|
_, err = file.Write([]byte(fileInfo.content))
|
||||||
|
file.Close()
|
||||||
|
require.NoError(t, err, "failed to write content to file %q", fileInfo.path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Commit
|
||||||
|
commitHash, err := wt.Commit(message, &git.CommitOptions{
|
||||||
|
All: true,
|
||||||
|
AllowEmptyCommits: true,
|
||||||
|
Author: author,
|
||||||
|
Committer: author,
|
||||||
|
})
|
||||||
|
require.NoError(t, err, "failed to commit")
|
||||||
|
|
||||||
|
// Create tags
|
||||||
|
for _, tagName := range opts.tags {
|
||||||
|
_, err = repo.CreateTag(tagName, commitHash, nil)
|
||||||
|
require.NoError(t, err, "failed to create tag %q", tagName)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithFile(path, content string) CommitOption {
|
||||||
|
return func(opts *commitOptions) {
|
||||||
|
opts.files = append(opts.files, commitFile{path: path, content: content})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithCleanFiles() CommitOption {
|
||||||
|
return func(opts *commitOptions) {
|
||||||
|
opts.cleanFiles = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithTag(name string) CommitOption {
|
||||||
|
return func(opts *commitOptions) {
|
||||||
|
opts.tags = append(opts.tags, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithTestRepo(commits ...Commit) Repo {
|
||||||
|
return func(t *testing.T) *git.Repository {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
repo, err := git.Init(memory.NewStorage(), memfs.New())
|
||||||
|
require.NoError(t, err, "failed to create in-memory repository")
|
||||||
|
|
||||||
|
// Make initial commit
|
||||||
|
err = WithCommit("chore: init")(t, repo)
|
||||||
|
require.NoError(t, err, "failed to create init commit")
|
||||||
|
|
||||||
|
for i, commit := range commits {
|
||||||
|
err = commit(t, repo)
|
||||||
|
require.NoError(t, err, "failed to create commit %d", i)
|
||||||
|
}
|
||||||
|
|
||||||
|
return repo
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
package updater
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"regexp"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
ChangelogHeader = "# Changelog"
|
|
||||||
ChangelogFile = "CHANGELOG.md"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
ChangelogUpdaterHeaderRegex = regexp.MustCompile(`^# Changelog\n`)
|
|
||||||
)
|
|
||||||
|
|
||||||
func Changelog(info ReleaseInfo) Updater {
|
|
||||||
return func(content string) (string, error) {
|
|
||||||
headerIndex := ChangelogUpdaterHeaderRegex.FindStringIndex(content)
|
|
||||||
if headerIndex == nil && len(content) != 0 {
|
|
||||||
return "", fmt.Errorf("unexpected format of CHANGELOG.md, header does not match")
|
|
||||||
}
|
|
||||||
if headerIndex != nil {
|
|
||||||
// Remove the header from the content
|
|
||||||
content = content[headerIndex[1]:]
|
|
||||||
}
|
|
||||||
|
|
||||||
content = ChangelogHeader + "\n\n" + info.ChangelogEntry + content
|
|
||||||
|
|
||||||
return content, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,60 +0,0 @@
|
||||||
package updater
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestChangelogUpdater_UpdateContent(t *testing.T) {
|
|
||||||
tests := []updaterTestCase{
|
|
||||||
{
|
|
||||||
name: "empty file",
|
|
||||||
content: "",
|
|
||||||
info: ReleaseInfo{ChangelogEntry: "## v1.0.0\n"},
|
|
||||||
want: "# Changelog\n\n## v1.0.0\n",
|
|
||||||
wantErr: assert.NoError,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "well-formatted changelog",
|
|
||||||
content: `# Changelog
|
|
||||||
|
|
||||||
## v0.0.1
|
|
||||||
|
|
||||||
- Bazzle
|
|
||||||
|
|
||||||
## v0.1.0
|
|
||||||
|
|
||||||
### Bazuuum
|
|
||||||
`,
|
|
||||||
info: ReleaseInfo{ChangelogEntry: "## v1.0.0\n\n- Version 1, juhu.\n"},
|
|
||||||
want: `# Changelog
|
|
||||||
|
|
||||||
## v1.0.0
|
|
||||||
|
|
||||||
- Version 1, juhu.
|
|
||||||
|
|
||||||
## v0.0.1
|
|
||||||
|
|
||||||
- Bazzle
|
|
||||||
|
|
||||||
## v0.1.0
|
|
||||||
|
|
||||||
### Bazuuum
|
|
||||||
`,
|
|
||||||
wantErr: assert.NoError,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "error on invalid header",
|
|
||||||
content: "What even is this file?",
|
|
||||||
info: ReleaseInfo{ChangelogEntry: "## v1.0.0\n\n- Version 1, juhu.\n"},
|
|
||||||
want: "",
|
|
||||||
wantErr: assert.Error,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
runUpdaterTest(t, Changelog, tt)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,17 +0,0 @@
|
||||||
package updater
|
|
||||||
|
|
||||||
import (
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
var GenericUpdaterSemVerRegex = regexp.MustCompile(`\d+\.\d+\.\d+(-[\w.]+)?(.*x-releaser-pleaser-version)`)
|
|
||||||
|
|
||||||
func Generic(info ReleaseInfo) Updater {
|
|
||||||
return func(content string) (string, error) {
|
|
||||||
// We strip the "v" prefix to avoid adding/removing it from the users input.
|
|
||||||
version := strings.TrimPrefix(info.Version, "v")
|
|
||||||
|
|
||||||
return GenericUpdaterSemVerRegex.ReplaceAllString(content, version+"${2}"), nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,53 +0,0 @@
|
||||||
package updater
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestGenericUpdater_UpdateContent(t *testing.T) {
|
|
||||||
tests := []updaterTestCase{
|
|
||||||
{
|
|
||||||
name: "single line",
|
|
||||||
content: "v1.0.0 // x-releaser-pleaser-version",
|
|
||||||
info: ReleaseInfo{
|
|
||||||
Version: "v1.2.0",
|
|
||||||
},
|
|
||||||
want: "v1.2.0 // x-releaser-pleaser-version",
|
|
||||||
wantErr: assert.NoError,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "multiline line",
|
|
||||||
content: "Foooo\n\v1.2.0\nv1.0.0 // x-releaser-pleaser-version\n",
|
|
||||||
info: ReleaseInfo{
|
|
||||||
Version: "v1.2.0",
|
|
||||||
},
|
|
||||||
want: "Foooo\n\v1.2.0\nv1.2.0 // x-releaser-pleaser-version\n",
|
|
||||||
wantErr: assert.NoError,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "invalid existing version",
|
|
||||||
content: "1.0 // x-releaser-pleaser-version",
|
|
||||||
info: ReleaseInfo{
|
|
||||||
Version: "v1.2.0",
|
|
||||||
},
|
|
||||||
want: "1.0 // x-releaser-pleaser-version",
|
|
||||||
wantErr: assert.NoError,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "complicated line",
|
|
||||||
content: "version: v1.2.0-alpha.1 => Awesome, isnt it? x-releaser-pleaser-version foobar",
|
|
||||||
info: ReleaseInfo{
|
|
||||||
Version: "v1.2.0",
|
|
||||||
},
|
|
||||||
want: "version: v1.2.0 => Awesome, isnt it? x-releaser-pleaser-version foobar",
|
|
||||||
wantErr: assert.NoError,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
runUpdaterTest(t, Generic, tt)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
||||||
package updater
|
|
||||||
|
|
||||||
type ReleaseInfo struct {
|
|
||||||
Version string
|
|
||||||
ChangelogEntry string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Updater func(string) (string, error)
|
|
||||||
|
|
||||||
type NewUpdater func(ReleaseInfo) Updater
|
|
||||||
|
|
||||||
func WithInfo(info ReleaseInfo, constructors ...NewUpdater) []Updater {
|
|
||||||
updaters := make([]Updater, 0, len(constructors))
|
|
||||||
for _, constructor := range constructors {
|
|
||||||
updaters = append(updaters, constructor(info))
|
|
||||||
}
|
|
||||||
|
|
||||||
return updaters
|
|
||||||
}
|
|
||||||
|
|
@ -1,26 +0,0 @@
|
||||||
package updater
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
type updaterTestCase struct {
|
|
||||||
name string
|
|
||||||
content string
|
|
||||||
info ReleaseInfo
|
|
||||||
want string
|
|
||||||
wantErr assert.ErrorAssertionFunc
|
|
||||||
}
|
|
||||||
|
|
||||||
func runUpdaterTest(t *testing.T, constructor NewUpdater, tt updaterTestCase) {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
got, err := constructor(tt.info)(tt.content)
|
|
||||||
if !tt.wantErr(t, err, fmt.Sprintf("Updater(%v, %v)", tt.content, tt.info)) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
assert.Equalf(t, tt.want, got, "Updater(%v, %v)", tt.content, tt.info)
|
|
||||||
}
|
|
||||||
|
|
@ -1,56 +0,0 @@
|
||||||
package versioning
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/leodido/go-conventionalcommits"
|
|
||||||
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/git"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Strategy = func(git.Releases, VersionBump, NextVersionType) (string, error)
|
|
||||||
|
|
||||||
type VersionBump conventionalcommits.VersionBump
|
|
||||||
|
|
||||||
const (
|
|
||||||
UnknownVersion VersionBump = iota
|
|
||||||
PatchVersion
|
|
||||||
MinorVersion
|
|
||||||
MajorVersion
|
|
||||||
)
|
|
||||||
|
|
||||||
type NextVersionType int
|
|
||||||
|
|
||||||
const (
|
|
||||||
NextVersionTypeUndefined NextVersionType = iota
|
|
||||||
NextVersionTypeNormal
|
|
||||||
NextVersionTypeRC
|
|
||||||
NextVersionTypeBeta
|
|
||||||
NextVersionTypeAlpha
|
|
||||||
)
|
|
||||||
|
|
||||||
func (n NextVersionType) String() string {
|
|
||||||
switch n {
|
|
||||||
case NextVersionTypeUndefined:
|
|
||||||
return "undefined"
|
|
||||||
case NextVersionTypeNormal:
|
|
||||||
return "normal"
|
|
||||||
case NextVersionTypeRC:
|
|
||||||
return "rc"
|
|
||||||
case NextVersionTypeBeta:
|
|
||||||
return "beta"
|
|
||||||
case NextVersionTypeAlpha:
|
|
||||||
return "alpha"
|
|
||||||
default:
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n NextVersionType) IsPrerelease() bool {
|
|
||||||
switch n {
|
|
||||||
case NextVersionTypeRC, NextVersionTypeBeta, NextVersionTypeAlpha:
|
|
||||||
return true
|
|
||||||
case NextVersionTypeUndefined, NextVersionTypeNormal:
|
|
||||||
return false
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package releasepr
|
package rp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
@ -12,10 +12,8 @@ import (
|
||||||
"github.com/yuin/goldmark/ast"
|
"github.com/yuin/goldmark/ast"
|
||||||
"github.com/yuin/goldmark/text"
|
"github.com/yuin/goldmark/text"
|
||||||
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/git"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/markdown"
|
"github.com/apricote/releaser-pleaser/internal/markdown"
|
||||||
ast2 "github.com/apricote/releaser-pleaser/internal/markdown/extensions/ast"
|
east "github.com/apricote/releaser-pleaser/internal/markdown/extensions/ast"
|
||||||
"github.com/apricote/releaser-pleaser/internal/versioning"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -35,7 +33,7 @@ func init() {
|
||||||
|
|
||||||
// ReleasePullRequest
|
// ReleasePullRequest
|
||||||
//
|
//
|
||||||
// TODO: Reuse [git.PullRequest]
|
// TODO: Reuse [PullRequest]
|
||||||
type ReleasePullRequest struct {
|
type ReleasePullRequest struct {
|
||||||
ID int
|
ID int
|
||||||
Title string
|
Title string
|
||||||
|
|
@ -43,12 +41,9 @@ type ReleasePullRequest struct {
|
||||||
Labels []Label
|
Labels []Label
|
||||||
|
|
||||||
Head string
|
Head string
|
||||||
ReleaseCommit *git.Commit
|
ReleaseCommit *Commit
|
||||||
}
|
}
|
||||||
|
|
||||||
// Label is the string identifier of a pull/merge request label on the forge.
|
|
||||||
type Label string
|
|
||||||
|
|
||||||
func NewReleasePullRequest(head, branch, version, changelogEntry string) (*ReleasePullRequest, error) {
|
func NewReleasePullRequest(head, branch, version, changelogEntry string) (*ReleasePullRequest, error) {
|
||||||
rp := &ReleasePullRequest{
|
rp := &ReleasePullRequest{
|
||||||
Head: head,
|
Head: head,
|
||||||
|
|
@ -66,9 +61,50 @@ func NewReleasePullRequest(head, branch, version, changelogEntry string) (*Relea
|
||||||
type ReleaseOverrides struct {
|
type ReleaseOverrides struct {
|
||||||
Prefix string
|
Prefix string
|
||||||
Suffix string
|
Suffix string
|
||||||
NextVersionType versioning.NextVersionType
|
NextVersionType NextVersionType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NextVersionType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
NextVersionTypeUndefined NextVersionType = iota
|
||||||
|
NextVersionTypeNormal
|
||||||
|
NextVersionTypeRC
|
||||||
|
NextVersionTypeBeta
|
||||||
|
NextVersionTypeAlpha
|
||||||
|
)
|
||||||
|
|
||||||
|
func (n NextVersionType) String() string {
|
||||||
|
switch n {
|
||||||
|
case NextVersionTypeUndefined:
|
||||||
|
return "undefined"
|
||||||
|
case NextVersionTypeNormal:
|
||||||
|
return "normal"
|
||||||
|
case NextVersionTypeRC:
|
||||||
|
return "rc"
|
||||||
|
case NextVersionTypeBeta:
|
||||||
|
return "beta"
|
||||||
|
case NextVersionTypeAlpha:
|
||||||
|
return "alpha"
|
||||||
|
default:
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n NextVersionType) IsPrerelease() bool {
|
||||||
|
switch n {
|
||||||
|
case NextVersionTypeRC, NextVersionTypeBeta, NextVersionTypeAlpha:
|
||||||
|
return true
|
||||||
|
case NextVersionTypeUndefined, NextVersionTypeNormal:
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Label is the string identifier of a pull/merge request label on the forge.
|
||||||
|
type Label string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
LabelNextVersionTypeNormal Label = "rp-next-version::normal"
|
LabelNextVersionTypeNormal Label = "rp-next-version::normal"
|
||||||
LabelNextVersionTypeRC Label = "rp-next-version::rc"
|
LabelNextVersionTypeRC Label = "rp-next-version::rc"
|
||||||
|
|
@ -122,13 +158,13 @@ func (pr *ReleasePullRequest) parseVersioningFlags(overrides ReleaseOverrides) R
|
||||||
switch label {
|
switch label {
|
||||||
// Versioning
|
// Versioning
|
||||||
case LabelNextVersionTypeNormal:
|
case LabelNextVersionTypeNormal:
|
||||||
overrides.NextVersionType = versioning.NextVersionTypeNormal
|
overrides.NextVersionType = NextVersionTypeNormal
|
||||||
case LabelNextVersionTypeRC:
|
case LabelNextVersionTypeRC:
|
||||||
overrides.NextVersionType = versioning.NextVersionTypeRC
|
overrides.NextVersionType = NextVersionTypeRC
|
||||||
case LabelNextVersionTypeBeta:
|
case LabelNextVersionTypeBeta:
|
||||||
overrides.NextVersionType = versioning.NextVersionTypeBeta
|
overrides.NextVersionType = NextVersionTypeBeta
|
||||||
case LabelNextVersionTypeAlpha:
|
case LabelNextVersionTypeAlpha:
|
||||||
overrides.NextVersionType = versioning.NextVersionTypeAlpha
|
overrides.NextVersionType = NextVersionTypeAlpha
|
||||||
case LabelReleasePending, LabelReleaseTagged:
|
case LabelReleasePending, LabelReleaseTagged:
|
||||||
// These labels have no effect on the versioning.
|
// These labels have no effect on the versioning.
|
||||||
break
|
break
|
||||||
|
|
@ -177,18 +213,18 @@ func (pr *ReleasePullRequest) ChangelogText() (string, error) {
|
||||||
gm := markdown.New()
|
gm := markdown.New()
|
||||||
descriptionAST := gm.Parser().Parse(text.NewReader(source))
|
descriptionAST := gm.Parser().Parse(text.NewReader(source))
|
||||||
|
|
||||||
var section *ast2.Section
|
var section *east.Section
|
||||||
|
|
||||||
err := ast.Walk(descriptionAST, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
|
err := ast.Walk(descriptionAST, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
|
||||||
if !entering {
|
if !entering {
|
||||||
return ast.WalkContinue, nil
|
return ast.WalkContinue, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.Type() != ast.TypeBlock || n.Kind() != ast2.KindSection {
|
if n.Type() != ast.TypeBlock || n.Kind() != east.KindSection {
|
||||||
return ast.WalkContinue, nil
|
return ast.WalkContinue, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
anySection, ok := n.(*ast2.Section)
|
anySection, ok := n.(*east.Section)
|
||||||
if !ok {
|
if !ok {
|
||||||
return ast.WalkStop, fmt.Errorf("node has unexpected type: %T", n)
|
return ast.WalkStop, fmt.Errorf("node has unexpected type: %T", n)
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package releasepr
|
package rp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -3,15 +3,13 @@ package rp
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/changelog"
|
"github.com/go-git/go-git/v5"
|
||||||
"github.com/apricote/releaser-pleaser/internal/commitparser"
|
"github.com/go-git/go-git/v5/config"
|
||||||
"github.com/apricote/releaser-pleaser/internal/forge"
|
"github.com/go-git/go-git/v5/plumbing"
|
||||||
"github.com/apricote/releaser-pleaser/internal/git"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/releasepr"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/updater"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/versioning"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -19,16 +17,16 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type ReleaserPleaser struct {
|
type ReleaserPleaser struct {
|
||||||
forge forge.Forge
|
forge Forge
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
targetBranch string
|
targetBranch string
|
||||||
commitParser commitparser.CommitParser
|
commitParser CommitParser
|
||||||
nextVersion versioning.Strategy
|
nextVersion VersioningStrategy
|
||||||
extraFiles []string
|
extraFiles []string
|
||||||
updaters []updater.NewUpdater
|
updaters []Updater
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(forge forge.Forge, logger *slog.Logger, targetBranch string, commitParser commitparser.CommitParser, versioningStrategy versioning.Strategy, extraFiles []string, updaters []updater.NewUpdater) *ReleaserPleaser {
|
func New(forge Forge, logger *slog.Logger, targetBranch string, commitParser CommitParser, versioningStrategy VersioningStrategy, extraFiles []string, updaters []Updater) *ReleaserPleaser {
|
||||||
return &ReleaserPleaser{
|
return &ReleaserPleaser{
|
||||||
forge: forge,
|
forge: forge,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
|
|
@ -42,8 +40,7 @@ func New(forge forge.Forge, logger *slog.Logger, targetBranch string, commitPars
|
||||||
|
|
||||||
func (rp *ReleaserPleaser) EnsureLabels(ctx context.Context) error {
|
func (rp *ReleaserPleaser) EnsureLabels(ctx context.Context) error {
|
||||||
// TODO: Wrap Error
|
// TODO: Wrap Error
|
||||||
|
return rp.forge.EnsureLabelsExist(ctx, KnownLabels)
|
||||||
return rp.forge.EnsureLabelsExist(ctx, releasepr.KnownLabels)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rp *ReleaserPleaser) Run(ctx context.Context) error {
|
func (rp *ReleaserPleaser) Run(ctx context.Context) error {
|
||||||
|
|
@ -78,7 +75,7 @@ func (rp *ReleaserPleaser) runCreatePendingReleases(ctx context.Context) error {
|
||||||
logger := rp.logger.With("method", "runCreatePendingReleases")
|
logger := rp.logger.With("method", "runCreatePendingReleases")
|
||||||
|
|
||||||
logger.InfoContext(ctx, "checking for pending releases")
|
logger.InfoContext(ctx, "checking for pending releases")
|
||||||
prs, err := rp.forge.PendingReleases(ctx, releasepr.LabelReleasePending)
|
prs, err := rp.forge.PendingReleases(ctx, LabelReleasePending)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -100,7 +97,7 @@ func (rp *ReleaserPleaser) runCreatePendingReleases(ctx context.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rp *ReleaserPleaser) createPendingRelease(ctx context.Context, pr *releasepr.ReleasePullRequest) error {
|
func (rp *ReleaserPleaser) createPendingRelease(ctx context.Context, pr *ReleasePullRequest) error {
|
||||||
logger := rp.logger.With(
|
logger := rp.logger.With(
|
||||||
"method", "createPendingRelease",
|
"method", "createPendingRelease",
|
||||||
"pr.id", pr.ID,
|
"pr.id", pr.ID,
|
||||||
|
|
@ -132,7 +129,7 @@ func (rp *ReleaserPleaser) createPendingRelease(ctx context.Context, pr *release
|
||||||
logger.DebugContext(ctx, "created release", "release.title", version, "release.url", rp.forge.ReleaseURL(version))
|
logger.DebugContext(ctx, "created release", "release.title", version, "release.url", rp.forge.ReleaseURL(version))
|
||||||
|
|
||||||
logger.DebugContext(ctx, "updating pr labels")
|
logger.DebugContext(ctx, "updating pr labels")
|
||||||
err = rp.forge.SetPullRequestLabels(ctx, pr, []releasepr.Label{releasepr.LabelReleasePending}, []releasepr.Label{releasepr.LabelReleaseTagged})
|
err = rp.forge.SetPullRequestLabels(ctx, pr, []Label{LabelReleasePending}, []Label{LabelReleaseTagged})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -147,13 +144,14 @@ func (rp *ReleaserPleaser) runReconcileReleasePR(ctx context.Context) error {
|
||||||
logger := rp.logger.With("method", "runReconcileReleasePR")
|
logger := rp.logger.With("method", "runReconcileReleasePR")
|
||||||
|
|
||||||
rpBranch := fmt.Sprintf(PullRequestBranchFormat, rp.targetBranch)
|
rpBranch := fmt.Sprintf(PullRequestBranchFormat, rp.targetBranch)
|
||||||
|
rpBranchRef := plumbing.NewBranchReferenceName(rpBranch)
|
||||||
|
|
||||||
pr, err := rp.forge.PullRequestForBranch(ctx, rpBranch)
|
pr, err := rp.forge.PullRequestForBranch(ctx, rpBranch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var releaseOverrides releasepr.ReleaseOverrides
|
var releaseOverrides ReleaseOverrides
|
||||||
|
|
||||||
if pr != nil {
|
if pr != nil {
|
||||||
logger = logger.With("pr.id", pr.ID, "pr.title", pr.Title)
|
logger = logger.With("pr.id", pr.ID, "pr.title", pr.Title)
|
||||||
|
|
@ -217,7 +215,7 @@ func (rp *ReleaserPleaser) runReconcileReleasePR(ctx context.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
versionBump := versioning.BumpFromCommits(analyzedCommits)
|
versionBump := VersionBumpFromCommits(analyzedCommits)
|
||||||
// TODO: Set version in release pr
|
// TODO: Set version in release pr
|
||||||
nextVersion, err := rp.nextVersion(releases, versionBump, releaseOverrides.NextVersionType)
|
nextVersion, err := rp.nextVersion(releases, versionBump, releaseOverrides.NextVersionType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -226,68 +224,161 @@ func (rp *ReleaserPleaser) runReconcileReleasePR(ctx context.Context) error {
|
||||||
logger.InfoContext(ctx, "next version", "version", nextVersion)
|
logger.InfoContext(ctx, "next version", "version", nextVersion)
|
||||||
|
|
||||||
logger.DebugContext(ctx, "cloning repository", "clone.url", rp.forge.CloneURL())
|
logger.DebugContext(ctx, "cloning repository", "clone.url", rp.forge.CloneURL())
|
||||||
repo, err := git.CloneRepo(ctx, logger, rp.forge.CloneURL(), rp.targetBranch, rp.forge.GitAuth())
|
repo, err := CloneRepo(ctx, rp.forge.CloneURL(), rp.targetBranch, rp.forge.GitAuth())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to clone repository: %w", err)
|
return fmt.Errorf("failed to clone repository: %w", err)
|
||||||
}
|
}
|
||||||
|
worktree, err := repo.Worktree()
|
||||||
if err = repo.DeleteBranch(ctx, rpBranch); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = repo.Checkout(ctx, rpBranch); err != nil {
|
if branch, _ := repo.Branch(rpBranch); branch != nil {
|
||||||
return err
|
logger.DebugContext(ctx, "deleting previous releaser-pleaser branch locally", "branch.name", rpBranch)
|
||||||
|
if err = repo.DeleteBranch(rpBranch); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
changelogEntry, err := changelog.NewChangelogEntry(analyzedCommits, nextVersion, rp.forge.ReleaseURL(nextVersion), releaseOverrides.Prefix, releaseOverrides.Suffix)
|
if err = worktree.Checkout(&git.CheckoutOptions{
|
||||||
|
Branch: rpBranchRef,
|
||||||
|
Create: true,
|
||||||
|
}); err != nil {
|
||||||
|
return fmt.Errorf("failed to check out branch: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
changelogEntry, err := NewChangelogEntry(analyzedCommits, nextVersion, rp.forge.ReleaseURL(nextVersion), releaseOverrides.Prefix, releaseOverrides.Suffix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to build changelog entry: %w", err)
|
return fmt.Errorf("failed to build changelog entry: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Info for updaters
|
// Info for updaters
|
||||||
info := updater.ReleaseInfo{Version: nextVersion, ChangelogEntry: changelogEntry}
|
info := ReleaseInfo{Version: nextVersion, ChangelogEntry: changelogEntry}
|
||||||
|
|
||||||
err = repo.UpdateFile(ctx, updater.ChangelogFile, updater.WithInfo(info, updater.Changelog))
|
updateFile := func(path string, updaters []Updater) error {
|
||||||
|
file, err := worktree.Filesystem.OpenFile(path, os.O_RDWR, 0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
content, err := io.ReadAll(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
updatedContent := string(content)
|
||||||
|
|
||||||
|
for _, updater := range updaters {
|
||||||
|
updatedContent, err = updater.UpdateContent(updatedContent, info)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to run updater %T on file %s", updater, path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = file.Truncate(0)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to replace file content: %w", err)
|
||||||
|
}
|
||||||
|
_, err = file.Seek(0, 0)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to replace file content: %w", err)
|
||||||
|
}
|
||||||
|
_, err = file.Write([]byte(updatedContent))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to replace file content: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = worktree.Add(path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to add updated file to git worktree: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err = updateFile(ChangelogFile, []Updater{&ChangelogUpdater{}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to update changelog file: %w", err)
|
return fmt.Errorf("failed to update changelog file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, path := range rp.extraFiles {
|
for _, path := range rp.extraFiles {
|
||||||
// TODO: Check for missing files
|
_, err = worktree.Filesystem.Stat(path)
|
||||||
err = repo.UpdateFile(ctx, path, updater.WithInfo(info, rp.updaters...))
|
if err != nil {
|
||||||
|
// TODO: Check for non existing file or dirs
|
||||||
|
return fmt.Errorf("failed to run file updater because the file %s does not exist: %w", path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = updateFile(path, rp.updaters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to run file updater: %w", err)
|
return fmt.Errorf("failed to run file updater: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
releaseCommitMessage := fmt.Sprintf("chore(%s): release %s", rp.targetBranch, nextVersion)
|
releaseCommitMessage := fmt.Sprintf("chore(%s): release %s", rp.targetBranch, nextVersion)
|
||||||
releaseCommit, err := repo.Commit(ctx, releaseCommitMessage)
|
releaseCommitHash, err := worktree.Commit(releaseCommitMessage, &git.CommitOptions{
|
||||||
|
Author: GitSignature(),
|
||||||
|
Committer: GitSignature(),
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to commit changes: %w", err)
|
return fmt.Errorf("failed to commit changes: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.InfoContext(ctx, "created release commit", "commit.hash", releaseCommit.Hash, "commit.message", releaseCommit.Message)
|
logger.InfoContext(ctx, "created release commit", "commit.hash", releaseCommitHash.String(), "commit.message", releaseCommitMessage)
|
||||||
|
|
||||||
|
newReleasePRChanges := true
|
||||||
|
|
||||||
// Check if anything changed in comparison to the remote branch (if exists)
|
// Check if anything changed in comparison to the remote branch (if exists)
|
||||||
newReleasePRChanges, err := repo.HasChangesWithRemote(ctx, rpBranch)
|
if remoteRef, err := repo.Reference(plumbing.NewRemoteReferenceName(GitRemoteName, rpBranch), false); err != nil {
|
||||||
if err != nil {
|
if err.Error() != "reference not found" {
|
||||||
return err
|
// "reference not found" is expected and we should always push
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
remoteCommit, err := repo.CommitObject(remoteRef.Hash())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
localCommit, err := repo.CommitObject(releaseCommitHash)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
diff, err := localCommit.PatchContext(ctx, remoteCommit)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
newReleasePRChanges = len(diff.FilePatches()) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
if newReleasePRChanges {
|
if newReleasePRChanges {
|
||||||
err = repo.ForcePush(ctx, rpBranch)
|
pushRefSpec := config.RefSpec(fmt.Sprintf(
|
||||||
if err != nil {
|
"+%s:%s",
|
||||||
|
rpBranchRef,
|
||||||
|
// This needs to be the local branch name, not the remotes/origin ref
|
||||||
|
// See https://stackoverflow.com/a/75727620
|
||||||
|
rpBranchRef,
|
||||||
|
))
|
||||||
|
logger.DebugContext(ctx, "pushing branch", "commit.hash", releaseCommitHash.String(), "branch.name", rpBranch, "refspec", pushRefSpec.String())
|
||||||
|
if err = repo.PushContext(ctx, &git.PushOptions{
|
||||||
|
RemoteName: GitRemoteName,
|
||||||
|
RefSpecs: []config.RefSpec{pushRefSpec},
|
||||||
|
Force: true,
|
||||||
|
Auth: rp.forge.GitAuth(),
|
||||||
|
}); err != nil {
|
||||||
return fmt.Errorf("failed to push branch: %w", err)
|
return fmt.Errorf("failed to push branch: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.InfoContext(ctx, "pushed branch", "commit.hash", releaseCommit.Hash, "branch.name", rpBranch)
|
logger.InfoContext(ctx, "pushed branch", "commit.hash", releaseCommitHash.String(), "branch.name", rpBranch, "refspec", pushRefSpec.String())
|
||||||
} else {
|
} else {
|
||||||
logger.InfoContext(ctx, "file content is already up-to-date in remote branch, skipping push")
|
logger.InfoContext(ctx, "file content is already up-to-date in remote branch, skipping push")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open/Update PR
|
// Open/Update PR
|
||||||
if pr == nil {
|
if pr == nil {
|
||||||
pr, err = releasepr.NewReleasePullRequest(rpBranch, rp.targetBranch, nextVersion, changelogEntry)
|
pr, err = NewReleasePullRequest(rpBranch, rp.targetBranch, nextVersion, changelogEntry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
47
updater.go
Normal file
47
updater.go
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
package rp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
GenericUpdaterSemVerRegex = regexp.MustCompile(`\d+\.\d+\.\d+(-[\w.]+)?(.*x-releaser-pleaser-version)`)
|
||||||
|
ChangelogUpdaterHeaderRegex = regexp.MustCompile(`^# Changelog\n`)
|
||||||
|
)
|
||||||
|
|
||||||
|
type ReleaseInfo struct {
|
||||||
|
Version string
|
||||||
|
ChangelogEntry string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Updater interface {
|
||||||
|
UpdateContent(content string, info ReleaseInfo) (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type GenericUpdater struct{}
|
||||||
|
|
||||||
|
func (u *GenericUpdater) UpdateContent(content string, info ReleaseInfo) (string, error) {
|
||||||
|
// We strip the "v" prefix to avoid adding/removing it from the users input.
|
||||||
|
version := strings.TrimPrefix(info.Version, "v")
|
||||||
|
|
||||||
|
return GenericUpdaterSemVerRegex.ReplaceAllString(content, version+"${2}"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type ChangelogUpdater struct{}
|
||||||
|
|
||||||
|
func (u *ChangelogUpdater) UpdateContent(content string, info ReleaseInfo) (string, error) {
|
||||||
|
headerIndex := ChangelogUpdaterHeaderRegex.FindStringIndex(content)
|
||||||
|
if headerIndex == nil && len(content) != 0 {
|
||||||
|
return "", fmt.Errorf("unexpected format of CHANGELOG.md, header does not match")
|
||||||
|
}
|
||||||
|
if headerIndex != nil {
|
||||||
|
// Remove the header from the content
|
||||||
|
content = content[headerIndex[1]:]
|
||||||
|
}
|
||||||
|
|
||||||
|
content = ChangelogHeader + "\n\n" + info.ChangelogEntry + content
|
||||||
|
|
||||||
|
return content, nil
|
||||||
|
}
|
||||||
129
updater_test.go
Normal file
129
updater_test.go
Normal file
|
|
@ -0,0 +1,129 @@
|
||||||
|
package rp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
type updaterTestCase struct {
|
||||||
|
name string
|
||||||
|
content string
|
||||||
|
info ReleaseInfo
|
||||||
|
want string
|
||||||
|
wantErr assert.ErrorAssertionFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func runUpdaterTest(t *testing.T, updater Updater, tt updaterTestCase) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
got, err := updater.UpdateContent(tt.content, tt.info)
|
||||||
|
if !tt.wantErr(t, err, fmt.Sprintf("UpdateContent(%v, %v)", tt.content, tt.info)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
assert.Equalf(t, tt.want, got, "UpdateContent(%v, %v)", tt.content, tt.info)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGenericUpdater_UpdateContent(t *testing.T) {
|
||||||
|
updater := &GenericUpdater{}
|
||||||
|
|
||||||
|
tests := []updaterTestCase{
|
||||||
|
{
|
||||||
|
name: "single line",
|
||||||
|
content: "v1.0.0 // x-releaser-pleaser-version",
|
||||||
|
info: ReleaseInfo{
|
||||||
|
Version: "v1.2.0",
|
||||||
|
},
|
||||||
|
want: "v1.2.0 // x-releaser-pleaser-version",
|
||||||
|
wantErr: assert.NoError,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multiline line",
|
||||||
|
content: "Foooo\n\v1.2.0\nv1.0.0 // x-releaser-pleaser-version\n",
|
||||||
|
info: ReleaseInfo{
|
||||||
|
Version: "v1.2.0",
|
||||||
|
},
|
||||||
|
want: "Foooo\n\v1.2.0\nv1.2.0 // x-releaser-pleaser-version\n",
|
||||||
|
wantErr: assert.NoError,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid existing version",
|
||||||
|
content: "1.0 // x-releaser-pleaser-version",
|
||||||
|
info: ReleaseInfo{
|
||||||
|
Version: "v1.2.0",
|
||||||
|
},
|
||||||
|
want: "1.0 // x-releaser-pleaser-version",
|
||||||
|
wantErr: assert.NoError,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "complicated line",
|
||||||
|
content: "version: v1.2.0-alpha.1 => Awesome, isnt it? x-releaser-pleaser-version foobar",
|
||||||
|
info: ReleaseInfo{
|
||||||
|
Version: "v1.2.0",
|
||||||
|
},
|
||||||
|
want: "version: v1.2.0 => Awesome, isnt it? x-releaser-pleaser-version foobar",
|
||||||
|
wantErr: assert.NoError,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
runUpdaterTest(t, updater, tt)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChangelogUpdater_UpdateContent(t *testing.T) {
|
||||||
|
updater := &ChangelogUpdater{}
|
||||||
|
|
||||||
|
tests := []updaterTestCase{
|
||||||
|
{
|
||||||
|
name: "empty file",
|
||||||
|
content: "",
|
||||||
|
info: ReleaseInfo{ChangelogEntry: "## v1.0.0\n"},
|
||||||
|
want: "# Changelog\n\n## v1.0.0\n",
|
||||||
|
wantErr: assert.NoError,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "well-formatted changelog",
|
||||||
|
content: `# Changelog
|
||||||
|
|
||||||
|
## v0.0.1
|
||||||
|
|
||||||
|
- Bazzle
|
||||||
|
|
||||||
|
## v0.1.0
|
||||||
|
|
||||||
|
### Bazuuum
|
||||||
|
`,
|
||||||
|
info: ReleaseInfo{ChangelogEntry: "## v1.0.0\n\n- Version 1, juhu.\n"},
|
||||||
|
want: `# Changelog
|
||||||
|
|
||||||
|
## v1.0.0
|
||||||
|
|
||||||
|
- Version 1, juhu.
|
||||||
|
|
||||||
|
## v0.0.1
|
||||||
|
|
||||||
|
- Bazzle
|
||||||
|
|
||||||
|
## v0.1.0
|
||||||
|
|
||||||
|
### Bazuuum
|
||||||
|
`,
|
||||||
|
wantErr: assert.NoError,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "error on invalid header",
|
||||||
|
content: "What even is this file?",
|
||||||
|
info: ReleaseInfo{ChangelogEntry: "## v1.0.0\n\n- Version 1, juhu.\n"},
|
||||||
|
want: "",
|
||||||
|
wantErr: assert.Error,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
runUpdaterTest(t, updater, tt)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,18 +1,23 @@
|
||||||
package versioning
|
package rp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/blang/semver/v4"
|
"github.com/blang/semver/v4"
|
||||||
|
"github.com/leodido/go-conventionalcommits"
|
||||||
"github.com/apricote/releaser-pleaser/internal/commitparser"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/git"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ Strategy = SemVerNextVersion
|
type Releases struct {
|
||||||
|
Latest *Tag
|
||||||
|
Stable *Tag
|
||||||
|
}
|
||||||
|
|
||||||
func SemVerNextVersion(r git.Releases, versionBump VersionBump, nextVersionType NextVersionType) (string, error) {
|
type VersioningStrategy = func(Releases, conventionalcommits.VersionBump, NextVersionType) (string, error)
|
||||||
|
|
||||||
|
var _ VersioningStrategy = SemVerNextVersion
|
||||||
|
|
||||||
|
func SemVerNextVersion(r Releases, versionBump conventionalcommits.VersionBump, nextVersionType NextVersionType) (string, error) {
|
||||||
latest, err := parseSemverWithDefault(r.Latest)
|
latest, err := parseSemverWithDefault(r.Latest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to parse latest version: %w", err)
|
return "", fmt.Errorf("failed to parse latest version: %w", err)
|
||||||
|
|
@ -31,13 +36,13 @@ func SemVerNextVersion(r git.Releases, versionBump VersionBump, nextVersionType
|
||||||
}
|
}
|
||||||
|
|
||||||
switch versionBump {
|
switch versionBump {
|
||||||
case UnknownVersion:
|
case conventionalcommits.UnknownVersion:
|
||||||
return "", fmt.Errorf("invalid latest bump (unknown)")
|
return "", fmt.Errorf("invalid latest bump (unknown)")
|
||||||
case PatchVersion:
|
case conventionalcommits.PatchVersion:
|
||||||
err = next.IncrementPatch()
|
err = next.IncrementPatch()
|
||||||
case MinorVersion:
|
case conventionalcommits.MinorVersion:
|
||||||
err = next.IncrementMinor()
|
err = next.IncrementMinor()
|
||||||
case MajorVersion:
|
case conventionalcommits.MajorVersion:
|
||||||
err = next.IncrementMajor()
|
err = next.IncrementMajor()
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -63,18 +68,18 @@ func SemVerNextVersion(r git.Releases, versionBump VersionBump, nextVersionType
|
||||||
return "v" + next.String(), nil
|
return "v" + next.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func BumpFromCommits(commits []commitparser.AnalyzedCommit) VersionBump {
|
func VersionBumpFromCommits(commits []AnalyzedCommit) conventionalcommits.VersionBump {
|
||||||
bump := UnknownVersion
|
bump := conventionalcommits.UnknownVersion
|
||||||
|
|
||||||
for _, commit := range commits {
|
for _, commit := range commits {
|
||||||
entryBump := UnknownVersion
|
entryBump := conventionalcommits.UnknownVersion
|
||||||
switch {
|
switch {
|
||||||
case commit.BreakingChange:
|
case commit.BreakingChange:
|
||||||
entryBump = MajorVersion
|
entryBump = conventionalcommits.MajorVersion
|
||||||
case commit.Type == "feat":
|
case commit.Type == "feat":
|
||||||
entryBump = MinorVersion
|
entryBump = conventionalcommits.MinorVersion
|
||||||
case commit.Type == "fix":
|
case commit.Type == "fix":
|
||||||
entryBump = PatchVersion
|
entryBump = conventionalcommits.PatchVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
if entryBump > bump {
|
if entryBump > bump {
|
||||||
|
|
@ -92,7 +97,7 @@ func setPRVersion(version *semver.Version, prType string, count uint64) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseSemverWithDefault(tag *git.Tag) (semver.Version, error) {
|
func parseSemverWithDefault(tag *Tag) (semver.Version, error) {
|
||||||
version := "v0.0.0"
|
version := "v0.0.0"
|
||||||
if tag != nil {
|
if tag != nil {
|
||||||
version = tag.Name
|
version = tag.Name
|
||||||
|
|
@ -1,19 +1,17 @@
|
||||||
package versioning
|
package rp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/leodido/go-conventionalcommits"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/commitparser"
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/git"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestReleases_NextVersion(t *testing.T) {
|
func TestReleases_NextVersion(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
releases git.Releases
|
releases Releases
|
||||||
versionBump VersionBump
|
versionBump conventionalcommits.VersionBump
|
||||||
nextVersionType NextVersionType
|
nextVersionType NextVersionType
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
|
@ -25,11 +23,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "simple bump (major)",
|
name: "simple bump (major)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.1.1"},
|
Latest: &Tag{Name: "v1.1.1"},
|
||||||
Stable: &git.Tag{Name: "v1.1.1"},
|
Stable: &Tag{Name: "v1.1.1"},
|
||||||
},
|
},
|
||||||
versionBump: MajorVersion,
|
versionBump: conventionalcommits.MajorVersion,
|
||||||
nextVersionType: NextVersionTypeUndefined,
|
nextVersionType: NextVersionTypeUndefined,
|
||||||
},
|
},
|
||||||
want: "v2.0.0",
|
want: "v2.0.0",
|
||||||
|
|
@ -38,11 +36,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "simple bump (minor)",
|
name: "simple bump (minor)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.1.1"},
|
Latest: &Tag{Name: "v1.1.1"},
|
||||||
Stable: &git.Tag{Name: "v1.1.1"},
|
Stable: &Tag{Name: "v1.1.1"},
|
||||||
},
|
},
|
||||||
versionBump: MinorVersion,
|
versionBump: conventionalcommits.MinorVersion,
|
||||||
nextVersionType: NextVersionTypeUndefined,
|
nextVersionType: NextVersionTypeUndefined,
|
||||||
},
|
},
|
||||||
want: "v1.2.0",
|
want: "v1.2.0",
|
||||||
|
|
@ -51,11 +49,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "simple bump (patch)",
|
name: "simple bump (patch)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.1.1"},
|
Latest: &Tag{Name: "v1.1.1"},
|
||||||
Stable: &git.Tag{Name: "v1.1.1"},
|
Stable: &Tag{Name: "v1.1.1"},
|
||||||
},
|
},
|
||||||
versionBump: PatchVersion,
|
versionBump: conventionalcommits.PatchVersion,
|
||||||
nextVersionType: NextVersionTypeUndefined,
|
nextVersionType: NextVersionTypeUndefined,
|
||||||
},
|
},
|
||||||
want: "v1.1.2",
|
want: "v1.1.2",
|
||||||
|
|
@ -64,11 +62,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "normal to prerelease (major)",
|
name: "normal to prerelease (major)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.1.1"},
|
Latest: &Tag{Name: "v1.1.1"},
|
||||||
Stable: &git.Tag{Name: "v1.1.1"},
|
Stable: &Tag{Name: "v1.1.1"},
|
||||||
},
|
},
|
||||||
versionBump: MajorVersion,
|
versionBump: conventionalcommits.MajorVersion,
|
||||||
nextVersionType: NextVersionTypeRC,
|
nextVersionType: NextVersionTypeRC,
|
||||||
},
|
},
|
||||||
want: "v2.0.0-rc.0",
|
want: "v2.0.0-rc.0",
|
||||||
|
|
@ -77,11 +75,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "normal to prerelease (minor)",
|
name: "normal to prerelease (minor)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.1.1"},
|
Latest: &Tag{Name: "v1.1.1"},
|
||||||
Stable: &git.Tag{Name: "v1.1.1"},
|
Stable: &Tag{Name: "v1.1.1"},
|
||||||
},
|
},
|
||||||
versionBump: MinorVersion,
|
versionBump: conventionalcommits.MinorVersion,
|
||||||
nextVersionType: NextVersionTypeRC,
|
nextVersionType: NextVersionTypeRC,
|
||||||
},
|
},
|
||||||
want: "v1.2.0-rc.0",
|
want: "v1.2.0-rc.0",
|
||||||
|
|
@ -90,11 +88,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "normal to prerelease (patch)",
|
name: "normal to prerelease (patch)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.1.1"},
|
Latest: &Tag{Name: "v1.1.1"},
|
||||||
Stable: &git.Tag{Name: "v1.1.1"},
|
Stable: &Tag{Name: "v1.1.1"},
|
||||||
},
|
},
|
||||||
versionBump: PatchVersion,
|
versionBump: conventionalcommits.PatchVersion,
|
||||||
nextVersionType: NextVersionTypeRC,
|
nextVersionType: NextVersionTypeRC,
|
||||||
},
|
},
|
||||||
want: "v1.1.2-rc.0",
|
want: "v1.1.2-rc.0",
|
||||||
|
|
@ -103,11 +101,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "prerelease bump (major)",
|
name: "prerelease bump (major)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v2.0.0-rc.0"},
|
Latest: &Tag{Name: "v2.0.0-rc.0"},
|
||||||
Stable: &git.Tag{Name: "v1.1.1"},
|
Stable: &Tag{Name: "v1.1.1"},
|
||||||
},
|
},
|
||||||
versionBump: MajorVersion,
|
versionBump: conventionalcommits.MajorVersion,
|
||||||
nextVersionType: NextVersionTypeRC,
|
nextVersionType: NextVersionTypeRC,
|
||||||
},
|
},
|
||||||
want: "v2.0.0-rc.1",
|
want: "v2.0.0-rc.1",
|
||||||
|
|
@ -116,11 +114,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "prerelease bump (minor)",
|
name: "prerelease bump (minor)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.2.0-rc.0"},
|
Latest: &Tag{Name: "v1.2.0-rc.0"},
|
||||||
Stable: &git.Tag{Name: "v1.1.1"},
|
Stable: &Tag{Name: "v1.1.1"},
|
||||||
},
|
},
|
||||||
versionBump: MinorVersion,
|
versionBump: conventionalcommits.MinorVersion,
|
||||||
nextVersionType: NextVersionTypeRC,
|
nextVersionType: NextVersionTypeRC,
|
||||||
},
|
},
|
||||||
want: "v1.2.0-rc.1",
|
want: "v1.2.0-rc.1",
|
||||||
|
|
@ -129,11 +127,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "prerelease bump (patch)",
|
name: "prerelease bump (patch)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.1.2-rc.0"},
|
Latest: &Tag{Name: "v1.1.2-rc.0"},
|
||||||
Stable: &git.Tag{Name: "v1.1.1"},
|
Stable: &Tag{Name: "v1.1.1"},
|
||||||
},
|
},
|
||||||
versionBump: PatchVersion,
|
versionBump: conventionalcommits.PatchVersion,
|
||||||
nextVersionType: NextVersionTypeRC,
|
nextVersionType: NextVersionTypeRC,
|
||||||
},
|
},
|
||||||
want: "v1.1.2-rc.1",
|
want: "v1.1.2-rc.1",
|
||||||
|
|
@ -142,11 +140,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "prerelease different bump (major)",
|
name: "prerelease different bump (major)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.2.0-rc.0"},
|
Latest: &Tag{Name: "v1.2.0-rc.0"},
|
||||||
Stable: &git.Tag{Name: "v1.1.1"},
|
Stable: &Tag{Name: "v1.1.1"},
|
||||||
},
|
},
|
||||||
versionBump: MajorVersion,
|
versionBump: conventionalcommits.MajorVersion,
|
||||||
nextVersionType: NextVersionTypeRC,
|
nextVersionType: NextVersionTypeRC,
|
||||||
},
|
},
|
||||||
want: "v2.0.0-rc.1",
|
want: "v2.0.0-rc.1",
|
||||||
|
|
@ -155,11 +153,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "prerelease different bump (minor)",
|
name: "prerelease different bump (minor)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.1.2-rc.0"},
|
Latest: &Tag{Name: "v1.1.2-rc.0"},
|
||||||
Stable: &git.Tag{Name: "v1.1.1"},
|
Stable: &Tag{Name: "v1.1.1"},
|
||||||
},
|
},
|
||||||
versionBump: MinorVersion,
|
versionBump: conventionalcommits.MinorVersion,
|
||||||
nextVersionType: NextVersionTypeRC,
|
nextVersionType: NextVersionTypeRC,
|
||||||
},
|
},
|
||||||
want: "v1.2.0-rc.1",
|
want: "v1.2.0-rc.1",
|
||||||
|
|
@ -168,11 +166,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "prerelease to prerelease",
|
name: "prerelease to prerelease",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.1.1-alpha.2"},
|
Latest: &Tag{Name: "v1.1.1-alpha.2"},
|
||||||
Stable: &git.Tag{Name: "v1.1.0"},
|
Stable: &Tag{Name: "v1.1.0"},
|
||||||
},
|
},
|
||||||
versionBump: PatchVersion,
|
versionBump: conventionalcommits.PatchVersion,
|
||||||
nextVersionType: NextVersionTypeRC,
|
nextVersionType: NextVersionTypeRC,
|
||||||
},
|
},
|
||||||
want: "v1.1.1-rc.0",
|
want: "v1.1.1-rc.0",
|
||||||
|
|
@ -181,11 +179,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "prerelease to normal (explicit)",
|
name: "prerelease to normal (explicit)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.1.1-alpha.2"},
|
Latest: &Tag{Name: "v1.1.1-alpha.2"},
|
||||||
Stable: &git.Tag{Name: "v1.1.0"},
|
Stable: &Tag{Name: "v1.1.0"},
|
||||||
},
|
},
|
||||||
versionBump: PatchVersion,
|
versionBump: conventionalcommits.PatchVersion,
|
||||||
nextVersionType: NextVersionTypeNormal,
|
nextVersionType: NextVersionTypeNormal,
|
||||||
},
|
},
|
||||||
want: "v1.1.1",
|
want: "v1.1.1",
|
||||||
|
|
@ -194,11 +192,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "prerelease to normal (implicit)",
|
name: "prerelease to normal (implicit)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.1.1-alpha.2"},
|
Latest: &Tag{Name: "v1.1.1-alpha.2"},
|
||||||
Stable: &git.Tag{Name: "v1.1.0"},
|
Stable: &Tag{Name: "v1.1.0"},
|
||||||
},
|
},
|
||||||
versionBump: PatchVersion,
|
versionBump: conventionalcommits.PatchVersion,
|
||||||
nextVersionType: NextVersionTypeUndefined,
|
nextVersionType: NextVersionTypeUndefined,
|
||||||
},
|
},
|
||||||
want: "v1.1.1",
|
want: "v1.1.1",
|
||||||
|
|
@ -207,11 +205,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "nil tag (major)",
|
name: "nil tag (major)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: nil,
|
Latest: nil,
|
||||||
Stable: nil,
|
Stable: nil,
|
||||||
},
|
},
|
||||||
versionBump: MajorVersion,
|
versionBump: conventionalcommits.MajorVersion,
|
||||||
nextVersionType: NextVersionTypeUndefined,
|
nextVersionType: NextVersionTypeUndefined,
|
||||||
},
|
},
|
||||||
want: "v1.0.0",
|
want: "v1.0.0",
|
||||||
|
|
@ -220,11 +218,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "nil tag (minor)",
|
name: "nil tag (minor)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: nil,
|
Latest: nil,
|
||||||
Stable: nil,
|
Stable: nil,
|
||||||
},
|
},
|
||||||
versionBump: MinorVersion,
|
versionBump: conventionalcommits.MinorVersion,
|
||||||
nextVersionType: NextVersionTypeUndefined,
|
nextVersionType: NextVersionTypeUndefined,
|
||||||
},
|
},
|
||||||
want: "v0.1.0",
|
want: "v0.1.0",
|
||||||
|
|
@ -233,11 +231,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "nil tag (patch)",
|
name: "nil tag (patch)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: nil,
|
Latest: nil,
|
||||||
Stable: nil,
|
Stable: nil,
|
||||||
},
|
},
|
||||||
versionBump: PatchVersion,
|
versionBump: conventionalcommits.PatchVersion,
|
||||||
nextVersionType: NextVersionTypeUndefined,
|
nextVersionType: NextVersionTypeUndefined,
|
||||||
},
|
},
|
||||||
want: "v0.0.1",
|
want: "v0.0.1",
|
||||||
|
|
@ -246,11 +244,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "nil stable release (major)",
|
name: "nil stable release (major)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.1.1-rc.0"},
|
Latest: &Tag{Name: "v1.1.1-rc.0"},
|
||||||
Stable: nil,
|
Stable: nil,
|
||||||
},
|
},
|
||||||
versionBump: MajorVersion,
|
versionBump: conventionalcommits.MajorVersion,
|
||||||
nextVersionType: NextVersionTypeUndefined,
|
nextVersionType: NextVersionTypeUndefined,
|
||||||
},
|
},
|
||||||
want: "v2.0.0",
|
want: "v2.0.0",
|
||||||
|
|
@ -259,11 +257,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "nil stable release (minor)",
|
name: "nil stable release (minor)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.1.1-rc.0"},
|
Latest: &Tag{Name: "v1.1.1-rc.0"},
|
||||||
Stable: nil,
|
Stable: nil,
|
||||||
},
|
},
|
||||||
versionBump: MinorVersion,
|
versionBump: conventionalcommits.MinorVersion,
|
||||||
nextVersionType: NextVersionTypeUndefined,
|
nextVersionType: NextVersionTypeUndefined,
|
||||||
},
|
},
|
||||||
want: "v1.2.0",
|
want: "v1.2.0",
|
||||||
|
|
@ -272,11 +270,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "nil stable release (patch)",
|
name: "nil stable release (patch)",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.1.1-rc.0"},
|
Latest: &Tag{Name: "v1.1.1-rc.0"},
|
||||||
Stable: nil,
|
Stable: nil,
|
||||||
},
|
},
|
||||||
versionBump: PatchVersion,
|
versionBump: conventionalcommits.PatchVersion,
|
||||||
nextVersionType: NextVersionTypeUndefined,
|
nextVersionType: NextVersionTypeUndefined,
|
||||||
},
|
},
|
||||||
// TODO: Is this actually correct our should it be v1.1.1?
|
// TODO: Is this actually correct our should it be v1.1.1?
|
||||||
|
|
@ -286,11 +284,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "error on invalid tag semver",
|
name: "error on invalid tag semver",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "foodazzle"},
|
Latest: &Tag{Name: "foodazzle"},
|
||||||
Stable: &git.Tag{Name: "foodazzle"},
|
Stable: &Tag{Name: "foodazzle"},
|
||||||
},
|
},
|
||||||
versionBump: PatchVersion,
|
versionBump: conventionalcommits.PatchVersion,
|
||||||
nextVersionType: NextVersionTypeRC,
|
nextVersionType: NextVersionTypeRC,
|
||||||
},
|
},
|
||||||
want: "",
|
want: "",
|
||||||
|
|
@ -299,11 +297,11 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "error on invalid tag prerelease",
|
name: "error on invalid tag prerelease",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.1.1-rc.foo"},
|
Latest: &Tag{Name: "v1.1.1-rc.foo"},
|
||||||
Stable: &git.Tag{Name: "v1.1.1-rc.foo"},
|
Stable: &Tag{Name: "v1.1.1-rc.foo"},
|
||||||
},
|
},
|
||||||
versionBump: PatchVersion,
|
versionBump: conventionalcommits.PatchVersion,
|
||||||
nextVersionType: NextVersionTypeRC,
|
nextVersionType: NextVersionTypeRC,
|
||||||
},
|
},
|
||||||
want: "",
|
want: "",
|
||||||
|
|
@ -312,12 +310,12 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "error on invalid bump",
|
name: "error on invalid bump",
|
||||||
args: args{
|
args: args{
|
||||||
releases: git.Releases{
|
releases: Releases{
|
||||||
Latest: &git.Tag{Name: "v1.1.1"},
|
Latest: &Tag{Name: "v1.1.1"},
|
||||||
Stable: &git.Tag{Name: "v1.1.1"},
|
Stable: &Tag{Name: "v1.1.1"},
|
||||||
},
|
},
|
||||||
|
|
||||||
versionBump: UnknownVersion,
|
versionBump: conventionalcommits.UnknownVersion,
|
||||||
nextVersionType: NextVersionTypeUndefined,
|
nextVersionType: NextVersionTypeUndefined,
|
||||||
},
|
},
|
||||||
want: "",
|
want: "",
|
||||||
|
|
@ -338,53 +336,53 @@ func TestReleases_NextVersion(t *testing.T) {
|
||||||
func TestVersionBumpFromCommits(t *testing.T) {
|
func TestVersionBumpFromCommits(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
analyzedCommits []commitparser.AnalyzedCommit
|
analyzedCommits []AnalyzedCommit
|
||||||
want VersionBump
|
want conventionalcommits.VersionBump
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "no entries (unknown)",
|
name: "no entries (unknown)",
|
||||||
analyzedCommits: []commitparser.AnalyzedCommit{},
|
analyzedCommits: []AnalyzedCommit{},
|
||||||
want: UnknownVersion,
|
want: conventionalcommits.UnknownVersion,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "non-release type (unknown)",
|
name: "non-release type (unknown)",
|
||||||
analyzedCommits: []commitparser.AnalyzedCommit{{Type: "docs"}},
|
analyzedCommits: []AnalyzedCommit{{Type: "docs"}},
|
||||||
want: UnknownVersion,
|
want: conventionalcommits.UnknownVersion,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "single breaking (major)",
|
name: "single breaking (major)",
|
||||||
analyzedCommits: []commitparser.AnalyzedCommit{{BreakingChange: true}},
|
analyzedCommits: []AnalyzedCommit{{BreakingChange: true}},
|
||||||
want: MajorVersion,
|
want: conventionalcommits.MajorVersion,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "single feat (minor)",
|
name: "single feat (minor)",
|
||||||
analyzedCommits: []commitparser.AnalyzedCommit{{Type: "feat"}},
|
analyzedCommits: []AnalyzedCommit{{Type: "feat"}},
|
||||||
want: MinorVersion,
|
want: conventionalcommits.MinorVersion,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "single fix (patch)",
|
name: "single fix (patch)",
|
||||||
analyzedCommits: []commitparser.AnalyzedCommit{{Type: "fix"}},
|
analyzedCommits: []AnalyzedCommit{{Type: "fix"}},
|
||||||
want: PatchVersion,
|
want: conventionalcommits.PatchVersion,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "multiple entries (major)",
|
name: "multiple entries (major)",
|
||||||
analyzedCommits: []commitparser.AnalyzedCommit{{Type: "fix"}, {BreakingChange: true}},
|
analyzedCommits: []AnalyzedCommit{{Type: "fix"}, {BreakingChange: true}},
|
||||||
want: MajorVersion,
|
want: conventionalcommits.MajorVersion,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "multiple entries (minor)",
|
name: "multiple entries (minor)",
|
||||||
analyzedCommits: []commitparser.AnalyzedCommit{{Type: "fix"}, {Type: "feat"}},
|
analyzedCommits: []AnalyzedCommit{{Type: "fix"}, {Type: "feat"}},
|
||||||
want: MinorVersion,
|
want: conventionalcommits.MinorVersion,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "multiple entries (patch)",
|
name: "multiple entries (patch)",
|
||||||
analyzedCommits: []commitparser.AnalyzedCommit{{Type: "docs"}, {Type: "fix"}},
|
analyzedCommits: []AnalyzedCommit{{Type: "docs"}, {Type: "fix"}},
|
||||||
want: PatchVersion,
|
want: conventionalcommits.PatchVersion,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
assert.Equalf(t, tt.want, BumpFromCommits(tt.analyzedCommits), "BumpFromCommits(%v)", tt.analyzedCommits)
|
assert.Equalf(t, tt.want, VersionBumpFromCommits(tt.analyzedCommits), "VersionBumpFromCommits(%v)", tt.analyzedCommits)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue