/
gitbranch.go
88 lines (73 loc) · 3.19 KB
/
gitbranch.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
/*
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 (
"context"
"github.com/katanomi/pkg/plugin/path"
metav1alpha1 "github.com/katanomi/pkg/apis/meta/v1alpha1"
"k8s.io/apimachinery/pkg/api/errors"
duckv1 "knative.dev/pkg/apis/duck/v1"
)
// ClientGitBranch client for branch
type ClientGitBranch interface {
List(ctx context.Context, baseURL *duckv1.Addressable, repo metav1alpha1.GitBranchOption, options ...OptionFunc) (*metav1alpha1.GitBranchList, error)
Create(ctx context.Context, baseURL *duckv1.Addressable, payload metav1alpha1.CreateBranchPayload, options ...OptionFunc) (*metav1alpha1.GitBranch, error)
Get(ctx context.Context, baseURL *duckv1.Addressable, repo metav1alpha1.GitRepo, branch string, options ...OptionFunc) (*metav1alpha1.GitBranch, error)
}
type gitBranch struct {
client Client
}
func newGitBranch(client Client) ClientGitBranch {
return &gitBranch{
client: client,
}
}
// List list branch
func (g *gitBranch) List(ctx context.Context, baseURL *duckv1.Addressable, repo metav1alpha1.GitBranchOption, options ...OptionFunc) (*metav1alpha1.GitBranchList, error) {
list := &metav1alpha1.GitBranchList{}
options = append(options, ResultOpts(list))
if repo.Repository == "" {
return nil, errors.NewBadRequest("repo is empty string")
}
uri := path.Format("projects/%s/coderepositories/%s/branches", repo.Project, repo.Repository)
if err := g.client.Get(ctx, baseURL, uri, options...); err != nil {
return nil, err
}
return list, nil
}
// Create create branch
func (g *gitBranch) Create(ctx context.Context, baseURL *duckv1.Addressable, payload metav1alpha1.CreateBranchPayload, options ...OptionFunc) (*metav1alpha1.GitBranch, error) {
branchObj := &metav1alpha1.GitBranch{}
options = append(options, BodyOpts(payload.CreateBranchParams), ResultOpts(branchObj))
if payload.Repository == "" {
return nil, errors.NewBadRequest("repo is empty string")
}
uri := path.Format("projects/%s/coderepositories/%s/branches", payload.Project, payload.Repository)
if err := g.client.Post(ctx, baseURL, uri, options...); err != nil {
return nil, err
}
return branchObj, nil
}
// Get branch info
func (g *gitBranch) Get(ctx context.Context, baseURL *duckv1.Addressable, repo metav1alpha1.GitRepo, branch string, options ...OptionFunc) (*metav1alpha1.GitBranch, error) {
branchObj := &metav1alpha1.GitBranch{}
options = append(options, ResultOpts(branchObj))
if repo.Repository == "" {
return nil, errors.NewBadRequest("repo is empty string")
}
uri := path.Format("projects/%s/coderepositories/%s/branches/%s", repo.Project, repo.Repository, branch)
if err := g.client.Get(ctx, baseURL, uri, options...); err != nil {
return nil, err
}
return branchObj, nil
}