Skip to content

Commit 68a90bc

Browse files
author
zhuxiaoyang
committed
support create & update content with github
Signed-off-by: zhuxiaoyang <sunzhu@yunify.com>
1 parent ee0770c commit 68a90bc

File tree

6 files changed

+218
-32
lines changed

6 files changed

+218
-32
lines changed

scm/content.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
package scm
66

7-
import "context"
7+
import (
8+
"context"
9+
)
810

911
type (
1012
// Content stores the contents of a repository file.
@@ -16,10 +18,11 @@ type (
1618
// ContentParams provide parameters for creating and
1719
// updating repository content.
1820
ContentParams struct {
19-
Ref string
20-
Branch string
21-
Message string
22-
Data []byte
21+
Branch string
22+
Message string
23+
Data []byte
24+
SHA string
25+
Signature Signature
2326
}
2427

2528
// 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: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import (
88
"context"
99
"encoding/base64"
1010
"fmt"
11-
"time"
12-
1311
"github.com/drone/go-scm/scm"
12+
"time"
1413
)
1514

1615
type contentService struct {
@@ -29,11 +28,33 @@ func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm
2928
}
3029

3130
func (s *contentService) Create(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
32-
return nil, scm.ErrNotSupported
31+
endpoint := fmt.Sprintf("repos/%s/contents/%s", repo, path)
32+
in := new(contentCreateUpdate)
33+
in.Committer.Name = params.Signature.Name
34+
in.Committer.Email = params.Signature.Email
35+
in.Author.Name = params.Signature.Name
36+
in.Author.Email = params.Signature.Email
37+
in.Message = params.Message
38+
in.Branch = params.Branch
39+
in.Content = params.Data
40+
in.SHA = params.SHA
41+
res, err := s.client.do(ctx, "PUT", endpoint, in, nil)
42+
return res, err
3343
}
3444

3545
func (s *contentService) Update(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
36-
return nil, scm.ErrNotSupported
46+
endpoint := fmt.Sprintf("repos/%s/contents/%s", repo, path)
47+
in := new(contentCreateUpdate)
48+
in.Committer.Name = params.Signature.Name
49+
in.Committer.Email = params.Signature.Email
50+
in.Author.Name = params.Signature.Name
51+
in.Author.Email = params.Signature.Email
52+
in.Message = params.Message
53+
in.Branch = params.Branch
54+
in.Content = params.Data
55+
in.SHA = params.SHA
56+
res, err := s.client.do(ctx, "PUT", endpoint, in, nil)
57+
return res, err
3758
}
3859

3960
func (s *contentService) Delete(ctx context.Context, repo, path, ref string) (*scm.Response, error) {
@@ -55,20 +76,19 @@ type content struct {
5576
Type string `json:"type"`
5677
}
5778

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"`
79+
type contentCreateUpdate struct {
80+
Branch string
81+
Message string
82+
Content []byte
83+
SHA string
84+
Author CommitAuthor
85+
Committer CommitAuthor
86+
}
87+
88+
type CommitAuthor struct {
89+
Date *time.Time
90+
Name string
91+
Email string
7292
}
7393

7494
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+
Email: "octocat@github.com",
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+
Email: "octocat@github.com",
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": "octocat@github.com"
27+
},
28+
"committer": {
29+
"date": "2014-11-07T22:01:45Z",
30+
"name": "Monalisa Octocat",
31+
"email": "octocat@github.com"
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": "octocat@github.com"
27+
},
28+
"committer": {
29+
"date": "2014-11-07T22:01:45Z",
30+
"name": "Monalisa Octocat",
31+
"email": "octocat@github.com"
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+
}

0 commit comments

Comments
 (0)