diff --git a/scm/driver/stash/content.go b/scm/driver/stash/content.go index b2cd1364..603d7d33 100644 --- a/scm/driver/stash/content.go +++ b/scm/driver/stash/content.go @@ -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) { diff --git a/scm/driver/stash/content_test.go b/scm/driver/stash/content_test.go index 1bc67cef..4d531233 100644 --- a/scm/driver/stash/content_test.go +++ b/scm/driver/stash/content_test.go @@ -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) } } diff --git a/scm/driver/stash/testdata/content_update.json b/scm/driver/stash/testdata/content_update.json new file mode 100644 index 00000000..574ac8dd --- /dev/null +++ b/scm/driver/stash/testdata/content_update.json @@ -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" + } + ] +} \ No newline at end of file