Skip to content

Commit 1f974ab

Browse files
Merge pull request #471 from zakisk/imple-create-ref-endpoint
Implement CreateRef Endpoint in Git Service for Bitbucket Server (stash)
2 parents 5298634 + ce86177 commit 1f974ab

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

scm/driver/stash/git.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ func (s *gitService) FindRef(ctx context.Context, repo, ref string) (string, *sc
4141
}
4242

4343
func (s *gitService) CreateRef(ctx context.Context, repo, ref, sha string) (*scm.Reference, *scm.Response, error) {
44-
return nil, nil, scm.ErrNotSupported
44+
namespace, repoName := scm.Split(repo)
45+
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/branches", namespace, repoName)
46+
out := new(branch)
47+
in := &createBranch{
48+
Name: ref,
49+
StartPoint: sha,
50+
}
51+
resp, err := s.client.do(ctx, "POST", path, in, out)
52+
return convertBranch(out), resp, err
4553
}
4654

4755
func (s *gitService) DeleteRef(ctx context.Context, repo, ref string) (*scm.Response, error) {
@@ -151,6 +159,11 @@ type branch struct {
151159
IsDefault bool `json:"isDefault"`
152160
}
153161

162+
type createBranch struct {
163+
Name string `json:"name"`
164+
StartPoint string `json:"startPoint"`
165+
}
166+
154167
type branches struct {
155168
pagination
156169
Values []*branch `json:"values"`

scm/driver/stash/git_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,34 @@ func TestGitFindCommit(t *testing.T) {
4444
}
4545
}
4646

47+
func TestGitCreateRef(t *testing.T) {
48+
defer gock.Off()
49+
50+
gock.New("http://example.com:7990").
51+
Post("/rest/api/1.0/projects/PRJ/repos/my-repo/branches").
52+
Reply(200).
53+
Type("application/json").
54+
File("testdata/create_ref.json")
55+
56+
client, _ := New("http://example.com:7990")
57+
got, _, err := client.Git.CreateRef(context.Background(), "PRJ/my-repo", "scm-branch", "refs/heads/main")
58+
if err != nil {
59+
t.Error(err)
60+
}
61+
62+
want := new(scm.Reference)
63+
raw, _ := os.ReadFile("testdata/create_ref.json.golden")
64+
err = json.Unmarshal(raw, &want)
65+
if err != nil {
66+
t.Error(err)
67+
}
68+
69+
if diff := cmp.Diff(want, got); diff != "" {
70+
t.Errorf("Unexpected Results")
71+
t.Log(diff)
72+
}
73+
}
74+
4775
func TestGitFindBranch(t *testing.T) {
4876
defer gock.Off()
4977

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "refs/heads/scm-branch",
3+
"displayId": "scm-branch",
4+
"type": "BRANCH",
5+
"latestCommit": "8d51122def5632836d1cb1026e879069e10a1e13",
6+
"latestChangeset": "8d51122def5632836d1cb1026e879069e10a1e13",
7+
"isDefault": false
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"Name": "scm-branch",
3+
"Path": "refs/heads/scm-branch",
4+
"Sha": "8d51122def5632836d1cb1026e879069e10a1e13"
5+
}

0 commit comments

Comments
 (0)