|
| 1 | +package githubtemplate |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "path" |
| 6 | + "regexp" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "gopkg.in/yaml.v3" |
| 11 | +) |
| 12 | + |
| 13 | +// Find returns the list of template file paths |
| 14 | +func Find(rootDir string, name string) []string { |
| 15 | + results := []string{} |
| 16 | + |
| 17 | + // https://help.github.com/en/github/building-a-strong-community/creating-a-pull-request-template-for-your-repository |
| 18 | + candidateDirs := []string{ |
| 19 | + path.Join(rootDir, ".github"), |
| 20 | + rootDir, |
| 21 | + path.Join(rootDir, "docs"), |
| 22 | + } |
| 23 | + |
| 24 | +mainLoop: |
| 25 | + for _, dir := range candidateDirs { |
| 26 | + files, err := ioutil.ReadDir(dir) |
| 27 | + if err != nil { |
| 28 | + continue |
| 29 | + } |
| 30 | + |
| 31 | + // detect multiple templates in a subdirectory |
| 32 | + for _, file := range files { |
| 33 | + if strings.EqualFold(file.Name(), name) && file.IsDir() { |
| 34 | + templates, err := ioutil.ReadDir(path.Join(dir, file.Name())) |
| 35 | + if err != nil { |
| 36 | + break |
| 37 | + } |
| 38 | + for _, tf := range templates { |
| 39 | + if strings.HasSuffix(tf.Name(), ".md") { |
| 40 | + results = append(results, path.Join(dir, file.Name(), tf.Name())) |
| 41 | + } |
| 42 | + } |
| 43 | + if len(results) > 0 { |
| 44 | + break mainLoop |
| 45 | + } |
| 46 | + break |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // detect a single template file |
| 51 | + for _, file := range files { |
| 52 | + if strings.EqualFold(file.Name(), name+".md") { |
| 53 | + results = append(results, path.Join(dir, file.Name())) |
| 54 | + break |
| 55 | + } |
| 56 | + } |
| 57 | + if len(results) > 0 { |
| 58 | + break |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + sort.Sort(sort.StringSlice(results)) |
| 63 | + return results |
| 64 | +} |
| 65 | + |
| 66 | +// ExtractName returns the name of the template from YAML front-matter |
| 67 | +func ExtractName(filePath string) string { |
| 68 | + contents, err := ioutil.ReadFile(filePath) |
| 69 | + if err == nil && detectFrontmatter(contents)[0] == 0 { |
| 70 | + templateData := struct { |
| 71 | + Name string |
| 72 | + }{} |
| 73 | + if err := yaml.Unmarshal(contents, &templateData); err == nil && templateData.Name != "" { |
| 74 | + return templateData.Name |
| 75 | + } |
| 76 | + } |
| 77 | + return path.Base(filePath) |
| 78 | +} |
| 79 | + |
| 80 | +// ExtractContents returns the template contents without the YAML front-matter |
| 81 | +func ExtractContents(filePath string) []byte { |
| 82 | + contents, err := ioutil.ReadFile(filePath) |
| 83 | + if err != nil { |
| 84 | + return []byte{} |
| 85 | + } |
| 86 | + if frontmatterBoundaries := detectFrontmatter(contents); frontmatterBoundaries[0] == 0 { |
| 87 | + return contents[frontmatterBoundaries[1]:] |
| 88 | + } |
| 89 | + return contents |
| 90 | +} |
| 91 | + |
| 92 | +var yamlPattern = regexp.MustCompile(`(?m)^---\r?\n(\s*\r?\n)?`) |
| 93 | + |
| 94 | +func detectFrontmatter(c []byte) []int { |
| 95 | + if matches := yamlPattern.FindAllIndex(c, 2); len(matches) > 1 { |
| 96 | + return []int{matches[0][0], matches[1][1]} |
| 97 | + } |
| 98 | + return []int{-1, -1} |
| 99 | +} |
0 commit comments