Skip to content

Commit 5d633c6

Browse files
Merge pull request #54 from soulseen/support_content
Support content create and update
2 parents 6b7893e + 55896a1 commit 5d633c6

File tree

11 files changed

+335
-41
lines changed

11 files changed

+335
-41
lines changed

scm/content.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ type (
1616
// ContentParams provide parameters for creating and
1717
// updating repository content.
1818
ContentParams struct {
19-
Ref string
20-
Branch string
21-
Message string
22-
Data []byte
19+
Ref string
20+
Branch string
21+
Message string
22+
Data []byte
23+
Sha string
24+
Signature Signature
2325
}
2426

2527
// ContentInfo stores the kind of any content in a repository.

scm/driver/gitea/webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func convertBranchHook(dst *createHook, action scm.Action) *scm.BranchHook {
209209
func convertPushHook(dst *pushHook) *scm.PushHook {
210210
if len(dst.Commits) > 0 {
211211
return &scm.PushHook{
212-
Ref: dst.Ref,
212+
Ref: dst.Ref,
213213
Before: dst.Before,
214214
Commit: scm.Commit{
215215
Sha: dst.After,

scm/driver/github/content.go

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"context"
99
"encoding/base64"
1010
"fmt"
11-
"time"
12-
1311
"github.com/drone/go-scm/scm"
1412
)
1513

@@ -29,11 +27,44 @@ func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm
2927
}
3028

3129
func (s *contentService) Create(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
32-
return nil, scm.ErrNotSupported
30+
endpoint := fmt.Sprintf("repos/%s/contents/%s", repo, path)
31+
in := &contentCreateUpdate{
32+
Message: params.Message,
33+
Branch: params.Branch,
34+
Content: params.Data,
35+
Sha: params.Sha,
36+
Committer: commitAuthor{
37+
Name: params.Signature.Name,
38+
Email: params.Signature.Email,
39+
},
40+
Author: commitAuthor{
41+
Name: params.Signature.Name,
42+
Email: params.Signature.Email,
43+
},
44+
}
45+
46+
res, err := s.client.do(ctx, "PUT", endpoint, in, nil)
47+
return res, err
3348
}
3449

3550
func (s *contentService) Update(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
36-
return nil, scm.ErrNotSupported
51+
endpoint := fmt.Sprintf("repos/%s/contents/%s", repo, path)
52+
in := &contentCreateUpdate{
53+
Message: params.Message,
54+
Branch: params.Branch,
55+
Content: params.Data,
56+
Sha: params.Sha,
57+
Committer: commitAuthor{
58+
Name: params.Signature.Name,
59+
Email: params.Signature.Email,
60+
},
61+
Author: commitAuthor{
62+
Name: params.Signature.Name,
63+
Email: params.Signature.Email,
64+
},
65+
}
66+
res, err := s.client.do(ctx, "PUT", endpoint, in, nil)
67+
return res, err
3768
}
3869

3970
func (s *contentService) Delete(ctx context.Context, repo, path, ref string) (*scm.Response, error) {
@@ -55,20 +86,18 @@ type content struct {
5586
Type string `json:"type"`
5687
}
5788

58-
type contentUpdate struct {
59-
Sha string `json:"sha"`
60-
Message string `json:"message"`
61-
HTMLURL string `json:"html_url"`
62-
Author struct {
63-
Name string `json:"name"`
64-
Email string `json:"email"`
65-
Date time.Time `json:"date"`
66-
} `json:"author"`
67-
Committer struct {
68-
Name string `json:"name"`
69-
Email string `json:"email"`
70-
Date time.Time `json:"date"`
71-
} `json:"committer"`
89+
type contentCreateUpdate struct {
90+
Branch string `json:"branch"`
91+
Message string `json:"message"`
92+
Content []byte `json:"content"`
93+
Sha string `json:"sha"`
94+
Author commitAuthor `json:"author"`
95+
Committer commitAuthor `json:"committer"`
96+
}
97+
98+
type commitAuthor struct {
99+
Name string `json:"name"`
100+
Email string `json:"email"`
72101
}
73102

74103
func convertContentInfoList(from []*content) []*scm.ContentInfo {

scm/driver/github/content_test.go

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,77 @@ func TestContentFind(t *testing.T) {
5252
}
5353

5454
func TestContentCreate(t *testing.T) {
55-
content := new(contentService)
56-
_, err := content.Create(context.Background(), "octocat/hello-world", "README", nil)
57-
if err != scm.ErrNotSupported {
58-
t.Errorf("Expect Not Supported error")
55+
defer gock.Off()
56+
57+
gock.New("https://api.github.com").
58+
Put("/repos/octocat/hello-world/contents/test/hello").
59+
Reply(201).
60+
Type("application/json").
61+
SetHeaders(mockHeaders).
62+
File("testdata/content_create.json")
63+
64+
params := &scm.ContentParams{
65+
Message: "my commit message",
66+
Data: []byte("bXkgbmV3IGZpbGUgY29udGVudHM="),
67+
Signature: scm.Signature{
68+
Name: "Monalisa Octocat",
69+
70+
},
71+
}
72+
73+
client := NewDefault()
74+
res, err := client.Contents.Create(
75+
context.Background(),
76+
"octocat/hello-world",
77+
"test/hello",
78+
params,
79+
)
80+
81+
if err != nil {
82+
t.Error(err)
83+
return
84+
}
85+
86+
if res.Status != 201 {
87+
t.Errorf("Unexpected Results")
5988
}
6089
}
6190

6291
func TestContentUpdate(t *testing.T) {
63-
content := new(contentService)
64-
_, err := content.Update(context.Background(), "octocat/hello-world", "README", nil)
65-
if err != scm.ErrNotSupported {
66-
t.Errorf("Expect Not Supported error")
92+
defer gock.Off()
93+
94+
gock.New("https://api.github.com").
95+
Put("/repos/octocat/hello-world/contents/test/hello").
96+
Reply(200).
97+
Type("application/json").
98+
SetHeaders(mockHeaders).
99+
File("testdata/content_update.json")
100+
101+
params := &scm.ContentParams{
102+
Message: "a new commit message",
103+
Data: []byte("bXkgdXBkYXRlZCBmaWxlIGNvbnRlbnRz"),
104+
Sha: "95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
105+
Signature: scm.Signature{
106+
Name: "Monalisa Octocat",
107+
108+
},
109+
}
110+
111+
client := NewDefault()
112+
res, err := client.Contents.Create(
113+
context.Background(),
114+
"octocat/hello-world",
115+
"test/hello",
116+
params,
117+
)
118+
119+
if err != nil {
120+
t.Error(err)
121+
return
122+
}
123+
124+
if res.Status != 200 {
125+
t.Errorf("Unexpected Results")
67126
}
68127
}
69128

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"content": {
3+
"name": "hello.txt",
4+
"path": "notes/hello.txt",
5+
"sha": "95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
6+
"size": 9,
7+
"url": "https://api.github.com/repos/octocat/Hello-World/contents/notes/hello.txt",
8+
"html_url": "https://github.com/octocat/Hello-World/blob/master/notes/hello.txt",
9+
"git_url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
10+
"download_url": "https://raw.githubusercontent.com/octocat/HelloWorld/master/notes/hello.txt",
11+
"type": "file",
12+
"_links": {
13+
"self": "https://api.github.com/repos/octocat/Hello-World/contents/notes/hello.txt",
14+
"git": "https://api.github.com/repos/octocat/Hello-World/git/blobs/95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
15+
"html": "https://github.com/octocat/Hello-World/blob/master/notes/hello.txt"
16+
}
17+
},
18+
"commit": {
19+
"sha": "7638417db6d59f3c431d3e1f261cc637155684cd",
20+
"node_id": "MDY6Q29tbWl0NzYzODQxN2RiNmQ1OWYzYzQzMWQzZTFmMjYxY2M2MzcxNTU2ODRjZA==",
21+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/7638417db6d59f3c431d3e1f261cc637155684cd",
22+
"html_url": "https://github.com/octocat/Hello-World/git/commit/7638417db6d59f3c431d3e1f261cc637155684cd",
23+
"author": {
24+
"date": "2014-11-07T22:01:45Z",
25+
"name": "Monalisa Octocat",
26+
"email": "[email protected]"
27+
},
28+
"committer": {
29+
"date": "2014-11-07T22:01:45Z",
30+
"name": "Monalisa Octocat",
31+
"email": "[email protected]"
32+
},
33+
"message": "my commit message",
34+
"tree": {
35+
"url": "https://api.github.com/repos/octocat/Hello-World/git/trees/691272480426f78a0138979dd3ce63b77f706feb",
36+
"sha": "691272480426f78a0138979dd3ce63b77f706feb"
37+
},
38+
"parents": [
39+
{
40+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/1acc419d4d6a9ce985db7be48c6349a0475975b5",
41+
"html_url": "https://github.com/octocat/Hello-World/git/commit/1acc419d4d6a9ce985db7be48c6349a0475975b5",
42+
"sha": "1acc419d4d6a9ce985db7be48c6349a0475975b5"
43+
}
44+
],
45+
"verification": {
46+
"verified": false,
47+
"reason": "unsigned",
48+
"signature": null,
49+
"payload": null
50+
}
51+
}
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"content": {
3+
"name": "hello.txt",
4+
"path": "notes/hello.txt",
5+
"sha": "a56507ed892d05a37c6d6128c260937ea4d287bd",
6+
"size": 9,
7+
"url": "https://api.github.com/repos/octocat/Hello-World/contents/notes/hello.txt",
8+
"html_url": "https://github.com/octocat/Hello-World/blob/master/notes/hello.txt",
9+
"git_url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/a56507ed892d05a37c6d6128c260937ea4d287bd",
10+
"download_url": "https://raw.githubusercontent.com/octocat/HelloWorld/master/notes/hello.txt",
11+
"type": "file",
12+
"_links": {
13+
"self": "https://api.github.com/repos/octocat/Hello-World/contents/notes/hello.txt",
14+
"git": "https://api.github.com/repos/octocat/Hello-World/git/blobs/a56507ed892d05a37c6d6128c260937ea4d287bd",
15+
"html": "https://github.com/octocat/Hello-World/blob/master/notes/hello.txt"
16+
}
17+
},
18+
"commit": {
19+
"sha": "18a43cd8e1e3a79c786e3d808a73d23b6d212b16",
20+
"node_id": "MDY6Q29tbWl0MThhNDNjZDhlMWUzYTc5Yzc4NmUzZDgwOGE3M2QyM2I2ZDIxMmIxNg==",
21+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/18a43cd8e1e3a79c786e3d808a73d23b6d212b16",
22+
"html_url": "https://github.com/octocat/Hello-World/git/commit/18a43cd8e1e3a79c786e3d808a73d23b6d212b16",
23+
"author": {
24+
"date": "2014-11-07T22:01:45Z",
25+
"name": "Monalisa Octocat",
26+
"email": "[email protected]"
27+
},
28+
"committer": {
29+
"date": "2014-11-07T22:01:45Z",
30+
"name": "Monalisa Octocat",
31+
"email": "[email protected]"
32+
},
33+
"message": "my commit message",
34+
"tree": {
35+
"url": "https://api.github.com/repos/octocat/Hello-World/git/trees/9a21f8e2018f42ffcf369b24d2cd20bc25c9e66f",
36+
"sha": "9a21f8e2018f42ffcf369b24d2cd20bc25c9e66f"
37+
},
38+
"parents": [
39+
{
40+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/da5a433788da5c255edad7979b328b67d79f53f6",
41+
"html_url": "https://github.com/octocat/Hello-World/git/commit/da5a433788da5c255edad7979b328b67d79f53f6",
42+
"sha": "da5a433788da5c255edad7979b328b67d79f53f6"
43+
}
44+
],
45+
"verification": {
46+
"verified": false,
47+
"reason": "unsigned",
48+
"signature": null,
49+
"payload": null
50+
}
51+
}
52+
}

scm/driver/gitlab/content.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,36 @@ func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm
4141
}
4242

4343
func (s *contentService) Create(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
44-
return nil, scm.ErrNotSupported
44+
path = url.QueryEscape(path)
45+
path = strings.Replace(path, ".", "%2E", -1)
46+
endpoint := fmt.Sprintf("api/v4/projects/%s/repository/files/%s", encode(repo), path)
47+
in := &createUpdateContent{
48+
Branch: params.Branch,
49+
Content: params.Data,
50+
CommitMessage: params.Message,
51+
Encoding: "base64",
52+
AuthorName: params.Signature.Name,
53+
AuthorEmail: params.Signature.Email,
54+
}
55+
res, err := s.client.do(ctx, "POST", endpoint, in, nil)
56+
return res, err
57+
4558
}
4659

4760
func (s *contentService) Update(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
48-
return nil, scm.ErrNotSupported
61+
path = url.QueryEscape(path)
62+
path = strings.Replace(path, ".", "%2E", -1)
63+
endpoint := fmt.Sprintf("api/v4/projects/%s/repository/files/%s", encode(repo), path)
64+
in := &createUpdateContent{
65+
Branch: params.Branch,
66+
Content: params.Data,
67+
CommitMessage: params.Message,
68+
Encoding: "base64",
69+
AuthorName: params.Signature.Name,
70+
AuthorEmail: params.Signature.Email,
71+
}
72+
res, err := s.client.do(ctx, "PUT", endpoint, in, nil)
73+
return res, err
4974
}
5075

5176
func (s *contentService) Delete(ctx context.Context, repo, path, ref string) (*scm.Response, error) {
@@ -71,6 +96,15 @@ type content struct {
7196
LastCommitID string `json:"last_commit_id"`
7297
}
7398

99+
type createUpdateContent struct {
100+
Branch string `json:"branch"`
101+
Content []byte `json:"content"`
102+
CommitMessage string `json:"commit_message"`
103+
Encoding string `json:"encoding"`
104+
AuthorEmail string `json:"author_email"`
105+
AuthorName string `json:"author_name"`
106+
}
107+
74108
type object struct {
75109
Path string `json:"path"`
76110
Mode string `json:"mode"`

0 commit comments

Comments
 (0)