From 186a80f1d2f80788ac1384ead507631508a1a74a Mon Sep 17 00:00:00 2001 From: Zaki Shaikh Date: Thu, 26 Dec 2024 15:45:40 +0530 Subject: [PATCH] Implement delete ref endpoint in git service implemented delete ref endpoint in git service for Bitbucket server. Signed-off-by: Zaki Shaikh --- scm/driver/stash/git.go | 10 +++++++++- scm/driver/stash/git_test.go | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/scm/driver/stash/git.go b/scm/driver/stash/git.go index 9a2f6398..d52d194b 100644 --- a/scm/driver/stash/git.go +++ b/scm/driver/stash/git.go @@ -45,7 +45,10 @@ func (s *gitService) CreateRef(ctx context.Context, repo, ref, sha string) (*scm } func (s *gitService) DeleteRef(ctx context.Context, repo, ref string) (*scm.Response, error) { - return nil, scm.ErrNotSupported + namespace, name := scm.Split(repo) + path := fmt.Sprintf("rest/branch-utils/latest/projects/%s/repos/%s/branches", namespace, name) + in := deleteRefInput{Name: ref} + return s.client.do(ctx, "DELETE", path, &in, nil) } func (s *gitService) FindBranch(ctx context.Context, repo, branch string) (*scm.Reference, *scm.Response, error) { @@ -142,6 +145,11 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string return convertDiffstats(out), res, err } +type deleteRefInput struct { + DryRun bool `json:"dryRun"` + Name string `json:"name"` +} + type branch struct { ID string `json:"id"` DisplayID string `json:"displayId"` diff --git a/scm/driver/stash/git_test.go b/scm/driver/stash/git_test.go index 641e8b80..4083da3e 100644 --- a/scm/driver/stash/git_test.go +++ b/scm/driver/stash/git_test.go @@ -44,6 +44,25 @@ func TestGitFindCommit(t *testing.T) { } } +func TestGitDeleteRef(t *testing.T) { + defer gock.Off() + + gock.New("http://example.com:7990"). + Delete("rest/branch-utils/latest/projects/PRJ/repos/my-repo/branches"). + Reply(204). + Type("application/json") + + client, _ := New("http://example.com:7990") + resp, err := client.Git.DeleteRef(context.Background(), "PRJ/my-repo", "delete") + if err != nil { + t.Error(err) + } + + if resp.Status != 204 { + t.Errorf("DeleteRef returned %v", resp.Status) + } +} + func TestGitFindBranch(t *testing.T) { defer gock.Off()