|
| 1 | +// +build tools |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2022 The Kubernetes Authors. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +//go:build tools |
| 20 | +package main |
| 21 | + |
| 22 | +import ( |
| 23 | + "bytes" |
| 24 | + "flag" |
| 25 | + "fmt" |
| 26 | + "os" |
| 27 | + "os/exec" |
| 28 | + "strings" |
| 29 | +) |
| 30 | + |
| 31 | +/* |
| 32 | +This tool prints all the titles of all PRs from previous release to HEAD. |
| 33 | +This needs to be run *before* a tag is created. |
| 34 | +
|
| 35 | +Use these as the base of your release notes. |
| 36 | +*/ |
| 37 | + |
| 38 | +const ( |
| 39 | + features = ":sparkles: New Features" |
| 40 | + bugs = ":bug: Bug Fixes" |
| 41 | + documentation = ":book: Documentation" |
| 42 | + proposals = ":memo: Proposals" |
| 43 | + warning = ":warning: Breaking Changes" |
| 44 | + other = ":seedling: Others" |
| 45 | + unknown = ":question: Sort these by hand" |
| 46 | +) |
| 47 | + |
| 48 | +var ( |
| 49 | + outputOrder = []string{ |
| 50 | + proposals, |
| 51 | + warning, |
| 52 | + features, |
| 53 | + bugs, |
| 54 | + other, |
| 55 | + documentation, |
| 56 | + unknown, |
| 57 | + } |
| 58 | + |
| 59 | + fromTag = flag.String("from", "", "The tag or commit to start from.") |
| 60 | +) |
| 61 | + |
| 62 | +func main() { |
| 63 | + flag.Parse() |
| 64 | + os.Exit(run()) |
| 65 | +} |
| 66 | + |
| 67 | +func lastTag() string { |
| 68 | + if fromTag != nil && *fromTag != "" { |
| 69 | + return *fromTag |
| 70 | + } |
| 71 | + cmd := exec.Command("git", "describe", "--tags", "--abbrev=0") |
| 72 | + out, err := cmd.Output() |
| 73 | + if err != nil { |
| 74 | + return firstCommit() |
| 75 | + } |
| 76 | + return string(bytes.TrimSpace(out)) |
| 77 | +} |
| 78 | + |
| 79 | +func firstCommit() string { |
| 80 | + cmd := exec.Command("git", "rev-list", "--max-parents=0", "HEAD") |
| 81 | + out, err := cmd.Output() |
| 82 | + if err != nil { |
| 83 | + return "UNKNOWN" |
| 84 | + } |
| 85 | + return string(bytes.TrimSpace(out)) |
| 86 | +} |
| 87 | + |
| 88 | +func run() int { |
| 89 | + lastTag := lastTag() |
| 90 | + cmd := exec.Command("git", "rev-list", lastTag+"..HEAD", "--merges", "--pretty=format:%B") //nolint:gosec |
| 91 | + |
| 92 | + merges := map[string][]string{ |
| 93 | + features: {}, |
| 94 | + bugs: {}, |
| 95 | + documentation: {}, |
| 96 | + warning: {}, |
| 97 | + other: {}, |
| 98 | + unknown: {}, |
| 99 | + } |
| 100 | + out, err := cmd.CombinedOutput() |
| 101 | + if err != nil { |
| 102 | + fmt.Println("Error") |
| 103 | + fmt.Println(string(out)) |
| 104 | + return 1 |
| 105 | + } |
| 106 | + |
| 107 | + commits := []*commit{} |
| 108 | + outLines := strings.Split(string(out), "\n") |
| 109 | + for _, line := range outLines { |
| 110 | + line = strings.TrimSpace(line) |
| 111 | + last := len(commits) - 1 |
| 112 | + switch { |
| 113 | + case strings.HasPrefix(line, "commit"): |
| 114 | + commits = append(commits, &commit{}) |
| 115 | + case strings.HasPrefix(line, "Merge"): |
| 116 | + commits[last].merge = line |
| 117 | + continue |
| 118 | + case line == "": |
| 119 | + default: |
| 120 | + commits[last].body = line |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + for _, c := range commits { |
| 125 | + body := strings.TrimSpace(c.body) |
| 126 | + var key, prNumber, fork string |
| 127 | + switch { |
| 128 | + case strings.HasPrefix(body, ":sparkles:"), strings.HasPrefix(body, "✨"): |
| 129 | + key = features |
| 130 | + body = strings.TrimPrefix(body, ":sparkles:") |
| 131 | + body = strings.TrimPrefix(body, "✨") |
| 132 | + case strings.HasPrefix(body, ":bug:"), strings.HasPrefix(body, "🐛"): |
| 133 | + key = bugs |
| 134 | + body = strings.TrimPrefix(body, ":bug:") |
| 135 | + body = strings.TrimPrefix(body, "🐛") |
| 136 | + case strings.HasPrefix(body, ":book:"), strings.HasPrefix(body, "📖"): |
| 137 | + key = documentation |
| 138 | + body = strings.TrimPrefix(body, ":book:") |
| 139 | + body = strings.TrimPrefix(body, "📖") |
| 140 | + if strings.Contains(body, "CAEP") || strings.Contains(body, "proposal") { |
| 141 | + key = proposals |
| 142 | + } |
| 143 | + case strings.HasPrefix(body, ":seedling:"), strings.HasPrefix(body, "🌱"): |
| 144 | + key = other |
| 145 | + body = strings.TrimPrefix(body, ":seedling:") |
| 146 | + body = strings.TrimPrefix(body, "🌱") |
| 147 | + case strings.HasPrefix(body, ":warning:"), strings.HasPrefix(body, "⚠️"): |
| 148 | + key = warning |
| 149 | + body = strings.TrimPrefix(body, ":warning:") |
| 150 | + body = strings.TrimPrefix(body, "⚠️") |
| 151 | + default: |
| 152 | + key = unknown |
| 153 | + } |
| 154 | + |
| 155 | + body = strings.TrimSpace(body) |
| 156 | + if body == "" { |
| 157 | + continue |
| 158 | + } |
| 159 | + body = fmt.Sprintf("- %s", body) |
| 160 | + _, _ = fmt.Sscanf(c.merge, "Merge pull request %s from %s", &prNumber, &fork) |
| 161 | + if key == documentation { |
| 162 | + merges[key] = append(merges[key], prNumber) |
| 163 | + continue |
| 164 | + } |
| 165 | + merges[key] = append(merges[key], formatMerge(body, prNumber)) |
| 166 | + } |
| 167 | + |
| 168 | + // TODO Turn this into a link (requires knowing the project name + organization) |
| 169 | + fmt.Printf("Changes since %v\n---\n", lastTag) |
| 170 | + |
| 171 | + for _, key := range outputOrder { |
| 172 | + mergeslice := merges[key] |
| 173 | + if len(mergeslice) == 0 { |
| 174 | + continue |
| 175 | + } |
| 176 | + |
| 177 | + switch key { |
| 178 | + case documentation: |
| 179 | + fmt.Printf( |
| 180 | + ":book: Additionally, there have been %d contributions to our documentation and book. (%s) \n\n", |
| 181 | + len(mergeslice), |
| 182 | + strings.Join(mergeslice, ", "), |
| 183 | + ) |
| 184 | + default: |
| 185 | + fmt.Println("## " + key) |
| 186 | + for _, merge := range mergeslice { |
| 187 | + fmt.Println(merge) |
| 188 | + } |
| 189 | + fmt.Println() |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + fmt.Println("") |
| 194 | + fmt.Println("_Thanks to all our contributors!_ 😊") |
| 195 | + |
| 196 | + return 0 |
| 197 | +} |
| 198 | + |
| 199 | +type commit struct { |
| 200 | + merge string |
| 201 | + body string |
| 202 | +} |
| 203 | + |
| 204 | +func formatMerge(line, prNumber string) string { |
| 205 | + if prNumber == "" { |
| 206 | + return line |
| 207 | + } |
| 208 | + return fmt.Sprintf("%s (%s)", line, prNumber) |
| 209 | +} |
0 commit comments