forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
content.go
105 lines (89 loc) · 2.72 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
// 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"
"github.com/jenkins-x/go-scm/scm"
)
type contentService struct {
client *wrapper
}
func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm.Content, *scm.Response, error) {
endpoint := fmt.Sprintf("repos/%s/contents/%s?ref=%s", repo, path, ref)
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,
Sha: out.Sha,
}, res, err
}
func (s *contentService) List(ctx context.Context, repo, path, ref string) ([]*scm.FileEntry, *scm.Response, error) {
endpoint := fmt.Sprintf("repos/%s/contents/%s?ref=%s", repo, path, ref)
out := []*entry{}
res, err := s.client.do(ctx, "GET", endpoint, nil, &out)
return convertEntryList(out), 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)
body := &contentBody{
Message: params.Message,
Content: params.Data,
Branch: params.Branch,
}
return s.client.do(ctx, "PUT", endpoint, &body, nil)
}
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)
body := &contentBody{
Message: params.Message,
Content: params.Data,
Branch: params.Branch,
Sha: params.Sha,
}
return s.client.do(ctx, "PUT", endpoint, &body, nil)
}
func (s *contentService) Delete(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}
type content struct {
Name string `json:"name"`
Path string `json:"path"`
Sha string `json:"sha"`
Content string `json:"content"`
}
type entry struct {
Name string `json:"name"`
Type string `json:"type"`
Path string `json:"path"`
Size int `json:"size"`
Sha string `json:"sha"`
URL string `json:"url"`
}
type contentBody struct {
Message string `json:"message"`
Content []byte `json:"content"`
Sha string `json:"sha,omitempty"`
Branch string `json:"branch,omitempty"`
}
func convertEntryList(out []*entry) []*scm.FileEntry {
answer := make([]*scm.FileEntry, 0, len(out))
for _, o := range out {
answer = append(answer, convertEntry(o))
}
return answer
}
func convertEntry(from *entry) *scm.FileEntry {
return &scm.FileEntry{
Name: from.Name,
Path: from.Path,
Type: from.Type,
Size: from.Size,
Sha: from.Sha,
Link: from.URL,
}
}