This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
/
repo.go
268 lines (240 loc) · 7.78 KB
/
repo.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package glrepo
import (
"errors"
"fmt"
"net/url"
"strings"
"github.com/profclems/glab/api"
"github.com/profclems/glab/internal/config"
"github.com/profclems/glab/internal/glinstance"
"github.com/profclems/glab/pkg/git"
"github.com/profclems/glab/pkg/utils"
"github.com/xanzy/go-gitlab"
)
// RemoteURL returns correct git clone URL of a repo
// based on the user's git_protocol preference
func RemoteURL(project *gitlab.Project, protocol string) string {
if protocol == "ssh" {
return project.SSHURLToRepo
}
return project.HTTPURLToRepo
}
// FullName returns the the repo with its namespace (like profclems/glab). Respects group and subgroups names
func FullNameFromURL(remoteURL string) (string, error) {
parts := strings.Split(remoteURL, "//")
if len(parts) == 1 {
// scp-like short syntax (e.g. git@gitlab.com...)
part := parts[0]
parts = strings.Split(part, ":")
} else if len(parts) == 2 {
// other protocols (e.g. ssh://, http://, git://)
part := parts[1]
parts = strings.SplitN(part, "/", 2)
} else {
return "", errors.New("cannot parse remote: " + remoteURL)
}
if len(parts) != 2 {
return "", errors.New("cannot parse remote: " + remoteURL)
}
repo := parts[1]
repo = strings.TrimSuffix(repo, ".git")
return repo, nil
}
// Interface describes an object that represents a GitLab repository
// Contains methods for these methods representing these placeholders for a
// project path with :host/:group/:namespace/:repo
// RepoHost = :host, RepoOwner = :group/:namespace, RepoNamespace = :namespace,
// FullName = :group/:namespace/:repo, RepoGroup = :group, RepoName = :repo
type Interface interface {
RepoName() string
RepoOwner() string
RepoNamespace() string
RepoGroup() string
RepoHost() string
FullName() string
Project(*gitlab.Client) (*gitlab.Project, error)
}
// New instantiates a GitLab repository from owner and repo name arguments
func New(owner, repo string) Interface {
return NewWithHost(owner, repo, glinstance.OverridableDefault())
}
// NewWithGroup instantiates a GitLab repository from group, namespace and repo name arguments
func NewWithGroup(group, namespace, repo, hostname string) Interface {
owner := fmt.Sprintf("%s/%s", group, namespace)
if hostname == "" {
return New(owner, repo)
}
return NewWithHost(owner, repo, hostname)
}
// NewWithHost is like New with an explicit host name
func NewWithHost(owner, repo, hostname string) Interface {
rp := &glRepo{
owner: owner,
name: repo,
fullname: fmt.Sprintf("%s/%s", owner, repo),
hostname: normalizeHostname(hostname),
}
if ri := strings.SplitN(owner, "/", 2); len(ri) == 2 {
rp.group = ri[0]
rp.namespace = ri[1]
} else {
rp.namespace = owner
}
return rp
}
// FromFullName extracts the GitLab repository information from the following
// formats: "OWNER/REPO", "HOST/OWNER/REPO", "HOST/GROUP/NAMESPACE/REPO", and a full URL.
func FromFullName(nwo string) (Interface, error) {
nwo = strings.TrimSpace(nwo)
// check if it's a valid git URL and parse it
if git.IsValidURL(nwo) {
u, err := git.ParseURL(nwo)
if err != nil {
return nil, err
}
return FromURL(u)
}
// check if it is valid URL and parse it
if utils.IsValidURL(nwo) {
u, _ := url.Parse(nwo)
return FromURL(u)
}
repo := nwo[strings.LastIndex(nwo, "/")+1:]
nwoWithoutRepo := strings.TrimSuffix(nwo[:strings.LastIndex(nwo, "/")+1], "/")
parts := strings.SplitN(nwoWithoutRepo, "/", 2)
if repo == "" {
return nil, fmt.Errorf(`expected the "[HOST/]OWNER/[NAMESPACE/]REPO" format, got %q`, nwo)
}
for _, p := range parts {
if p == "" {
return nil, fmt.Errorf(`expected the "[HOST/]OWNER/[NAMESPACE/]REPO" format, got %q`, nwo)
}
}
switch len(parts) {
case 2: // GROUP/NAMESPACE/REPO or HOST/OWNER/REPO or //HOST/GROUP/NAMESPACE/REPO
// First, checks if the first part matches the default instance host (i.e. gitlab.com) or the
// overridden default host (mostly from the GITLAB_HOST env variable)
if parts[0] == glinstance.Default() || parts[0] == glinstance.OverridableDefault() {
return NewWithHost(parts[1], repo, normalizeHostname(parts[0])), nil
}
// Dots (.) are allowed in group names by GitLab.
// So we check if if the first part contains a dot.
// However, it could be that the user is specifying a hostname but we can't be sure of that
// So we check in the list of authenticated hosts and see if it matches any
// if not, we assume it is a group name that contains a dot
if strings.ContainsRune(parts[0], '.') {
var rI Interface
cfg, err := config.Init()
if err == nil {
hosts, _ := cfg.Hosts()
for _, host := range hosts {
if host == parts[0] {
rI = NewWithHost(parts[1], repo, normalizeHostname(parts[0]))
break
}
}
if rI != nil {
return rI, nil
}
}
}
// if the first part is not a valid URL, and does not match an
// authenticated hostname then we assume it is in
// the format GROUP/NAMESPACE/REPO
return NewWithGroup(parts[0], parts[1], repo, ""), nil
case 1: // OWNER/REPO
return New(parts[0], repo), nil
default:
return nil, fmt.Errorf(`expected the "[HOST/]OWNER/[NAMESPACE/]REPO" format, got %q`, nwo)
}
}
// FromURL extracts the GitLab repository information from a git remote URL
func FromURL(u *url.URL) (Interface, error) {
if u.Hostname() == "" {
return nil, fmt.Errorf("no hostname detected")
}
path := strings.Trim(strings.TrimSuffix(u.Path, ".git"), "/")
repo := path[strings.LastIndex(path, "/")+1:]
pathWithoutRepo := strings.TrimSuffix(path[:strings.LastIndex(path, "/")+1], "/")
if repo != "" && pathWithoutRepo != "" {
parts := strings.SplitN(pathWithoutRepo, "/", 2)
if len(parts) == 1 {
return NewWithHost(parts[0], repo, u.Hostname()), nil
}
if len(parts) == 2 {
return NewWithGroup(parts[0], parts[1], repo, u.Hostname()), nil
}
}
return nil, fmt.Errorf("invalid path: %s", u.Path)
}
func normalizeHostname(h string) string {
return strings.ToLower(strings.TrimPrefix(h, "www."))
}
// IsSame compares two GitLab repositories
func IsSame(a, b Interface) bool {
if a == nil || b == nil {
return false
}
return strings.EqualFold(a.FullName(), b.FullName()) &&
normalizeHostname(a.RepoHost()) == normalizeHostname(b.RepoHost())
}
type glRepo struct {
group string
owner string
name string
fullname string
hostname string
namespace string
project *Project
}
type Project struct {
*gitlab.Project
// for cache invalidation
fullname string
// for cache invalidation
hostname string
}
func (r glRepo) Project(client *gitlab.Client) (*gitlab.Project, error) {
if r.project != nil && r.project.fullname == r.fullname && r.project.hostname == r.hostname {
return r.project.Project, nil
}
p, err := api.GetProject(client, r.fullname)
if err != nil {
return nil, err
}
r.project = &Project{
Project: p,
fullname: r.fullname,
hostname: r.hostname,
}
return r.project.Project, err
}
// RepoNamespace returns the namespace of the project. Eg. if project path is :group/:namespace:/repo
// RepoNamespace returns the :namespace
func (r glRepo) RepoNamespace() string {
return r.namespace
}
// RepoGroup returns the group namespace of the project. Eg. if project path is :group/:namespace:/repo
// RepoGroup returns the :group
func (r glRepo) RepoGroup() string {
return r.group
}
// RepoOwner returns the group and namespace in the form "group/namespace". Returns "namespace" if group is not present
func (r glRepo) RepoOwner() string {
if r.group != "" {
return r.group + "/" + r.namespace
}
return r.owner
}
// RepoName returns the repo name without the path or namespace.
func (r glRepo) RepoName() string {
return r.name
}
// RepoHost returns the hostname
func (r glRepo) RepoHost() string {
return r.hostname
}
// FullName returns the full project path :group/:namespace/:repo or :namespace/:repo if group is not present
func (r glRepo) FullName() string {
return r.fullname
}