/
content.go
149 lines (134 loc) · 4.1 KB
/
content.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"encoding/base64"
"fmt"
"net/url"
"github.com/drone/go-scm/scm"
)
type contentService struct {
client *wrapper
}
func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm.Content, *scm.Response, error) {
urlEncodedRef := url.QueryEscape(ref)
endpoint := fmt.Sprintf("repos/%s/contents/%s?ref=%s", repo, path, urlEncodedRef)
out := new(content)
res, err := s.client.do(ctx, "GET", endpoint, nil, out)
raw, _ := base64.StdEncoding.DecodeString(out.Content)
return &scm.Content{
Path: out.Path,
Data: raw,
// NB the sha returned for github rest api is the blob sha, not the commit sha
BlobID: out.Sha,
}, res, err
}
func (s *contentService) Create(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
endpoint := fmt.Sprintf("repos/%s/contents/%s", repo, path)
in := &contentCreateUpdate{
Message: params.Message,
Branch: params.Branch,
Content: params.Data,
Committer: commitAuthor{
Name: params.Signature.Name,
Email: params.Signature.Email,
},
Author: commitAuthor{
Name: params.Signature.Name,
Email: params.Signature.Email,
},
}
res, err := s.client.do(ctx, "PUT", endpoint, in, nil)
return res, err
}
func (s *contentService) Update(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
endpoint := fmt.Sprintf("repos/%s/contents/%s", repo, path)
in := &contentCreateUpdate{
Message: params.Message,
Branch: params.Branch,
Content: params.Data,
// NB the sha passed to github rest api is the blob sha, not the commit sha
Sha: params.BlobID,
Committer: commitAuthor{
Name: params.Signature.Name,
Email: params.Signature.Email,
},
Author: commitAuthor{
Name: params.Signature.Name,
Email: params.Signature.Email,
},
}
res, err := s.client.do(ctx, "PUT", endpoint, in, nil)
return res, err
}
func (s *contentService) Delete(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
endpoint := fmt.Sprintf("repos/%s/contents/%s", repo, path)
in := &contentCreateUpdate{
Message: params.Message,
Branch: params.Branch,
// NB the sha passed to github rest api is the blob sha, not the commit sha
Sha: params.BlobID,
Committer: commitAuthor{
Name: params.Signature.Name,
Email: params.Signature.Email,
},
Author: commitAuthor{
Name: params.Signature.Name,
Email: params.Signature.Email,
},
}
res, err := s.client.do(ctx, "DELETE", endpoint, in, nil)
return res, err
}
func (s *contentService) List(ctx context.Context, repo, path, ref string, _ scm.ListOptions) ([]*scm.ContentInfo, *scm.Response, error) {
endpoint := fmt.Sprintf("repos/%s/contents/%s?ref=%s", repo, path, ref)
out := []*content{}
res, err := s.client.do(ctx, "GET", endpoint, nil, &out)
return convertContentInfoList(out), res, err
}
type content struct {
Name string `json:"name"`
Path string `json:"path"`
Sha string `json:"sha"`
Content string `json:"content"`
Type string `json:"type"`
}
type contentCreateUpdate struct {
Branch string `json:"branch"`
Message string `json:"message"`
Content []byte `json:"content"`
Sha string `json:"sha"`
Author commitAuthor `json:"author"`
Committer commitAuthor `json:"committer"`
}
type commitAuthor struct {
Name string `json:"name"`
Email string `json:"email"`
}
func convertContentInfoList(from []*content) []*scm.ContentInfo {
to := []*scm.ContentInfo{}
for _, v := range from {
to = append(to, convertContentInfo(v))
}
return to
}
func convertContentInfo(from *content) *scm.ContentInfo {
to := &scm.ContentInfo{
Path: from.Path,
BlobID: from.Sha}
switch from.Type {
case "file":
to.Kind = scm.ContentKindFile
case "dir":
to.Kind = scm.ContentKindDirectory
case "symlink":
to.Kind = scm.ContentKindSymlink
case "submodule":
to.Kind = scm.ContentKindGitlink
default:
to.Kind = scm.ContentKindUnsupported
}
return to
}