Skip to content
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

chore: Leverages next-terraform-provider-release version #2149

Merged
merged 4 commits into from
Apr 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 54 additions & 11 deletions tools/jira-release-version/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ import (
)

const (
envJira = "JIRA_API_TOKEN"
envVersion = "VERSION_NUMBER"
projectID = "CLOUDP"
jiraURL = "https://jira.mongodb.org"
versionPrefix = "terraform-provider-"
envJira = "JIRA_API_TOKEN"
envVersion = "VERSION_NUMBER"
projectKey = "CLOUDP"
jiraURL = "https://jira.mongodb.org"
versionPrefix = "terraform-provider-"
versionNameNext = "next-terraform-provider-release"
)

func main() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[q] Curious if we were able to test this in any way, or if we will wait for a proper release.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i tested with a fake version

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding an example of output in PR description

client := getJiraClient()
versionName := versionPrefix + getVersion()
versionID := getVersionID(client, versionName)
versionID := getOrCreateVersion(client, versionName)
moveDoneIssues(client, versionName)
setReleased(client, versionID)
url := fmt.Sprintf("%s/projects/%s/versions/%s", jiraURL, projectID, versionID)
url := fmt.Sprintf("%s/projects/%s/versions/%s", jiraURL, projectKey, versionID)
fmt.Printf("Version released, please check all tickets are marked as done: %s\n", url)
}

Expand All @@ -50,19 +52,60 @@ func getVersion() string {
return strings.TrimPrefix(version, "v")
}

func getVersionID(client *jira.Client, versionName string) string {
projects, _, err := client.Project.Get(context.Background(), projectID)
func getOrCreateVersion(client *jira.Client, versionName string) string {
var projectID int
ctx := context.Background()
projects, _, err := client.Project.Get(ctx, projectKey)
if err != nil {
log.Fatalf("Error getting project info: %v", err)
}
for i := range projects.Versions {
v := &projects.Versions[i]
if projectID == 0 {
projectID = v.ProjectID
}
if v.Name == versionName {
return v.ID
}
}
log.Fatalf("Version not found: %s", versionName)
return ""

version, _, err := client.Version.Create(ctx, &jira.Version{ProjectID: projectID, Name: versionName})
if err != nil {
log.Fatalf("Error creating version %s: %v", versionName, err)
}
fmt.Printf("Version not found so it has been created: %s, id: %s\n", versionName, version.ID)
return version.ID
}

func moveDoneIssues(client *jira.Client, versionName string) {
jql := fmt.Sprintf("project = %s AND status in (Resolved, Closed) AND fixVersion = %s", projectKey, versionNameNext)
options := &jira.SearchOptions{MaxResults: 1000, Fields: []string{"NoFieldsNeeded"}}
list, _, err := client.Issue.Search(context.Background(), jql, options)
if err != nil {
log.Fatalf("Error retrieving issues: %v", err)
}
keys := make([]string, len(list))
for i := range list {
key := list[i].Key
keys[i] = key
moveIssue(client, key, versionName)
}
if len(keys) > 0 {
fmt.Println("Done issues moved:", strings.Join(keys, ", "))
}
}

func moveIssue(client *jira.Client, issueKey, versionName string) {
issue := &jira.Issue{
Key: issueKey,
Fields: &jira.IssueFields{
FixVersions: []*jira.FixVersion{{Name: versionName}},
},
}
_, _, err := client.Issue.Update(context.Background(), issue, nil)
if err != nil {
log.Fatalf("Error moving issue %s: %v", issueKey, err)
}
}

func setReleased(client *jira.Client, versionID string) {
Expand Down