|
| 1 | +/* |
| 2 | +Copyright 2025 The Tekton Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/* |
| 18 | +This utility can be used to generate a new release name in the format: |
| 19 | + <cat breed> <robot name> |
| 20 | +
|
| 21 | +to be used for a Tekton Pipelines release. |
| 22 | +It looks for cat breeds from CatAPIURL and it parses robot names out |
| 23 | +of Wikipedia WikiURL. It filters names that have been used already, |
| 24 | +based on the GitHub API GitHubReleasesURL |
| 25 | +
|
| 26 | +To use, run: |
| 27 | + go run release_names.go |
| 28 | +
|
| 29 | +
|
| 30 | +Example output: |
| 31 | + { |
| 32 | + "release_name": "California Spangled Clank", |
| 33 | + "cat_breed_url": "https://en.wikipedia.org/wiki/California_Spangled", |
| 34 | + "robot_url": "https://en.wikipedia.org/wiki/Clank" |
| 35 | + } |
| 36 | +*/ |
| 37 | + |
| 38 | +package main |
| 39 | + |
| 40 | +import ( |
| 41 | + "context" |
| 42 | + "crypto/rand" |
| 43 | + "encoding/json" |
| 44 | + "errors" |
| 45 | + "fmt" |
| 46 | + "io" |
| 47 | + "math/big" |
| 48 | + "net/http" |
| 49 | + "regexp" |
| 50 | + "strings" |
| 51 | +) |
| 52 | + |
| 53 | +// API Endpoints |
| 54 | +const ( |
| 55 | + CatAPIURL = "https://api.thecatapi.com/v1/breeds" |
| 56 | + RobotWikiURL = "https://en.wikipedia.org/wiki/List_of_fictional_robots_and_androids" |
| 57 | + WikiURL = "https://en.wikipedia.org/wiki/" |
| 58 | + GitHubReleasesURL = "https://api.github.com/repos/tektoncd/pipeline/releases" |
| 59 | +) |
| 60 | + |
| 61 | +// Structs to hold API responses |
| 62 | +type CatBreed struct { |
| 63 | + Name string `json:"name"` |
| 64 | +} |
| 65 | + |
| 66 | +type Release struct { |
| 67 | + Name string `json:"name"` |
| 68 | +} |
| 69 | + |
| 70 | +func httpGet(url string) (*http.Response, error) { |
| 71 | + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + return http.DefaultClient.Do(req) |
| 76 | +} |
| 77 | + |
| 78 | +// Fetch cat breeds and organize them by first letter |
| 79 | +func getCatBreeds() (map[string][]string, error) { |
| 80 | + resp, err := httpGet(CatAPIURL) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + defer resp.Body.Close() |
| 85 | + |
| 86 | + var breeds []CatBreed |
| 87 | + if err := json.NewDecoder(resp.Body).Decode(&breeds); err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + catDict := make(map[string][]string) |
| 92 | + for _, breed := range breeds { |
| 93 | + firstLetter := strings.ToUpper(string(breed.Name[0])) |
| 94 | + catDict[firstLetter] = append(catDict[firstLetter], breed.Name) |
| 95 | + } |
| 96 | + |
| 97 | + return catDict, nil |
| 98 | +} |
| 99 | + |
| 100 | +// Scrape Wikipedia for robot names |
| 101 | +func getRobotNames() (map[string][]string, error) { |
| 102 | + resp, err := httpGet(RobotWikiURL) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + defer resp.Body.Close() |
| 107 | + |
| 108 | + bodyBytes, err := io.ReadAll(resp.Body) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + robotDict := make(map[string][]string) |
| 114 | + |
| 115 | + // Regex to extract robot names from <li><b>Robot Name</b> |
| 116 | + re := regexp.MustCompile(`<li>\s*<b>\s*<a[^>]*>([^<]+)</a>\s*</b>`) |
| 117 | + matches := re.FindAllStringSubmatch(string(bodyBytes), -1) |
| 118 | + |
| 119 | + for _, match := range matches { |
| 120 | + if len(match) > 1 { |
| 121 | + name := strings.TrimSpace(match[1]) |
| 122 | + firstLetter := strings.ToUpper(string(name[0])) |
| 123 | + robotDict[firstLetter] = append(robotDict[firstLetter], name) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return robotDict, nil |
| 128 | +} |
| 129 | + |
| 130 | +// Fetch past releases from GitHub |
| 131 | +func getPastReleases() (map[string]bool, error) { |
| 132 | + resp, err := httpGet(GitHubReleasesURL + "?per_page=100") |
| 133 | + if err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + defer resp.Body.Close() |
| 137 | + |
| 138 | + var releases []Release |
| 139 | + if err := json.NewDecoder(resp.Body).Decode(&releases); err != nil { |
| 140 | + return nil, err |
| 141 | + } |
| 142 | + |
| 143 | + pastReleases := make(map[string]bool) |
| 144 | + for _, release := range releases { |
| 145 | + pastReleases[release.Name] = true |
| 146 | + } |
| 147 | + |
| 148 | + return pastReleases, nil |
| 149 | +} |
| 150 | + |
| 151 | +func randomElement(array []string) (string, error) { |
| 152 | + n, err := rand.Int(rand.Reader, big.NewInt(int64(len(array)))) |
| 153 | + if err != nil { |
| 154 | + return "", err |
| 155 | + } |
| 156 | + return array[n.Int64()], nil |
| 157 | +} |
| 158 | + |
| 159 | +// Generate a unique release name |
| 160 | +func generateUniqueTuple() (string, string, error) { |
| 161 | + catBreeds, err := getCatBreeds() |
| 162 | + if err != nil { |
| 163 | + return "", "", err |
| 164 | + } |
| 165 | + |
| 166 | + robotNames, err := getRobotNames() |
| 167 | + if err != nil { |
| 168 | + return "", "", err |
| 169 | + } |
| 170 | + |
| 171 | + pastReleases, err := getPastReleases() |
| 172 | + if err != nil { |
| 173 | + return "", "", err |
| 174 | + } |
| 175 | + |
| 176 | + // Find common letters |
| 177 | + commonLetters := []string{} |
| 178 | + for letter := range catBreeds { |
| 179 | + if _, exists := robotNames[letter]; exists { |
| 180 | + commonLetters = append(commonLetters, letter) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + if len(commonLetters) == 0 { |
| 185 | + return "", "", errors.New("no matching names found") |
| 186 | + } |
| 187 | + |
| 188 | + maxAttempts := 10 |
| 189 | + for range maxAttempts { |
| 190 | + chosenLetter, err := randomElement(commonLetters) |
| 191 | + if err != nil { |
| 192 | + return "", "", err |
| 193 | + } |
| 194 | + |
| 195 | + cat, err := randomElement(catBreeds[chosenLetter]) |
| 196 | + if err != nil { |
| 197 | + return "", "", err |
| 198 | + } |
| 199 | + |
| 200 | + robot, err := randomElement(robotNames[chosenLetter]) |
| 201 | + if err != nil { |
| 202 | + return "", "", err |
| 203 | + } |
| 204 | + |
| 205 | + newName := cat + " " + robot |
| 206 | + if !pastReleases[newName] { |
| 207 | + return cat, robot, nil |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + return "", "", errors.New("could not generate a unique name after multiple attempts") |
| 212 | +} |
| 213 | + |
| 214 | +func printJsonError(err error) { |
| 215 | + fmt.Println(`{"error": "` + err.Error() + `"}`) //nolint:forbidigo |
| 216 | +} |
| 217 | + |
| 218 | +func main() { |
| 219 | + cat, robot, err := generateUniqueTuple() |
| 220 | + if err != nil { |
| 221 | + printJsonError(err) |
| 222 | + return |
| 223 | + } |
| 224 | + |
| 225 | + output := map[string]string{ |
| 226 | + "release_name": cat + " " + robot, |
| 227 | + "cat_breed_url": WikiURL + strings.ReplaceAll(cat, " ", "_"), |
| 228 | + "robot_url": WikiURL + strings.ReplaceAll(robot, " ", "_"), |
| 229 | + } |
| 230 | + |
| 231 | + jsonOutput, err := json.MarshalIndent(output, "", " ") |
| 232 | + if err != nil { |
| 233 | + printJsonError(err) |
| 234 | + return |
| 235 | + } |
| 236 | + fmt.Println(string(jsonOutput)) //nolint:forbidigo |
| 237 | +} |
0 commit comments