Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MI-2186]:Solved the bug: Jira issues cannot be created anymore with Jira Server/Datacenter 9.x #9

Merged
merged 8 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
121 changes: 119 additions & 2 deletions server/client_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
package main

import (
"fmt"
"net/http"

jira "github.com/andygrunwald/go-jira"
"github.com/hashicorp/go-version"
"github.com/pkg/errors"
)

Expand All @@ -22,10 +24,125 @@ 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 {
FieldID string `json:"fieldId,omitempty"`
}

type FieldId struct {
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
Values []FieldValues `json:"values,omitempty"`
}
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved

type Version struct {
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
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) {
cimd, resp, err := client.Jira.Issue.GetCreateMetaWithOptions(options)
ServerInfoApiEndpoint := "rest/api/2/serverInfo"
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
v := new(Version)
req, err := client.Jira.NewRequest(http.MethodGet, ServerInfoApiEndpoint, nil)
if err != nil {
return nil, err
}

_, err = client.Jira.Do(req, v)
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

currentVersion, err := version.NewVersion(v.VersionInfo)
if err != nil {
return nil, err
}

pivotVersion, err := version.NewVersion(PivotVersion)
if err != nil {
return nil, err
}

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
} else {
cd, response, err := client.Jira.Project.ListWithOptions(options)
meta := new(jira.CreateMetaInfo)

if err == nil {
manojmalik20 marked this conversation as resolved.
Show resolved Hide resolved
for i := 0; i < len(*cd); i++ {
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
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 {
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
break
}

issues := new(IssueInfo)
response, err = client.Jira.Do(req, issues)
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,
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)
response, err = client.Jira.Do(req, field)
if err != nil {
break
}

fieldID := new(FieldId)
response, err = client.Jira.Do(req, fieldID)
if err != nil {
break
}

fieldMap := make(map[string]interface{})
for f, fieldValue := range field.Values {
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
fieldMap[fieldID.Values[f].FieldID] = fieldValue
}
issue.Fields = fieldMap
}
proj := meta.Projects
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
proj = append(proj, project)
meta.Projects = proj
}
}
info = meta
resp = response
}

if err != nil {
if resp == nil {
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
return nil, err
Expand All @@ -36,7 +153,7 @@ func (client jiraServerClient) GetCreateMeta(options *jira.GetQueryOptions) (*ji
}
return nil, RESTError{err, resp.StatusCode}
}
return cimd, nil
return info, nil
}

// SearchUsersAssignableToIssue finds all users that can be assigned to an issue.
Expand Down