Skip to content

Commit

Permalink
Merge pull request #471 from zakisk/imple-create-ref-endpoint
Browse files Browse the repository at this point in the history
Implement CreateRef Endpoint in Git Service for Bitbucket Server (stash)
  • Loading branch information
jenkins-x-bot authored Dec 27, 2024
2 parents 5298634 + ce86177 commit 1f974ab
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
15 changes: 14 additions & 1 deletion scm/driver/stash/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ func (s *gitService) FindRef(ctx context.Context, repo, ref string) (string, *sc
}

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

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

type createBranch struct {
Name string `json:"name"`
StartPoint string `json:"startPoint"`
}

type branches struct {
pagination
Values []*branch `json:"values"`
Expand Down
28 changes: 28 additions & 0 deletions scm/driver/stash/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,34 @@ func TestGitFindCommit(t *testing.T) {
}
}

func TestGitCreateRef(t *testing.T) {
defer gock.Off()

gock.New("http://example.com:7990").
Post("/rest/api/1.0/projects/PRJ/repos/my-repo/branches").
Reply(200).
Type("application/json").
File("testdata/create_ref.json")

client, _ := New("http://example.com:7990")
got, _, err := client.Git.CreateRef(context.Background(), "PRJ/my-repo", "scm-branch", "refs/heads/main")
if err != nil {
t.Error(err)
}

want := new(scm.Reference)
raw, _ := os.ReadFile("testdata/create_ref.json.golden")
err = json.Unmarshal(raw, &want)
if err != nil {
t.Error(err)
}

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}

func TestGitFindBranch(t *testing.T) {
defer gock.Off()

Expand Down
8 changes: 8 additions & 0 deletions scm/driver/stash/testdata/create_ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "refs/heads/scm-branch",
"displayId": "scm-branch",
"type": "BRANCH",
"latestCommit": "8d51122def5632836d1cb1026e879069e10a1e13",
"latestChangeset": "8d51122def5632836d1cb1026e879069e10a1e13",
"isDefault": false
}
5 changes: 5 additions & 0 deletions scm/driver/stash/testdata/create_ref.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Name": "scm-branch",
"Path": "refs/heads/scm-branch",
"Sha": "8d51122def5632836d1cb1026e879069e10a1e13"
}

0 comments on commit 1f974ab

Please sign in to comment.