-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.go
More file actions
89 lines (81 loc) · 2.72 KB
/
Copy pathurl.go
File metadata and controls
89 lines (81 loc) · 2.72 KB
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
package github
import (
"errors"
"fmt"
"net/url"
"strings"
)
type GitURL struct {
URL *url.URL
Owner string
Repo string
FullName string
}
// ParseGitPseudoURL attempts to parse rawURL as a git remote URL compatible with the
// Github naming conventions.
//
// It supports the following types of git pseudo URLs:
// - ssh: git@github.com:Pix4D/go-kit.git; will be rewritten to the valid URL
// ssh://git@github.com/Pix4D/go-kit.git
// - https: https://github.com/Pix4D/go-kit.git
// - https with u:p: https//username:password@github.com/Pix4D/go-kit.git
// - http: http://github.com/Pix4D/go-kit.git
// - http with u:p: http://username:password@github.com/Pix4D/go-kit.git
func ParseGitPseudoURL(rawURL string) (GitURL, error) {
workURL := rawURL
// If ssh pseudo URL, we need to massage the rawURL ourselves :-(
if strings.HasPrefix(workURL, "git@") {
if strings.Count(workURL, ":") != 1 {
return GitURL{}, fmt.Errorf("invalid git SSH URL %s: want exactly one ':'", rawURL)
}
// Make the URL a real URL, ready to be parsed. For example:
// git@github.com:Pix4D/go-kit.git -> ssh://git@github.com/Pix4D/go-kit.git
workURL = "ssh://" + strings.Replace(workURL, ":", "/", 1)
}
anyUrl, err := safeUrlParse(workURL)
if err != nil {
return GitURL{}, err
}
scheme := anyUrl.Scheme
if scheme == "" {
return GitURL{}, fmt.Errorf("invalid git URL %s: missing scheme", rawURL)
}
if scheme != "ssh" && scheme != "http" && scheme != "https" {
return GitURL{}, fmt.Errorf("invalid git URL %s: invalid scheme: %s", rawURL, scheme)
}
// Further parse the path component of the URL to see if it complies with the GitHub
// naming conventions.
// Example of compliant path: github.com/Pix4D/go-kit.git
tokens := strings.Split(anyUrl.Path, "/")
if have, want := len(tokens), 3; have != want {
return GitURL{},
fmt.Errorf("invalid git URL: path: want: %d components; have: %d %s",
want, have, tokens)
}
owner := tokens[1]
repo := strings.TrimSuffix(tokens[2], ".git")
// All OK. Fill our gitURL struct
gu := GitURL{
URL: anyUrl,
Owner: owner,
Repo: repo,
FullName: owner + "/" + repo,
}
return gu, nil
}
// safeUrlParse wraps [url.Parse] and returns only the error and not the URL to avoid leaking
// passwords of the form http://user:password@example.com
//
// From https://github.com/golang/go/issues/53993
func safeUrlParse(rawURL string) (*url.URL, error) {
parsedUrl, err := url.Parse(rawURL)
if err != nil {
if uerr, ok := errors.AsType[*url.Error](err); ok {
// url.Parse returns a wrapped error that contains also the URL.
// Instead, we return only the error.
return nil, uerr.Err
}
return nil, errors.New("invalid URL")
}
return parsedUrl, nil
}