Skip to content

Commit

Permalink
fix: gitlab defaulting to github token and parsing for urls with toke…
Browse files Browse the repository at this point in the history
…ns (#23)

Co-authored-by: Wayne Starr <Racer159@users.noreply.github.com>
  • Loading branch information
corang and Racer159 authored Oct 30, 2024
1 parent 97d2d41 commit 61a4ad1
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 52 deletions.
11 changes: 6 additions & 5 deletions src/cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ import (
"github.com/spf13/cobra"
)

var tokenVarName string
var gitlabTokenVarName string
var githubTokenVarName string

// gitlabCmd represents the gitlab command
var gitlabCmd = &cobra.Command{
Use: "gitlab flavor",
Short: "Create a tag and release on GitLab based on flavor",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return platforms.LoadAndTag(releaserDir, args[0], tokenVarName, gitlab.Platform{})
return platforms.LoadAndTag(releaserDir, args[0], gitlabTokenVarName, gitlab.Platform{})
},
}

Expand All @@ -40,7 +41,7 @@ var githubCmd = &cobra.Command{
Short: "Create a tag and release on GitHub based on flavor",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return platforms.LoadAndTag(releaserDir, args[0], tokenVarName, github.Platform{})
return platforms.LoadAndTag(releaserDir, args[0], githubTokenVarName, github.Platform{})
},
}

Expand All @@ -54,6 +55,6 @@ func init() {
rootCmd.AddCommand(releaseCmd)
releaseCmd.AddCommand(gitlabCmd)
releaseCmd.AddCommand(githubCmd)
gitlabCmd.Flags().StringVarP(&tokenVarName, "token-var-name", "t", "GITLAB_RELEASE_TOKEN", "Environment variable name for GitLab token")
githubCmd.Flags().StringVarP(&tokenVarName, "token-var-name", "t", "GITHUB_TOKEN", "Environment variable name for GitHub token")
gitlabCmd.Flags().StringVarP(&gitlabTokenVarName, "token-var-name", "t", "GITLAB_RELEASE_TOKEN", "Environment variable name for GitLab token")
githubCmd.Flags().StringVarP(&githubTokenVarName, "token-var-name", "t", "GITHUB_TOKEN", "Environment variable name for GitHub token")
}
98 changes: 71 additions & 27 deletions src/platforms/github/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,80 @@ import (
)

func TestCreateGitHubTag(t *testing.T) {
// Create a new tag
tagName := "v1.0.0-uds.0-unicorn"
releaseName := "testing-package v1.0.0-uds.0-unicorn"
hash := "1234567890"
tests := []struct {
name string
tagName string
releaseName string
hash string
}{
{
name: "ValidTag",
tagName: "v1.0.0-uds.0-unicorn",
releaseName: "testing-package v1.0.0-uds.0-unicorn",
hash: "1234567890",
},
}

tag := createGitHubTag(tagName, releaseName, hash)

assert.Equal(t, tagName, *tag.Tag)
assert.Equal(t, releaseName, *tag.Message)
assert.Equal(t, hash, *tag.Object.SHA)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tag := createGitHubTag(tt.tagName, tt.releaseName, tt.hash)
assert.Equal(t, tt.tagName, *tag.Tag)
assert.Equal(t, tt.releaseName, *tag.Message)
assert.Equal(t, tt.hash, *tag.Object.SHA)
})
}
}

func TestGetGithubOwnerAndRepo(t *testing.T) {
// Get the owner and repo from a remote URL
httpsRemoteURL := "https://github.com/defenseunicorns/uds-releaser.git"
sshRemoteURL := "git@github.com:defenseunicorns/uds-releaser.git"

owner, repo, err := getGithubOwnerAndRepo(httpsRemoteURL)
assert.NoError(t, err)
assert.Equal(t, "defenseunicorns", owner)
assert.Equal(t, "uds-releaser", repo)

owner, repo, err = getGithubOwnerAndRepo(sshRemoteURL)
assert.NoError(t, err)
assert.Equal(t, "defenseunicorns", owner)
assert.Equal(t, "uds-releaser", repo)
tests := []struct {
name string
remoteURL string
expectedOwner string
expectedRepo string
expectError bool
}{
{
name: "HTTPSRemoteURL",
remoteURL: "https://github.com/defenseunicorns/uds-releaser.git",
expectedOwner: "defenseunicorns",
expectedRepo: "uds-releaser",
expectError: false,
},
{
name: "HTTPSRemoteURLWithToken",
remoteURL: "https://test:token@github.com/defenseunicorns/uds-releaser.git",
expectedOwner: "defenseunicorns",
expectedRepo: "uds-releaser",
expectError: false,
},
{
name: "SSHRemoteURL",
remoteURL: "git@github.com:defenseunicorns/uds-releaser.git",
expectedOwner: "defenseunicorns",
expectedRepo: "uds-releaser",
expectError: false,
},
{
name: "GitlabRemoteURL",
remoteURL: "https://gitlab.com/defenseunicorns/uds-releaser.git",
expectedOwner: "",
expectedRepo: "",
expectError: true,
},
}

gitlabRemoteURL := "https://gitlab.com/defenseunicorns/uds-releaser.git"
owner, repo, err = getGithubOwnerAndRepo(gitlabRemoteURL)
assert.Error(t, err)
assert.Empty(t, owner)
assert.Empty(t, repo)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
owner, repo, err := getGithubOwnerAndRepo(tt.remoteURL)
if tt.expectError {
assert.Error(t, err)
assert.Empty(t, owner)
assert.Empty(t, repo)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectedOwner, owner)
assert.Equal(t, tt.expectedRepo, repo)
}
})
}
}
39 changes: 19 additions & 20 deletions src/platforms/gitlab/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gitlab

