Skip to content

Commit

Permalink
Merge pull request #6236 from ministryofjustice/upgrade-script-updates
Browse files Browse the repository at this point in the history
feat: 🎸 update k8s upgrade ticket script and add usage readme
  • Loading branch information
sj-williams authored Oct 8, 2024
2 parents 8ba961c + f0da54e commit af0f886
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
28 changes: 28 additions & 0 deletions cmd/create-upgrade-issues/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Create Kubernetes Upgrade Issue

This script will generate a set of GitHub issues based on the content of the [Cloud Platform Kubernetes Upgrade Template ticket](https://raw.githubusercontent.com/ministryofjustice/cloud-platform/refs/heads/main/.github/ISSUE_TEMPLATE/cloud-platform-k8s-upgrade-template.md)

It works by parsing the template ticket and generating an individual issue for specific element of EKS upgrade work, splitting the issues by "## Issue" headers in the template.

## Prerequisites

- Go installed on your machine
- GH token with required priveledges for creating issues in the Cloud Platform repo.

## Usage

- Create a [template ticket](https://github.com/ministryofjustice/cloud-platform/issues/new?template=cloud-platform-k8s-upgrade-template.md) for feeding into the script.

- Give the template ticket a title that refers to the target upgrade version. Once created take note of the template issue number.

- Set your GH Token in your environment with:

```sh
export GITHUB_TOKEN="your-token-here"
```

- Run the script, passing the required flags:

```sh
go run main.go -owner="ministryofjustice" -repo="cloud-platform" -issue=template issue number from above -upgrade-version="target-k8s-version"
```
25 changes: 17 additions & 8 deletions cmd/create-upgrade-issues/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type GitHubAccess struct {
}

// ParseIssue parses the given issue and returns a slice of issues
func ParseIssue(issue string) []string {
func ParseIssue(issue string, upgradeVersion string) []string {
var issues []string
sections := strings.Split(issue, "## Issue")

Expand All @@ -29,24 +29,27 @@ func ParseIssue(issue string) []string {
lines := strings.Split(strings.TrimSpace(section), "\n")
title := strings.TrimSpace(lines[0])
body := strings.TrimSpace(strings.Join(lines[1:], "\n"))
body = strings.ReplaceAll(body, "<upgrade-version>", upgradeVersion)
issues = append(issues, fmt.Sprintf("## %s\n%s", title, body))
}
}
return issues
}

// CreateIssue creates a GitHub issue
func CreateIssue(client *github.Client, ghAccess GitHubAccess, issue string, epicIssueNumber int) error {
func CreateIssue(client *github.Client, ghAccess GitHubAccess, issue string, templateIssueNumber int, upgradeVersion string) error {
issueParts := strings.SplitN(issue, "\n", 3)
title := strings.TrimSpace(strings.TrimPrefix(issueParts[1], "###"))
body := strings.TrimSpace(issueParts[2])

// Append the body with the epic issue number
body = fmt.Sprintf("%s\n\n Related to: #%d", body, epicIssueNumber)
// Append the body with the template issue number
body = fmt.Sprintf("%s\n\n Related to: #%d", body, templateIssueNumber)
label := "eks-" + upgradeVersion + "-upgrade"

issueRequest := &github.IssueRequest{
Title: &title,
Body: &body,
Title: &title,
Body: &body,
Labels: &[]string{label},
}
_, _, err := client.Issues.Create(context.Background(), ghAccess.RepoOwner, ghAccess.RepoName, issueRequest)
return err
Expand All @@ -59,9 +62,15 @@ func main() {
flag.StringVar(&ghAccess.RepoOwner, "owner", "ministryofjustice", "the repository to create issues")
flag.StringVar(&ghAccess.RepoName, "repo", "cloud-platform", "the repository to create issues")
flag.IntVar(&ghAccess.IssueNumber, "issue", 0, "the issue number to create issues")
upgradeVersion := flag.String("upgrade-version", "", "the target EKS upgrade version")

flag.Parse()

if *upgradeVersion == "" {
fmt.Println("Error: you must specify the k8s upgrade version")
os.Exit(1)
}

ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: ghAccess.AccessToken},
Expand All @@ -76,10 +85,10 @@ func main() {
os.Exit(1)
}

issues := ParseIssue(*issueContent.Body)
issues := ParseIssue(*issueContent.Body, *upgradeVersion)
for _, issue := range issues {
//fmt.Println("Creating issue:", issue)
err := CreateIssue(client, ghAccess, issue, ghAccess.IssueNumber)
err := CreateIssue(client, ghAccess, issue, ghAccess.IssueNumber, *upgradeVersion)
if err != nil {
fmt.Println("Error creating issue:", err)
os.Exit(1)
Expand Down

0 comments on commit af0f886

Please sign in to comment.