Compare commits

..

4 commits

Author SHA1 Message Date
ee83cec049
fix(gitlab): use project path wherever possible (#54)
Turns out that all we need is the path, and not the project id. The path
is way more user friendly, and we can easily get it from a CI variable
or combine it from the namespace & project name.
2024-09-08 21:07:03 +02:00
634eac3b76
fix(parser): invalid handling of empty lines (#53)
GitLab generates commit messages of the pattern "scope: message\n" if no
body is present. This throws up the conventional commits parser we use,
and results in the error message "missing a blank line".

We now `strings.TrimSpace()` the commit message to avoid this problem.
2024-09-08 21:05:18 +02:00
ee5c7aa142
fix(cli): command name in help output (#52) 2024-09-08 21:01:46 +02:00
2fba5414e5
fix(gitlab): hardcoded project id (#51)
The GitLab project ID was still hardcoded to my playground project on
GitLab.com.

This commit instead reads from the predefined GitLab CI/CD variable for
the projects ID (`CI_PROJECT_ID`).
2024-09-08 20:03:19 +02:00
5 changed files with 37 additions and 24 deletions

View file

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

View file

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

View file

@ -3,6 +3,7 @@ package conventionalcommits
import (
"fmt"
"log/slog"
"strings"
"github.com/leodido/go-conventionalcommits"
"github.com/leodido/go-conventionalcommits/parser"
@ -32,7 +33,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(commit.Message))
msg, err := c.machine.Parse([]byte(strings.TrimSpace(commit.Message)))
if err != nil {
c.logger.Warn("failed to parse message of commit, skipping", "commit.hash", commit.Hash, "err", err)
continue

View file

@ -33,6 +33,19 @@ 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{

View file

@ -25,6 +25,7 @@ const (
PRStateMerged = "merged"
PRStateEventClose = "close"
EnvAPIToken = "GITLAB_TOKEN" // nolint:gosec // Not actually a hardcoded credential
EnvProjectPath = "CI_PROJECT_PATH"
)
type GitLab struct {
@ -35,19 +36,19 @@ type GitLab struct {
}
func (g *GitLab) RepoURL() string {
return fmt.Sprintf("https://gitlab.com/%s", g.options.Repository)
return fmt.Sprintf("https://gitlab.com/%s", g.options.Path)
}
func (g *GitLab) CloneURL() string {
return fmt.Sprintf("https://gitlab.com/%s/%s.git", g.options.Path, g.options.Repo)
return fmt.Sprintf("https://gitlab.com/%s.git", g.options.Path)
}
func (g *GitLab) ReleaseURL(version string) string {
return fmt.Sprintf("https://gitlab.com/%s/%s/-/releases/%s", g.options.Path, g.options.Repo, version)
return fmt.Sprintf("https://gitlab.com/%s/-/releases/%s", g.options.Path, version)
}
func (g *GitLab) PullRequestURL(id int) string {
return fmt.Sprintf("https://gitlab.com/%s/%s/-/merge_requests/%d", g.options.Path, g.options.Repo, id)
return fmt.Sprintf("https://gitlab.com/%s/-/merge_requests/%d", g.options.Path, id)
}
func (g *GitLab) GitAuth() transport.AuthMethod {
@ -62,7 +63,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.ProjectID, &gitlab.ListTagsOptions{
return g.client.Tags.ListTags(g.options.Path, &gitlab.ListTagsOptions{
OrderBy: pointer.Pointer("updated"),
ListOptions: listOptions,
}, gitlab.WithContext(ctx))
@ -120,7 +121,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.ProjectID, &gitlab.ListCommitsOptions{
return g.client.Commits.ListCommits(g.options.Path, &gitlab.ListCommitsOptions{
RefName: &refName,
ListOptions: listOptions,
}, gitlab.WithContext(ctx))
@ -156,7 +157,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.ProjectID, commit.Hash,
g.options.Path, commit.Hash,
gitlab.WithContext(ctx),
)
if err != nil {
@ -182,7 +183,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.ProjectID, &gitlab.ListLabelsOptions{
return g.client.Labels.ListLabels(g.options.Path, &gitlab.ListLabelsOptions{
ListOptions: listOptions,
}, gitlab.WithContext(ctx))
})
@ -195,7 +196,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.ProjectID, &gitlab.CreateLabelOptions{
_, _, err := g.client.Labels.CreateLabel(g.options.Path, &gitlab.CreateLabelOptions{
Name: pointer.Pointer(label.Name),
Color: pointer.Pointer("#" + label.Color),
Description: pointer.Pointer(label.Description),
@ -213,7 +214,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.ProjectID, &gitlab.ListProjectMergeRequestsOptions{
mrs, _, err := g.client.MergeRequests.ListProjectMergeRequests(g.options.Path, &gitlab.ListProjectMergeRequestsOptions{
State: pointer.Pointer(PRStateOpen),
SourceBranch: pointer.Pointer(branch),
TargetBranch: pointer.Pointer(g.options.BaseBranch),
@ -239,7 +240,7 @@ func (g *GitLab) CreatePullRequest(ctx context.Context, pr *releasepr.ReleasePul
labels = append(labels, label.Name)
}
glMR, _, err := g.client.MergeRequests.CreateMergeRequest(g.options.ProjectID, &gitlab.CreateMergeRequestOptions{
glMR, _, err := g.client.MergeRequests.CreateMergeRequest(g.options.Path, &gitlab.CreateMergeRequestOptions{
Title: &pr.Title,
Description: &pr.Description,
SourceBranch: &pr.Head,
@ -256,7 +257,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.ProjectID, pr.ID, &gitlab.UpdateMergeRequestOptions{
_, _, err := g.client.MergeRequests.UpdateMergeRequest(g.options.Path, pr.ID, &gitlab.UpdateMergeRequestOptions{
Title: &pr.Title,
Description: &pr.Description,
}, gitlab.WithContext(ctx))
@ -279,7 +280,7 @@ func (g *GitLab) SetPullRequestLabels(ctx context.Context, pr *releasepr.Release
addLabels = append(addLabels, label.Name)
}
_, _, err := g.client.MergeRequests.UpdateMergeRequest(g.options.ProjectID, pr.ID, &gitlab.UpdateMergeRequestOptions{
_, _, err := g.client.MergeRequests.UpdateMergeRequest(g.options.Path, pr.ID, &gitlab.UpdateMergeRequestOptions{
RemoveLabels: &removeLabels,
AddLabels: &addLabels,
}, gitlab.WithContext(ctx))
@ -292,7 +293,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.ProjectID, pr.ID, &gitlab.UpdateMergeRequestOptions{
_, _, err := g.client.MergeRequests.UpdateMergeRequest(g.options.Path, pr.ID, &gitlab.UpdateMergeRequestOptions{
StateEvent: pointer.Pointer(PRStateEventClose),
}, gitlab.WithContext(ctx))
@ -326,7 +327,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.ProjectID, &gitlab.CreateReleaseOptions{
_, _, err := g.client.Releases.CreateRelease(g.options.Path, &gitlab.CreateReleaseOptions{
Name: &title,
TagName: &title,
Description: &changelog,
@ -396,16 +397,15 @@ func (g *Options) autodiscover() {
g.APIToken = apiToken
}
// TODO: Replace hardcode project-id with a better alternative
g.ProjectID = 60698565
if projectPath := os.Getenv(EnvProjectPath); projectPath != "" {
g.Path = projectPath
}
}
type Options struct {
forge.Options
Path string
Repo string
ProjectID int
APIToken string
}