diff options
author | Ahmad Sherif <me@ahmadsherif.com> | 2017-08-28 14:46:41 +0300 |
---|---|---|
committer | Ahmad Sherif <me@ahmadsherif.com> | 2017-08-31 13:28:48 +0300 |
commit | c455fa83339a5da75ea02eaeef3f7d72b59ec6c1 (patch) | |
tree | b7d67deee07fbce538667c80c0f2675e3d040ce5 | |
parent | af66130f437865ef5a9a6dd0c1b1233bcaf50540 (diff) |
Implement FindBranch RPC
Closes gitaly#501
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | internal/service/ref/branches.go | 12 | ||||
-rw-r--r-- | internal/service/ref/branches_test.go | 96 | ||||
-rw-r--r-- | ruby/lib/gitaly_server/ref_service.rb | 14 |
4 files changed, 123 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cf30bbef..1efd299a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ UNRELEASED - Terminate commands when their context cancels https://gitlab.com/gitlab-org/gitaly/merge_requests/318 +- Implement FindBranch RPC + https://gitlab.com/gitlab-org/gitaly/merge_requests/315 - Implement {Create,Delete}Branch RPCs https://gitlab.com/gitlab-org/gitaly/merge_requests/311 diff --git a/internal/service/ref/branches.go b/internal/service/ref/branches.go index 06724df86..5b4e12e4f 100644 --- a/internal/service/ref/branches.go +++ b/internal/service/ref/branches.go @@ -37,5 +37,15 @@ func (s *server) DeleteBranch(ctx context.Context, req *pb.DeleteBranchRequest) } func (s *server) FindBranch(ctx context.Context, req *pb.FindBranchRequest) (*pb.FindBranchResponse, error) { - return nil, nil + client, err := rubyserver.RefServiceClient(ctx) + if err != nil { + return nil, err + } + + clientCtx, err := rubyserver.SetHeaders(ctx, req.GetRepository()) + if err != nil { + return nil, err + } + + return client.FindBranch(clientCtx, req) } diff --git a/internal/service/ref/branches_test.go b/internal/service/ref/branches_test.go index 1b6c21360..585f3b467 100644 --- a/internal/service/ref/branches_test.go +++ b/internal/service/ref/branches_test.go @@ -208,3 +208,99 @@ func TestFailedDeleteBranchRequest(t *testing.T) { }) } } + +func TestSuccessfulFindBranchRequest(t *testing.T) { + server := runRefServiceServer(t) + defer server.Stop() + + client, conn := newRefClient(t) + defer conn.Close() + + branchNameInput := "master" + branchTarget, err := log.GetCommit(context.Background(), testRepo, branchNameInput, "") + require.NoError(t, err) + + branch := &pb.Branch{ + Name: []byte(branchNameInput), + TargetCommit: branchTarget, + } + + testCases := []struct { + desc string + branchName string + expectedBranch *pb.Branch + }{ + { + desc: "regular branch name", + branchName: branchNameInput, + expectedBranch: branch, + }, + { + desc: "absolute reference path", + branchName: "refs/heads/" + branchNameInput, + expectedBranch: branch, + }, + { + desc: "heads path", + branchName: "heads/" + branchNameInput, + expectedBranch: branch, + }, + { + desc: "non-existent branch", + branchName: "i-do-not-exist-on-this-repo", + }, + } + + for _, testCase := range testCases { + t.Run(testCase.desc, func(t *testing.T) { + request := &pb.FindBranchRequest{ + Repository: testRepo, + Name: []byte(testCase.branchName), + } + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + response, err := client.FindBranch(ctx, request) + + require.NoError(t, err) + require.Equal(t, testCase.expectedBranch, response.Branch, "mismatched branches") + }) + } +} + +func TestFailedFindBranchRequest(t *testing.T) { + server := runRefServiceServer(t) + defer server.Stop() + + client, conn := newRefClient(t) + defer conn.Close() + + testCases := []struct { + desc string + branchName string + code codes.Code + }{ + { + desc: "empty branch name", + branchName: "", + code: codes.InvalidArgument, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.desc, func(t *testing.T) { + + request := &pb.FindBranchRequest{ + Repository: testRepo, + Name: []byte(testCase.branchName), + } + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + _, err := client.FindBranch(ctx, request) + testhelper.AssertGrpcError(t, err, testCase.code, "") + }) + } +} diff --git a/ruby/lib/gitaly_server/ref_service.rb b/ruby/lib/gitaly_server/ref_service.rb index 158a7f803..7abed4da9 100644 --- a/ruby/lib/gitaly_server/ref_service.rb +++ b/ruby/lib/gitaly_server/ref_service.rb @@ -41,5 +41,19 @@ module GitalyServer rescue Rugged::ReferenceError => e raise GRPC::Internal.new(e.to_s) end + + def find_branch(request, _call) + branch_name = request.name + raise GRPC::InvalidArgument.new("empty Name") if branch_name.empty? + + repo = Gitlab::Git::Repository.from_call(_call) + rugged_branch = repo.find_branch(branch_name) + gitaly_branch = Gitaly::Branch.new( + name: rugged_branch.name.b, + target_commit: gitaly_commit_from_rugged(rugged_branch.dereferenced_target.raw_commit), + ) unless rugged_branch.nil? + + Gitaly::FindBranchResponse.new(branch: gitaly_branch) + end end end |