Skip to content

Commit e5fc288

Browse files
committed
implemented content update endpoint
Signed-off-by: Zaki Shaikh <[email protected]>
1 parent 0fc4cd4 commit e5fc288

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

scm/driver/stash/content.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,20 @@ func (s *contentService) Create(ctx context.Context, repo, path string, params *
5656
}
5757

5858
func (s *contentService) Update(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
59-
return nil, scm.ErrNotSupported
59+
namespace, repoName := scm.Split(repo)
60+
endpoint := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/browse/%s", namespace, repoName, path)
61+
message := params.Message
62+
if params.Signature.Name != "" && params.Signature.Email != "" {
63+
message = fmt.Sprintf("%s\nSigned-off-by: %s <%s>", params.Message, params.Signature.Name, params.Signature.Email)
64+
}
65+
in := &contentCreateUpdate{
66+
Message: message,
67+
Branch: params.Branch,
68+
Content: params.Data,
69+
Sha: params.Sha,
70+
}
71+
72+
return s.client.do(ctx, "PUT", endpoint, in, nil)
6073
}
6174

6275
func (s *contentService) Delete(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {

scm/driver/stash/content_test.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,37 @@ func TestContentCreate(t *testing.T) {
9191
}
9292

9393
if resp.Status != 200 {
94-
t.Error(err)
94+
t.Errorf("got %d", resp.Status)
9595
}
9696
}
9797

9898
func TestContentUpdate(t *testing.T) {
99-
content := new(contentService)
100-
_, err := content.Update(context.Background(), "atlassian/atlaskit", "README", nil)
101-
if err != scm.ErrNotSupported {
102-
t.Errorf("Expect Not Supported error")
99+
defer gock.Off()
100+
101+
gock.New("http://example.com:7990").
102+
Put("/rest/api/1.0/projects/octocat/repos/hello-world/browse/README").
103+
Reply(200).
104+
Type("application/json").
105+
File("testdata/content_update.json")
106+
107+
params := &scm.ContentParams{
108+
Message: "my commit message",
109+
Data: []byte("bXkgbmV3IGZpbGUgY29udGVudHM="),
110+
Sha: "95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
111+
Signature: scm.Signature{
112+
Name: "Zaki",
113+
114+
},
115+
}
116+
117+
client, _ := New("http://example.com:7990")
118+
resp, err := client.Contents.Update(context.Background(), "octocat/hello-world", "README", params)
119+
if err != nil {
120+
t.Error(err)
121+
}
122+
123+
if resp.Status != 200 {
124+
t.Errorf("got %d", resp.Status)
103125
}
104126
}
105127

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"id": "abcdef0123abcdef4567abcdef8987abcdef6543",
3+
"displayId": "abcdef0123a",
4+
"author": {
5+
"name": "Zaki",
6+
"emailAddress": "[email protected]"
7+
},
8+
"authorTimestamp": 1734286823374,
9+
"committer": {
10+
"name": "Zaki",
11+
"emailAddress": "[email protected]"
12+
},
13+
"committerTimestamp": 1734286823374,
14+
"message": "WIP on feature 1",
15+
"parents": [
16+
{
17+
"id": "abcdef0123abcdef4567abcdef8987abcdef6543",
18+
"displayId": "abcdef0"
19+
}
20+
]
21+
}

0 commit comments

Comments
 (0)