Skip to content

Commit

Permalink
Merge pull request openshift#448 from AlexVulaj/jira-create-command
Browse files Browse the repository at this point in the history
add new 'jira quick-task' command
  • Loading branch information
openshift-ci[bot] authored Oct 4, 2023
2 parents 3ff769f + 166442e commit 5e7c50c
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/openshift/osdctl/cmd/jira"
"os"
"strings"

Expand Down Expand Up @@ -93,6 +94,7 @@ func NewCmdRoot(streams genericclioptions.IOStreams) *cobra.Command {
rootCmd.AddCommand(org.NewCmdOrg())
rootCmd.AddCommand(sts.NewCmdSts(streams, kubeFlags, kubeClient))
rootCmd.AddCommand(promote.NewCmdPromote(kubeFlags, globalOpts))
rootCmd.AddCommand(jira.Cmd)

// add completion command
rootCmd.AddCommand(newCmdCompletion(streams))
Expand Down
13 changes: 13 additions & 0 deletions cmd/jira/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package jira

import "github.com/spf13/cobra"

var Cmd = &cobra.Command{
Use: "jira",
Short: "Provides a set of commands for interacting with Jira",
Args: cobra.NoArgs,
}

func init() {
Cmd.AddCommand(quickTaskCmd)
}
126 changes: 126 additions & 0 deletions cmd/jira/quick-task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package jira

import (
"fmt"
"github.com/andygrunwald/go-jira"
"github.com/openshift/osdctl/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"strings"
)

const (
TeamNameKey = "jira_team"
TeamLabelKey = "jira_team_label"
BoardIdLabel = "jira_board_id"
DefaultDescription = ""
DefaultTicketType = "Task"
DefaultProject = "OSD"
SprintState = "active"
AddToSprintFlag = "add-to-sprint"
)

var quickTaskCmd = &cobra.Command{
Use: "quick-task <title>",
Short: "creates a new ticket with the given name",
Long: `Creates a new ticket with the given name and a label specified by "jira_team_label" from the osdctl config. The flags "jira_board_id" and "jira_team" are also required for running this command.
The ticket will be assigned to the caller and added to their team's current sprint as an OSD Task.
A link to the created ticket will be printed to the console.`,
Example: `#Create a new Jira issue
osdctl jira create "Update command to take new flag"
#Create a new Jira issue and add to the caller's current sprint
osdctl jira create "Update command to take new flag" --add-to-sprint
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
addToSprint, err := cmd.Flags().GetBool(AddToSprintFlag)
if err != nil {
return fmt.Errorf("error reading --%v flag: %w", AddToSprintFlag, err)
}

if !viper.IsSet(TeamNameKey) {
return fmt.Errorf("%v value missing from config", TeamNameKey)
}
teamName := viper.GetString(TeamNameKey)

if !viper.IsSet(TeamLabelKey) {
return fmt.Errorf("%v value missing from config", TeamLabelKey)
}
teamLabel := viper.GetString(TeamLabelKey)

if !viper.IsSet(BoardIdLabel) {
return fmt.Errorf("%v value missing from config", BoardIdLabel)
}
boardId := viper.GetInt(BoardIdLabel)

jiraClient, err := utils.GetJiraClient()
if err != nil {
return fmt.Errorf("failed to get Jira client: %w", err)
}

issue, err := CreateQuickTicket(jiraClient.User, jiraClient.Issue, args[0], teamLabel)
if err != nil {
return fmt.Errorf("error creating ticket: %w", err)
}
fmt.Printf("Successfully created ticket:\n%v/browse/%v\n", utils.JiraBaseURL, issue.Key)

if addToSprint {
err = addTicketToCurrentSprint(jiraClient.Board, jiraClient.Sprint, issue, boardId, teamName)
if err != nil {
return fmt.Errorf("failed to add ticket to current sprint: %w", err)
}
}

return nil
},
}

func init() {
quickTaskCmd.Flags().Bool("add-to-sprint", false, "whether or not to add the created Jira task to the SRE's current sprint.")
}

func CreateQuickTicket(userService *jira.UserService, issueService *jira.IssueService, summary string, teamLabel string) (*jira.Issue, error) {
user, _, err := userService.GetSelf()
if err != nil {
return nil, fmt.Errorf("failed to get jira user for self: %w", err)
}

issue, err := utils.CreateIssue(
issueService,
summary,
DefaultDescription,
DefaultTicketType,
DefaultProject,
user,
user,
[]string{teamLabel},
)
if err != nil {
return nil, fmt.Errorf("failed to create issue: %w", err)
}

return issue, nil
}

func addTicketToCurrentSprint(boardService *jira.BoardService, sprintService *jira.SprintService, issue *jira.Issue, boardId int, teamName string) error {
sprints, _, err := boardService.GetAllSprintsWithOptions(boardId, &jira.GetAllSprintsOptions{State: SprintState})
if err != nil {
return fmt.Errorf("failed to get active sprints for board %v: %w", boardId, err)
}

var activeSprint jira.Sprint
for _, sprint := range sprints.Values {
if strings.Contains(sprint.Name, teamName) {
activeSprint = sprint
break
}
}

_, err = sprintService.MoveIssuesToSprint(activeSprint.ID, []string{issue.ID})
if err != nil {
return fmt.Errorf("issue %v was not moved to active sprint: %w", issue.Key, err)
}

return nil
}
30 changes: 30 additions & 0 deletions pkg/utils/jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,33 @@ func GetJiraSupportExceptionsForOrg(organizationID string) ([]jira.Issue, error)

return issues, nil
}

func CreateIssue(
service *jira.IssueService,
summary string,
description string,
ticketType string,
project string,
reporter *jira.User,
assignee *jira.User,
labels []string,
) (*jira.Issue, error) {
issue := &jira.Issue{
Fields: &jira.IssueFields{
Reporter: reporter,
Assignee: assignee,
Type: jira.IssueType{Name: ticketType},
Project: jira.Project{Key: project},
Description: description,
Summary: summary,
Labels: labels,
},
}

createdIssue, _, err := service.Create(issue)
if err != nil {
return nil, fmt.Errorf("failed to create issue: %w", err)
}

return createdIssue, nil
}

0 comments on commit 5e7c50c

Please sign in to comment.