git url without : with empty login

This commit is contained in:
Petr 2024-07-07 17:07:09 +09:00
parent 36be80cce8
commit 65b33dbb8d
2 changed files with 43 additions and 3 deletions

View File

@ -2,12 +2,13 @@ package db
import ( import (
"fmt" "fmt"
"github.com/ansible-semaphore/semaphore/util"
"os" "os"
"path" "path"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"github.com/ansible-semaphore/semaphore/util"
) )
type RepositoryType string type RepositoryType string
@ -81,8 +82,12 @@ func (r Repository) GetGitURL() string {
auth := "" auth := ""
switch r.SSHKey.Type { switch r.SSHKey.Type {
case AccessKeyLoginPassword: case AccessKeyLoginPassword:
if r.SSHKey.LoginPassword.Login == "" {
auth = r.SSHKey.LoginPassword.Password
} else {
auth = r.SSHKey.LoginPassword.Login + ":" + r.SSHKey.LoginPassword.Password auth = r.SSHKey.LoginPassword.Login + ":" + r.SSHKey.LoginPassword.Password
} }
}
if auth != "" { if auth != "" {
auth += "@" auth += "@"
} }

View File

@ -1,11 +1,12 @@
package db package db
import ( import (
"github.com/ansible-semaphore/semaphore/util"
"math/rand" "math/rand"
"os" "os"
"path" "path"
"testing" "testing"
"github.com/ansible-semaphore/semaphore/util"
) )
func TestRepository_GetSchema(t *testing.T) { func TestRepository_GetSchema(t *testing.T) {
@ -38,3 +39,37 @@ func TestRepository_ClearCache(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
} }
func TestRepository_GetGitURL(t *testing.T) {
for _, v := range []struct {
Repository Repository
ExpectedGitUrl string
}{
{
Repository: Repository{GitURL: "https://github.com/user/project.git", SSHKey: AccessKey{
Type: AccessKeyLoginPassword,
LoginPassword: LoginPassword{
Login: "login",
Password: "password",
},
},
},
ExpectedGitUrl: "https://login:password@github.com/user/project.git",
},
{
Repository: Repository{GitURL: "https://github.com/user/project.git", SSHKey: AccessKey{
Type: AccessKeyLoginPassword,
LoginPassword: LoginPassword{
Password: "password",
},
},
},
ExpectedGitUrl: "https://password@github.com/user/project.git",
},
} {
gitUrl := v.Repository.GetGitURL()
if gitUrl != v.ExpectedGitUrl {
t.Error("wrong gitUrl", "expected: ", v.ExpectedGitUrl, " got: ", gitUrl)
}
}
}