1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
18 "git.arvados.org/arvados-dev.git/lib/redmine"
19 survey "github.com/AlecAivazis/survey/v2"
20 "github.com/Masterminds/semver"
21 "github.com/go-git/go-git/v5"
22 "github.com/go-git/go-git/v5/plumbing"
23 "github.com/go-git/go-git/v5/plumbing/object"
24 "github.com/go-git/go-git/v5/plumbing/storer"
25 "github.com/go-git/go-git/v5/storage/memory"
26 "github.com/spf13/cobra"
30 rootCmd.AddCommand(redmineCmd)
31 redmineCmd.AddCommand(issuesCmd)
32 redmineCmd.AddCommand(releasesCmd)
34 associateIssueCmd.Flags().IntP("release", "r", 0, "Redmine release ID")
35 err := associateIssueCmd.MarkFlagRequired("release")
37 log.Fatalf(err.Error())
39 associateIssueCmd.Flags().IntP("issue", "i", 0, "Redmine issue ID")
40 err = associateIssueCmd.MarkFlagRequired("issue")
42 log.Fatalf(err.Error())
44 issuesCmd.AddCommand(associateIssueCmd)
46 associateOrphans.Flags().IntP("release", "r", 0, "Redmine release ID")
47 err = associateOrphans.MarkFlagRequired("release")
49 log.Fatalf(err.Error())
51 associateOrphans.Flags().StringP("project", "p", "", "Redmine project name")
52 err = associateOrphans.MarkFlagRequired("project")
54 log.Fatalf(err.Error())
56 associateOrphans.Flags().BoolP("dry-run", "", false, "Only report what will happen without making any change")
57 issuesCmd.AddCommand(associateOrphans)
59 findAndAssociateIssuesCmd.Flags().IntP("release", "r", 0, "Redmine release ID")
60 err = findAndAssociateIssuesCmd.MarkFlagRequired("release")
62 log.Fatalf(err.Error())
64 findAndAssociateIssuesCmd.Flags().StringP("previous-release-tag", "p", "", "Semantic version number of the previous release")
65 err = findAndAssociateIssuesCmd.MarkFlagRequired("previous-release-tag")
67 log.Fatalf(err.Error())
69 findAndAssociateIssuesCmd.Flags().StringP("new-release-commit", "n", "", "Git commit for the new release")
70 err = findAndAssociateIssuesCmd.MarkFlagRequired("new-release-commit")
72 log.Fatalf(err.Error())
74 findAndAssociateIssuesCmd.Flags().BoolP("auto-set", "a", false, "Associate issues without existing release without prompting")
75 findAndAssociateIssuesCmd.Flags().BoolP("skip-release-change", "s", false, "Skip issues already assigned to another release (do not prompt)")
76 findAndAssociateIssuesCmd.Flags().StringP("source-repo", "", "https://github.com/arvados/arvados.git", "Source repository to clone from")
78 log.Fatalf(err.Error())
81 issuesCmd.AddCommand(findAndAssociateIssuesCmd)
83 createReleaseIssueCmd.Flags().StringP("new-release-version", "n", "", "Semantic version number of the new release")
84 err = createReleaseIssueCmd.MarkFlagRequired("new-release-version")
86 log.Fatalf(err.Error())
88 createReleaseIssueCmd.Flags().IntP("sprint", "s", 0, "Redmine sprint (aka Version) ID")
89 err = createReleaseIssueCmd.MarkFlagRequired("sprint")
91 log.Fatalf(err.Error())
93 createReleaseIssueCmd.Flags().StringP("project", "p", "", "Redmine project name")
94 err = createReleaseIssueCmd.MarkFlagRequired("project")
96 log.Fatalf(err.Error())
98 issuesCmd.AddCommand(createReleaseIssueCmd)
100 getReleaseCmd.Flags().IntP("release", "r", 0, "ID of the redmine release")
101 err = getReleaseCmd.MarkFlagRequired("release")
103 log.Fatalf(err.Error())
105 releasesCmd.AddCommand(getReleaseCmd)
108 var redmineCmd = &cobra.Command{
110 Short: "Manage Redmine",
111 Long: "Manage Redmine.\n" +
112 "\nThe REDMINE_ENDPOINT environment variable must be set to the base URL of your redmine server." +
113 "\nThe REDMINE_APIKEY environment variable must be set to your redmine API key.",
114 PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
115 if conf.Endpoint == "" {
118 fmt.Println("Error: the REDMINE_ENDPOINT environment variable must be set to the base URL of your redmine server")
121 if conf.Apikey == "" {
124 fmt.Println("Error: the REDMINE_APIKEY environment variable must be set to your redmine API key")
131 var issuesCmd = &cobra.Command{
133 Short: "Manage Redmine issues",
134 Long: "Manage Redmine issues.\n" +
135 "\nThe REDMINE_ENDPOINT environment variable must be set to the base URL of your redmine server." +
136 "\nThe REDMINE_APIKEY environment variable must be set to your redmine API key.",
139 var associateOrphans = &cobra.Command{
140 Use: "associate-orphans", // FIXME
141 Short: "Find open issues without a release and version, assign them to the given release",
142 Long: "Find open issues without a release and version, assign them to the given release.\n" +
143 "\nThe REDMINE_ENDPOINT environment variable must be set to the base URL of your redmine server." +
144 "\nThe REDMINE_APIKEY environment variable must be set to your redmine API key.",
145 Run: func(cmd *cobra.Command, args []string) {
146 rID, err := cmd.Flags().GetInt("release")
148 fmt.Printf("Error converting Redmine release ID to integer: %s", err)
151 pName, err := cmd.Flags().GetString("project")
153 log.Fatalf("Error getting the requested project name: %s", err)
155 dryRun, err := cmd.Flags().GetBool("dry-run")
157 log.Fatalf("Error getting the dry-run parameter")
160 rm := redmine.NewClient(conf.Endpoint, conf.Apikey)
161 p, err := rm.GetProjectByName(pName)
163 log.Fatalf("Error retrieving project ID for '%s': %s", pName, err)
165 r, err := rm.GetRelease(rID)
167 log.Fatalf("Error retrieving release '%d': %s", rID, err)
169 flt := redmine.IssueFilter{
171 ProjectID: fmt.Sprintf("%d", p.ID),
172 // No values assigned on the following fields. It seems that using
173 // an empty string is interpreted as 'any value'. The documentation
174 // isn't clear, but after some trial & error, '!*' seems to do the trick.
175 // https://www.redmine.org/projects/redmine/wiki/Rest_Issues
180 issues, err := rm.FilteredIssues(&flt)
182 fmt.Printf("Error requesting unassigned open issues from project %d: %s", p.ID, err)
184 fmt.Printf("Found %d issues from project '%s' to assign to release '%s'...\n", len(issues), p.Name, r.Name)
185 for _, issue := range issues {
186 fmt.Printf("#%d - %s ", issue.ID, issue.Subject)
188 err = rm.SetRelease(issue, rID)
190 fmt.Printf("[error]\n")
191 log.Fatalf("Error trying to assign issue %d to release %d: %s", issue.ID, rID, err)
193 fmt.Printf("[changed]\n")
195 fmt.Printf("[skipped]\n")
201 var associateIssueCmd = &cobra.Command{
203 Short: "Associate an issue with a release",
204 Long: "Associate an issue with a release.\n" +
205 "\nThe REDMINE_ENDPOINT environment variable must be set to the base URL of your redmine server." +
206 "\nThe REDMINE_APIKEY environment variable must be set to your redmine API key.",
207 Run: func(cmd *cobra.Command, args []string) {
208 issueID, err := cmd.Flags().GetInt("issue")
210 fmt.Printf("Error converting Redmine issue ID to integer: %s", err)
214 releaseID, err := cmd.Flags().GetInt("release")
216 fmt.Printf("Error converting Redmine release ID to integer: %s", err)
220 redmine := redmine.NewClient(conf.Endpoint, conf.Apikey)
222 i, err := redmine.GetIssue(issueID)
224 fmt.Printf("%s\n", err.Error())
229 if i.Release == nil || i.Release["release"].ID == 0 {
231 } else if i.Release["release"].ID != releaseID {
235 err = redmine.SetRelease(*i, releaseID)
237 fmt.Printf("%s\n", err.Error())
240 fmt.Printf("[changed] release for issue %d set to %d\n", i.ID, releaseID)
243 fmt.Printf("[ok] release for issue %d was already set to %d, not updating\n", i.ID, i.Release["release"].ID)
248 func checkError(err error) {
250 fmt.Printf("%s\n", err.Error())
255 func checkError2(msg string, err error) {
257 fmt.Printf("%s: %s\n", msg, err.Error())
262 var findAndAssociateIssuesCmd = &cobra.Command{
263 Use: "find-and-associate",
264 Short: "Find all issue numbers to associate with a release, and associate them",
265 Long: "Find all issue numbers to associate with a release, and associate them.\n" +
266 "\nThe REDMINE_ENDPOINT environment variable must be set to the base URL of your redmine server." +
267 "\nThe REDMINE_APIKEY environment variable must be set to your redmine API key.",
268 Run: func(cmd *cobra.Command, args []string) {
269 previousReleaseTag, err := cmd.Flags().GetString("previous-release-tag")
271 log.Fatal(fmt.Errorf("Error retrieving previous release: %s", err))
275 newReleaseCommitHash, err := cmd.Flags().GetString("new-release-commit")
277 log.Fatal(fmt.Errorf("Error retrieving new release: %s", err))
280 releaseID, err := cmd.Flags().GetInt("release")
282 log.Fatal(fmt.Errorf("Error converting Redmine release ID to integer: %s", err))
286 autoSet, err := cmd.Flags().GetBool("auto-set")
288 log.Fatal(fmt.Errorf("Error getting auto-set value: %s", err))
291 skipReleaseChange, err := cmd.Flags().GetBool("skip-release-change")
293 log.Fatal(fmt.Errorf("Error getting skip-release-change value: %s", err))
296 arvRepo, err := cmd.Flags().GetString("source-repo")
298 log.Fatal(fmt.Errorf("Error getting source-repo value: %s", err))
302 if len(previousReleaseTag) < 5 || len(previousReleaseTag) > 8 {
303 log.Fatal(fmt.Errorf("The previous-release-tag argument is of an unexpected format. Expecting a semantic version (e.g. 2.3.0)"))
306 if len(newReleaseCommitHash) != 7 && len(newReleaseCommitHash) != 40 {
307 log.Fatal(fmt.Errorf("The new-release-commit argument is of an unexpected format. Expecting a git commit hash (7 or 40 digits long)"))
311 // Clone the repo in memory
313 // our own arvados repo won't clone,
314 //arvRepo := "https://git.arvados.org/arvados.git"
315 //arvRepo := "https://github.com/arvados/arvados.git"
317 fmt.Println("Cloning " + arvRepo)
318 repo, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
322 fmt.Println("... done")
324 start, err := repo.ResolveRevision(plumbing.Revision("refs/tags/" + previousReleaseTag))
325 checkError2("repo.ResolveRevision", err)
326 fmt.Printf("previous-release-tag: %s (%s)\n", previousReleaseTag, start)
327 fmt.Printf("new-release-commit: %s\n", newReleaseCommitHash)
330 // Build the exclusion list
331 seen := make(map[plumbing.Hash]bool)
332 excludeIter, err := repo.Log(&git.LogOptions{From: *start, Order: git.LogOrderCommitterTime})
333 checkError2("repo.Log", err)
334 excludeIter.ForEach(func(c *object.Commit) error {
339 // isValid returns merge commits that are not in the exclusion list
340 var isValid object.CommitFilter = func(commit *object.Commit) bool {
341 _, ok := seen[commit.Hash]
343 // use len(commit.ParentHashes) to only get merge commits
344 return !ok && len(commit.ParentHashes) >= 2
347 headCommit, err := repo.CommitObject(plumbing.NewHash(newReleaseCommitHash))
348 checkError2("repo.CommitObject", err)
350 iter := object.NewFilterCommitIter(headCommit, &isValid, nil)
352 issues := make(map[int]bool)
353 re := regexp.MustCompile(`Merge branch `)
354 reNotMain := regexp.MustCompile(`Merge branch .(main|master)`)
355 reIssueRef := regexp.MustCompile(`(Closes|closes|Refs|refs|Fixes|fixes) #(\d+)`)
356 err = iter.ForEach(func(c *object.Commit) error {
357 // We have a git commit hook that requires an issue reference on merge commits
358 if re.MatchString(c.Message) && !reNotMain.MatchString(c.Message) {
359 m := reIssueRef.FindStringSubmatch(c.Message)
361 i, err := strconv.Atoi(m[2])
369 if c.Hash == *start {
370 return storer.ErrStop
376 // Sort the issue map keys
377 keys := make([]int, 0, len(issues))
378 for k := range issues {
379 keys = append(keys, k)
383 r := redmine.NewClient(conf.Endpoint, conf.Apikey)
385 for c, k := range keys {
386 fmt.Printf("%d (%d/%d): ", k, c+1, len(keys))
387 // Look up the issue, see if it is already associated with the desired release
389 i, err := r.GetIssue(k)
392 fmt.Printf("[error] unable to retrieve issue: %s\n", err.Error())
393 fmt.Println("============================================")
396 fmt.Println(i.Subject)
398 if i.Release != nil && i.Release["release"].ID != 0 {
399 if i.Release["release"].ID == releaseID {
400 fmt.Printf("[ok] release is already set to %d, nothing to do\n", i.Release["release"].ID)
401 } else if !skipReleaseChange {
402 fmt.Printf("%s/issues/%d\n", conf.Endpoint, k)
404 prompt := &survey.Confirm{
405 Message: fmt.Sprintf("release is set to %d, do you want to change it to %d ?", i.Release["release"].ID, releaseID),
407 err = survey.AskOne(prompt, &confirm)
412 err = r.SetRelease(*i, releaseID)
416 fmt.Printf("[changed] release for issue %d set to %d\n", i.ID, releaseID)
420 fmt.Printf("[ok] release is set to %d, not changing it to %d\n", i.Release["release"].ID, releaseID)
423 fmt.Printf("%s/issues/%d\n", conf.Endpoint, k)
426 prompt := &survey.Confirm{
427 Message: fmt.Sprintf("Release is not set, do you want to set it to %d ?", releaseID),
429 err = survey.AskOne(prompt, &confirm)
434 if confirm || autoSet {
435 err = r.SetRelease(*i, releaseID)
439 fmt.Printf("[changed] release for issue %d set to %d\n", i.ID, releaseID)
443 fmt.Println("============================================")
448 var createReleaseIssueCmd = &cobra.Command{
449 Use: "create-release-issue",
450 Short: "Create a release ticket with numbered subtasks for all the steps on the release checklist",
451 Long: "Create a release ticket with numbered subtasks for all the steps on the release checklist.\n" +
452 "\nThe subtask subjects are read from a file named TASKS in the current directory.\n" +
453 "\nFinally, a new Redmine release will also be created for the next release.\n" +
454 "\nThe REDMINE_ENDPOINT environment variable must be set to the base URL of your redmine server." +
455 "\nThe REDMINE_APIKEY environment variable must be set to your redmine API key.",
456 Run: func(cmd *cobra.Command, args []string) {
457 newReleaseVersion, err := cmd.Flags().GetString("new-release-version")
459 log.Fatal(fmt.Errorf("[error] can not get new release version: %s", err))
463 versionID, err := cmd.Flags().GetInt("sprint")
465 log.Fatal(fmt.Errorf("[error] can not convert Redmine sprint (version) ID to integer: %s", err))
468 projectName, err := cmd.Flags().GetString("project")
470 log.Fatal(fmt.Errorf("[error] can not get Redmine project name: %s", err))
474 r := redmine.NewClient(conf.Endpoint, conf.Apikey)
476 // Does this project exist?
477 project, err := r.GetProjectByName(projectName)
479 log.Fatalf("[error] can not find project with name %s: %s", projectName, err)
482 // Is the sprint (aka "version" in redmine) in the correct state?
483 v, err := r.Version(versionID)
485 log.Fatal(fmt.Errorf("[error] can not find sprint with id %d: %s", versionID, err))
487 if v.Status != "open" {
488 log.Fatal(fmt.Errorf("[error] the sprint must be open; the status of the sprint with id %d is '%s'", v.ID, v.Status))
491 i, err := r.FindOrCreateIssue("Release Arvados "+newReleaseVersion, 0, v.ID, project.ID)
495 if i.Status.Name != "New" {
496 log.Fatal(fmt.Errorf("the release ticket status must be 'New'; the status of the release issue with id %d is '%s'", i.ID, v.Status))
499 fmt.Printf("[ok] the release ticket is '%s' with ID #%d (%s/issues/%d)\n", i.Subject, i.ID, conf.Endpoint, i.ID)
501 // Get the list of subtasks from the "TASKS" file
502 tasks, err := os.Open("TASKS")
504 log.Fatal(fmt.Errorf("[error] unable to open the \"TASKS\" file: %s", err.Error()))
508 scanner := bufio.NewScanner(tasks)
511 task := scanner.Text()
512 taskIssue, err := r.FindOrCreateIssue(fmt.Sprintf("%d. %s", count, task), i.ID, v.ID, project.ID)
513 fmt.Printf("[ok] #%d: %d. %s\n", taskIssue.ID, count, task)
516 log.Fatal(fmt.Errorf("Error reading from file: %s", err))
520 // Create the next release in Redmine
521 version, err := semver.NewVersion(newReleaseVersion)
523 log.Fatalf("Error parsing version: %s", err)
525 nextVersion := version.IncPatch()
527 var release *redmine.Release
529 release, err = r.FindReleaseByName(project.Name, "Arvados "+nextVersion.String())
531 log.Fatalf("Error finding release with name %s in project with name %s: %s", release.Name, project.Name, err)
534 // No release found, create it
535 release = &redmine.Release{}
536 release.Name = "Arvados " + nextVersion.String()
537 release.Sharing = "hierarchy"
538 release.ReleaseStartDate = time.Now().AddDate(0, 0, 7*1).Format("2006-01-02") // arbitrary choice, 1 week from today
539 release.ReleaseEndDate = time.Now().AddDate(0, 0, 7*5).Format("2006-01-02") // also arbitrary, 5 weeks from today
540 release.ProjectID = project.ID
541 release.Status = "open"
543 tmp, err := r.GetProject(release.ProjectID)
545 log.Fatalf("Unable to find project with ID %d: %s", release.ProjectID, err)
547 release.Project = &redmine.IDName{ID: release.ProjectID, Name: tmp.Name}
549 release, err = r.CreateRelease(*release)
551 log.Fatalf("Unable to create release: %s", err)
554 fmt.Printf("[ok] the redmine release object for the next release is '%s' (%s/rb/release/%d)\n", release.Name, conf.Endpoint, release.ID)
558 var releasesCmd = &cobra.Command{
560 Short: "Manage Redmine releases",
561 Long: "Manage Redmine releases.\n" +
562 "\nThe REDMINE_ENDPOINT environment variable must be set to the base URL of your redmine server." +
563 "\nThe REDMINE_APIKEY environment variable must be set to your redmine API key.",
566 var getReleaseCmd = &cobra.Command{
568 Short: "get a release",
569 Long: "Get a release.\n" +
570 "\nThe REDMINE_ENDPOINT environment variable must be set to the base URL of your redmine server." +
571 "\nThe REDMINE_APIKEY environment variable must be set to your redmine API key.",
572 Run: func(cmd *cobra.Command, args []string) {
573 releaseID, err := cmd.Flags().GetInt("release")
575 fmt.Printf("Error converting Redmine release ID to integer: %s", err)
579 r := redmine.NewClient(conf.Endpoint, conf.Apikey)
581 release, err := r.GetRelease(releaseID)
583 log.Fatalf("Error finding release with id %d: %s", releaseID, err)
585 releaseStr, err := json.MarshalIndent(release, "", " ")
587 log.Fatalf("Error decoding release with id %d: %s", releaseID, err)
589 fmt.Println(string(releaseStr))