| 1 | // Package desctpl leest beschrijvingssjablonen uit de standaardbranch |
| 2 | // en vult de variabelen van een merge request in. |
| 3 | package desctpl |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "regexp" |
| 8 | "sort" |
| 9 | "strings" |
| 10 | |
| 11 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 12 | ) |
| 13 | |
| 14 | // Template is één Markdown-bestand zonder het achtervoegsel .md. |
| 15 | type Template struct { |
| 16 | Name string |
| 17 | Body string |
| 18 | } |
| 19 | |
| 20 | // Commit is een bericht op de bronbranch, oudste eerst. |
| 21 | type Commit struct { |
| 22 | Subject string |
| 23 | Body string |
| 24 | Author string |
| 25 | Email string |
| 26 | Merge bool |
| 27 | } |
| 28 | |
| 29 | // Vars zijn de waarden voor %{...} bij het aanmaken van een merge request. |
| 30 | type Vars struct { |
| 31 | Source string |
| 32 | Target string |
| 33 | Commits []Commit |
| 34 | } |
| 35 | |
| 36 | const hugeBody = 10000 |
| 37 | |
| 38 | var ( |
| 39 | closesBranch = regexp.MustCompile(`^(\d+)-`) |
| 40 | coAuthor = regexp.MustCompile(`(?m)^Co-authored-by: .+$`) |
| 41 | ) |
| 42 | |
| 43 | // List leest .bluegit/<kind>_templates en valt terug op .gitlab/. |
| 44 | // Een gelijke naam uit .bluegit wint. kind is issue of mr. |
| 45 | // Ontbreken beide mappen, dan is de lijst leeg. Dat is geen fout en er wordt niets aangemaakt. |
| 46 | func List(ctx context.Context, gitBin, dir, rev, kind string) ([]Template, error) { |
| 47 | sub, err := subdir(kind) |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | found := map[string]Template{} |
| 52 | var order []string |
| 53 | for _, root := range []string{".bluegit", ".gitlab"} { |
| 54 | entries, err := gitrepo.Tree(ctx, gitBin, dir, rev, root+"/"+sub) |
| 55 | if err != nil { |
| 56 | continue |
| 57 | } |
| 58 | for _, e := range entries { |
| 59 | if e.Type != "blob" || !strings.HasSuffix(e.Name, ".md") { |
| 60 | continue |
| 61 | } |
| 62 | name := strings.TrimSuffix(e.Name, ".md") |
| 63 | if !validName(name) { |
| 64 | continue |
| 65 | } |
| 66 | if _, ok := found[name]; ok { |
| 67 | continue |
| 68 | } |
| 69 | text, binary, err := gitrepo.Blob(ctx, gitBin, dir, rev, root+"/"+sub+"/"+e.Name, 100000) |
| 70 | if err != nil || binary { |
| 71 | continue |
| 72 | } |
| 73 | if len(text) > 20000 { |
| 74 | text = text[:20000] |
| 75 | } |
| 76 | found[name] = Template{Name: name, Body: text} |
| 77 | order = append(order, name) |
| 78 | } |
| 79 | } |
| 80 | list := make([]Template, 0, len(order)) |
| 81 | for _, name := range order { |
| 82 | list = append(list, found[name]) |
| 83 | } |
| 84 | sort.SliceStable(list, func(i, j int) bool { |
| 85 | di := strings.EqualFold(list[i].Name, "Default") |
| 86 | dj := strings.EqualFold(list[j].Name, "Default") |
| 87 | if di != dj { |
| 88 | return di |
| 89 | } |
| 90 | return strings.ToLower(list[i].Name) < strings.ToLower(list[j].Name) |
| 91 | }) |
| 92 | return list, nil |
| 93 | } |
| 94 | |
| 95 | // Find zoekt een sjabloon op naam. Een hoofdletterverschil telt alleen als er één kandidaat is. |
| 96 | func Find(list []Template, name string) (Template, bool) { |
| 97 | name = strings.TrimSpace(name) |
| 98 | if name == "" { |
| 99 | return Template{}, false |
| 100 | } |
| 101 | for _, t := range list { |
| 102 | if t.Name == name { |
| 103 | return t, true |
| 104 | } |
| 105 | } |
| 106 | var hit Template |
| 107 | n := 0 |
| 108 | for _, t := range list { |
| 109 | if strings.EqualFold(t.Name, name) { |
| 110 | hit = t |
| 111 | n++ |
| 112 | } |
| 113 | } |
| 114 | if n == 1 { |
| 115 | return hit, true |
| 116 | } |
| 117 | return Template{}, false |
| 118 | } |
| 119 | |
| 120 | // Default is Default.md, ongeacht hoofdletters. Een exacte naam wint. |
| 121 | func Default(list []Template) (Template, bool) { |
| 122 | var loose Template |
| 123 | looseOK := false |
| 124 | for _, t := range list { |
| 125 | if t.Name == "Default" { |
| 126 | return t, true |
| 127 | } |
| 128 | if !looseOK && strings.EqualFold(t.Name, "Default") { |
| 129 | loose = t |
| 130 | looseOK = true |
| 131 | } |
| 132 | } |
| 133 | return loose, looseOK |
| 134 | } |
| 135 | |
| 136 | // Expand vervangt de variabelen. Een merge-commit en een heel lang bericht |
| 137 | // tellen niet mee voor de commitlijsten. Er worden hooguit 100 commits gebruikt. |
| 138 | func Expand(body string, v Vars) string { |
| 139 | commits := v.Commits |
| 140 | if len(commits) > 100 { |
| 141 | commits = commits[:100] |
| 142 | } |
| 143 | repl := map[string]string{ |
| 144 | "%{source_branch}": v.Source, |
| 145 | "%{target_branch}": v.Target, |
| 146 | "%{first_commit}": firstCommit(commits), |
| 147 | "%{all_commits}": allCommits(commits), |
| 148 | "%{co_authored_by}": coAuthored(commits), |
| 149 | "%{first_multiline_commit}": firstMultiline(commits), |
| 150 | "%{closes_issue}": closesIssue(v.Source), |
| 151 | } |
| 152 | for token, value := range repl { |
| 153 | body = strings.ReplaceAll(body, token, value) |
| 154 | } |
| 155 | return body |
| 156 | } |
| 157 | |
| 158 | func closesIssue(branch string) string { |
| 159 | m := closesBranch.FindStringSubmatch(branch) |
| 160 | if m == nil { |
| 161 | return "" |
| 162 | } |
| 163 | return "Closes #" + m[1] |
| 164 | } |
| 165 | |
| 166 | func usable(c Commit) bool { |
| 167 | return !c.Merge && len(c.Subject) <= hugeBody && len(c.Body) <= hugeBody |
| 168 | } |
| 169 | |
| 170 | func message(c Commit) string { |
| 171 | if c.Body == "" { |
| 172 | return c.Subject |
| 173 | } |
| 174 | return c.Subject + "\n\n" + c.Body |
| 175 | } |
| 176 | |
| 177 | func firstCommit(commits []Commit) string { |
| 178 | for _, c := range commits { |
| 179 | if usable(c) { |
| 180 | return message(c) |
| 181 | } |
| 182 | } |
| 183 | return "" |
| 184 | } |
| 185 | |
| 186 | func firstMultiline(commits []Commit) string { |
| 187 | for _, c := range commits { |
| 188 | if usable(c) && c.Body != "" { |
| 189 | return message(c) |
| 190 | } |
| 191 | } |
| 192 | return "" |
| 193 | } |
| 194 | |
| 195 | func allCommits(commits []Commit) string { |
| 196 | var parts []string |
| 197 | for _, c := range commits { |
| 198 | if !usable(c) { |
| 199 | continue |
| 200 | } |
| 201 | parts = append(parts, message(c)) |
| 202 | } |
| 203 | return strings.Join(parts, "\n\n") |
| 204 | } |
| 205 | |
| 206 | func coAuthored(commits []Commit) string { |
| 207 | seen := map[string]bool{} |
| 208 | var lines []string |
| 209 | for _, c := range commits { |
| 210 | if len(c.Body) > hugeBody { |
| 211 | continue |
| 212 | } |
| 213 | for _, line := range coAuthor.FindAllString(c.Body, -1) { |
| 214 | if seen[line] { |
| 215 | continue |
| 216 | } |
| 217 | seen[line] = true |
| 218 | lines = append(lines, line) |
| 219 | } |
| 220 | } |
| 221 | return strings.Join(lines, "\n") |
| 222 | } |
| 223 | |
| 224 | func subdir(kind string) (string, error) { |
| 225 | switch kind { |
| 226 | case "issue": |
| 227 | return "issue_templates", nil |
| 228 | case "mr": |
| 229 | return "merge_request_templates", nil |
| 230 | default: |
| 231 | return "", errKind |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | type kindError struct{} |
| 236 | |
| 237 | func (kindError) Error() string { return "onbekend sjabloon" } |
| 238 | |
| 239 | var errKind kindError |
| 240 | |
| 241 | func validName(name string) bool { |
| 242 | if name == "" || name == "." || name == ".." { |
| 243 | return false |
| 244 | } |
| 245 | return !strings.ContainsAny(name, "/\\") |
| 246 | } |