diff options
| author | Aaron Crickenberger <spiffxp@gmail.com> | 2021-03-07 23:49:54 -0500 |
|---|---|---|
| committer | Aaron Crickenberger <spiffxp@google.com> | 2021-03-09 14:12:03 -0500 |
| commit | 7a3a9e4e5705517f272c613b6b5fa22f34dbd43e (patch) | |
| tree | 4cbb3ecb2cb572636838118a450cbdb69ac350aa /generator | |
| parent | 320c5f10d6495cbdeb8b87ab9e8acd1b140db226 (diff) | |
generator: add githubURL, orgRepoPath funcs
Diffstat (limited to 'generator')
| -rw-r--r-- | generator/app.go | 36 | ||||
| -rw-r--r-- | generator/app_test.go | 70 |
2 files changed, 106 insertions, 0 deletions
diff --git a/generator/app.go b/generator/app.go index e48a123b..a73dc7d2 100644 --- a/generator/app.go +++ b/generator/app.go @@ -23,6 +23,7 @@ import ( "net/url" "os" "path/filepath" + "regexp" "sort" "strings" "text/template" @@ -47,6 +48,9 @@ const ( endCustomMarkdown = "<!-- END CUSTOM CONTENT -->" beginCustomYaml = "## BEGIN CUSTOM CONTENT" endCustomYaml = "## END CUSTOM CONTENT" + + regexRawGitHubURL = "https://raw.githubusercontent.com/(?P<org>[^/]+)/(?P<repo>[^/]+)/(?P<branch>[^/]+)/(?P<path>.*)" + regexGitHubURL = "https://github.com/(?P<org>[^/]+)/(?P<repo>[^/]+)/(blob|tree)/(?P<branch>[^/]+)/(?P<path>.*)" ) var ( @@ -369,6 +373,37 @@ func getExistingContent(path string, fileFormat string) (string, error) { var funcMap = template.FuncMap{ "tzUrlEncode": tzURLEncode, "trimSpace": strings.TrimSpace, + "githubURL": githubURL, + "orgRepoPath": orgRepoPath, +} + +// githubURL converts a raw GitHub url (links directly to file contents) into a +// regular GitHub url (links to Code view for file), otherwise returns url untouched +func githubURL(url string) string { + re := regexp.MustCompile(regexRawGitHubURL) + mat := re.FindStringSubmatchIndex(url) + if mat == nil { + return url + } + result := re.ExpandString([]byte{}, "https://github.com/${org}/${repo}/blob/${branch}/${path}", url, mat) + return string(result) +} + +// orgRepoPath converts either +// - a regular GitHub url of form https://github.com/org/repo/blob/branch/path/to/file +// - a raw GitHub url of form https://raw.githubusercontent.com/org/repo/branch/path/to/file +// to a string of form 'org/repo/path/to/file' +func orgRepoPath(url string) string { + for _, regex := range []string{regexRawGitHubURL, regexGitHubURL} { + re := regexp.MustCompile(regex) + mat := re.FindStringSubmatchIndex(url) + if mat == nil { + continue + } + result := re.ExpandString([]byte{}, "${org}/${repo}/${path}", url, mat) + return string(result) + } + return url } // tzUrlEncode returns a url encoded string without the + shortcut. This is @@ -525,6 +560,7 @@ func main() { log.Fatal(err) } + fmt.Println("Generating group READMEs") for prefix, groups := range ctx.PrefixToGroupMap() { err = createGroupReadme(groups, prefix) if err != nil { diff --git a/generator/app_test.go b/generator/app_test.go index 381acc46..184c2b54 100644 --- a/generator/app_test.go +++ b/generator/app_test.go @@ -236,3 +236,73 @@ func TestFullGeneration(t *testing.T) { } } } + +func TestGitHubURL(t *testing.T) { + cases := []struct { + name string + url string + expected string + }{ + { + name: "kubernetes-sigs root raw github url", + url: "https://raw.githubusercontent.com/kubernetes-sigs/boskos/main/OWNERS", + expected: "https://github.com/kubernetes-sigs/boskos/blob/main/OWNERS", + }, + { + name: "kubernetes non-root raw github url", + url: "https://raw.githubusercontent.com/kubernetes/kubernetes/main/test/OWNERS", + expected: "https://github.com/kubernetes/kubernetes/blob/main/test/OWNERS", + }, + { + name: "kubernetes github url should be unchanged", + url: "https://github.com/kubernetes/kubernetes/blob/main/test/OWNERS", + expected: "https://github.com/kubernetes/kubernetes/blob/main/test/OWNERS", + }, + { + name: "non-github url should be unchanged", + url: "https://viewsource.com/github/kubernetes/community/generator/app.go", + expected: "https://viewsource.com/github/kubernetes/community/generator/app.go", + }, + } + for _, c := range cases { + actual := githubURL(c.url) + if actual != c.expected { + t.Errorf("FAIL %s: got: '%s' but expected: '%s'", c.name, actual, c.expected) + } + } +} + +func TestOrgRepoPath(t *testing.T) { + cases := []struct { + name string + url string + expected string + }{ + { + name: "kubernetes-sigs root raw github url", + url: "https://raw.githubusercontent.com/kubernetes-sigs/boskos/main/OWNERS", + expected: "kubernetes-sigs/boskos/OWNERS", + }, + { + name: "kubernetes non-root raw github url", + url: "https://raw.githubusercontent.com/kubernetes/kubernetes/main/test/OWNERS", + expected: "kubernetes/kubernetes/test/OWNERS", + }, + { + name: "kubernetes github url", + url: "https://github.com/kubernetes/kubernetes/blob/main/test/OWNERS", + expected: "kubernetes/kubernetes/test/OWNERS", + }, + { + name: "non-github url should be unchanged", + url: "https://viewsource.com/github/kubernetes/community/generator/app.go", + expected: "https://viewsource.com/github/kubernetes/community/generator/app.go", + }, + } + for _, c := range cases { + actual := orgRepoPath(c.url) + if actual != c.expected { + t.Errorf("FAIL %s: got: '%s' but expected: '%s'", c.name, actual, c.expected) + } + } +} |
