Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
feat: add real move issue to epic
Browse files Browse the repository at this point in the history
  • Loading branch information
YCK1130 committed Aug 11, 2024
1 parent 89c6f6c commit d43ce7f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
19 changes: 15 additions & 4 deletions application/jira/v0/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
_ "embed"
"fmt"
"reflect"
"strings"

"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -414,7 +415,8 @@ type UpdateIssueInput struct {
}
type UpdateIssueRequset struct {
Body struct {
Update map[string][]AdditionalFields `json:"update"`
Update map[string][]AdditionalFields `json:"update,omitempty"`
Fields map[string]interface{} `json:"fields,omitempty"`
}
Query struct {
NotifyUsers bool `json:"notify-users" api:"notifyUsers"`
Expand Down Expand Up @@ -457,7 +459,6 @@ func (jiraClient *Client) updateIssueTask(ctx context.Context, props *structpb.S
return nil, err
}
input.Update.UpdateType = "Custom Update"
// TODO: both api seems not working
input.Update.UpdateFields = append(input.Update.UpdateFields, UpdateField{
Action: "set",
FieldName: "parent",
Expand All @@ -468,6 +469,7 @@ func (jiraClient *Client) updateIssueTask(ctx context.Context, props *structpb.S
debug.Info("Updating issue with parent key")
debug.Info("Input: ", input.Update.UpdateFields)
if _, err = jiraClient.updateIssue(ctx, &input); err != nil {
debug.Error("updateIssue err: ", err)
return nil, errmsg.AddMessage(
fmt.Errorf("failed to update issue with parent key"),
"You can only move issues to epics.",
Expand Down Expand Up @@ -531,6 +533,7 @@ func (jiraClient *Client) updateIssue(_ context.Context, input *UpdateIssueInput
)
}
updateInfo := make(map[string][]AdditionalFields)
fieldsInfo := make(map[string]interface{})
for _, field := range input.Update.UpdateFields {
if field.FieldName == "" {
return nil, errmsg.AddMessage(
Expand All @@ -543,7 +546,12 @@ func (jiraClient *Client) updateIssue(_ context.Context, input *UpdateIssueInput
}
switch field.Action {
case "set":
updateInfo[field.FieldName] = append(updateInfo[field.FieldName], AdditionalFields{Set: field.Value})
if v := reflect.ValueOf(field.Value); v.Kind() != reflect.Slice || v.Len() <= 1 {
fieldsInfo[field.FieldName] = field.Value
delete(updateInfo, field.FieldName)
} else {
updateInfo[field.FieldName] = append(updateInfo[field.FieldName], AdditionalFields{Set: field.Value})
}
case "add":
updateInfo[field.FieldName] = append(updateInfo[field.FieldName], AdditionalFields{Add: field.Value})
case "remove":
Expand All @@ -560,11 +568,14 @@ func (jiraClient *Client) updateIssue(_ context.Context, input *UpdateIssueInput
}
}
apiEndpoint := "rest/api/2/issue/" + input.IssueKey
debug.Info("apiEndpoint: ", apiEndpoint)
request := UpdateIssueRequset{
Body: struct {
Update map[string][]AdditionalFields `json:"update"`
Update map[string][]AdditionalFields `json:"update,omitempty"`
Fields map[string]interface{} `json:"fields,omitempty"`
}{
Update: updateInfo,
Fields: fieldsInfo,
},
Query: struct {
NotifyUsers bool `json:"notify-users" api:"notifyUsers"`
Expand Down
41 changes: 30 additions & 11 deletions application/jira/v0/mock_update_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type MockUpdateIssueRequset struct {
IssueKey string `json:"issue-key"`
Update map[string][]AdditionalFields `json:"update"`
Fields map[string]interface{} `json:"fields"`
NotifyUsers bool `json:"notify-users" api:"notifyUsers"`
ReturnIssue bool `json:"return-issue" api:"returnIssue"`
}
Expand All @@ -23,7 +24,7 @@ type MockUpdateIssueResp struct {
// UpdateIssue updates an issue in Jira.
func mockUpdateIssue(res http.ResponseWriter, req *http.Request) {
var debug logger.Session
defer debug.SessionStart("UpdateIssue", logger.Develop).SessionEnd()
defer debug.SessionStart("mock UpdateIssue", logger.Develop).SessionEnd()

var request MockUpdateIssueRequset
err := json.NewDecoder(req.Body).Decode(&request)
Expand Down Expand Up @@ -53,22 +54,40 @@ func mockUpdateIssue(res http.ResponseWriter, req *http.Request) {
returnIssue := opt.Get("returnIssue")
for key, fields := range request.Update {
for _, field := range fields {
debug.Info("field", field)
if field.Set != "" {
issue.Fields[key] = field.Set
}
}
}
for key, field := range request.Fields {
if field != "" {
issue.Fields[key] = field
}
}
newIssue := Issue{
ID: issue.ID,
Key: issue.Key,
Self: issue.Self,
Fields: issue.Fields,
Description: issue.Fields["description"].(string),
IssueType: issue.Fields["issuetype"].(map[string]interface{})["name"].(string),
Summary: issue.Fields["summary"].(string),
Status: issue.Fields["status"].(map[string]interface{})["name"].(string),
}
for issue := range fakeIssues {
if fakeIssues[issue].ID == newIssue.ID {
fakeIssues[issue] = FakeIssue{
ID: newIssue.ID,
Key: newIssue.Key,
Self: newIssue.Self,
Fields: newIssue.Fields,
}
break
}
}
resp := MockUpdateIssueResp{
Issue: Issue{
ID: issue.ID,
Key: issue.Key,
Self: issue.Self,
Fields: issue.Fields,
Description: issue.Fields["description"].(string),
IssueType: issue.Fields["issuetype"].(map[string]interface{})["name"].(string),
Summary: issue.Fields["summary"].(string),
Status: issue.Fields["status"].(map[string]interface{})["name"].(string),
},
Issue: newIssue,
NotifyUsers: notifyUsers == "true",
ReturnIssue: returnIssue == "true",
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ require (
gitlab.com/golang-commonmark/mdurl v0.0.0-20191124015652-932350d1cb84 // indirect
gitlab.com/golang-commonmark/puny v0.0.0-20191124015043-9f83538fa04f // indirect
gitlab.com/nyarla/go-crypt v0.0.0-20160106005555-d9a5dc2b789b // indirect
go.mongodb.org/mongo-driver v1.16.0
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
Expand Down

0 comments on commit d43ce7f

Please sign in to comment.