Skip to content

Commit bcbae96

Browse files
committed
Support GitHub URLs with credentials and remove/redact them
1 parent ce79ae6 commit bcbae96

File tree

3 files changed

+61
-23
lines changed

3 files changed

+61
-23
lines changed

cmd/graylog_version.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ func graylogVersionCommand(cmd *cobra.Command, args []string) {
5757
if graylogVersionShort {
5858
fmt.Println(version)
5959
} else {
60-
url, _ := utils.ParseGitHubURL(module.Repository)
61-
logger.Info("%-50s %s", strings.TrimSuffix(url.Repository, ".git"), version)
60+
url, err := utils.ParseGitHubURL(module.Repository)
61+
if err != nil {
62+
logger.Error("Couldn't parse repository URL: %s", err)
63+
return
64+
}
65+
logger.Info("%-50s %s", strings.TrimSuffix(url.Repository(), ".git"), version)
6266
}
6367
})
6468
})

utils/github.go

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,31 @@ package utils
33
import (
44
"fmt"
55
"github.com/pkg/errors"
6-
"net/url"
6+
neturl "net/url"
77
"path/filepath"
88
"regexp"
99
"strconv"
1010
"strings"
1111
)
1212

1313
func ParseGitHubURL(url string) (GitHubURL, error) {
14-
gitHubURL := GitHubURL{URL: url}
15-
1614
if !strings.HasSuffix(url, ".git") {
17-
return gitHubURL, errors.Errorf("GitHub URL is missing .git suffix: %s", url)
15+
return GitHubURL{}, errors.Errorf("GitHub URL is missing .git suffix: %s", url)
1816
}
1917

18+
var repository string
2019
switch {
2120
case strings.HasPrefix(url, "github://"):
22-
gitHubURL.Repository = strings.Split(url, "//")[1]
21+
repository = strings.Split(url, "//")[1]
2322
case strings.HasPrefix(url, "git@github"):
24-
gitHubURL.Repository = strings.Split(url, ":")[1]
25-
case strings.HasPrefix(url, "https://github"):
26-
gitHubURL.Repository = strings.Split(url, "github.com/")[1]
23+
repository = strings.Split(url, ":")[1]
24+
case strings.HasPrefix(url, "https://"):
25+
repository = strings.Split(url, "github.com/")[1]
2726
default:
2827
return GitHubURL{}, errors.Errorf("unknown GitHub URL: %s", url)
2928
}
3029

31-
return gitHubURL, nil
30+
return CreateGitHubURL(url, repository)
3231
}
3332

3433
func ParseGitHubPRString(prString string) (string, int, error) {
@@ -38,7 +37,7 @@ func ParseGitHubPRString(prString string) (string, int, error) {
3837

3938
if strings.HasPrefix(strings.ToLower(prString), "https://github.com/") && strings.Contains(prString, "/pull/") {
4039
// Input is a PR URL like this: https://github.com/Graylog2/graylog2-server/pull/9692
41-
u, err := url.Parse(prString)
40+
u, err := neturl.Parse(prString)
4241
if err != nil {
4342
return "", 0, errors.Wrapf(err, "couldn't parse GitHub pull request URL <%s>", prString)
4443
}
@@ -129,40 +128,63 @@ func ReplaceGitHubURL(url string, repoName string) (string, error) {
129128
}
130129
}
131130

131+
func CreateGitHubURL(url string, repository string) (GitHubURL, error) {
132+
if url == "" || repository == "" {
133+
return GitHubURL{}, fmt.Errorf("url and repository cannot be empty")
134+
}
135+
if strings.HasPrefix(url, "https://") {
136+
u, err := neturl.Parse(url)
137+
if err != nil {
138+
return GitHubURL{}, fmt.Errorf("couldn't parse URL: %s", err)
139+
}
140+
u.User = nil
141+
return GitHubURL{url: u.Redacted(), repository: repository}, nil
142+
}
143+
return GitHubURL{url: url, repository: repository}, nil
144+
}
145+
132146
type GitHubURL struct {
133-
URL string
134-
Repository string
147+
url string
148+
repository string
149+
}
150+
151+
func (url GitHubURL) URL() string {
152+
return url.url
153+
}
154+
155+
func (url GitHubURL) Repository() string {
156+
return url.repository
135157
}
136158

137159
func (url GitHubURL) IsHTTPS() bool {
138-
return strings.HasPrefix(url.URL, "https://")
160+
return strings.HasPrefix(url.url, "https://")
139161
}
140162

141163
func (url GitHubURL) IsSSH() bool {
142-
return strings.HasPrefix(url.URL, "git@")
164+
return strings.HasPrefix(url.url, "git@")
143165
}
144166

145167
func (url GitHubURL) SSH() string {
146-
return "git@github.com:" + url.Repository
168+
return "git@github.com:" + url.repository
147169
}
148170

149171
func (url GitHubURL) HTTPS() string {
150-
return "https://github.com/" + url.Repository
172+
return "https://github.com/" + url.repository
151173
}
152174

153175
func (url GitHubURL) Directory() string {
154-
return strings.TrimSuffix(filepath.Base(url.Repository), filepath.Ext(url.Repository))
176+
return strings.TrimSuffix(filepath.Base(url.repository), filepath.Ext(url.repository))
155177
}
156178

157179
func (url GitHubURL) BrowserURL() string {
158-
return strings.TrimSuffix(url.HTTPS(), filepath.Ext(url.Repository))
180+
return strings.TrimSuffix(url.HTTPS(), filepath.Ext(url.repository))
159181
}
160182

161183
func (url GitHubURL) Matches(match string) bool {
162-
repoName := strings.TrimSuffix(url.Repository, filepath.Ext(url.Repository))
184+
repoName := strings.TrimSuffix(url.repository, filepath.Ext(url.repository))
163185
return strings.Compare(strings.ToLower(repoName), strings.ToLower(match)) == 0
164186
}
165187

166188
func (url GitHubURL) String() string {
167-
return "github://" + url.Repository
189+
return "github://" + url.repository
168190
}

utils/github_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package utils
22

3-
import "testing"
3+
import (
4+
"testing"
5+
)
46

57
func TestParseGitHubURL(t *testing.T) {
68
var url GitHubURL
@@ -86,6 +88,16 @@ func TestParseGitHubURL(t *testing.T) {
8688
t.Error("expected URL without .git suffix to fail")
8789
}
8890

91+
// With authentication
92+
url, err = ParseGitHubURL("https://user:password@github.com/Graylog2/graylog2-server.git")
93+
match = "https://github.com/Graylog2/graylog2-server.git"
94+
if err != nil {
95+
t.Errorf("expected URL with user:password not to fail: %s", err)
96+
}
97+
if url.URL() != match {
98+
t.Errorf("expected <%s> to be <%s>", url.URL(), match)
99+
}
100+
89101
// Unknown URL format
90102
_, err = ParseGitHubURL("https://example.com/Graylog2/graylog2-server")
91103
if err == nil {

0 commit comments

Comments
 (0)