Skip to content

Commit 27f35ef

Browse files
author
zhuxiaoyang
committed
support create & update content with gitlab
Signed-off-by: zhuxiaoyang <[email protected]>
1 parent 68a90bc commit 27f35ef

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

scm/driver/gitlab/content.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,34 @@ 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 := 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
54+
res, err := s.client.do(ctx, "POST", endpoint, in, nil)
55+
return res, err
56+
4557
}
4658

4759
func (s *contentService) Update(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
48-
return nil, scm.ErrNotSupported
60+
path = url.QueryEscape(path)
61+
path = strings.Replace(path, ".", "%2E", -1)
62+
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
70+
res, err := s.client.do(ctx, "PUT", endpoint, in, nil)
71+
return res, err
4972
}
5073

5174
func (s *contentService) Delete(ctx context.Context, repo, path, ref string) (*scm.Response, error) {
@@ -71,6 +94,15 @@ type content struct {
7194
LastCommitID string `json:"last_commit_id"`
7295
}
7396

97+
type createUpdateContent struct {
98+
Branch string `json:"branch"`
99+
Content []byte `json:"content"`
100+
CommitMessage string `json:"commit_message"`
101+
Encoding string `json:"encoding"`
102+
AuthorEmail string `json:"author_email"`
103+
AuthorName string `json:"author_name"`
104+
}
105+
74106
type object struct {
75107
Path string `json:"path"`
76108
Mode string `json:"mode"`

0 commit comments

Comments
 (0)