From b310ee6843e2f56c81a9a6dc05d0f83a1cde8e3c Mon Sep 17 00:00:00 2001 From: Seila Gamo Date: Thu, 3 Nov 2022 13:10:10 +0100 Subject: [PATCH 1/2] feat: guessing mandatory fields --- pkg/tracking/jira/client.go | 73 ++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/pkg/tracking/jira/client.go b/pkg/tracking/jira/client.go index dab0cfb..f549b48 100644 --- a/pkg/tracking/jira/client.go +++ b/pkg/tracking/jira/client.go @@ -1,8 +1,11 @@ package jira import ( + "fmt" + "github.com/adevinta/vulcan-tracker/pkg/model" gojira "github.com/andygrunwald/go-jira" + "github.com/trivago/tgo/tcontainer" ) type Client struct { @@ -58,18 +61,84 @@ func (cl *Client) GetTicket(id string) (*model.Ticket, error) { } +func contains(elems []string, v string) bool { + for _, s := range elems { + if v == s { + return true + } + } + return false +} + // CreateTicket creates a ticket in Jira. -func (cl *Client) CreateTicket(ticket *model.Ticket, issueType string) (*model.Ticket, error) { +func (cl *Client) CreateTicket(ticket *model.Ticket) (*model.Ticket, error) { + + queryOptions := &gojira.GetQueryOptions{ + ProjectKeys: ticket.Project, + Expand: "projects.issuetypes.fields", + } + + // Retrieve the metadata needed to create a ticket. + createMetaInfo, _, err := cl.c.Issue.GetCreateMetaWithOptions(queryOptions) + if err != nil { + return nil, err + } + + metaProject := createMetaInfo.GetProjectWithKey(ticket.Project) + metaIssueType := metaProject.GetIssueTypeWithName(ticket.TicketType) + mandatoryFields, err := metaIssueType.GetMandatoryFields() + if err != nil { + return nil, err + } + + customfields := tcontainer.NewMarshalMap() + + for _, field := range mandatoryFields { + // We already have the values for this fields. + if contains([]string{"summary", "issuetype", "components", "project", "reporter"}, field) { + continue + } + + required, err := metaIssueType.Fields.Bool(field + "/required") + if err != nil { + return nil, err + } + hasDefaultValue, err := metaIssueType.Fields.Bool(field + "/hasDefaultValue") + if err != nil { + return nil, err + } + + //We only have to manage the required fields without default value. + if required && !hasDefaultValue { + + fieldType, exists := metaIssueType.Fields.Value(field + "/schema/type") + if !exists { + return nil, fmt.Errorf("error retrieving the type of the custom field %s", field) + } + switch fieldType { + case "option": + var firstOption interface{} + firstOption, exists = metaIssueType.Fields.Value(field + "/allowedValues[0]value") + if !exists { + return nil, fmt.Errorf("error retrieving the first option of the custom field %s", field) + } + customfields[field] = map[string]string{"value": firstOption.(string)} + } + } + + } + newTicket := &gojira.Issue{ Fields: &gojira.IssueFields{ Description: ticket.Description, Summary: ticket.Summary, Type: gojira.IssueType{ - Name: issueType, + Name: ticket.TicketType, }, Project: gojira.Project{ Key: ticket.Project, }, + Unknowns: customfields, }, } From 120f48fbf4b16cf4b9116d6ec501aa4b8f043339 Mon Sep 17 00:00:00 2001 From: Seila Gamo Date: Mon, 21 Nov 2022 14:41:04 +0100 Subject: [PATCH 2/2] fix: error in create issue method interface --- pkg/tracking/jira/client.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/tracking/jira/client.go b/pkg/tracking/jira/client.go index f549b48..41dd4fd 100644 --- a/pkg/tracking/jira/client.go +++ b/pkg/tracking/jira/client.go @@ -71,7 +71,7 @@ func contains(elems []string, v string) bool { } // CreateTicket creates a ticket in Jira. -func (cl *Client) CreateTicket(ticket *model.Ticket) (*model.Ticket, error) { +func (cl *Client) CreateTicket(ticket *model.Ticket, issueType string) (*model.Ticket, error) { queryOptions := &gojira.GetQueryOptions{ ProjectKeys: ticket.Project, @@ -133,7 +133,7 @@ func (cl *Client) CreateTicket(ticket *model.Ticket) (*model.Ticket, error) { Description: ticket.Description, Summary: ticket.Summary, Type: gojira.IssueType{ - Name: ticket.TicketType, + Name: issueType, }, Project: gojira.Project{ Key: ticket.Project,