-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a tool to generate the release name #8629
Open
afrittoli
wants to merge
1
commit into
tektoncd:main
Choose a base branch
from
afrittoli:release_name_automation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,237 @@ | ||
/* | ||
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 ( | ||
"context" | ||
"crypto/rand" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"math/big" | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// API Endpoints | ||
const ( | ||
CatAPIURL = "https://api.thecatapi.com/v1/breeds" | ||
RobotWikiURL = "https://en.wikipedia.org/wiki/List_of_fictional_robots_and_androids" | ||
WikiURL = "https://en.wikipedia.org/wiki/" | ||
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"` | ||
} | ||
|
||
func httpGet(url string) (*http.Response, error) { | ||
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return http.DefaultClient.Do(req) | ||
} | ||
|
||
// Fetch cat breeds and organize them by first letter | ||
func getCatBreeds() (map[string][]string, error) { | ||
resp, err := httpGet(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][]string) | ||
for _, breed := range breeds { | ||
firstLetter := strings.ToUpper(string(breed.Name[0])) | ||
catDict[firstLetter] = append(catDict[firstLetter], breed.Name) | ||
} | ||
|
||
return catDict, nil | ||
} | ||
|
||
// Scrape Wikipedia for robot names | ||
func getRobotNames() (map[string][]string, error) { | ||
resp, err := httpGet(RobotWikiURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
bodyBytes, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
robotDict := make(map[string][]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(bodyBytes), -1) | ||
|
||
for _, match := range matches { | ||
if len(match) > 1 { | ||
name := strings.TrimSpace(match[1]) | ||
firstLetter := strings.ToUpper(string(name[0])) | ||
robotDict[firstLetter] = append(robotDict[firstLetter], name) | ||
} | ||
} | ||
|
||
return robotDict, nil | ||
} | ||
|
||
// Fetch past releases from GitHub | ||
func getPastReleases() (map[string]bool, error) { | ||
resp, err := httpGet(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 | ||
} | ||
|
||
func randomElement(array []string) (string, error) { | ||
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(array)))) | ||
if err != nil { | ||
return "", err | ||
} | ||
return array[n.Int64()], nil | ||
} | ||
|
||
// Generate a unique release name | ||
func generateUniqueTuple() (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 range maxAttempts { | ||
chosenLetter, err := randomElement(commonLetters) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
cat, err := randomElement(catBreeds[chosenLetter]) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
robot, err := randomElement(robotNames[chosenLetter]) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
newName := cat + " " + robot | ||
if !pastReleases[newName] { | ||
return cat, robot, nil | ||
} | ||
} | ||
|
||
return "", "", errors.New("could not generate a unique name after multiple attempts") | ||
} | ||
|
||
func printJsonError(err error) { | ||
fmt.Println(`{"error": "` + err.Error() + `"}`) //nolint:forbidigo | ||
} | ||
|
||
func main() { | ||
cat, robot, err := generateUniqueTuple() | ||
if err != nil { | ||
printJsonError(err) | ||
return | ||
} | ||
|
||
output := map[string]string{ | ||
"release_name": cat + " " + robot, | ||
"cat_breed_url": WikiURL + strings.ReplaceAll(cat, " ", "_"), | ||
"robot_url": WikiURL + strings.ReplaceAll(robot, " ", "_"), | ||
} | ||
|
||
jsonOutput, err := json.MarshalIndent(output, "", " ") | ||
if err != nil { | ||
printJsonError(err) | ||
return | ||
} | ||
fmt.Println(string(jsonOutput)) //nolint:forbidigo | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
won't we have to get the total number of releases and then paginate through the releases ? this doesn't look like it's scalable in case we have more than a 100 releases in the project.