Get Netrc machine from clone url (#800) (#803)

We previously got the machine hostname for Netrc from the url of the remote, but in cases where the clone-url does not match the api url this can lead to errors.

Co-authored-by: Anbraten <anton@ju60.de>
This commit is contained in:
6543
2022-02-26 14:36:14 +01:00
committed by GitHub
parent 7a4c6d32de
commit ce07632ffd
14 changed files with 97 additions and 85 deletions

View File

@@ -27,6 +27,7 @@ import (
"github.com/woodpecker-ci/woodpecker/server/model"
"github.com/woodpecker-ci/woodpecker/server/remote"
"github.com/woodpecker-ci/woodpecker/server/remote/coding/internal"
"github.com/woodpecker-ci/woodpecker/server/remote/common"
)
const (
@@ -39,7 +40,6 @@ type Opts struct {
Client string // Coding oauth client id.
Secret string // Coding oauth client secret.
Scopes []string // Coding oauth scopes.
Machine string // Optional machine name.
Username string // Optional machine account username.
Password string // Optional machine account password.
SkipVerify bool // Skip ssl verification.
@@ -53,7 +53,6 @@ func New(opts Opts) (remote.Remote, error) {
Client: opts.Client,
Secret: opts.Secret,
Scopes: opts.Scopes,
Machine: opts.Machine,
Username: opts.Username,
Password: opts.Password,
SkipVerify: opts.SkipVerify,
@@ -70,7 +69,6 @@ type Coding struct {
Client string
Secret string
Scopes []string
Machine string
Username string
Password string
SkipVerify bool
@@ -251,17 +249,23 @@ func (c *Coding) Status(ctx context.Context, u *model.User, r *model.Repo, b *mo
// Netrc returns a .netrc file that can be used to clone
// private repositories from a remote system.
func (c *Coding) Netrc(u *model.User, r *model.Repo) (*model.Netrc, error) {
host, err := common.ExtractHostFromCloneURL(r.Clone)
if err != nil {
return nil, err
}
if c.Password != "" {
return &model.Netrc{
Login: c.Username,
Password: c.Password,
Machine: c.Machine,
Machine: host,
}, nil
}
return &model.Netrc{
Login: u.Token,
Password: "x-oauth-basic",
Machine: c.Machine,
Machine: host,
}, nil
}

View File

@@ -48,7 +48,6 @@ func Test_coding(t *testing.T) {
Client: "KTNF2ALdm3ofbtxLh6IbV95Ro5AKWJUP",
Secret: "zVtxJrKhNhBcNyqCz1NggNAAmehAxnRO3Z0fXmCp",
Scopes: []string{"user", "project", "project:depot"},
Machine: "git.coding.net",
Username: "someuser",
Password: "password",
SkipVerify: true,
@@ -57,7 +56,6 @@ func Test_coding(t *testing.T) {
g.Assert(remote.(*Coding).Client).Equal("KTNF2ALdm3ofbtxLh6IbV95Ro5AKWJUP")
g.Assert(remote.(*Coding).Secret).Equal("zVtxJrKhNhBcNyqCz1NggNAAmehAxnRO3Z0fXmCp")
g.Assert(remote.(*Coding).Scopes).Equal([]string{"user", "project", "project:depot"})
g.Assert(remote.(*Coding).Machine).Equal("git.coding.net")
g.Assert(remote.(*Coding).Username).Equal("someuser")
g.Assert(remote.(*Coding).Password).Equal("password")
g.Assert(remote.(*Coding).SkipVerify).Equal(true)
@@ -172,21 +170,18 @@ func Test_coding(t *testing.T) {
g.Describe("When requesting a netrc config", func() {
g.It("Should return the netrc file for global credential", func() {
remote, _ := New(Opts{
Machine: "git.coding.net",
Username: "someuser",
Password: "password",
})
netrc, err := remote.Netrc(fakeUser, nil)
netrc, err := remote.Netrc(fakeUser, fakeRepo)
g.Assert(err).IsNil()
g.Assert(netrc.Login).Equal("someuser")
g.Assert(netrc.Password).Equal("password")
g.Assert(netrc.Machine).Equal("git.coding.net")
})
g.It("Should return the netrc file for specified user", func() {
remote, _ := New(Opts{
Machine: "git.coding.net",
})
netrc, err := remote.Netrc(fakeUser, nil)
remote, _ := New(Opts{})
netrc, err := remote.Netrc(fakeUser, fakeRepo)
g.Assert(err).IsNil()
g.Assert(netrc.Login).Equal(fakeUser.Token)
g.Assert(netrc.Password).Equal("x-oauth-basic")