Skip to content

Commit

Permalink
implemented content update endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Zaki Shaikh <zashaikh@redhat.com>
  • Loading branch information
zakisk committed Jan 3, 2025
1 parent 0fc4cd4 commit e5fc288
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
15 changes: 14 additions & 1 deletion scm/driver/stash/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,20 @@ func (s *contentService) Create(ctx context.Context, repo, path string, params *
}

func (s *contentService) Update(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
return nil, scm.ErrNotSupported
namespace, repoName := scm.Split(repo)
endpoint := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/browse/%s", namespace, repoName, path)
message := params.Message
if params.Signature.Name != "" && params.Signature.Email != "" {
message = fmt.Sprintf("%s\nSigned-off-by: %s <%s>", params.Message, params.Signature.Name, params.Signature.Email)
}
in := &contentCreateUpdate{
Message: message,
Branch: params.Branch,
Content: params.Data,
Sha: params.Sha,
}

return s.client.do(ctx, "PUT", endpoint, in, nil)
}

func (s *contentService) Delete(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
Expand Down
32 changes: 27 additions & 5 deletions scm/driver/stash/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,37 @@ func TestContentCreate(t *testing.T) {
}

if resp.Status != 200 {
t.Error(err)
t.Errorf("got %d", resp.Status)
}
}

func TestContentUpdate(t *testing.T) {
content := new(contentService)
_, err := content.Update(context.Background(), "atlassian/atlaskit", "README", nil)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
defer gock.Off()

gock.New("http://example.com:7990").
Put("/rest/api/1.0/projects/octocat/repos/hello-world/browse/README").
Reply(200).
Type("application/json").
File("testdata/content_update.json")

params := &scm.ContentParams{
Message: "my commit message",
Data: []byte("bXkgbmV3IGZpbGUgY29udGVudHM="),
Sha: "95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
Signature: scm.Signature{
Name: "Zaki",
Email: "zaki@example.com",
},
}

client, _ := New("http://example.com:7990")
resp, err := client.Contents.Update(context.Background(), "octocat/hello-world", "README", params)
if err != nil {
t.Error(err)
}

if resp.Status != 200 {
t.Errorf("got %d", resp.Status)
}
}

Expand Down
21 changes: 21 additions & 0 deletions scm/driver/stash/testdata/content_update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"id": "abcdef0123abcdef4567abcdef8987abcdef6543",
"displayId": "abcdef0123a",
"author": {
"name": "Zaki",
"emailAddress": "zaki@example.com"
},
"authorTimestamp": 1734286823374,
"committer": {
"name": "Zaki",
"emailAddress": "zaki@example.com"
},
"committerTimestamp": 1734286823374,
"message": "WIP on feature 1",
"parents": [
{
"id": "abcdef0123abcdef4567abcdef8987abcdef6543",
"displayId": "abcdef0"
}
]
}

0 comments on commit e5fc288

Please sign in to comment.