From 6c9dbd9e2dd3bff4c742372df880c071a2e9cf80 Mon Sep 17 00:00:00 2001 From: Nityanand Rai Date: Tue, 11 Oct 2022 13:22:45 +0530 Subject: [PATCH 1/8] [MI-2186]:Solved the bug: Jira issues cannot be created anymore with Jira Server/Datacenter 9.x --- go.mod | 1 + go.sum | 1 + server/client_server.go | 101 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 101 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index d66e09214..1834305ef 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/dghubble/oauth1 v0.5.0 github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/gorilla/mux v1.8.0 + github.com/hashicorp/go-version v1.2.0 github.com/jarcoal/httpmock v1.0.8 github.com/mattermost/mattermost-plugin-api v0.0.26-0.20220223141232-cb8b1984774a github.com/mattermost/mattermost-plugin-autolink v1.2.2-0.20210709183311-c8fa30db649f diff --git a/go.sum b/go.sum index 64fa7ea74..dc2f421df 100644 --- a/go.sum +++ b/go.sum @@ -838,6 +838,7 @@ github.com/hashicorp/go-sockaddr v1.0.2/go.mod h1:rB4wwRAUzs07qva3c5SdrY/NEtAUjG github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E= github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= diff --git a/server/client_server.go b/server/client_server.go index 8b19bf3f4..eb5c15f7c 100644 --- a/server/client_server.go +++ b/server/client_server.go @@ -7,6 +7,7 @@ import ( "net/http" jira "github.com/andygrunwald/go-jira" + "github.com/hashicorp/go-version" "github.com/pkg/errors" ) @@ -22,10 +23,106 @@ func newServerClient(jiraClient *jira.Client) Client { } } +type IssueInfo struct { + Values []*jira.MetaIssueType `json:"values,omitempty"` +} + +type FieldInfo struct { + Values []interface{} `json:"values,omitempty"` +} + +type FieldValues struct { + Id string `json:"fieldId,omitempty"` +} + +type FieldId struct { + Values []FieldValues `json:"values,omitempty"` +} +type Version struct { + VersionInfo string `json:"version,omitempty"` +} + // GetCreateMeta returns the metadata needed to implement the UI and validation of // creating new Jira issues. func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*jira.CreateMetaInfo, error) { - cimd, resp, err := client.Jira.Issue.GetCreateMetaWithOptions(options) + apiEndpoint := "rest/api/2/serverInfo" + v := new(Version) + req, err := client.Jira.NewRequest("GET", apiEndpoint, nil) + if err != nil { + return nil, err + } + _, err = client.Jira.Do(req, v) + if err != nil { + return nil, err + } + v1, err := version.NewVersion(v.VersionInfo) + if err != nil { + return nil, err + } + v2, err := version.NewVersion("8.4.0") + if err != nil { + return nil, err + } + if v1.LessThan(v2) { + cimd, resp, err := client.Jira.Issue.GetCreateMetaWithOptions(options) + if err != nil { + if resp == nil { + return nil, err + } + resp.Body.Close() + if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnauthorized { + err = errors.New("not authorized to create issues") + } + return nil, RESTError{err, resp.StatusCode} + } + return cimd, nil + } + cd, resp, err := client.Jira.Project.ListWithOptions(options) + meta := new(jira.CreateMetaInfo) + for i := 0; i < len(*cd); i++ { + meta.Expand = (*cd)[i].Expand + project := new(jira.MetaProject) + project.Self = (*cd)[i].Self + project.Id = (*cd)[i].ID + project.Key = (*cd)[i].Key + project.Name = (*cd)[i].Name + apiEndpoint := "rest/api/2/issue/createmeta/" + (*cd)[i].ID + "/issuetypes" + req, err := client.Jira.NewRequest("GET", apiEndpoint, nil) + if err != nil { + break + } + issues := new(IssueInfo) + resp, err = client.Jira.Do(req, issues) + if err != nil { + break + } + project.IssueTypes = issues.Values + for j := 0; j < len(project.IssueTypes); j++ { + apiEndpoint := "rest/api/2/issue/createmeta/" + (*cd)[i].ID + "/issuetypes/" + project.IssueTypes[j].Id + req, err := client.Jira.NewRequest("GET", apiEndpoint, nil) + if err != nil { + break + } + field := new(FieldInfo) + resp, err = client.Jira.Do(req, field) + if err != nil { + break + } + fieldId := new(FieldId) + resp, err = client.Jira.Do(req, fieldId) + if err != nil { + break + } + newMap := make(map[string]interface{}) + for k := 0; k < len(field.Values); k++ { + newMap[fieldId.Values[k].Id] = field.Values[k] + } + project.IssueTypes[j].Fields = newMap + } + proj := meta.Projects + proj = append(proj, project) + meta.Projects = proj + } if err != nil { if resp == nil { return nil, err @@ -36,7 +133,7 @@ func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*ji } return nil, RESTError{err, resp.StatusCode} } - return cimd, nil + return meta, nil } // SearchUsersAssignableToIssue finds all users that can be assigned to an issue. From 3b0b2eb8ed0fdc63a4054a39fd8400e4b523e135 Mon Sep 17 00:00:00 2001 From: Nityanand Rai Date: Tue, 11 Oct 2022 17:48:46 +0530 Subject: [PATCH 2/8] [MI-2186]: Self Review done 1.Improved code quality --- server/client_server.go | 131 +++++++++++++++++++++------------------- 1 file changed, 70 insertions(+), 61 deletions(-) diff --git a/server/client_server.go b/server/client_server.go index eb5c15f7c..73077106c 100644 --- a/server/client_server.go +++ b/server/client_server.go @@ -55,85 +55,94 @@ func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*ji if err != nil { return nil, err } + v1, err := version.NewVersion(v.VersionInfo) if err != nil { return nil, err } + v2, err := version.NewVersion("8.4.0") if err != nil { return nil, err } + + var info *jira.CreateMetaInfo + var response *jira.Response if v1.LessThan(v2) { - cimd, resp, err := client.Jira.Issue.GetCreateMetaWithOptions(options) - if err != nil { - if resp == nil { - return nil, err - } - resp.Body.Close() - if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnauthorized { - err = errors.New("not authorized to create issues") - } - return nil, RESTError{err, resp.StatusCode} - } - return cimd, nil - } - cd, resp, err := client.Jira.Project.ListWithOptions(options) - meta := new(jira.CreateMetaInfo) - for i := 0; i < len(*cd); i++ { - meta.Expand = (*cd)[i].Expand - project := new(jira.MetaProject) - project.Self = (*cd)[i].Self - project.Id = (*cd)[i].ID - project.Key = (*cd)[i].Key - project.Name = (*cd)[i].Name - apiEndpoint := "rest/api/2/issue/createmeta/" + (*cd)[i].ID + "/issuetypes" - req, err := client.Jira.NewRequest("GET", apiEndpoint, nil) - if err != nil { - break - } - issues := new(IssueInfo) - resp, err = client.Jira.Do(req, issues) - if err != nil { - break - } - project.IssueTypes = issues.Values - for j := 0; j < len(project.IssueTypes); j++ { - apiEndpoint := "rest/api/2/issue/createmeta/" + (*cd)[i].ID + "/issuetypes/" + project.IssueTypes[j].Id - req, err := client.Jira.NewRequest("GET", apiEndpoint, nil) - if err != nil { - break - } - field := new(FieldInfo) - resp, err = client.Jira.Do(req, field) - if err != nil { - break - } - fieldId := new(FieldId) - resp, err = client.Jira.Do(req, fieldId) - if err != nil { - break - } - newMap := make(map[string]interface{}) - for k := 0; k < len(field.Values); k++ { - newMap[fieldId.Values[k].Id] = field.Values[k] + cimd, resp, error := client.Jira.Issue.GetCreateMetaWithOptions(options) + info = cimd + response = resp + err = error + } else { + cd, resp, err := client.Jira.Project.ListWithOptions(options) + meta := new(jira.CreateMetaInfo) + + if err == nil { + for i := 0; i < len(*cd); i++ { + meta.Expand = (*cd)[i].Expand + project := new(jira.MetaProject) + project.Self = (*cd)[i].Self + project.Id = (*cd)[i].ID + project.Key = (*cd)[i].Key + project.Name = (*cd)[i].Name + apiEndpoint := "rest/api/2/issue/createmeta/" + (*cd)[i].ID + "/issuetypes" + req, err := client.Jira.NewRequest("GET", apiEndpoint, nil) + if err != nil { + break + } + + issues := new(IssueInfo) + resp, err = client.Jira.Do(req, issues) + if err != nil { + break + } + + project.IssueTypes = issues.Values + for j := 0; j < len(project.IssueTypes); j++ { + apiEndpoint := "rest/api/2/issue/createmeta/" + (*cd)[i].ID + "/issuetypes/" + project.IssueTypes[j].Id + req, err := client.Jira.NewRequest("GET", apiEndpoint, nil) + if err != nil { + break + } + + field := new(FieldInfo) + resp, err = client.Jira.Do(req, field) + if err != nil { + break + } + + fieldId := new(FieldId) + resp, err = client.Jira.Do(req, fieldId) + if err != nil { + break + } + + newMap := make(map[string]interface{}) + for k := 0; k < len(field.Values); k++ { + newMap[fieldId.Values[k].Id] = field.Values[k] + } + project.IssueTypes[j].Fields = newMap + } + proj := meta.Projects + proj = append(proj, project) + meta.Projects = proj } - project.IssueTypes[j].Fields = newMap } - proj := meta.Projects - proj = append(proj, project) - meta.Projects = proj + info = meta + response = resp } + if err != nil { - if resp == nil { + if response == nil { return nil, err } - resp.Body.Close() - if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnauthorized { + response.Body.Close() + if response.StatusCode == http.StatusForbidden || response.StatusCode == http.StatusUnauthorized { err = errors.New("not authorized to create issues") } - return nil, RESTError{err, resp.StatusCode} + return nil, RESTError{err, response.StatusCode} } - return meta, nil + return info, nil } // SearchUsersAssignableToIssue finds all users that can be assigned to an issue. From 82a84a328c9fe3698aa2ac6891a4a4292eb26f70 Mon Sep 17 00:00:00 2001 From: Nityanand Rai Date: Wed, 12 Oct 2022 20:43:58 +0530 Subject: [PATCH 3/8] [MI-2186]: Review fixes done 1. Improved code quality 2. Used constants where required 3. Used range loops where required --- server/client_server.go | 79 +++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/server/client_server.go b/server/client_server.go index 73077106c..db7d0df26 100644 --- a/server/client_server.go +++ b/server/client_server.go @@ -4,6 +4,7 @@ package main import ( + "fmt" "net/http" jira "github.com/andygrunwald/go-jira" @@ -32,96 +33,106 @@ type FieldInfo struct { } type FieldValues struct { - Id string `json:"fieldId,omitempty"` + FieldID string `json:"fieldId,omitempty"` } type FieldId struct { Values []FieldValues `json:"values,omitempty"` } + type Version struct { VersionInfo string `json:"version,omitempty"` } +const ( + CreateMetaAPIEndpoint = "rest/api/2/issue/createmeta/" + PivotVersion = "8.4.0" +) + // GetCreateMeta returns the metadata needed to implement the UI and validation of // creating new Jira issues. func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*jira.CreateMetaInfo, error) { - apiEndpoint := "rest/api/2/serverInfo" + ServerInfoApiEndpoint := "rest/api/2/serverInfo" v := new(Version) - req, err := client.Jira.NewRequest("GET", apiEndpoint, nil) + req, err := client.Jira.NewRequest(http.MethodGet, ServerInfoApiEndpoint, nil) if err != nil { return nil, err } + _, err = client.Jira.Do(req, v) if err != nil { return nil, err } - v1, err := version.NewVersion(v.VersionInfo) + currentVersion, err := version.NewVersion(v.VersionInfo) if err != nil { return nil, err } - v2, err := version.NewVersion("8.4.0") + pivotVersion, err := version.NewVersion(PivotVersion) if err != nil { return nil, err } var info *jira.CreateMetaInfo - var response *jira.Response - if v1.LessThan(v2) { - cimd, resp, error := client.Jira.Issue.GetCreateMetaWithOptions(options) + var resp *jira.Response + if currentVersion.LessThan(pivotVersion) { + cimd, response, error := client.Jira.Issue.GetCreateMetaWithOptions(options) info = cimd - response = resp + resp = response err = error } else { - cd, resp, err := client.Jira.Project.ListWithOptions(options) + cd, response, err := client.Jira.Project.ListWithOptions(options) meta := new(jira.CreateMetaInfo) if err == nil { for i := 0; i < len(*cd); i++ { meta.Expand = (*cd)[i].Expand - project := new(jira.MetaProject) - project.Self = (*cd)[i].Self - project.Id = (*cd)[i].ID - project.Key = (*cd)[i].Key - project.Name = (*cd)[i].Name - apiEndpoint := "rest/api/2/issue/createmeta/" + (*cd)[i].ID + "/issuetypes" - req, err := client.Jira.NewRequest("GET", apiEndpoint, nil) + apiEndpoint := fmt.Sprintf("%s%s/issuetypes", CreateMetaAPIEndpoint, (*cd)[i].ID) + req, err := client.Jira.NewRequest(http.MethodGet, apiEndpoint, nil) if err != nil { break } issues := new(IssueInfo) - resp, err = client.Jira.Do(req, issues) + response, err = client.Jira.Do(req, issues) if err != nil { break } - project.IssueTypes = issues.Values - for j := 0; j < len(project.IssueTypes); j++ { - apiEndpoint := "rest/api/2/issue/createmeta/" + (*cd)[i].ID + "/issuetypes/" + project.IssueTypes[j].Id - req, err := client.Jira.NewRequest("GET", apiEndpoint, nil) + project := &jira.MetaProject{ + Expand: (*cd)[i].Expand, + Self: (*cd)[i].Self, + Id: (*cd)[i].ID, + Key: (*cd)[i].Key, + Name: (*cd)[i].Name, + IssueTypes: issues.Values, + } + + for _, issue := range project.IssueTypes { + apiEndpoint := fmt.Sprintf("%s%s/issuetypes/%s", CreateMetaAPIEndpoint, (*cd)[i].ID, issue.Id) + req, err := client.Jira.NewRequest(http.MethodGet, apiEndpoint, nil) if err != nil { break } field := new(FieldInfo) - resp, err = client.Jira.Do(req, field) + response, err = client.Jira.Do(req, field) if err != nil { break } - fieldId := new(FieldId) - resp, err = client.Jira.Do(req, fieldId) + fieldID := new(FieldId) + response, err = client.Jira.Do(req, fieldID) if err != nil { break } - newMap := make(map[string]interface{}) - for k := 0; k < len(field.Values); k++ { - newMap[fieldId.Values[k].Id] = field.Values[k] + fieldMap := make(map[string]interface{}) + for f, fieldValue := range field.Values { + fieldMap[fieldID.Values[f].FieldID] = fieldValue } - project.IssueTypes[j].Fields = newMap + issue.Fields = fieldMap } proj := meta.Projects proj = append(proj, project) @@ -129,18 +140,18 @@ func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*ji } } info = meta - response = resp + resp = response } if err != nil { - if response == nil { + if resp == nil { return nil, err } - response.Body.Close() - if response.StatusCode == http.StatusForbidden || response.StatusCode == http.StatusUnauthorized { + resp.Body.Close() + if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnauthorized { err = errors.New("not authorized to create issues") } - return nil, RESTError{err, response.StatusCode} + return nil, RESTError{err, resp.StatusCode} } return info, nil } From 66024d7089a26b846375b863bf339ac5edf1cd67 Mon Sep 17 00:00:00 2001 From: Nityanand Rai Date: Fri, 14 Oct 2022 16:13:34 +0530 Subject: [PATCH 4/8] [MI-2186]: Review fixes done 1. Improved code quality --- server/client_server.go | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/server/client_server.go b/server/client_server.go index db7d0df26..059791fb5 100644 --- a/server/client_server.go +++ b/server/client_server.go @@ -36,7 +36,7 @@ type FieldValues struct { FieldID string `json:"fieldId,omitempty"` } -type FieldId struct { +type FieldID struct { Values []FieldValues `json:"values,omitempty"` } @@ -59,8 +59,7 @@ func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*ji return nil, err } - _, err = client.Jira.Do(req, v) - if err != nil { + if _, err = client.Jira.Do(req, v); err != nil { return nil, err } @@ -77,19 +76,16 @@ func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*ji var info *jira.CreateMetaInfo var resp *jira.Response if currentVersion.LessThan(pivotVersion) { - cimd, response, error := client.Jira.Issue.GetCreateMetaWithOptions(options) - info = cimd - resp = response - err = error + info, resp, err = client.Jira.Issue.GetCreateMetaWithOptions(options) } else { - cd, response, err := client.Jira.Project.ListWithOptions(options) + cd, response, er := client.Jira.Project.ListWithOptions(options) meta := new(jira.CreateMetaInfo) - if err == nil { + if er == nil { for i := 0; i < len(*cd); i++ { meta.Expand = (*cd)[i].Expand apiEndpoint := fmt.Sprintf("%s%s/issuetypes", CreateMetaAPIEndpoint, (*cd)[i].ID) - req, err := client.Jira.NewRequest(http.MethodGet, apiEndpoint, nil) + req, err = client.Jira.NewRequest(http.MethodGet, apiEndpoint, nil) if err != nil { break } @@ -111,7 +107,7 @@ func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*ji for _, issue := range project.IssueTypes { apiEndpoint := fmt.Sprintf("%s%s/issuetypes/%s", CreateMetaAPIEndpoint, (*cd)[i].ID, issue.Id) - req, err := client.Jira.NewRequest(http.MethodGet, apiEndpoint, nil) + req, err = client.Jira.NewRequest(http.MethodGet, apiEndpoint, nil) if err != nil { break } @@ -122,7 +118,7 @@ func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*ji break } - fieldID := new(FieldId) + fieldID := new(FieldID) response, err = client.Jira.Do(req, fieldID) if err != nil { break @@ -134,13 +130,14 @@ func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*ji } issue.Fields = fieldMap } - proj := meta.Projects - proj = append(proj, project) - meta.Projects = proj + meta.Projects = append(meta.Projects, project) } } info = meta resp = response + if er != nil { + err = er + } } if err != nil { From af7e08fc55976a99254dd63f8667474383cb1238 Mon Sep 17 00:00:00 2001 From: Nityanand Rai Date: Mon, 17 Oct 2022 19:10:27 +0530 Subject: [PATCH 5/8] [MI-2186]: Review fixes done 1.Improved code quality --- server/client_server.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/client_server.go b/server/client_server.go index 059791fb5..753328567 100644 --- a/server/client_server.go +++ b/server/client_server.go @@ -78,10 +78,10 @@ func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*ji if currentVersion.LessThan(pivotVersion) { info, resp, err = client.Jira.Issue.GetCreateMetaWithOptions(options) } else { - cd, response, er := client.Jira.Project.ListWithOptions(options) + cd, response, apiErr := client.Jira.Project.ListWithOptions(options) meta := new(jira.CreateMetaInfo) - if er == nil { + if apiErr == nil { for i := 0; i < len(*cd); i++ { meta.Expand = (*cd)[i].Expand apiEndpoint := fmt.Sprintf("%s%s/issuetypes", CreateMetaAPIEndpoint, (*cd)[i].ID) @@ -135,8 +135,8 @@ func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*ji } info = meta resp = response - if er != nil { - err = er + if apiErr != nil { + err = apiErr } } From 3e72f27ca5f0103559c01bd4c41a0e05a7dc6d7f Mon Sep 17 00:00:00 2001 From: Nityanand Rai Date: Tue, 18 Oct 2022 16:14:16 +0530 Subject: [PATCH 6/8] [MI-2186]: Review fixes done 1. Improved code quality --- server/client_server.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/server/client_server.go b/server/client_server.go index 753328567..5b47f1f6e 100644 --- a/server/client_server.go +++ b/server/client_server.go @@ -12,6 +12,12 @@ import ( "github.com/pkg/errors" ) +const ( + ServerInfoApiEndpoint = "rest/api/2/serverInfo" + CreateMetaAPIEndpoint = "rest/api/2/issue/createmeta/" + PivotVersion = "8.4.0" +) + type jiraServerClient struct { JiraClient } @@ -44,15 +50,9 @@ type Version struct { VersionInfo string `json:"version,omitempty"` } -const ( - CreateMetaAPIEndpoint = "rest/api/2/issue/createmeta/" - PivotVersion = "8.4.0" -) - // GetCreateMeta returns the metadata needed to implement the UI and validation of // creating new Jira issues. func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*jira.CreateMetaInfo, error) { - ServerInfoApiEndpoint := "rest/api/2/serverInfo" v := new(Version) req, err := client.Jira.NewRequest(http.MethodGet, ServerInfoApiEndpoint, nil) if err != nil { From 669ea469b94827512ef82331596d1404279e0ad3 Mon Sep 17 00:00:00 2001 From: Nityanand Rai Date: Thu, 20 Oct 2022 12:34:54 +0530 Subject: [PATCH 7/8] [MI-2186]: Review fixes done 1. Improved code quality 2. Used range loop 3. Changed various names --- server/client_server.go | 61 ++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/server/client_server.go b/server/client_server.go index 5b47f1f6e..9622cf6de 100644 --- a/server/client_server.go +++ b/server/client_server.go @@ -46,14 +46,28 @@ type FieldID struct { Values []FieldValues `json:"values,omitempty"` } -type Version struct { +type ServerVersion struct { VersionInfo string `json:"version,omitempty"` } +// GetIssueInfo returns the issues information based on project id. +func (client jiraServerClient) GetIssueInfo(projectID string) (*IssueInfo, *jira.Response, error) { + apiEndpoint := fmt.Sprintf("%s%s/issuetypes", CreateMetaAPIEndpoint, projectID) + req, err := client.Jira.NewRequest(http.MethodGet, apiEndpoint, nil) + if err != nil { + return nil, nil, err + } + + issues := new(IssueInfo) + response, err := client.Jira.Do(req, issues) + + return issues, response, err +} + // GetCreateMeta returns the metadata needed to implement the UI and validation of // creating new Jira issues. func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*jira.CreateMetaInfo, error) { - v := new(Version) + v := new(ServerVersion) req, err := client.Jira.NewRequest(http.MethodGet, ServerInfoApiEndpoint, nil) if err != nil { return nil, err @@ -75,58 +89,53 @@ func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*ji var info *jira.CreateMetaInfo var resp *jira.Response + var issues *IssueInfo + var projectList *jira.ProjectList if currentVersion.LessThan(pivotVersion) { info, resp, err = client.Jira.Issue.GetCreateMetaWithOptions(options) } else { - cd, response, apiErr := client.Jira.Project.ListWithOptions(options) + projectList, resp, err = client.Jira.Project.ListWithOptions(options) meta := new(jira.CreateMetaInfo) - if apiErr == nil { - for i := 0; i < len(*cd); i++ { - meta.Expand = (*cd)[i].Expand - apiEndpoint := fmt.Sprintf("%s%s/issuetypes", CreateMetaAPIEndpoint, (*cd)[i].ID) - req, err = client.Jira.NewRequest(http.MethodGet, apiEndpoint, nil) - if err != nil { - break - } - - issues := new(IssueInfo) - response, err = client.Jira.Do(req, issues) + if err == nil { + for _, proj := range *projectList { + meta.Expand = proj.Expand + issues, resp, err = client.GetIssueInfo(proj.ID) if err != nil { break } project := &jira.MetaProject{ - Expand: (*cd)[i].Expand, - Self: (*cd)[i].Self, - Id: (*cd)[i].ID, - Key: (*cd)[i].Key, - Name: (*cd)[i].Name, + Expand: proj.Expand, + Self: proj.Self, + Id: proj.ID, + Key: proj.Key, + Name: proj.Name, IssueTypes: issues.Values, } for _, issue := range project.IssueTypes { - apiEndpoint := fmt.Sprintf("%s%s/issuetypes/%s", CreateMetaAPIEndpoint, (*cd)[i].ID, issue.Id) + apiEndpoint := fmt.Sprintf("%s%s/issuetypes/%s", CreateMetaAPIEndpoint, proj.ID, issue.Id) req, err = client.Jira.NewRequest(http.MethodGet, apiEndpoint, nil) if err != nil { break } field := new(FieldInfo) - response, err = client.Jira.Do(req, field) + resp, err = client.Jira.Do(req, field) if err != nil { break } fieldID := new(FieldID) - response, err = client.Jira.Do(req, fieldID) + resp, err = client.Jira.Do(req, fieldID) if err != nil { break } fieldMap := make(map[string]interface{}) - for f, fieldValue := range field.Values { - fieldMap[fieldID.Values[f].FieldID] = fieldValue + for index, fieldValue := range field.Values { + fieldMap[fieldID.Values[index].FieldID] = fieldValue } issue.Fields = fieldMap } @@ -134,10 +143,6 @@ func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*ji } } info = meta - resp = response - if apiErr != nil { - err = apiErr - } } if err != nil { From ccfd6d830aef00d9bfa1c20962ba211cd23c348a Mon Sep 17 00:00:00 2001 From: Nityanand Rai Date: Thu, 20 Oct 2022 17:03:16 +0530 Subject: [PATCH 8/8] [MI-2186]: Review fixes done 1. Improved code quality --- server/client_server.go | 1 - 1 file changed, 1 deletion(-) diff --git a/server/client_server.go b/server/client_server.go index 9622cf6de..e81d5ecb6 100644 --- a/server/client_server.go +++ b/server/client_server.go @@ -60,7 +60,6 @@ func (client jiraServerClient) GetIssueInfo(projectID string) (*IssueInfo, *jira issues := new(IssueInfo) response, err := client.Jira.Do(req, issues) - return issues, response, err }