Skip to content

Commit 7a3a9e4

Browse files
committed
generator: add githubURL, orgRepoPath funcs
1 parent 320c5f1 commit 7a3a9e4

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

Diff for: generator/app.go

+36
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net/url"
2424
"os"
2525
"path/filepath"
26+
"regexp"
2627
"sort"
2728
"strings"
2829
"text/template"
@@ -47,6 +48,9 @@ const (
4748
endCustomMarkdown = "<!-- END CUSTOM CONTENT -->"
4849
beginCustomYaml = "## BEGIN CUSTOM CONTENT"
4950
endCustomYaml = "## END CUSTOM CONTENT"
51+
52+
regexRawGitHubURL = "https://raw.githubusercontent.com/(?P<org>[^/]+)/(?P<repo>[^/]+)/(?P<branch>[^/]+)/(?P<path>.*)"
53+
regexGitHubURL = "https://github.com/(?P<org>[^/]+)/(?P<repo>[^/]+)/(blob|tree)/(?P<branch>[^/]+)/(?P<path>.*)"
5054
)
5155

5256
var (
@@ -369,6 +373,37 @@ func getExistingContent(path string, fileFormat string) (string, error) {
369373
var funcMap = template.FuncMap{
370374
"tzUrlEncode": tzURLEncode,
371375
"trimSpace": strings.TrimSpace,
376+
"githubURL": githubURL,
377+
"orgRepoPath": orgRepoPath,
378+
}
379+
380+
// githubURL converts a raw GitHub url (links directly to file contents) into a
381+
// regular GitHub url (links to Code view for file), otherwise returns url untouched
382+
func githubURL(url string) string {
383+
re := regexp.MustCompile(regexRawGitHubURL)
384+
mat := re.FindStringSubmatchIndex(url)
385+
if mat == nil {
386+
return url
387+
}
388+
result := re.ExpandString([]byte{}, "https://github.com/${org}/${repo}/blob/${branch}/${path}", url, mat)
389+
return string(result)
390+
}
391+
392+
// orgRepoPath converts either
393+
// - a regular GitHub url of form https://github.com/org/repo/blob/branch/path/to/file
394+
// - a raw GitHub url of form https://raw.githubusercontent.com/org/repo/branch/path/to/file
395+
// to a string of form 'org/repo/path/to/file'
396+
func orgRepoPath(url string) string {
397+
for _, regex := range []string{regexRawGitHubURL, regexGitHubURL} {
398+
re := regexp.MustCompile(regex)
399+
mat := re.FindStringSubmatchIndex(url)
400+
if mat == nil {
401+
continue
402+
}
403+
result := re.ExpandString([]byte{}, "${org}/${repo}/${path}", url, mat)
404+
return string(result)
405+
}
406+
return url
372407
}
373408

374409
// tzUrlEncode returns a url encoded string without the + shortcut. This is
@@ -525,6 +560,7 @@ func main() {
525560
log.Fatal(err)
526561
}
527562

563+
fmt.Println("Generating group READMEs")
528564
for prefix, groups := range ctx.PrefixToGroupMap() {
529565
err = createGroupReadme(groups, prefix)
530566
if err != nil {

Diff for: generator/app_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,73 @@ func TestFullGeneration(t *testing.T) {
236236
}
237237
}
238238
}
239+
240+
func TestGitHubURL(t *testing.T) {
241+
cases := []struct {
242+
name string
243+
url string
244+
expected string
245+
}{
246+
{
247+
name: "kubernetes-sigs root raw github url",
248+
url: "https://raw.githubusercontent.com/kubernetes-sigs/boskos/main/OWNERS",
249+
expected: "https://github.com/kubernetes-sigs/boskos/blob/main/OWNERS",
250+
},
251+
{
252+
name: "kubernetes non-root raw github url",
253+
url: "https://raw.githubusercontent.com/kubernetes/kubernetes/main/test/OWNERS",
254+
expected: "https://github.com/kubernetes/kubernetes/blob/main/test/OWNERS",
255+
},
256+
{
257+
name: "kubernetes github url should be unchanged",
258+
url: "https://github.com/kubernetes/kubernetes/blob/main/test/OWNERS",
259+
expected: "https://github.com/kubernetes/kubernetes/blob/main/test/OWNERS",
260+
},
261+
{
262+
name: "non-github url should be unchanged",
263+
url: "https://viewsource.com/github/kubernetes/community/generator/app.go",
264+
expected: "https://viewsource.com/github/kubernetes/community/generator/app.go",
265+
},
266+
}
267+
for _, c := range cases {
268+
actual := githubURL(c.url)
269+
if actual != c.expected {
270+
t.Errorf("FAIL %s: got: '%s' but expected: '%s'", c.name, actual, c.expected)
271+
}
272+
}
273+
}
274+
275+
func TestOrgRepoPath(t *testing.T) {
276+
cases := []struct {
277+
name string
278+
url string
279+
expected string
280+
}{
281+
{
282+
name: "kubernetes-sigs root raw github url",
283+
url: "https://raw.githubusercontent.com/kubernetes-sigs/boskos/main/OWNERS",
284+
expected: "kubernetes-sigs/boskos/OWNERS",
285+
},
286+
{
287+
name: "kubernetes non-root raw github url",
288+
url: "https://raw.githubusercontent.com/kubernetes/kubernetes/main/test/OWNERS",
289+
expected: "kubernetes/kubernetes/test/OWNERS",
290+
},
291+
{
292+
name: "kubernetes github url",
293+
url: "https://github.com/kubernetes/kubernetes/blob/main/test/OWNERS",
294+
expected: "kubernetes/kubernetes/test/OWNERS",
295+
},
296+
{
297+
name: "non-github url should be unchanged",
298+
url: "https://viewsource.com/github/kubernetes/community/generator/app.go",
299+
expected: "https://viewsource.com/github/kubernetes/community/generator/app.go",
300+
},
301+
}
302+
for _, c := range cases {
303+
actual := orgRepoPath(c.url)
304+
if actual != c.expected {
305+
t.Errorf("FAIL %s: got: '%s' but expected: '%s'", c.name, actual, c.expected)
306+
}
307+
}
308+
}

0 commit comments

Comments
 (0)