Skip to content

Commit

Permalink
Merge pull request #615 from fluxcd/push-proxy
Browse files Browse the repository at this point in the history
git/gogit: add proxy support for pushing to remote
  • Loading branch information
aryan9600 authored Aug 3, 2023
2 parents 33880da + 926f56f commit 6d61e1b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
13 changes: 7 additions & 6 deletions git/gogit/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,13 @@ func (g *Client) Push(ctx context.Context, cfg repository.PushConfig) error {
}

err = g.repository.PushContext(ctx, &extgogit.PushOptions{
RefSpecs: refspecs,
Force: cfg.Force,
RemoteName: extgogit.DefaultRemoteName,
Auth: authMethod,
Progress: nil,
CABundle: caBundle(g.authOpts),
RefSpecs: refspecs,
Force: cfg.Force,
RemoteName: extgogit.DefaultRemoteName,
Auth: authMethod,
Progress: nil,
CABundle: caBundle(g.authOpts),
ProxyOptions: g.proxy,
})
return goGitError(err)
}
Expand Down
23 changes: 22 additions & 1 deletion git/gogit/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ func Test_ssh_HostKeyAlgos(t *testing.T) {
}
}

func TestClone_WithProxy(t *testing.T) {
func TestCloneAndPush_WithProxy(t *testing.T) {
g := NewWithT(t)

server, err := gittestserver.NewTempGitServer()
Expand Down Expand Up @@ -1178,6 +1178,27 @@ func TestClone_WithProxy(t *testing.T) {
})
g.Expect(err).ToNot(HaveOccurred())
g.Expect(proxiedRequests).To(BeNumerically(">", 0))

// reset proxy requests counter
proxiedRequests = 0
// make a commit on master and push it.
cc1, err := commitFile(ggc.repository, "test", "testing gogit push", time.Now())
g.Expect(err).ToNot(HaveOccurred())
err = ggc.Push(context.TODO(), repository.PushConfig{})
g.Expect(err).ToNot(HaveOccurred())
g.Expect(proxiedRequests).To(BeNumerically(">", 0))

// check if we indeed pushed the commit to remote.
ggc, err = NewClient(t.TempDir(), authOpts, WithDiskStorage(), WithProxy(proxyOpts))
g.Expect(err).ToNot(HaveOccurred())
g.Expect(ggc.proxy.URL).ToNot(BeEmpty())
cc2, err := ggc.Clone(context.TODO(), repoURL, repository.CloneConfig{
CheckoutStrategy: repository.CheckoutStrategy{
Branch: git.DefaultBranch,
},
})
g.Expect(err).ToNot(HaveOccurred())
g.Expect(cc1.String()).To(Equal(cc2.Hash.String()))
}

func Test_getRemoteHEAD(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion git/internal/e2e/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ if [[ "${GO_TEST_PREFIX}" = "" ]] || [[ "${GO_TEST_PREFIX}" = *"TestGitLabCEE2E"
fi

cd "${DIR}"
CGO_ENABLED=1 go test -v -tags 'netgo,osusergo,static_build,e2e' -race -run "^${GO_TEST_PREFIX}.*" ./...
go test -v -tags 'netgo,osusergo,static_build,e2e' -race -run "^${GO_TEST_PREFIX}.*" ./...
24 changes: 20 additions & 4 deletions git/internal/e2e/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import (

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz1234567890")

const timeout = time.Second * 20

func testUsingClone(g *WithT, client repository.Client, repoURL *url.URL, upstreamRepo upstreamRepoInfo) {
// clone repo
_, err := client.Clone(context.TODO(), repoURL.String(), repository.CloneConfig{
Expand All @@ -62,8 +64,16 @@ func testUsingClone(g *WithT, client repository.Client, repoURL *url.URL, upstre
)
g.Expect(err).ToNot(HaveOccurred(), "first commit")

err = client.Push(context.TODO(), repository.PushConfig{})
g.Expect(err).ToNot(HaveOccurred())
// GitHub sometimes takes a long time to propogate its deploy key and this leads
// to mysterious push errors like "unknown error: ERROR: Unknown public SSH key".
// This helps us get around that by retrying for a fixed amount of time.
g.Eventually(func() bool {
err = client.Push(context.TODO(), repository.PushConfig{})
if err != nil {
return false
}
return true
}, timeout).Should(BeTrue())

headCommit, _, err := headCommitWithBranch(upstreamRepo.url, "main", upstreamRepo.username, upstreamRepo.password)
g.Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -120,8 +130,13 @@ func testUsingInit(g *WithT, client repository.Client, repoURL *url.URL, upstrea
)
g.Expect(err).ToNot(HaveOccurred(), "first commit")

err = client.Push(context.TODO(), repository.PushConfig{})
g.Expect(err).ToNot(HaveOccurred())
g.Eventually(func() bool {
err = client.Push(context.TODO(), repository.PushConfig{})
if err != nil {
return false
}
return true
}, timeout).Should(BeTrue())

headCommit, _, err := headCommitWithBranch(upstreamRepo.url, "main", upstreamRepo.username, upstreamRepo.password)
g.Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -157,6 +172,7 @@ func testUsingInit(g *WithT, client repository.Client, repoURL *url.URL, upstrea
g.Expect(err).ToNot(HaveOccurred(), "third commit")
err = client.Push(context.TODO(), repository.PushConfig{})
g.Expect(err).ToNot(HaveOccurred())

headCommit, _, err = headCommitWithBranch(upstreamRepo.url, "new", upstreamRepo.username, upstreamRepo.password)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(headCommit).To(Equal(cc))
Expand Down

0 comments on commit 6d61e1b

Please sign in to comment.