-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
adapter.go
128 lines (101 loc) · 4.35 KB
/
adapter.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
120
121
122
123
124
125
126
127
128
/*
Copyright 2019 The Kubernetes 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 git
import (
"errors"
"fmt"
"strings"
"k8s.io/test-infra/prow/git"
"k8s.io/test-infra/prow/git/types"
)
func OrgRepo(full string) (string, string, error) {
if strings.Count(full, "/") != 1 {
return "", "", fmt.Errorf("full repo name %s does not follow the org/repo format", full)
}
parts := strings.Split(full, "/")
return parts[0], parts[1], nil
}
// ClientFactoryFrom adapts the v1 client to a v2 client
func ClientFactoryFrom(c *git.Client) ClientFactory {
return &clientFactoryAdapter{Client: c}
}
type clientFactoryAdapter struct {
*git.Client
}
// ClientFromDir creates a client that operates on a repo that has already
// been cloned to the given directory.
//
// CloneURI is the third arg that's ignored here, it's currently only used for
// cloning Gerrit repos. This client is not used for cloning Gerrit repos yet,
// so leave it unimplemented.
// (TODO: chaodaiG) Either implement or remove this struct.
func (a *clientFactoryAdapter) ClientFromDir(org, repo, dir string) (RepoClient, error) {
return nil, errors.New("no ClientFromDir implementation exists in the v1 git client")
}
// Repo creates a client that operates on a new clone of the repo.
func (a *clientFactoryAdapter) ClientFor(org, repo string) (RepoClient, error) {
r, err := a.Client.Clone(org, repo)
return &repoClientAdapter{Repo: r}, err
}
// v1 clients do not support customizing pre-repo options.
func (a *clientFactoryAdapter) ClientForWithRepoOpts(org, repo string, repoOpts RepoOpts) (RepoClient, error) {
r, err := a.Client.Clone(org, repo)
return &repoClientAdapter{Repo: r}, err
}
type repoClientAdapter struct {
*git.Repo
}
func (a *repoClientAdapter) MergeAndCheckout(baseSHA string, mergeStrategy string, headSHAs ...string) error {
return a.Repo.MergeAndCheckout(baseSHA, types.PullRequestMergeType(mergeStrategy), headSHAs...)
}
func (a *repoClientAdapter) MergeWithStrategy(commitlike, mergeStrategy string, opts ...MergeOpt) (bool, error) {
return a.Repo.MergeWithStrategy(commitlike, types.PullRequestMergeType(mergeStrategy))
}
func (a *repoClientAdapter) Clone(from string) error {
return errors.New("no Clone implementation exists in the v1 repo client")
}
func (a *repoClientAdapter) Commit(title, body string) error {
return errors.New("no Commit implementation exists in the v1 repo client")
}
func (a *repoClientAdapter) PushToFork(branch string, force bool) error {
return a.Repo.Push(branch, force)
}
func (a *repoClientAdapter) PushToNamedFork(forkName, branch string, force bool) error {
return a.Repo.PushToNamedFork(forkName, branch, force)
}
func (a *repoClientAdapter) CommitExists(sha string) (bool, error) {
return false, errors.New("no CommitExists implementation exists in the v1 repo client")
}
func (a *repoClientAdapter) PushToCentral(branch string, force bool) error {
return errors.New("no PushToCentral implementation exists in the v1 repo client")
}
func (a *repoClientAdapter) MirrorClone() error {
return errors.New("no MirrorClone implementation exists in the v1 repo client")
}
func (a *repoClientAdapter) Fetch(arg ...string) error {
// TODO(mpherman): Bring adapter Fetch in line with gitv2 fetch without hard-coding origin as remote.
args := append([]string{"origin"}, arg...)
return a.Repo.Fetch(args...)
}
func (a *repoClientAdapter) FetchFromRemote(resolver RemoteResolver, branch string) error {
return errors.New("no FetchFromRemote implementation exists in the v1 repo client")
}
func (a *repoClientAdapter) RemoteUpdate() error {
return errors.New("no RemoteUpdate implementation exists in the v1 repo client")
}
func (a *repoClientAdapter) FetchRef(refspec string) error {
return errors.New("no FetchRef implementation exists in the v1 repo client")
}
func (a *repoClientAdapter) Fsck() (bool, error) {
return false, errors.New("no Fsck implementation exists in the v1 repo client")
}