Skip to content

Commit 55896a1

Browse files
author
zhuxiaoyang
committed
fix struct & consistency
Signed-off-by: zhuxiaoyang <[email protected]>
1 parent 109f73b commit 55896a1

File tree

4 files changed

+58
-48
lines changed

4 files changed

+58
-48
lines changed

scm/content.go

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

55
package scm
66

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

119
type (
1210
// Content stores the contents of a repository file.
@@ -18,10 +16,11 @@ type (
1816
// ContentParams provide parameters for creating and
1917
// updating repository content.
2018
ContentParams struct {
19+
Ref string
2120
Branch string
2221
Message string
2322
Data []byte
24-
SHA string
23+
Sha string
2524
Signature Signature
2625
}
2726

scm/driver/github/content.go

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"encoding/base64"
1010
"fmt"
1111
"github.com/drone/go-scm/scm"
12-
"time"
1312
)
1413

1514
type contentService struct {
@@ -29,30 +28,41 @@ func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm
2928

3029
func (s *contentService) Create(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
3130
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
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+
4146
res, err := s.client.do(ctx, "PUT", endpoint, in, nil)
4247
return res, err
4348
}
4449

4550
func (s *contentService) Update(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
4651
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
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+
}
5666
res, err := s.client.do(ctx, "PUT", endpoint, in, nil)
5767
return res, err
5868
}
@@ -77,18 +87,17 @@ type content struct {
7787
}
7888

7989
type contentCreateUpdate struct {
80-
Branch string
81-
Message string
82-
Content []byte
83-
SHA string
84-
Author CommitAuthor
85-
Committer CommitAuthor
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"`
8696
}
8797

88-
type CommitAuthor struct {
89-
Date *time.Time
90-
Name string
91-
Email string
98+
type commitAuthor struct {
99+
Name string `json:"name"`
100+
Email string `json:"email"`
92101
}
93102

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

scm/driver/github/content_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func TestContentUpdate(t *testing.T) {
101101
params := &scm.ContentParams{
102102
Message: "a new commit message",
103103
Data: []byte("bXkgdXBkYXRlZCBmaWxlIGNvbnRlbnRz"),
104-
SHA: "95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
104+
Sha: "95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
105105
Signature: scm.Signature{
106106
Name: "Monalisa Octocat",
107107

scm/driver/gitlab/content.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ func (s *contentService) Create(ctx context.Context, repo, path string, params *
4444
path = url.QueryEscape(path)
4545
path = strings.Replace(path, ".", "%2E", -1)
4646
endpoint := fmt.Sprintf("api/v4/projects/%s/repository/files/%s", encode(repo), path)
47-
in := new(createUpdateContent)
48-
in.Branch = params.Branch
49-
in.Content = params.Data
50-
in.CommitMessage = params.Message
51-
in.Encoding = "base64"
52-
in.AuthorEmail = params.Signature.Email
53-
in.AuthorName = params.Signature.Name
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+
}
5455
res, err := s.client.do(ctx, "POST", endpoint, in, nil)
5556
return res, err
5657

@@ -60,13 +61,14 @@ func (s *contentService) Update(ctx context.Context, repo, path string, params *
6061
path = url.QueryEscape(path)
6162
path = strings.Replace(path, ".", "%2E", -1)
6263
endpoint := fmt.Sprintf("api/v4/projects/%s/repository/files/%s", encode(repo), path)
63-
in := new(createUpdateContent)
64-
in.Branch = params.Branch
65-
in.Content = params.Data
66-
in.CommitMessage = params.Message
67-
in.Encoding = "base64"
68-
in.AuthorEmail = params.Signature.Email
69-
in.AuthorName = params.Signature.Name
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+
}
7072
res, err := s.client.do(ctx, "PUT", endpoint, in, nil)
7173
return res, err
7274
}

0 commit comments

Comments
 (0)