forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
content.go
119 lines (93 loc) · 2.99 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
// 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 gitea
import (
"context"
"encoding/base64"
"strings"
"code.gitea.io/sdk/gitea"
"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) {
namespace, name := scm.Split(repo)
ref = strings.TrimPrefix(ref, "refs/heads/")
ref = strings.TrimPrefix(ref, "refs/tags/")
out, resp, err := s.client.GiteaClient.GetContents(namespace, name, ref, path)
if err != nil {
return nil, toSCMResponse(resp), err
}
raw, _ := base64.StdEncoding.DecodeString(*out.Content)
return &scm.Content{
Path: path,
Data: []byte(raw),
Sha: out.SHA,
}, toSCMResponse(resp), err
}
func (s *contentService) List(ctx context.Context, repo, path, ref string) ([]*scm.FileEntry, *scm.Response, error) {
namespace, name := scm.Split(repo)
ref = strings.TrimPrefix(ref, "refs/heads/")
ref = strings.TrimPrefix(ref, "refs/tags/")
path = strings.TrimPrefix(path, "/")
c, resp, err := s.client.GiteaClient.ListContents(namespace, name, ref, path)
if err != nil {
return nil, toSCMResponse(resp), err
}
return convertEntryList(c), toSCMResponse(resp), err
}
func (s *contentService) Create(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
namespace, name := scm.Split(repo)
path = strings.TrimPrefix(path, "/")
content := base64.StdEncoding.EncodeToString(params.Data)
o := gitea.CreateFileOptions{
FileOptions: gitea.FileOptions{
Message: params.Message,
BranchName: params.Branch,
},
Content: content,
}
_, resp, err := s.client.GiteaClient.CreateFile(namespace, name, path, o)
return toSCMResponse(resp), err
}
func (s *contentService) Update(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
namespace, name := scm.Split(repo)
path = strings.TrimPrefix(path, "/")
content := base64.StdEncoding.EncodeToString(params.Data)
o := gitea.UpdateFileOptions{
FileOptions: gitea.FileOptions{
Message: params.Message,
BranchName: params.Branch,
},
Content: content,
SHA: params.Sha,
}
_, resp, err := s.client.GiteaClient.UpdateFile(namespace, name, path, o)
return toSCMResponse(resp), err
}
func (s *contentService) Delete(ctx context.Context, repo, path, ref string) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}
func convertEntryList(out []*gitea.ContentsResponse) []*scm.FileEntry {
answer := make([]*scm.FileEntry, 0, len(out))
for _, o := range out {
answer = append(answer, convertEntry(o))
}
return answer
}
func convertEntry(from *gitea.ContentsResponse) *scm.FileEntry {
link := ""
if from.DownloadURL != nil {
link = *from.DownloadURL
}
return &scm.FileEntry{
Name: from.Name,
Path: from.Path,
Type: from.Type,
Size: int(from.Size),
Sha: from.SHA,
Link: link,
}
}