Commit a11ea797 authored by Matthias Bartelmeß's avatar Matthias Bartelmeß Committed by Jordan Caussat

Implement UnmarshalJSON for Jira Service Properties

parent dbe6fb3e
......@@ -17,7 +17,11 @@
package gitlab
import (
"encoding/json"
"fmt"
"net/url"
"reflect"
"strconv"
"time"
)
......@@ -396,6 +400,40 @@ type JiraServiceProperties struct {
JiraIssueTransitionID string `json:"jira_issue_transition_id,omitempty"`
}
// UnmarshalJSON decodes the Jira Service Properties.
//
// The Gitlab API returns under some circumstances JiraIssueTransitionID
// as a string and sometimes as an integer. This function converts the
// returned value into an integer.
func (p *JiraServiceProperties) UnmarshalJSON(b []byte) error {
type Alias JiraServiceProperties
aux := &struct {
JiraIssueTransitionID interface{} `url:"jira_issue_transition_id,omitempty" json:"jira_issue_transition_id,omitempty"`
*Alias
}{
Alias: (*Alias)(p),
}
if err := json.Unmarshal(b, &aux); err != nil {
return err
}
switch id := aux.JiraIssueTransitionID.(type) {
case string:
converted, err := strconv.Atoi(id)
if err != nil {
return err
}
p.JiraIssueTransitionID = func(i int) *int { return &i }(converted)
case float64:
p.JiraIssueTransitionID = Int(int(id))
default:
return fmt.Errorf("failed to unmarshal JiraTransitionID of type: %s", reflect.TypeOf(id))
}
return nil
}
// GetJiraService gets Jira service settings for a project.
//
// GitLab API docs:
......
......@@ -115,14 +115,39 @@ func TestGetJiraService(t *testing.T) {
mux.HandleFunc("/api/v4/projects/1/services/jira", func(w http.ResponseWriter, r *http.Request) {
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}}
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}}`)
})
mux.HandleFunc("/api/v4/projects/3/services/jira", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":3, "properties": {"jira_issue_transition_id": "2,3"}}`)
})
want := &JiraService{
Service: Service{ID: 1},
Properties: &JiraServiceProperties{
JiraIssueTransitionID: Int(2),
}}
service, _, err := client.Services.GetJiraService(1)
if err != nil {
t.Fatalf("Services.GetJiraService returns an error: %v", err)
}
if !reflect.DeepEqual(want, service) {
t.Errorf("Services.GetJiraService returned %+v, want %+v", service, want)
}
service, _, err = client.Services.GetJiraService(2)
if err != nil {
t.Fatalf("Services.GetJiraService returns an error: %v", err)
}
if !reflect.DeepEqual(want, service) {
t.Errorf("Services.GetJiraService returned %+v, want %+v", service, want)
}
......
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