Skip to content

[content-service] Revert #11895 #13549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 14 additions & 25 deletions components/content-service/pkg/initializer/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,38 +174,27 @@ func (ws *GitInitializer) realizeCloneTarget(ctx context.Context) (err error) {
// checkout branch
switch ws.TargetMode {
case RemoteBranch:
// we already cloned the git repository but we need to check CloneTarget exists,
// except when the name is main or master because either value could be wrong
// and we are going to use the incorrect value (default is main).
if ws.CloneTarget == "main" || ws.CloneTarget == "master" {
// confirm the value of the default branch name using rev-parse
gitout, _ := ws.GitWithOutput(ctx, nil, "rev-parse", "--abbrev-ref", "origin/HEAD")
defaultBranch := strings.TrimSpace(strings.Replace(string(gitout), "origin/", "", -1))
if defaultBranch != ws.CloneTarget {
// the default branch name we cloned is not the one specified by the user
// check if the branch exits in the repository
gitout, err := ws.GitWithOutput(ctx, nil, "ls-remote", "--exit-code", "origin", ws.CloneTarget)
if err != nil || len(gitout) == 0 {
log.WithField("remoteURI", ws.RemoteURI).WithField("branch", ws.CloneTarget).Warnf("Invalid default branch name. Changing to %v", defaultBranch)
ws.CloneTarget = defaultBranch
}
}
} else {
// check remote branch exists before git checkout when the branch is not the default
gitout, err := ws.GitWithOutput(ctx, nil, "ls-remote", "--exit-code", "origin", ws.CloneTarget)
if err != nil || len(gitout) == 0 {
log.WithError(err).WithField("remoteURI", ws.RemoteURI).WithField("branch", ws.CloneTarget).Error("Remote branch doesn't exist.")
return xerrors.Errorf("Remote branch %v does not exist in %v", ws.CloneTarget, ws.RemoteURI)
}
// confirm the value of the default branch name using rev-parse
gitout, _ := ws.GitWithOutput(ctx, nil, "rev-parse", "--abbrev-ref", "origin/HEAD")
defaultBranch := strings.TrimSpace(strings.Replace(string(gitout), "origin/", "", -1))

branchName := ws.CloneTarget

// we already cloned the git repository but we need to check CloneTarget exists
// to avoid calling fetch from a non-existing branch
gitout, err := ws.GitWithOutput(ctx, nil, "ls-remote", "--exit-code", "origin", ws.CloneTarget)
if err != nil || len(gitout) == 0 {
log.WithField("remoteURI", ws.RemoteURI).WithField("branch", ws.CloneTarget).Warnf("Invalid default branch name. Changing to %v", defaultBranch)
ws.CloneTarget = defaultBranch
}

if err := ws.Git(ctx, "fetch", "--depth=1", "origin", ws.CloneTarget); err != nil {
log.WithError(err).WithField("remoteURI", ws.RemoteURI).WithField("branch", ws.CloneTarget).Error("Cannot fetch remote branch")
return err
}

if err := ws.Git(ctx, "checkout", ws.CloneTarget); err != nil {
log.WithError(err).WithField("remoteURI", ws.RemoteURI).WithField("branch", ws.CloneTarget).Error("Cannot fetch remote branch")
if err := ws.Git(ctx, "checkout", "-B", branchName, "origin/"+ws.CloneTarget); err != nil {
log.WithError(err).WithField("remoteURI", ws.RemoteURI).WithField("branch", branchName).Error("Cannot fetch remote branch")
return err
}
case LocalBranch:
Expand Down
117 changes: 117 additions & 0 deletions test/tests/components/ws-manager/additional_repositories_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package wsmanager

import (
"context"
"fmt"
"strings"
"testing"
"time"

"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"

csapi "github.com/gitpod-io/gitpod/content-service/api"
agent "github.com/gitpod-io/gitpod/test/pkg/agent/workspace/api"
"github.com/gitpod-io/gitpod/test/pkg/integration"
wsmanapi "github.com/gitpod-io/gitpod/ws-manager/api"
)

func TestAdditionalRepositories(t *testing.T) {
f := features.New("additional-repositories").
WithLabel("component", "ws-manager").
Assess("can open a workspace using the additionalRepositories property", func(_ context.Context, t *testing.T, cfg *envconf.Config) context.Context {
tests := []struct {
Name string
ContextURL string
CloneTaget string
}{
{
Name: "workspace with additionalRepositories using a branch",
ContextURL: "https://github.com/gitpod-io/gitpod-test-repo",
CloneTaget: "aledbf/test-additional-repositories",
},
{
Name: "workspace with additionalRepositories also using a branch in one of the additionalRepositories",
ContextURL: "https://github.com/gitpod-io/gitpod-test-repo",
CloneTaget: "aledbf/test-additional-repositories-with-branches",
},
}

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*len(tests))*time.Minute)
defer cancel()

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())
t.Cleanup(func() {
api.Done(t)
})

testRepoName := "gitpod-test-repo"
ws, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
req.Spec.WorkspaceLocation = testRepoName
req.Spec.Initializer = &csapi.WorkspaceInitializer{
Spec: &csapi.WorkspaceInitializer_Git{
Git: &csapi.GitInitializer{
RemoteUri: test.ContextURL,
CloneTaget: test.CloneTaget,
Config: &csapi.GitConfig{},
TargetMode: csapi.CloneTargetMode_REMOTE_BRANCH,
CheckoutLocation: testRepoName,
},
},
}

return nil
}))
if err != nil {
t.Fatal(err)
}

defer func() {
// stop workspace in defer function to prevent we forget to stop the workspace
if err := stopWorkspace(t, cfg, stopWs); err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
}()

rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(),
integration.WithInstanceID(ws.Req.Id),
)
if err != nil {
t.Fatal(err)
}

integration.DeferCloser(t, closer)
defer rsa.Close()

var gitOut agent.ExecResponse
err = rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
Dir: fmt.Sprintf("/workspace/%v", testRepoName),
Command: "bash",
Args: []string{
"-c",
"git symbolic-ref --short HEAD",
},
}, &gitOut)
if err != nil {
t.Fatal(err)
}

out := strings.TrimSpace(gitOut.Stdout)
if test.CloneTaget != out {
t.Errorf("returned branch %v is not %v", out, test.CloneTaget)
}
})
}

return ctx
}).
Feature()

testEnv.Test(t, f)
}