diff options
author | James Fargher <jfargher@gitlab.com> | 2022-06-17 04:10:50 +0300 |
---|---|---|
committer | James Fargher <jfargher@gitlab.com> | 2022-07-04 04:44:20 +0300 |
commit | 2d3e1b91b6ca7d904acc018df8b32f191474c550 (patch) | |
tree | 142cb9d5bc8b74261cbb58591f78581905005a7a | |
parent | 19f47f2757ab7afdbb22e0ae7a0a60e96cc773e3 (diff) |
Implement FullPathfull_path_rpc
FullPath reads the path from the repository's gitconfig under the key
"gitlab.fullpath".
-rw-r--r-- | internal/gitaly/service/repository/fullpath.go | 25 | ||||
-rw-r--r-- | internal/gitaly/service/repository/fullpath_test.go | 67 |
2 files changed, 92 insertions, 0 deletions
diff --git a/internal/gitaly/service/repository/fullpath.go b/internal/gitaly/service/repository/fullpath.go index df4379017..2f5194906 100644 --- a/internal/gitaly/service/repository/fullpath.go +++ b/internal/gitaly/service/repository/fullpath.go @@ -2,7 +2,10 @@ package repository import ( "context" + "strings" + "gitlab.com/gitlab-org/gitaly/v15/internal/errors" + "gitlab.com/gitlab-org/gitaly/v15/internal/git" "gitlab.com/gitlab-org/gitaly/v15/internal/helper" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" ) @@ -31,3 +34,25 @@ func (s *server) SetFullPath( return &gitalypb.SetFullPathResponse{}, nil } + +// FullPath reads the path from the repository's gitconfig under the +// "gitlab.fullpath" key. +func (s *server) FullPath(ctx context.Context, request *gitalypb.FullPathRequest) (*gitalypb.FullPathResponse, error) { + if request.GetRepository() == nil { + return nil, helper.ErrInvalidArgument(errors.ErrEmptyRepository) + } + + repo := s.localrepo(request.GetRepository()) + var stdout strings.Builder + err := repo.ExecAndWait(ctx, git.SubCmd{ + Name: "config", + Args: []string{fullPathKey}, + }, git.WithStdout(&stdout)) + if err != nil { + return nil, helper.ErrInternalf("fetch config: %w", err) + } + + return &gitalypb.FullPathResponse{ + Path: strings.TrimSuffix(stdout.String(), "\n"), + }, nil +} diff --git a/internal/gitaly/service/repository/fullpath_test.go b/internal/gitaly/service/repository/fullpath_test.go index 071ce13a8..36b3947f7 100644 --- a/internal/gitaly/service/repository/fullpath_test.go +++ b/internal/gitaly/service/repository/fullpath_test.go @@ -13,6 +13,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/helper/text" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" + "google.golang.org/grpc/codes" ) func TestSetFullPath(t *testing.T) { @@ -145,3 +146,69 @@ func TestSetFullPath(t *testing.T) { require.Equal(t, "replace", text.ChompBytes(fullPath)) }) } + +func TestFullPath(t *testing.T) { + t.Parallel() + ctx := testhelper.Context(t) + + cfg, client := setupRepositoryServiceWithoutRepo(t) + + t.Run("missing repository", func(t *testing.T) { + response, err := client.FullPath(ctx, &gitalypb.FullPathRequest{ + Repository: nil, + }) + require.Nil(t, response) + require.Error(t, err) + require.Contains(t, err.Error(), "empty Repository") + testhelper.RequireGrpcCode(t, err, codes.InvalidArgument) + }) + + t.Run("nonexistent repository", func(t *testing.T) { + repo := &gitalypb.Repository{ + RelativePath: "/path/to/repo.git", + StorageName: cfg.Storages[0].Name, + } + repoPath, err := config.NewLocator(cfg).GetPath(repo) + require.NoError(t, err) + + response, err := client.FullPath(ctx, &gitalypb.FullPathRequest{ + Repository: repo, + }) + require.Nil(t, response) + + expectedErr := fmt.Sprintf("rpc error: code = NotFound desc = fetch config: rpc "+ + "error: code = NotFound desc = GetRepoPath: not a git repository: %q", repoPath) + if testhelper.IsPraefectEnabled() { + expectedErr = `rpc error: code = NotFound desc = accessor call: route repository accessor: consistent storages: repository "default"/"/path/to/repo.git" not found` + } + + require.EqualError(t, err, expectedErr) + testhelper.RequireGrpcCode(t, err, codes.NotFound) + }) + + t.Run("missing config", func(t *testing.T) { + repo, repoPath := gittest.CreateRepository(ctx, t, cfg) + + configPath := filepath.Join(repoPath, "config") + require.NoError(t, os.Remove(configPath)) + + response, err := client.FullPath(ctx, &gitalypb.FullPathRequest{ + Repository: repo, + }) + require.Nil(t, response) + require.EqualError(t, err, "rpc error: code = Internal desc = fetch config: exit status 1") + testhelper.RequireGrpcCode(t, err, codes.Internal) + }) + + t.Run("existing config", func(t *testing.T) { + repo, repoPath := gittest.CreateRepository(ctx, t, cfg) + + gittest.Exec(t, cfg, "-C", repoPath, "config", "--add", fullPathKey, "foo/bar") + + response, err := client.FullPath(ctx, &gitalypb.FullPathRequest{ + Repository: repo, + }) + require.NoError(t, err) + testhelper.ProtoEqual(t, &gitalypb.FullPathResponse{Path: "foo/bar"}, response) + }) +} |