-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a tool to generate the release name
Signed-off-by: Andrea Frittoli <[email protected]>
- Loading branch information
Showing
2 changed files
with
239 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
/* | ||
Copyright 2025 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/* | ||
This utility can be used to generate a new release name in the format: | ||
<cat breed> <robot name> | ||
to be used for a Tekton Pipelines release. | ||
It looks for cat breeds from CatAPIURL and it parses robot names out | ||
of Wikipedia WikiURL. It filters names that have been used already, | ||
based on the GitHub API GitHubReleasesURL | ||
To use, run: | ||
go run release_names.go | ||
Example output: | ||
{ | ||
"release_name": "California Spangled Clank", | ||
"cat_breed_url": "https://en.wikipedia.org/wiki/California_Spangled", | ||
"robot_url": "https://en.wikipedia.org/wiki/Clank" | ||
} | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"math/rand" | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// API Endpoints | ||
const ( | ||
CatAPIURL = "https://api.thecatapi.com/v1/breeds" | ||
WikiURL = "https://en.wikipedia.org/wiki/List_of_fictional_robots_and_androids" | ||
GitHubReleasesURL = "https://api.github.com/repos/tektoncd/pipeline/releases" | ||
) | ||
|
||
// Structs to hold API responses | ||
type CatBreed struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
type Release struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
// Fetch cat breeds and organize them by first letter | ||
func getCatBreeds() (map[string][][2]string, error) { | ||
resp, err := http.Get(CatAPIURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
var breeds []CatBreed | ||
if err := json.NewDecoder(resp.Body).Decode(&breeds); err != nil { | ||
return nil, err | ||
} | ||
|
||
catDict := make(map[string][][2]string) | ||
for _, breed := range breeds { | ||
firstLetter := strings.ToUpper(string(breed.Name[0])) | ||
wikiURL := "https://en.wikipedia.org/wiki/" + strings.ReplaceAll(breed.Name, " ", "_") | ||
catDict[firstLetter] = append(catDict[firstLetter], [2]string{breed.Name, wikiURL}) | ||
} | ||
|
||
return catDict, nil | ||
} | ||
|
||
// Scrape Wikipedia for robot names | ||
func getRobotNames() (map[string][][2]string, error) { | ||
resp, err := http.Get(WikiURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
robotDict := make(map[string][][2]string) | ||
|
||
// Regex to extract robot names from <li><b>Robot Name</b> | ||
re := regexp.MustCompile(`<li>\s*<b>\s*<a[^>]*>([^<]+)</a>\s*</b>`) | ||
matches := re.FindAllStringSubmatch(string(body), -1) | ||
|
||
for _, match := range matches { | ||
if len(match) > 1 { | ||
name := strings.TrimSpace(match[1]) | ||
firstLetter := strings.ToUpper(string(name[0])) | ||
wikiURL := "https://en.wikipedia.org/wiki/" + strings.ReplaceAll(name, " ", "_") | ||
robotDict[firstLetter] = append(robotDict[firstLetter], [2]string{name, wikiURL}) | ||
} | ||
} | ||
|
||
return robotDict, nil | ||
} | ||
|
||
// Fetch past releases from GitHub | ||
func getPastReleases() (map[string]bool, error) { | ||
resp, err := http.Get(GitHubReleasesURL + "?per_page=100") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
var releases []Release | ||
if err := json.NewDecoder(resp.Body).Decode(&releases); err != nil { | ||
return nil, err | ||
} | ||
|
||
pastReleases := make(map[string]bool) | ||
for _, release := range releases { | ||
pastReleases[release.Name] = true | ||
} | ||
|
||
return pastReleases, nil | ||
} | ||
|
||
// Generate a unique release name | ||
func generateUniqueName() (string, string, string, error) { | ||
catBreeds, err := getCatBreeds() | ||
if err != nil { | ||
return "", "", "", err | ||
} | ||
|
||
robotNames, err := getRobotNames() | ||
if err != nil { | ||
return "", "", "", err | ||
} | ||
|
||
pastReleases, err := getPastReleases() | ||
if err != nil { | ||
return "", "", "", err | ||
} | ||
|
||
// Find common letters | ||
commonLetters := []string{} | ||
for letter := range catBreeds { | ||
if _, exists := robotNames[letter]; exists { | ||
commonLetters = append(commonLetters, letter) | ||
} | ||
} | ||
|
||
if len(commonLetters) == 0 { | ||
return "", "", "", errors.New("no matching names found") | ||
} | ||
|
||
maxAttempts := 10 | ||
for i := 0; i < maxAttempts; i++ { | ||
chosenLetter := commonLetters[rand.Intn(len(commonLetters))] | ||
|
||
cat := catBreeds[chosenLetter][rand.Intn(len(catBreeds[chosenLetter]))] | ||
robot := robotNames[chosenLetter][rand.Intn(len(robotNames[chosenLetter]))] | ||
|
||
newName := cat[0] + " " + robot[0] | ||
if !pastReleases[newName] { | ||
return newName, cat[1], robot[1], nil | ||
} | ||
} | ||
|
||
return "", "", "", errors.New("could not generate a unique name after multiple attempts") | ||
} | ||
|
||
func main() { | ||
name, catURL, robotURL, err := generateUniqueName() | ||
if err != nil { | ||
fmt.Println(`{"error": "` + err.Error() + `"}`) | ||
return | ||
} | ||
|
||
output := map[string]string{ | ||
"release_name": name, | ||
"cat_breed_url": catURL, | ||
"robot_url": robotURL, | ||
} | ||
|
||
jsonOutput, _ := json.MarshalIndent(output, "", " ") | ||
fmt.Println(string(jsonOutput)) | ||
} |