Unverified Commit 39fbdb29 authored by Sander van Harmelen's avatar Sander van Harmelen Committed by GitHub

Merge pull request #623 from jorcau/update-jira-service-api

Update Jira service: change JiraTransitionID from int to string
parents 8cb66008 a53cb177
...@@ -17,7 +17,9 @@ ...@@ -17,7 +17,9 @@
package gitlab package gitlab
import ( import (
"encoding/json"
"fmt" "fmt"
"strconv"
"time" "time"
) )
...@@ -396,6 +398,36 @@ type JiraServiceProperties struct { ...@@ -396,6 +398,36 @@ type JiraServiceProperties struct {
JiraIssueTransitionID string `json:"jira_issue_transition_id,omitempty"` JiraIssueTransitionID string `json:"jira_issue_transition_id,omitempty"`
} }
// UnmarshalJSON decodes the Jira Service Properties.
//
// This allows support of JiraIssueTransitionID for both type string (>11.9) and float64 (<11.9)
func (p *JiraServiceProperties) UnmarshalJSON(b []byte) error {
type Alias JiraServiceProperties
raw := struct {
*Alias
JiraIssueTransitionID interface{} `json:"jira_issue_transition_id"`
}{
Alias: (*Alias)(p),
}
if err := json.Unmarshal(b, &raw); err != nil {
return err
}
switch id := raw.JiraIssueTransitionID.(type) {
case nil:
p.JiraIssueTransitionID = ""
case string:
p.JiraIssueTransitionID = id
case float64:
p.JiraIssueTransitionID = strconv.Itoa(int(id))
default:
return fmt.Errorf("failed to unmarshal JiraTransitionID of type: %T", id)
}
return nil
}
// GetJiraService gets Jira service settings for a project. // GetJiraService gets Jira service settings for a project.
// //
// GitLab API docs: // GitLab API docs:
......
...@@ -113,18 +113,60 @@ func TestGetJiraService(t *testing.T) { ...@@ -113,18 +113,60 @@ func TestGetJiraService(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/api/v4/projects/0/services/jira", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":1, "properties": {"jira_issue_transition_id": "2"}}`)
})
mux.HandleFunc("/api/v4/projects/1/services/jira", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/services/jira", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":1}`) fmt.Fprint(w, `{"id":1, "properties": {"jira_issue_transition_id": 2}}`)
}) })
want := &JiraService{Service: Service{ID: 1}}
service, _, err := client.Services.GetJiraService(1) mux.HandleFunc("/api/v4/projects/2/services/jira", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":1, "properties": {"jira_issue_transition_id": "2,3"}}`)
})
mux.HandleFunc("/api/v4/projects/3/services/jira", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":1, "properties": {}}`)
})
want := []*JiraService{
&JiraService{
Service: Service{ID: 1},
Properties: &JiraServiceProperties{
JiraIssueTransitionID: "2",
},
},
&JiraService{
Service: Service{ID: 1},
Properties: &JiraServiceProperties{
JiraIssueTransitionID: "2",
},
},
&JiraService{
Service: Service{ID: 1},
Properties: &JiraServiceProperties{
JiraIssueTransitionID: "2,3",
},
},
&JiraService{
Service: Service{ID: 1},
Properties: &JiraServiceProperties{},
},
}
for testcase := 0; testcase < len(want); testcase++ {
service, _, err := client.Services.GetJiraService(testcase)
if err != nil { if err != nil {
t.Fatalf("Services.GetJiraService returns an error: %v", err) t.Fatalf("Services.GetJiraService returns an error: %v", err)
} }
if !reflect.DeepEqual(want, service) {
t.Errorf("Services.GetJiraService returned %+v, want %+v", service, want) if !reflect.DeepEqual(want[testcase], service) {
t.Errorf("Services.GetJiraService returned %+v, want %+v", service, want[testcase])
}
} }
} }
...@@ -142,7 +184,7 @@ func TestSetJiraService(t *testing.T) { ...@@ -142,7 +184,7 @@ func TestSetJiraService(t *testing.T) {
ProjectKey: String("as"), ProjectKey: String("as"),
Username: String("aas"), Username: String("aas"),
Password: String("asd"), Password: String("asd"),
JiraIssueTransitionID: String("2"), JiraIssueTransitionID: String("2,3"),
} }
_, err := client.Services.SetJiraService(1, opt) _, err := client.Services.SetJiraService(1, opt)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment