Skip to content

Commit d8fc906

Browse files
Merge pull request #473 from zakisk/imple-delete-ref-endpoint
Implement Delete Ref Endpoint in Git Service for Bitbucket Server (stash)
2 parents 1269559 + 4096de8 commit d8fc906

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

scm/driver/stash/git.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ func (s *gitService) CreateRef(ctx context.Context, repo, ref, sha string) (*scm
5353
}
5454

5555
func (s *gitService) DeleteRef(ctx context.Context, repo, ref string) (*scm.Response, error) {
56-
return nil, scm.ErrNotSupported
56+
namespace, name := scm.Split(repo)
57+
path := fmt.Sprintf("rest/branch-utils/latest/projects/%s/repos/%s/branches", namespace, name)
58+
in := deleteRefInput{Name: ref}
59+
return s.client.do(ctx, "DELETE", path, &in, nil)
5760
}
5861

5962
func (s *gitService) FindBranch(ctx context.Context, repo, branch string) (*scm.Reference, *scm.Response, error) {
@@ -150,6 +153,12 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string
150153
return convertDiffstats(out), res, err
151154
}
152155

156+
type deleteRefInput struct {
157+
DryRun bool `json:"dryRun,omitempty"`
158+
EndPoint string `json:"endPoint,omitempty"`
159+
Name string `json:"name,omitempty"`
160+
}
161+
153162
type branch struct {
154163
ID string `json:"id"`
155164
DisplayID string `json:"displayId"`

scm/driver/stash/git_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ func TestGitCreateRef(t *testing.T) {
7272
}
7373
}
7474

75+
func TestGitDeleteRef(t *testing.T) {
76+
defer gock.Off()
77+
78+
gock.New("http://example.com:7990").
79+
Delete("rest/branch-utils/latest/projects/PRJ/repos/my-repo/branches").
80+
Reply(204).
81+
Type("application/json")
82+
83+
client, _ := New("http://example.com:7990")
84+
resp, err := client.Git.DeleteRef(context.Background(), "PRJ/my-repo", "delete")
85+
if err != nil {
86+
t.Error(err)
87+
}
88+
89+
if resp.Status != 204 {
90+
t.Errorf("DeleteRef returned %v", resp.Status)
91+
}
92+
}
93+
7594
func TestGitFindBranch(t *testing.T) {
7695
defer gock.Off()
7796

0 commit comments

Comments
 (0)