From ce86177a35d02c880e76b8e3af38dd41394b2eb7 Mon Sep 17 00:00:00 2001 From: Zaki Shaikh Date: Tue, 24 Dec 2024 21:27:16 +0530 Subject: [PATCH] implement create ref endpoint Signed-off-by: Zaki Shaikh --- scm/driver/stash/git.go | 15 +++++++++- scm/driver/stash/git_test.go | 28 +++++++++++++++++++ scm/driver/stash/testdata/create_ref.json | 8 ++++++ .../stash/testdata/create_ref.json.golden | 5 ++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 scm/driver/stash/testdata/create_ref.json create mode 100644 scm/driver/stash/testdata/create_ref.json.golden diff --git a/scm/driver/stash/git.go b/scm/driver/stash/git.go index 9a2f6398..38d8f54f 100644 --- a/scm/driver/stash/git.go +++ b/scm/driver/stash/git.go @@ -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) { @@ -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"` diff --git a/scm/driver/stash/git_test.go b/scm/driver/stash/git_test.go index 641e8b80..280b4390 100644 --- a/scm/driver/stash/git_test.go +++ b/scm/driver/stash/git_test.go @@ -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() diff --git a/scm/driver/stash/testdata/create_ref.json b/scm/driver/stash/testdata/create_ref.json new file mode 100644 index 00000000..fe7945b2 --- /dev/null +++ b/scm/driver/stash/testdata/create_ref.json @@ -0,0 +1,8 @@ +{ + "id": "refs/heads/scm-branch", + "displayId": "scm-branch", + "type": "BRANCH", + "latestCommit": "8d51122def5632836d1cb1026e879069e10a1e13", + "latestChangeset": "8d51122def5632836d1cb1026e879069e10a1e13", + "isDefault": false +} \ No newline at end of file diff --git a/scm/driver/stash/testdata/create_ref.json.golden b/scm/driver/stash/testdata/create_ref.json.golden new file mode 100644 index 00000000..91d9ab2d --- /dev/null +++ b/scm/driver/stash/testdata/create_ref.json.golden @@ -0,0 +1,5 @@ +{ + "Name": "scm-branch", + "Path": "refs/heads/scm-branch", + "Sha": "8d51122def5632836d1cb1026e879069e10a1e13" +} \ No newline at end of file