@@ -3,32 +3,31 @@ package utils
33import (
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
1313func 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
3433func 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+
132146type 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
137159func (url GitHubURL ) IsHTTPS () bool {
138- return strings .HasPrefix (url .URL , "https://" )
160+ return strings .HasPrefix (url .url , "https://" )
139161}
140162
141163func (url GitHubURL ) IsSSH () bool {
142- return strings .HasPrefix (url .URL , "git@" )
164+ return strings .HasPrefix (url .url , "git@" )
143165}
144166
145167func (url GitHubURL ) SSH () string {
146- return "git@github.com:" + url .Repository
168+ return "git@github.com:" + url .repository
147169}
148170
149171func (url GitHubURL ) HTTPS () string {
150- return "https://github.com/" + url .Repository
172+ return "https://github.com/" + url .repository
151173}
152174
153175func (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
157179func (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
161183func (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
166188func (url GitHubURL ) String () string {
167- return "github://" + url .Repository
189+ return "github://" + url .repository
168190}
0 commit comments