Add set-sprint subcommand to 'art', no issue #
authorPeter Amstutz <peter.amstutz@curii.com>
Fri, 5 Apr 2024 20:20:37 +0000 (16:20 -0400)
committerPeter Amstutz <peter.amstutz@curii.com>
Fri, 5 Apr 2024 20:21:26 +0000 (16:21 -0400)
Arvados-DCO-1.1-Signed-off-by: Peter Amstutz <peter.amstutz@curii.com>

cmd/art/redmine.go
lib/redmine/issues.go

index 1ad293b52b70a87044d2e06a554c9494204d9bad..a170a6ecf609f0e5fdc798171e64c3c7d73880a1 100644 (file)
@@ -44,6 +44,19 @@ func init() {
        }
        issuesCmd.AddCommand(associateIssueCmd)
 
+
+       setIssueSprintCmd.Flags().IntP("sprint", "r", 0, "Redmine sprint ID")
+       err = setIssueSprintCmd.MarkFlagRequired("sprint")
+       if err != nil {
+               log.Fatalf(err.Error())
+       }
+       setIssueSprintCmd.Flags().IntP("issue", "i", 0, "Redmine issue ID")
+       err = setIssueSprintCmd.MarkFlagRequired("issue")
+       if err != nil {
+               log.Fatalf(err.Error())
+       }
+       issuesCmd.AddCommand(setIssueSprintCmd)
+
        associateOrphans.Flags().IntP("release", "r", 0, "Redmine release ID")
        err = associateOrphans.MarkFlagRequired("release")
        if err != nil {
@@ -312,6 +325,54 @@ var associateIssueCmd = &cobra.Command{
        },
 }
 
+
+var setIssueSprintCmd = &cobra.Command{
+       Use:   "set-sprint",
+       Short: "Set sprint for issue",
+       Long: "Set the sprint for an issue.\n" +
+               "\nThe REDMINE_ENDPOINT environment variable must be set to the base URL of your redmine server." +
+               "\nThe REDMINE_APIKEY environment variable must be set to your redmine API key.",
+       Run: func(cmd *cobra.Command, args []string) {
+               issueID, err := cmd.Flags().GetInt("issue")
+               if err != nil {
+                       fmt.Printf("Error converting Redmine issue ID to integer: %s", err)
+                       os.Exit(1)
+               }
+
+               sprintID, err := cmd.Flags().GetInt("sprint")
+               if err != nil {
+                       fmt.Printf("Error converting Redmine sprint ID to integer: %s", err)
+                       os.Exit(1)
+               }
+
+               redmine := redmine.NewClient(conf.Endpoint, conf.Apikey)
+
+               i, err := redmine.GetIssue(issueID)
+               if err != nil {
+                       fmt.Printf("%s\n", err.Error())
+                       os.Exit(1)
+               }
+
+               var setIt bool
+               if i.FixedVersion == nil {
+                       setIt = true
+               } else if i.FixedVersion.ID != sprintID {
+                       setIt = true
+               }
+               if setIt {
+                       err = redmine.SetSprint(*i, sprintID)
+                       if err != nil {
+                               fmt.Printf("%s\n", err.Error())
+                               os.Exit(1)
+                       } else {
+                               fmt.Printf("[changed] sprint for issue %d set to %d\n", i.ID, sprintID)
+                       }
+               } else {
+                       fmt.Printf("[ok] sprint for issue %d was already set to %d, not updating\n", i.ID, i.FixedVersion.ID)
+               }
+       },
+}
+
 func checkError(err error) {
        if err != nil {
                fmt.Printf("%s\n", err.Error())
index ea2f584cef2b0316832e0878963bab6a045cc71a..5e022721aeb47dc2d2d3882ccf4858207ce6fa6e 100644 (file)
@@ -233,3 +233,17 @@ func (c *Client) SetRelease(issue Issue, release int) error {
        issue.Release = nil
        return c.UpdateIssue(issue)
 }
+
+// SetSprint updates the sprint (fixed_version) for an issue
+func (c *Client) SetSprint(issue Issue, version int) error {
+       issue.FixedVersionID = version
+       issue.FixedVersion = nil
+       return c.UpdateIssue(issue)
+}
+
+// SetStatus updates the status for an issue
+func (c *Client) SetStatus(issue Issue, status int) error {
+       issue.StatusID = status
+       issue.Status = nil
+       return c.UpdateIssue(issue)
+}