Skip to content

Commit

Permalink
ehc: Make assignee operation atomic on create
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitpokhrel committed Dec 21, 2022
1 parent 3964468 commit 50bcf26
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 29 deletions.
31 changes: 17 additions & 14 deletions internal/cmd/epic/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func create(cmd *cobra.Command, _ []string) {
server := viper.GetString("server")
project := viper.GetString("project.key")
projectType := viper.GetString("project.type")
installation := viper.GetString("installation")

params := parseFlags(cmd.Flags())
client := api.Client(jira.Config{Debug: params.debug})
Expand Down Expand Up @@ -130,6 +131,19 @@ func create(cmd *cobra.Command, _ []string) {
}
}

var assignee string

if params.assignee != "" {
user, err := api.ProxyUserSearch(client, &jira.UserSearchOptions{
Query: params.assignee,
Project: project,
})
if err != nil || len(user) == 0 {
cmdutil.Failed("Unable to find assignee")
}
assignee = cmdcommon.GetUserKeyForConfiguredInstallation(user[0])
}

key, err := func() (string, error) {
s := cmdutil.Info("Creating an epic...")
defer s.Stop()
Expand All @@ -139,6 +153,7 @@ func create(cmd *cobra.Command, _ []string) {
IssueType: jira.IssueTypeEpic,
Summary: params.summary,
Body: params.body,
Assignee: assignee,
Priority: params.priority,
Labels: params.labels,
Components: params.components,
Expand All @@ -150,6 +165,7 @@ func create(cmd *cobra.Command, _ []string) {
cr.Name = params.name
}
cr.ForProjectType(projectType)
cr.ForInstallationType(installation)
if configuredCustomFields, err := cmdcommon.GetConfiguredCustomFields(); err == nil {
cmdcommon.ValidateCustomFields(cr.CustomFields, configuredCustomFields)
cr.WithCustomFields(configuredCustomFields)
Expand All @@ -161,23 +177,10 @@ func create(cmd *cobra.Command, _ []string) {
}
return resp.Key, nil
}()
cmdutil.ExitIfError(err)

cmdutil.ExitIfError(err)
cmdutil.Success("Epic created\n%s/browse/%s", server, key)

if params.assignee != "" {
user, err := api.ProxyUserSearch(client, &jira.UserSearchOptions{
Query: params.assignee,
Project: project,
})
if err != nil || len(user) == 0 {
cmdutil.Failed("Unable to find assignee")
}
if err = api.ProxyAssignIssue(client, key, user[0], jira.AssigneeDefault); err != nil {
cmdutil.Failed("Unable to set assignee: %s", err.Error())
}
}

if web, _ := cmd.Flags().GetBool("web"); web {
err := cmdutil.Navigate(server, key)
cmdutil.ExitIfError(err)
Expand Down
31 changes: 17 additions & 14 deletions internal/cmd/issue/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func create(cmd *cobra.Command, _ []string) {
server := viper.GetString("server")
project := viper.GetString("project.key")
projectType := viper.GetString("project.type")
installation := viper.GetString("installation")

params := parseFlags(cmd.Flags())
client := api.Client(jira.Config{Debug: params.debug})
Expand Down Expand Up @@ -127,6 +128,19 @@ func create(cmd *cobra.Command, _ []string) {
}
}

var assignee string

if params.assignee != "" {
user, err := api.ProxyUserSearch(client, &jira.UserSearchOptions{
Query: params.assignee,
Project: project,
})
if err != nil || len(user) == 0 {
cmdutil.Failed("Unable to find assignee")
}
assignee = cmdcommon.GetUserKeyForConfiguredInstallation(user[0])
}

key, err := func() (string, error) {
s := cmdutil.Info("Creating an issue...")
defer s.Stop()
Expand All @@ -137,6 +151,7 @@ func create(cmd *cobra.Command, _ []string) {
ParentIssueKey: params.parentIssueKey,
Summary: params.summary,
Body: params.body,
Assignee: assignee,
Priority: params.priority,
Labels: params.labels,
Components: params.components,
Expand All @@ -145,6 +160,7 @@ func create(cmd *cobra.Command, _ []string) {
EpicField: viper.GetString("epic.link"),
}
cr.ForProjectType(projectType)
cr.ForInstallationType(installation)
if configuredCustomFields, err := cmdcommon.GetConfiguredCustomFields(); err == nil {
cmdcommon.ValidateCustomFields(cr.CustomFields, configuredCustomFields)
cr.WithCustomFields(configuredCustomFields)
Expand All @@ -160,23 +176,10 @@ func create(cmd *cobra.Command, _ []string) {
}
return resp.Key, nil
}()
cmdutil.ExitIfError(err)

cmdutil.ExitIfError(err)
cmdutil.Success("Issue created\n%s/browse/%s", server, key)

if params.assignee != "" {
user, err := api.ProxyUserSearch(client, &jira.UserSearchOptions{
Query: params.assignee,
Project: project,
})
if err != nil || len(user) == 0 {
cmdutil.Failed("Unable to find assignee")
}
if err = api.ProxyAssignIssue(client, key, user[0], jira.AssigneeDefault); err != nil {
cmdutil.Failed("Unable to set assignee: %s", err.Error())
}
}

if web, _ := cmd.Flags().GetBool("web"); web {
err := cmdutil.Navigate(server, key)
cmdutil.ExitIfError(err)
Expand Down
9 changes: 9 additions & 0 deletions internal/cmdcommon/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,12 @@ Invalid custom fields used in the command: %s`,
)
}
}

// GetUserKeyForConfiguredInstallation returns either the user name or account ID based on jira installation type.
func GetUserKeyForConfiguredInstallation(user *jira.User) string {
it := viper.GetString("installation")
if it == jira.InstallationTypeLocal {
return user.Name
}
return user.AccountID
}
26 changes: 25 additions & 1 deletion pkg/jira/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type CreateRequest struct {
ParentIssueKey string
Summary string
Body interface{} // string in v1/v2 and adf.ADF in v3
Assignee string
Priority string
Labels []string
Components []string
Expand All @@ -43,6 +44,7 @@ type CreateRequest struct {
CustomFields map[string]string

projectType string
installationType string
configuredCustomFields []IssueTypeField
}

Expand All @@ -51,6 +53,11 @@ func (cr *CreateRequest) ForProjectType(pt string) {
cr.projectType = pt
}

// ForInstallationType sets jira project type.
func (cr *CreateRequest) ForInstallationType(it string) {
cr.installationType = it
}

// WithCustomFields sets valid custom fields for the issue.
func (cr *CreateRequest) WithCustomFields(cf []IssueTypeField) {
cr.configuredCustomFields = cf
Expand Down Expand Up @@ -151,6 +158,19 @@ func (*Client) getRequestData(req *CreateRequest) *createRequest {
data.Fields.M.Name = req.ParentIssueKey
}
}
if req.Assignee != "" {
if req.installationType == InstallationTypeLocal {
data.Fields.M.Assignee = &struct {
Name *string `json:"name,omitempty"`
AccountID *string `json:"accountId,omitempty"`
}{Name: &req.Assignee}
} else {
data.Fields.M.Assignee = &struct {
Name *string `json:"name,omitempty"`
AccountID *string `json:"accountId,omitempty"`
}{AccountID: &req.Assignee}
}
}
if req.Priority != "" {
data.Fields.M.Priority = &struct {
Name string `json:"name,omitempty"`
Expand Down Expand Up @@ -248,7 +268,11 @@ type createFields struct {
Name string `json:"name,omitempty"`
Summary string `json:"summary"`
Description interface{} `json:"description,omitempty"`
Priority *struct {
Assignee *struct {
Name *string `json:"name,omitempty"` // For local installation.
AccountID *string `json:"accountId,omitempty"` // For cloud installation.
} `json:"assignee,omitempty"`
Priority *struct {
Name string `json:"name,omitempty"`
} `json:"priority,omitempty"`
Labels []string `json:"labels,omitempty"`
Expand Down

0 comments on commit 50bcf26

Please sign in to comment.