From a68e7e13f1c445284dc9c05f3cfdafdefc8e7951 Mon Sep 17 00:00:00 2001 From: Christoph Witzko <github@christophwitzko.com> Date: Tue, 7 Feb 2023 13:52:16 +0100 Subject: [PATCH] fix: correctly get owner/repo information and list initial commits --- pkg/provider/gitlab.go | 17 +++++++++++++---- pkg/provider/gitlab_test.go | 4 +++- scripts/build-local.sh | 11 +++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100755 scripts/build-local.sh diff --git a/pkg/provider/gitlab.go b/pkg/provider/gitlab.go index 26524d5..75487da 100644 --- a/pkg/provider/gitlab.go +++ b/pkg/provider/gitlab.go @@ -6,6 +6,7 @@ import ( "os" "regexp" "strconv" + "strings" "time" "github.com/Masterminds/semver/v3" @@ -81,22 +82,30 @@ func (repo *GitLabRepository) GetInfo() (*provider.RepositoryInfo, error) { if err != nil { return nil, err } + namespace, repoName, _ := strings.Cut(project.PathWithNamespace, "/") return &provider.RepositoryInfo{ - Owner: "", - Repo: "", + Owner: namespace, + Repo: repoName, DefaultBranch: project.DefaultBranch, Private: project.Visibility == gitlab.PrivateVisibility, }, nil } func (repo *GitLabRepository) GetCommits(fromSha, toSha string) ([]*semrel.RawCommit, error) { + var refName *string + if fromSha == "" { + refName = gitlab.String(toSha) + } else { + // No Matter the order ofr fromSha and toSha gitlab always returns commits in reverse chronological order + refName = gitlab.String(fmt.Sprintf("%s...%s", fromSha, toSha)) + } + opts := &gitlab.ListCommitsOptions{ ListOptions: gitlab.ListOptions{ Page: 1, PerPage: 100, }, - // No Matter the order ofr fromSha and toSha gitlab always returns commits in reverse chronological order - RefName: gitlab.String(fmt.Sprintf("%s...%s", fromSha, toSha)), + RefName: refName, } allCommits := make([]*semrel.RawCommit, 0) diff --git a/pkg/provider/gitlab_test.go b/pkg/provider/gitlab_test.go index b766107..9a8c5b5 100644 --- a/pkg/provider/gitlab_test.go +++ b/pkg/provider/gitlab_test.go @@ -71,7 +71,7 @@ func createGitlabTag(name string) *gitlab.Tag { var ( gitlabProjectID = 12324322 gitlabDefaultBranch = "master" - gitlabProjects = gitlab.Project{DefaultBranch: gitlabDefaultBranch, Visibility: gitlab.PrivateVisibility, ID: gitlabProjectID} + gitlabProjects = gitlab.Project{DefaultBranch: gitlabDefaultBranch, Visibility: gitlab.PrivateVisibility, ID: gitlabProjectID, PathWithNamespace: "owner/repo"} gitlabCommits = []*gitlab.Commit{ createGitlabCommit("abcd", "feat(app): new feature"), createGitlabCommit("dcba", "Fix: bug"), @@ -156,6 +156,8 @@ func TestGitlabGetInfo(t *testing.T) { require.NoError(t, err) require.Equal(t, gitlabDefaultBranch, repoInfo.DefaultBranch) require.True(t, repoInfo.Private) + require.Equal(t, "owner", repoInfo.Owner) + require.Equal(t, "repo", repoInfo.Repo) } func TestGitlabGetCommits(t *testing.T) { diff --git a/scripts/build-local.sh b/scripts/build-local.sh new file mode 100755 index 0000000..9551e61 --- /dev/null +++ b/scripts/build-local.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -euo pipefail + +pluginDir=".semrel/$(go env GOOS)_$(go env GOARCH)/provider-gitlab/0.0.0-dev/" +[[ ! -d "$pluginDir" ]] && { + echo "creating $pluginDir" + mkdir -p "$pluginDir" +} + +go build -o "$pluginDir/provider-gitlab" ./cmd/provider-gitlab -- GitLab