diff --git a/CHANGELOG.md b/CHANGELOG.md index f7be68b..d8018c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,6 @@ ### Bug Fixes - **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) diff --git a/cmd/rp/cmd/root.go b/cmd/rp/cmd/root.go index 4da80e5..dd4f9a7 100644 --- a/cmd/rp/cmd/root.go +++ b/cmd/rp/cmd/root.go @@ -11,7 +11,7 @@ import ( var logger *slog.Logger var rootCmd = &cobra.Command{ - Use: "rp", + Use: "releaser-pleaser", Short: "", Long: ``, Version: version(), diff --git a/cmd/rp/cmd/run.go b/cmd/rp/cmd/run.go index 8fe818f..55ca419 100644 --- a/cmd/rp/cmd/run.go +++ b/cmd/rp/cmd/run.go @@ -62,7 +62,8 @@ func run(cmd *cobra.Command, _ []string) error { logger.DebugContext(ctx, "using forge GitLab") f, err = gitlab.New(logger, &gitlab.Options{ Options: forgeOptions, - Path: fmt.Sprintf("%s/%s", flagOwner, flagRepo), + Path: flagOwner, + Repo: flagRepo, }) if err != nil { logger.ErrorContext(ctx, "failed to create client", "err", err) diff --git a/internal/commitparser/conventionalcommits/conventionalcommits.go b/internal/commitparser/conventionalcommits/conventionalcommits.go index d11970c..b253354 100644 --- a/internal/commitparser/conventionalcommits/conventionalcommits.go +++ b/internal/commitparser/conventionalcommits/conventionalcommits.go @@ -3,7 +3,6 @@ package conventionalcommits import ( "fmt" "log/slog" - "strings" "github.com/leodido/go-conventionalcommits" "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)) 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 { c.logger.Warn("failed to parse message of commit, skipping", "commit.hash", commit.Hash, "err", err) continue diff --git a/internal/commitparser/conventionalcommits/conventionalcommits_test.go b/internal/commitparser/conventionalcommits/conventionalcommits_test.go index ddfbaf4..bc969de 100644 --- a/internal/commitparser/conventionalcommits/conventionalcommits_test.go +++ b/internal/commitparser/conventionalcommits/conventionalcommits_test.go @@ -33,19 +33,6 @@ func TestAnalyzeCommits(t *testing.T) { expectedCommits: []commitparser.AnalyzedCommit{}, 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", commits: []git.Commit{ diff --git a/internal/forge/gitlab/gitlab.go b/internal/forge/gitlab/gitlab.go index 1394898..8808773 100644 --- a/internal/forge/gitlab/gitlab.go +++ b/internal/forge/gitlab/gitlab.go @@ -25,7 +25,6 @@ const ( PRStateMerged = "merged" PRStateEventClose = "close" EnvAPIToken = "GITLAB_TOKEN" // nolint:gosec // Not actually a hardcoded credential - EnvProjectPath = "CI_PROJECT_PATH" ) type GitLab struct { @@ -36,19 +35,19 @@ type GitLab struct { } 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 { - 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 { - 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 { - 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 { @@ -63,7 +62,7 @@ func (g *GitLab) LatestTags(ctx context.Context) (git.Releases, error) { g.log.DebugContext(ctx, "listing all tags in gitlab repository") 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"), ListOptions: listOptions, }, 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) 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, ListOptions: listOptions, }, 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") associatedMRs, _, err := g.client.Commits.ListMergeRequestsByCommit( - g.options.Path, commit.Hash, + g.options.ProjectID, commit.Hash, gitlab.WithContext(ctx), ) 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 { g.log.Debug("fetching labels on repo") 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, }, gitlab.WithContext(ctx)) }) @@ -196,7 +195,7 @@ func (g *GitLab) EnsureLabelsExist(ctx context.Context, labels []releasepr.Label return glLabel.Name == label.Name }) { 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), Color: pointer.Pointer("#" + label.Color), 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) { // 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. - 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), SourceBranch: pointer.Pointer(branch), 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) } - 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, Description: &pr.Description, 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 { - _, _, 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, Description: &pr.Description, }, gitlab.WithContext(ctx)) @@ -280,7 +279,7 @@ func (g *GitLab) SetPullRequestLabels(ctx context.Context, pr *releasepr.Release 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, AddLabels: &addLabels, }, 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 { - _, _, 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), }, 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 { - _, _, err := g.client.Releases.CreateRelease(g.options.Path, &gitlab.CreateReleaseOptions{ + _, _, err := g.client.Releases.CreateRelease(g.options.ProjectID, &gitlab.CreateReleaseOptions{ Name: &title, TagName: &title, Description: &changelog, @@ -397,15 +396,16 @@ func (g *Options) autodiscover() { g.APIToken = apiToken } - if projectPath := os.Getenv(EnvProjectPath); projectPath != "" { - g.Path = projectPath - } + // TODO: Replace hardcode project-id with a better alternative + g.ProjectID = 60698565 } type Options struct { forge.Options - Path string + Path string + Repo string + ProjectID int APIToken string }