Skip to content

Commit d7b4554

Browse files
authored
feat: Allow to set reporter on issue create (#539)
Fixes #189 #521
1 parent 76ebaf2 commit d7b4554

File tree

4 files changed

+55
-37
lines changed

4 files changed

+55
-37
lines changed

internal/cmd/epic/create/create.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ func create(cmd *cobra.Command, _ []string) {
8989
cmdutil.ExitIfError(err)
9090
}
9191

92-
params.Assignee = cmdcommon.GetAssignee(client, project, params.Assignee)
92+
params.Reporter = cmdcommon.GetRelevantUser(client, project, params.Reporter)
93+
params.Assignee = cmdcommon.GetRelevantUser(client, project, params.Assignee)
9394

9495
key, err := func() (string, error) {
9596
s := cmdutil.Info("Creating an epic...")
@@ -100,6 +101,7 @@ func create(cmd *cobra.Command, _ []string) {
100101
IssueType: jira.IssueTypeEpic,
101102
Summary: params.Summary,
102103
Body: params.Body,
104+
Reporter: params.Reporter,
103105
Assignee: params.Assignee,
104106
Priority: params.Priority,
105107
Labels: params.Labels,
@@ -211,6 +213,9 @@ func parseFlags(flags query.FlagParser) *cmdcommon.CreateParams {
211213
priority, err := flags.GetString("priority")
212214
cmdutil.ExitIfError(err)
213215

216+
reporter, err := flags.GetString("reporter")
217+
cmdutil.ExitIfError(err)
218+
214219
assignee, err := flags.GetString("assignee")
215220
cmdutil.ExitIfError(err)
216221

@@ -240,6 +245,7 @@ func parseFlags(flags query.FlagParser) *cmdcommon.CreateParams {
240245
Summary: summary,
241246
Body: body,
242247
Priority: priority,
248+
Reporter: reporter,
243249
Assignee: assignee,
244250
Labels: labels,
245251
Components: components,

internal/cmd/issue/create/create.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ func create(cmd *cobra.Command, _ []string) {
9090
cmdutil.ExitIfError(err)
9191
}
9292

93-
params.Assignee = cmdcommon.GetAssignee(client, project, params.Assignee)
93+
params.Reporter = cmdcommon.GetRelevantUser(client, project, params.Reporter)
94+
params.Assignee = cmdcommon.GetRelevantUser(client, project, params.Assignee)
9495

9596
key, err := func() (string, error) {
9697
s := cmdutil.Info("Creating an issue...")
@@ -102,6 +103,7 @@ func create(cmd *cobra.Command, _ []string) {
102103
ParentIssueKey: params.ParentIssueKey,
103104
Summary: params.Summary,
104105
Body: params.Body,
106+
Reporter: params.Reporter,
105107
Assignee: params.Assignee,
106108
Priority: params.Priority,
107109
Labels: params.Labels,
@@ -325,6 +327,9 @@ func parseFlags(flags query.FlagParser) *cmdcommon.CreateParams {
325327
priority, err := flags.GetString("priority")
326328
cmdutil.ExitIfError(err)
327329

330+
reporter, err := flags.GetString("reporter")
331+
cmdutil.ExitIfError(err)
332+
328333
assignee, err := flags.GetString("assignee")
329334
cmdutil.ExitIfError(err)
330335

@@ -357,6 +362,7 @@ func parseFlags(flags query.FlagParser) *cmdcommon.CreateParams {
357362
Priority: priority,
358363
Assignee: assignee,
359364
Labels: labels,
365+
Reporter: reporter,
360366
Components: components,
361367
FixVersions: fixVersions,
362368
CustomFields: custom,

internal/cmdcommon/create.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type CreateParams struct {
2929
Summary string
3030
Body string
3131
Priority string
32+
Reporter string
3233
Assignee string
3334
Labels []string
3435
Components []string
@@ -55,7 +56,8 @@ And, this field is mandatory when creating a sub-task.`)
5556
cmd.Flags().StringP("summary", "s", "", prefix+" summary or title")
5657
cmd.Flags().StringP("body", "b", "", prefix+" description")
5758
cmd.Flags().StringP("priority", "y", "", prefix+" priority")
58-
cmd.Flags().StringP("assignee", "a", "", prefix+" assignee (email or display name)")
59+
cmd.Flags().StringP("reporter", "r", "", prefix+" reporter (username, email or display name)")
60+
cmd.Flags().StringP("assignee", "a", "", prefix+" assignee (username, email or display name)")
5961
cmd.Flags().StringArrayP("label", "l", []string{}, prefix+" labels")
6062
cmd.Flags().StringArrayP("component", "C", []string{}, prefix+" components")
6163
cmd.Flags().StringArray("fix-version", []string{}, "Release info (fixVersions)")
@@ -185,20 +187,28 @@ func HandleNoInput(params *CreateParams) error {
185187
return nil
186188
}
187189

188-
// GetAssignee finds and returns assignee based on user input.
189-
func GetAssignee(client *jira.Client, project string, assignee string) string {
190-
if assignee == "" {
190+
// GetRelevantUser finds and returns a valid user name or account ID based on user input.
191+
func GetRelevantUser(client *jira.Client, project string, user string) string {
192+
if user == "" {
191193
return ""
192194
}
193-
194-
user, err := api.ProxyUserSearch(client, &jira.UserSearchOptions{
195-
Query: assignee,
195+
u, err := api.ProxyUserSearch(client, &jira.UserSearchOptions{
196+
Query: user,
196197
Project: project,
197198
})
198-
if err != nil || len(user) == 0 {
199-
cmdutil.Failed("Unable to find assignee")
199+
if err != nil || len(u) == 0 {
200+
cmdutil.Failed("Unable to find associated user for %s", user)
201+
}
202+
return GetUserKeyForConfiguredInstallation(u[0])
203+
}
204+
205+
// GetUserKeyForConfiguredInstallation returns either the user name or account ID based on jira installation type.
206+
func GetUserKeyForConfiguredInstallation(user *jira.User) string {
207+
it := viper.GetString("installation")
208+
if it == jira.InstallationTypeLocal {
209+
return user.Name
200210
}
201-
return GetUserKeyForConfiguredInstallation(user[0])
211+
return user.AccountID
202212
}
203213

204214
// GetConfiguredCustomFields returns the custom fields configured by the user.
@@ -242,12 +252,3 @@ Invalid custom fields used in the command: %s`,
242252
)
243253
}
244254
}
245-
246-
// GetUserKeyForConfiguredInstallation returns either the user name or account ID based on jira installation type.
247-
func GetUserKeyForConfiguredInstallation(user *jira.User) string {
248-
it := viper.GetString("installation")
249-
if it == jira.InstallationTypeLocal {
250-
return user.Name
251-
}
252-
return user.AccountID
253-
}

pkg/jira/create.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type CreateRequest struct {
2727
ParentIssueKey string
2828
Summary string
2929
Body interface{} // string in v1/v2 and adf.ADF in v3
30+
Reporter string
3031
Assignee string
3132
Priority string
3233
Labels []string
@@ -158,17 +159,18 @@ func (*Client) getRequestData(req *CreateRequest) *createRequest {
158159
data.Fields.M.Name = req.ParentIssueKey
159160
}
160161
}
162+
if req.Reporter != "" {
163+
if req.installationType == InstallationTypeLocal {
164+
data.Fields.M.Reporter = &nameOrAccountID{Name: &req.Reporter}
165+
} else {
166+
data.Fields.M.Reporter = &nameOrAccountID{AccountID: &req.Reporter}
167+
}
168+
}
161169
if req.Assignee != "" {
162170
if req.installationType == InstallationTypeLocal {
163-
data.Fields.M.Assignee = &struct {
164-
Name *string `json:"name,omitempty"`
165-
AccountID *string `json:"accountId,omitempty"`
166-
}{Name: &req.Assignee}
171+
data.Fields.M.Assignee = &nameOrAccountID{Name: &req.Assignee}
167172
} else {
168-
data.Fields.M.Assignee = &struct {
169-
Name *string `json:"name,omitempty"`
170-
AccountID *string `json:"accountId,omitempty"`
171-
}{AccountID: &req.Assignee}
173+
data.Fields.M.Assignee = &nameOrAccountID{AccountID: &req.Assignee}
172174
}
173175
}
174176
if req.Priority != "" {
@@ -255,6 +257,11 @@ type createRequest struct {
255257
Fields createFieldsMarshaler `json:"fields"`
256258
}
257259

260+
type nameOrAccountID struct {
261+
Name *string `json:"name,omitempty"` // For local installation.
262+
AccountID *string `json:"accountId,omitempty"` // For cloud installation.
263+
}
264+
258265
type createFields struct {
259266
Project struct {
260267
Key string `json:"key"`
@@ -265,14 +272,12 @@ type createFields struct {
265272
Parent *struct {
266273
Key string `json:"key"`
267274
} `json:"parent,omitempty"`
268-
Name string `json:"name,omitempty"`
269-
Summary string `json:"summary"`
270-
Description interface{} `json:"description,omitempty"`
271-
Assignee *struct {
272-
Name *string `json:"name,omitempty"` // For local installation.
273-
AccountID *string `json:"accountId,omitempty"` // For cloud installation.
274-
} `json:"assignee,omitempty"`
275-
Priority *struct {
275+
Name string `json:"name,omitempty"`
276+
Summary string `json:"summary"`
277+
Description interface{} `json:"description,omitempty"`
278+
Reporter *nameOrAccountID `json:"reporter,omitempty"`
279+
Assignee *nameOrAccountID `json:"assignee,omitempty"`
280+
Priority *struct {
276281
Name string `json:"name,omitempty"`
277282
} `json:"priority,omitempty"`
278283
Labels []string `json:"labels,omitempty"`

0 commit comments

Comments
 (0)