Skip to content

Commit

Permalink
chore: refactor getrelativeurl
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Sep 20, 2018
1 parent 23b82c1 commit 72e2b6f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 14 deletions.
28 changes: 14 additions & 14 deletions issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,31 +247,31 @@ func (i Issue) NodeTitle() string {
return fmt.Sprintf(`<<table><tr><td>%s</td></tr>%s%s%s</table>>`, title, labelsText, assigneeText, errorsText)
}

func normalizeURL(input string) string {
parts := strings.Split(input, "://")
output := fmt.Sprintf("%s://%s", parts[0], strings.Replace(parts[1], "//", "/", -1))
output = strings.TrimRight(output, "#")
output = strings.TrimRight(output, "/")
return output
}

func (i Issue) GetRelativeIssueURL(target string) string {
if strings.Contains(target, "://") {
return target
return normalizeURL(target)
}

if target[0] == '#' {
return fmt.Sprintf("%s/issues/%s", i.RepoURL, target[1:])
}

//target = strings.Replace(target, "#", "issues/", -1)

if len(strings.Split(target, "/")) > 3 {
target = fmt.Sprintf("http://%s", target)
}
target = strings.Replace(target, "#", "/issues/", -1)

u, err := url.Parse(target)
if err != nil {
return ""
}
path := u.Path
if path == "" {
path = i.Path()
parts := strings.Split(target, "/")
if strings.Contains(parts[0], ".") && isDNSName(parts[0]) {
return fmt.Sprintf("https://%s", target)
}

return fmt.Sprintf("%s/%s/issues/%s", strings.TrimRight(i.ProviderURL(), "/"), path, u.Fragment)
return fmt.Sprintf("%s/%s", strings.TrimRight(i.ProviderURL(), "/"), target)
}

func (i Issue) BlocksAnEpic() bool {
Expand Down
69 changes: 69 additions & 0 deletions issue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import "fmt"

func ExampleIssue_GetRelativeIssueURL() {
issue := Issue{
URL: "https://github.com/moul/depviz/issues/42",
RepoURL: "https://github.com/moul/depviz",
}
for _, target := range []string{
"#43",
"moul/depviz#44",
"github.com/moul/depviz/issues/45",
"https://github.com/moul/depviz/issues/46",
"test/test#47",
"github.com/test/test/issues/48",
"https://github.com/test/test/issues/49",
"gitlab.com/moul/depviz/issues/50",
"https://gitlab.com/moul/depviz/issues/51",
"gitlab.com/test2/issues/52",
"gitlab.com/test2/test3/test4/issues/53",
} {
fmt.Printf("%-42s -> %s\n", target, issue.GetRelativeIssueURL(target))
}

issue = Issue{
URL: "https://gitlab.com/moul/depviz/issues/42",
RepoURL: "https://gitlab.com/moul/depviz",
}
for _, target := range []string{
"#43",
"moul/depviz#44",
"gitlab.com/moul/depviz/issues/45",
"https://gitlab.com/moul/depviz/issues/46",
"test/test#47",
"gitlab.com/test/test/issues/48",
"https://gitlab.com/test/test/issues/49",
"github.com/moul/depviz/issues/50",
"https://github.com/moul/depviz/issues/51",
"github.com/test2/issues/52",
"github.com/test2/test3/test4/issues/53",
} {
fmt.Printf("%-42s -> %s\n", target, issue.GetRelativeIssueURL(target))
}

// Output:
// #43 -> https://github.com/moul/depviz/issues/43
// moul/depviz#44 -> https://github.com/moul/depviz/issues/44
// github.com/moul/depviz/issues/45 -> https://github.com/moul/depviz/issues/45
// https://github.com/moul/depviz/issues/46 -> https://github.com/moul/depviz/issues/46
// test/test#47 -> https://github.com/test/test/issues/47
// github.com/test/test/issues/48 -> https://github.com/test/test/issues/48
// https://github.com/test/test/issues/49 -> https://github.com/test/test/issues/49
// gitlab.com/moul/depviz/issues/50 -> https://gitlab.com/moul/depviz/issues/50
// https://gitlab.com/moul/depviz/issues/51 -> https://gitlab.com/moul/depviz/issues/51
// gitlab.com/test2/issues/52 -> https://gitlab.com/test2/issues/52
// gitlab.com/test2/test3/test4/issues/53 -> https://gitlab.com/test2/test3/test4/issues/53
// #43 -> https://gitlab.com/moul/depviz/issues/43
// moul/depviz#44 -> https://gitlab.com/moul/depviz/issues/44
// gitlab.com/moul/depviz/issues/45 -> https://gitlab.com/moul/depviz/issues/45
// https://gitlab.com/moul/depviz/issues/46 -> https://gitlab.com/moul/depviz/issues/46
// test/test#47 -> https://gitlab.com/test/test/issues/47
// gitlab.com/test/test/issues/48 -> https://gitlab.com/test/test/issues/48
// https://gitlab.com/test/test/issues/49 -> https://gitlab.com/test/test/issues/49
// github.com/moul/depviz/issues/50 -> https://github.com/moul/depviz/issues/50
// https://github.com/moul/depviz/issues/51 -> https://github.com/moul/depviz/issues/51
// github.com/test2/issues/52 -> https://github.com/test2/issues/52
// github.com/test2/test3/test4/issues/53 -> https://github.com/test2/test3/test4/issues/53
}
7 changes: 7 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"regexp"
"strings"
)

Expand Down Expand Up @@ -67,3 +68,9 @@ func uniqueStrings(input []string) []string {

return u
}

var rxDNSName = regexp.MustCompile(`^([a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*[\._]?$`)

func isDNSName(input string) bool {
return rxDNSName.MatchString(input)
}

0 comments on commit 72e2b6f

Please sign in to comment.