import (
"fmt"
"net/url"
"os"
"regexp"
"strings"
Expand All @@ -19,14 +20,20 @@ func (Platform) TagAndRelease(flavor types.Flavor, tokenVarName string) error {
return err
}

fmt.Println("Remote URL: ", remoteURL)

// Parse the GitLab base URL from the remote URL
gitlabBaseURL, err := getGitlabBaseUrl(remoteURL)
if err != nil {
return err
}

fmt.Println("Base URL: ", gitlabBaseURL)

fmt.Printf("Default branch: %s\n", defaultBranch)

fmt.Println("token var name: ", tokenVarName)

// Create a new GitLab client
gitlabClient, err := gitlab.NewClient(os.Getenv(tokenVarName), gitlab.WithBaseURL(gitlabBaseURL))
if err != nil {
Expand All @@ -46,6 +53,7 @@ func (Platform) TagAndRelease(flavor types.Flavor, tokenVarName string) error {
// Create the release
release, _, err := gitlabClient.Releases.CreateRelease(os.Getenv("CI_PROJECT_ID"), releaseOpts)
if err != nil {
fmt.Println("Error creating release: ", err)
return err
}

Expand All @@ -65,27 +73,18 @@ func createReleaseOptions(zarfPackageName string, flavor types.Flavor, branchRef

func getGitlabBaseUrl(remoteURL string) (gitlabBaseURL string, err error) {
if strings.Contains(remoteURL, "gitlab.com") {
gitlabBaseURL = "https://gitlab.com/api/v4"
} else {

parts := strings.Split(remoteURL, "/")
containsAt := strings.Contains(remoteURL, "@")
if len(parts) > 2 {
gitlabBaseURL = fmt.Sprintf("https://%s/api/v4", parts[2])
} else if containsAt {
regex := regexp.MustCompile(`@(.+):`)

matches := regex.FindStringSubmatch(remoteURL)
if len(matches) > 1 {
gitlabBaseURL = fmt.Sprintf("https://%s/api/v4", matches[1])
} else {
return "", fmt.Errorf("error parsing gitlab base url from remote url: %s", remoteURL)
}
} else {
return "", fmt.Errorf("error parsing gitlab base url from remote url: %s", remoteURL)
return "https://gitlab.com/api/v4", nil
}

parsedURL, err := url.Parse(remoteURL)
if err != nil {
regex := regexp.MustCompile(`@([^:/]+)`)
matches := regex.FindStringSubmatch(remoteURL)
if len(matches) > 1 {
return fmt.Sprintf("https://%s/api/v4", matches[1]), nil
}
return "", fmt.Errorf("error parsing GitLab base URL from remote URL: %s", remoteURL)
}

fmt.Printf("GitLab base URL: %s\n", gitlabBaseURL)
return gitlabBaseURL, nil
return fmt.Sprintf("%s://%s/api/v4", parsedURL.Scheme, parsedURL.Host), nil
}
10 changes: 10 additions & 0 deletions src/platforms/gitlab/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ func TestGetGitlabBaseUrl(t *testing.T) {
remoteURL: "https://gitlab.fake.com/defenseunicorns/uds-releaser.git",
expected: "https://gitlab.fake.com/api/v4",
},
{
name: "https-with-token",
remoteURL: "https://test:token@gitlab.com/defenseunicorns/uds-releaser.git",
expected: "https://gitlab.com/api/v4",
},
{
name: "https-self-hosted-with-token",
remoteURL: "https://test:token@gitlab.fake.com/defenseunicorns/uds-releaser.git",
expected: "https://gitlab.fake.com/api/v4",
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 61a4ad1

Please sign in to comment.