-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgitcontent.go
85 lines (70 loc) · 2.94 KB
/
gitcontent.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
/*
Copyright 2021 The Katanomi Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"bytes"
"context"
"encoding/base64"
metav1alpha1 "github.com/katanomi/pkg/apis/meta/v1alpha1"
"github.com/katanomi/pkg/plugin/path"
"k8s.io/apimachinery/pkg/api/errors"
duckv1 "knative.dev/pkg/apis/duck/v1"
)
//go:generate mockgen -source=gitcontent.go -destination=../../testing/mock/github.com/katanomi/pkg/plugin/client/gitcontent.go -package=client ClientGitContent
type ClientGitContent interface {
Get(ctx context.Context, baseURL *duckv1.Addressable, option metav1alpha1.GitRepoFileOption, options ...OptionFunc) (*metav1alpha1.GitRepoFile, error)
Create(ctx context.Context, baseURL *duckv1.Addressable, payload metav1alpha1.CreateRepoFilePayload, options ...OptionFunc) (*metav1alpha1.GitCommit, error)
}
type gitContent struct {
client Client
}
func newGitContent(client Client) ClientGitContent {
return &gitContent{
client: client,
}
}
func (g *gitContent) Get(ctx context.Context, baseURL *duckv1.Addressable, option metav1alpha1.GitRepoFileOption, options ...OptionFunc) (*metav1alpha1.GitRepoFile, error) {
fileInfo := &metav1alpha1.GitRepoFile{}
options = append(options, QueryOpts(map[string]string{"ref": option.Ref}), ResultOpts(fileInfo))
if option.Repository == "" {
return nil, errors.NewBadRequest("repo is empty string")
}
if option.Path == "" {
return nil, errors.NewBadRequest("file path is empty string")
}
uri := path.Format("projects/%s/coderepositories/%s/content/%s", option.Project, option.Repository, option.Path)
if err := g.client.Get(ctx, baseURL, uri, options...); err != nil {
return nil, err
}
return fileInfo, nil
}
func (g *gitContent) Create(ctx context.Context, baseURL *duckv1.Addressable, payload metav1alpha1.CreateRepoFilePayload, options ...OptionFunc) (*metav1alpha1.GitCommit, error) {
commitInfo := &metav1alpha1.GitCommit{}
var b bytes.Buffer
w := base64.NewEncoder(base64.StdEncoding, &b)
_, err := w.Write(payload.Content)
if err != nil {
return nil, err
}
w.Close()
payload.Content = b.Bytes()
options = append(options, BodyOpts(payload.CreateRepoFileParams), ResultOpts(commitInfo))
if payload.Repository == "" {
return nil, errors.NewBadRequest("repo is empty string")
}
uri := path.Format("projects/%s/coderepositories/%s/content/%s", payload.Project, payload.Repository, payload.FilePath)
if err := g.client.Post(ctx, baseURL, uri, options...); err != nil {
return nil, err
}
return commitInfo, nil
}