-
Notifications
You must be signed in to change notification settings - Fork 234
/
content.go
89 lines (77 loc) · 2.68 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
// 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 stash
import (
"bytes"
"context"
"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)
namespace, name := scm.Split(repo)
endpoint := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/raw/%s?at=%s", namespace, name, path, urlEncodedRef)
out := new(bytes.Buffer)
res, err := s.client.do(ctx, "GET", endpoint, nil, out)
return &scm.Content{
Path: path,
Data: out.Bytes(),
}, res, err
}
func (s *contentService) Create(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
namespace, repoName := scm.Split(repo)
endpoint := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/browse/%s", namespace, repoName, path)
in := &contentCreateUpdate{
Message: params.Message,
Branch: params.Branch,
Content: params.Data,
}
return s.client.do(ctx, "PUT", endpoint, in, nil)
}
func (s *contentService) Update(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
namespace, repoName := scm.Split(repo)
endpoint := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/browse/%s", namespace, repoName, path)
in := &contentCreateUpdate{
Message: params.Message,
Branch: params.Branch,
Content: params.Data,
Sha: params.Sha,
}
return s.client.do(ctx, "PUT", endpoint, in, nil)
}
func (s *contentService) Delete(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}
func (s *contentService) List(ctx context.Context, repo, path, ref string, opts scm.ListOptions) ([]*scm.ContentInfo, *scm.Response, error) {
namespace, name := scm.Split(repo)
endpoint := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/files/%s?at=%s&%s", namespace, name, path, ref, encodeListOptions(opts))
out := new(contents)
res, err := s.client.do(ctx, "GET", endpoint, nil, out)
copyPagination(out.pagination, res)
return convertContentInfoList(out), res, err
}
type contents struct {
pagination
Values []string `json:"values"`
}
type contentCreateUpdate struct {
Branch string `json:"branch"`
Message string `json:"message"`
Content []byte `json:"content"`
Sha string `json:"sourceCommitId"`
}
func convertContentInfoList(from *contents) []*scm.ContentInfo {
to := []*scm.ContentInfo{}
for _, v := range from.Values {
to = append(to, &scm.ContentInfo{
Path: v,
Kind: scm.ContentKindFile,
})
}
return to
}