Compare commits

..

1 commit

Author SHA1 Message Date
releaser-pleaser
3705fe7c82 chore(main): release v0.4.0 2024-09-07 19:55:03 +00:00
6 changed files with 24 additions and 39 deletions

View file

@ -9,8 +9,6 @@
### Bug Fixes ### Bug Fixes
- **parser**: continue on unparsable commit message (#48) - **parser**: continue on unparsable commit message (#48)
- **cli**: command name in help output (#52)
- **parser**: invalid handling of empty lines (#53)
## [v0.3.0](https://github.com/apricote/releaser-pleaser/releases/tag/v0.3.0) ## [v0.3.0](https://github.com/apricote/releaser-pleaser/releases/tag/v0.3.0)

View file

@ -11,7 +11,7 @@ import (
var logger *slog.Logger var logger *slog.Logger
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "rp", Use: "releaser-pleaser",
Short: "", Short: "",
Long: ``, Long: ``,
Version: version(), Version: version(),

View file

@ -62,7 +62,8 @@ func run(cmd *cobra.Command, _ []string) error {
logger.DebugContext(ctx, "using forge GitLab") logger.DebugContext(ctx, "using forge GitLab")
f, err = gitlab.New(logger, &gitlab.Options{ f, err = gitlab.New(logger, &gitlab.Options{
Options: forgeOptions, Options: forgeOptions,
Path: fmt.Sprintf("%s/%s", flagOwner, flagRepo), Path: flagOwner,
Repo: flagRepo,
}) })
if err != nil { if err != nil {
logger.ErrorContext(ctx, "failed to create client", "err", err) logger.ErrorContext(ctx, "failed to create client", "err", err)

View file

@ -3,7 +3,6 @@ package conventionalcommits
import ( import (
"fmt" "fmt"
"log/slog" "log/slog"
"strings"
"github.com/leodido/go-conventionalcommits" "github.com/leodido/go-conventionalcommits"
"github.com/leodido/go-conventionalcommits/parser" "github.com/leodido/go-conventionalcommits/parser"
@ -33,7 +32,7 @@ func (c *Parser) Analyze(commits []git.Commit) ([]commitparser.AnalyzedCommit, e
analyzedCommits := make([]commitparser.AnalyzedCommit, 0, len(commits)) analyzedCommits := make([]commitparser.AnalyzedCommit, 0, len(commits))
for _, commit := range commits { for _, commit := range commits {
msg, err := c.machine.Parse([]byte(strings.TrimSpace(commit.Message))) msg, err := c.machine.Parse([]byte(commit.Message))
if err != nil { if err != nil {
c.logger.Warn("failed to parse message of commit, skipping", "commit.hash", commit.Hash, "err", err) c.logger.Warn("failed to parse message of commit, skipping", "commit.hash", commit.Hash, "err", err)
continue continue

View file

@ -33,19 +33,6 @@ func TestAnalyzeCommits(t *testing.T) {
expectedCommits: []commitparser.AnalyzedCommit{}, expectedCommits: []commitparser.AnalyzedCommit{},
wantErr: assert.NoError, wantErr: assert.NoError,
}, },
{
// GitLab seems to create commits with pattern "scope: message\n" if no body is added.
// This has previously caused a parser error "missing a blank line".
// We added a workaround with `strings.TrimSpace()` and this test make sure that it does not break again.
name: "handles title with new line",
commits: []git.Commit{
{
Message: "aksdjaklsdjka",
},
},
expectedCommits: []commitparser.AnalyzedCommit{},
wantErr: assert.NoError,
},
{ {
name: "drops unreleasable", name: "drops unreleasable",
commits: []git.Commit{ commits: []git.Commit{

View file

@ -25,7 +25,6 @@ const (
PRStateMerged = "merged" PRStateMerged = "merged"
PRStateEventClose = "close" PRStateEventClose = "close"
EnvAPIToken = "GITLAB_TOKEN" // nolint:gosec // Not actually a hardcoded credential EnvAPIToken = "GITLAB_TOKEN" // nolint:gosec // Not actually a hardcoded credential
EnvProjectPath = "CI_PROJECT_PATH"
) )
type GitLab struct { type GitLab struct {
@ -36,19 +35,19 @@ type GitLab struct {
} }
func (g *GitLab) RepoURL() string { func (g *GitLab) RepoURL() string {
return fmt.Sprintf("https://gitlab.com/%s", g.options.Path) return fmt.Sprintf("https://gitlab.com/%s", g.options.Repository)
} }
func (g *GitLab) CloneURL() string { func (g *GitLab) CloneURL() string {
return fmt.Sprintf("https://gitlab.com/%s.git", g.options.Path) return fmt.Sprintf("https://gitlab.com/%s/%s.git", g.options.Path, g.options.Repo)
} }
func (g *GitLab) ReleaseURL(version string) string { func (g *GitLab) ReleaseURL(version string) string {
return fmt.Sprintf("https://gitlab.com/%s/-/releases/%s", g.options.Path, version) return fmt.Sprintf("https://gitlab.com/%s/%s/-/releases/%s", g.options.Path, g.options.Repo, version)
} }
func (g *GitLab) PullRequestURL(id int) string { func (g *GitLab) PullRequestURL(id int) string {
return fmt.Sprintf("https://gitlab.com/%s/-/merge_requests/%d", g.options.Path, id) return fmt.Sprintf("https://gitlab.com/%s/%s/-/merge_requests/%d", g.options.Path, g.options.Repo, id)
} }
func (g *GitLab) GitAuth() transport.AuthMethod { func (g *GitLab) GitAuth() transport.AuthMethod {
@ -63,7 +62,7 @@ func (g *GitLab) LatestTags(ctx context.Context) (git.Releases, error) {
g.log.DebugContext(ctx, "listing all tags in gitlab repository") g.log.DebugContext(ctx, "listing all tags in gitlab repository")
tags, err := all(func(listOptions gitlab.ListOptions) ([]*gitlab.Tag, *gitlab.Response, error) { tags, err := all(func(listOptions gitlab.ListOptions) ([]*gitlab.Tag, *gitlab.Response, error) {
return g.client.Tags.ListTags(g.options.Path, &gitlab.ListTagsOptions{ return g.client.Tags.ListTags(g.options.ProjectID, &gitlab.ListTagsOptions{
OrderBy: pointer.Pointer("updated"), OrderBy: pointer.Pointer("updated"),
ListOptions: listOptions, ListOptions: listOptions,
}, gitlab.WithContext(ctx)) }, gitlab.WithContext(ctx))
@ -121,7 +120,7 @@ func (g *GitLab) CommitsSince(ctx context.Context, tag *git.Tag) ([]git.Commit,
log.Debug("listing commits", "ref.name", refName) log.Debug("listing commits", "ref.name", refName)
gitLabCommits, err := all(func(listOptions gitlab.ListOptions) ([]*gitlab.Commit, *gitlab.Response, error) { gitLabCommits, err := all(func(listOptions gitlab.ListOptions) ([]*gitlab.Commit, *gitlab.Response, error) {
return g.client.Commits.ListCommits(g.options.Path, &gitlab.ListCommitsOptions{ return g.client.Commits.ListCommits(g.options.ProjectID, &gitlab.ListCommitsOptions{
RefName: &refName, RefName: &refName,
ListOptions: listOptions, ListOptions: listOptions,
}, gitlab.WithContext(ctx)) }, gitlab.WithContext(ctx))
@ -157,7 +156,7 @@ func (g *GitLab) prForCommit(ctx context.Context, commit git.Commit) (*git.PullR
log.Debug("fetching pull requests associated with commit") log.Debug("fetching pull requests associated with commit")
associatedMRs, _, err := g.client.Commits.ListMergeRequestsByCommit( associatedMRs, _, err := g.client.Commits.ListMergeRequestsByCommit(
g.options.Path, commit.Hash, g.options.ProjectID, commit.Hash,
gitlab.WithContext(ctx), gitlab.WithContext(ctx),
) )
if err != nil { if err != nil {
@ -183,7 +182,7 @@ func (g *GitLab) prForCommit(ctx context.Context, commit git.Commit) (*git.PullR
func (g *GitLab) EnsureLabelsExist(ctx context.Context, labels []releasepr.Label) error { func (g *GitLab) EnsureLabelsExist(ctx context.Context, labels []releasepr.Label) error {
g.log.Debug("fetching labels on repo") g.log.Debug("fetching labels on repo")
glLabels, err := all(func(listOptions gitlab.ListOptions) ([]*gitlab.Label, *gitlab.Response, error) { glLabels, err := all(func(listOptions gitlab.ListOptions) ([]*gitlab.Label, *gitlab.Response, error) {
return g.client.Labels.ListLabels(g.options.Path, &gitlab.ListLabelsOptions{ return g.client.Labels.ListLabels(g.options.ProjectID, &gitlab.ListLabelsOptions{
ListOptions: listOptions, ListOptions: listOptions,
}, gitlab.WithContext(ctx)) }, gitlab.WithContext(ctx))
}) })
@ -196,7 +195,7 @@ func (g *GitLab) EnsureLabelsExist(ctx context.Context, labels []releasepr.Label
return glLabel.Name == label.Name return glLabel.Name == label.Name
}) { }) {
g.log.Info("creating label in repository", "label.name", label) g.log.Info("creating label in repository", "label.name", label)
_, _, err := g.client.Labels.CreateLabel(g.options.Path, &gitlab.CreateLabelOptions{ _, _, err := g.client.Labels.CreateLabel(g.options.ProjectID, &gitlab.CreateLabelOptions{
Name: pointer.Pointer(label.Name), Name: pointer.Pointer(label.Name),
Color: pointer.Pointer("#" + label.Color), Color: pointer.Pointer("#" + label.Color),
Description: pointer.Pointer(label.Description), Description: pointer.Pointer(label.Description),
@ -214,7 +213,7 @@ func (g *GitLab) EnsureLabelsExist(ctx context.Context, labels []releasepr.Label
func (g *GitLab) PullRequestForBranch(ctx context.Context, branch string) (*releasepr.ReleasePullRequest, error) { func (g *GitLab) PullRequestForBranch(ctx context.Context, branch string) (*releasepr.ReleasePullRequest, error) {
// There should only be a single open merge request from branch into g.options.BaseBranch at any given moment. // There should only be a single open merge request from branch into g.options.BaseBranch at any given moment.
// We can skip pagination and just return the first result. // We can skip pagination and just return the first result.
mrs, _, err := g.client.MergeRequests.ListProjectMergeRequests(g.options.Path, &gitlab.ListProjectMergeRequestsOptions{ mrs, _, err := g.client.MergeRequests.ListProjectMergeRequests(g.options.ProjectID, &gitlab.ListProjectMergeRequestsOptions{
State: pointer.Pointer(PRStateOpen), State: pointer.Pointer(PRStateOpen),
SourceBranch: pointer.Pointer(branch), SourceBranch: pointer.Pointer(branch),
TargetBranch: pointer.Pointer(g.options.BaseBranch), TargetBranch: pointer.Pointer(g.options.BaseBranch),
@ -240,7 +239,7 @@ func (g *GitLab) CreatePullRequest(ctx context.Context, pr *releasepr.ReleasePul
labels = append(labels, label.Name) labels = append(labels, label.Name)
} }
glMR, _, err := g.client.MergeRequests.CreateMergeRequest(g.options.Path, &gitlab.CreateMergeRequestOptions{ glMR, _, err := g.client.MergeRequests.CreateMergeRequest(g.options.ProjectID, &gitlab.CreateMergeRequestOptions{
Title: &pr.Title, Title: &pr.Title,
Description: &pr.Description, Description: &pr.Description,
SourceBranch: &pr.Head, SourceBranch: &pr.Head,
@ -257,7 +256,7 @@ func (g *GitLab) CreatePullRequest(ctx context.Context, pr *releasepr.ReleasePul
} }
func (g *GitLab) UpdatePullRequest(ctx context.Context, pr *releasepr.ReleasePullRequest) error { func (g *GitLab) UpdatePullRequest(ctx context.Context, pr *releasepr.ReleasePullRequest) error {
_, _, err := g.client.MergeRequests.UpdateMergeRequest(g.options.Path, pr.ID, &gitlab.UpdateMergeRequestOptions{ _, _, err := g.client.MergeRequests.UpdateMergeRequest(g.options.ProjectID, pr.ID, &gitlab.UpdateMergeRequestOptions{
Title: &pr.Title, Title: &pr.Title,
Description: &pr.Description, Description: &pr.Description,
}, gitlab.WithContext(ctx)) }, gitlab.WithContext(ctx))
@ -280,7 +279,7 @@ func (g *GitLab) SetPullRequestLabels(ctx context.Context, pr *releasepr.Release
addLabels = append(addLabels, label.Name) addLabels = append(addLabels, label.Name)
} }
_, _, err := g.client.MergeRequests.UpdateMergeRequest(g.options.Path, pr.ID, &gitlab.UpdateMergeRequestOptions{ _, _, err := g.client.MergeRequests.UpdateMergeRequest(g.options.ProjectID, pr.ID, &gitlab.UpdateMergeRequestOptions{
RemoveLabels: &removeLabels, RemoveLabels: &removeLabels,
AddLabels: &addLabels, AddLabels: &addLabels,
}, gitlab.WithContext(ctx)) }, gitlab.WithContext(ctx))
@ -293,7 +292,7 @@ func (g *GitLab) SetPullRequestLabels(ctx context.Context, pr *releasepr.Release
} }
func (g *GitLab) ClosePullRequest(ctx context.Context, pr *releasepr.ReleasePullRequest) error { func (g *GitLab) ClosePullRequest(ctx context.Context, pr *releasepr.ReleasePullRequest) error {
_, _, err := g.client.MergeRequests.UpdateMergeRequest(g.options.Path, pr.ID, &gitlab.UpdateMergeRequestOptions{ _, _, err := g.client.MergeRequests.UpdateMergeRequest(g.options.ProjectID, pr.ID, &gitlab.UpdateMergeRequestOptions{
StateEvent: pointer.Pointer(PRStateEventClose), StateEvent: pointer.Pointer(PRStateEventClose),
}, gitlab.WithContext(ctx)) }, gitlab.WithContext(ctx))
@ -327,7 +326,7 @@ func (g *GitLab) PendingReleases(ctx context.Context, pendingLabel releasepr.Lab
} }
func (g *GitLab) CreateRelease(ctx context.Context, commit git.Commit, title, changelog string, _, _ bool) error { func (g *GitLab) CreateRelease(ctx context.Context, commit git.Commit, title, changelog string, _, _ bool) error {
_, _, err := g.client.Releases.CreateRelease(g.options.Path, &gitlab.CreateReleaseOptions{ _, _, err := g.client.Releases.CreateRelease(g.options.ProjectID, &gitlab.CreateReleaseOptions{
Name: &title, Name: &title,
TagName: &title, TagName: &title,
Description: &changelog, Description: &changelog,
@ -397,15 +396,16 @@ func (g *Options) autodiscover() {
g.APIToken = apiToken g.APIToken = apiToken
} }
if projectPath := os.Getenv(EnvProjectPath); projectPath != "" { // TODO: Replace hardcode project-id with a better alternative
g.Path = projectPath g.ProjectID = 60698565
}
} }
type Options struct { type Options struct {
forge.Options forge.Options
Path string Path string
Repo string
ProjectID int
APIToken string APIToken string
} }