From 2205d5b7ca311d3e16b79ac12884e16a5907c65b Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Sat, 2 Sep 2023 14:43:07 +0200 Subject: [PATCH] Add custom MergRequest unmarshal logic for label details --- merge_requests.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/merge_requests.go b/merge_requests.go index 34a244a1d..822b0ded6 100644 --- a/merge_requests.go +++ b/merge_requests.go @@ -17,6 +17,7 @@ package gitlab import ( + "encoding/json" "fmt" "net/http" "time" @@ -53,6 +54,7 @@ type MergeRequest struct { SourceProjectID int `json:"source_project_id"` TargetProjectID int `json:"target_project_id"` Labels Labels `json:"labels"` + LabelDetails []*LabelDetails `json:"label_details"` Description string `json:"description"` Draft bool `json:"draft"` WorkInProgress bool `json:"work_in_progress"` @@ -116,6 +118,40 @@ func (m MergeRequest) String() string { return Stringify(m) } +func (m *MergeRequest) UnmarshalJSON(data []byte) error { + type alias MergeRequest + + raw := make(map[string]interface{}) + err := json.Unmarshal(data, &raw) + if err != nil { + return err + } + + labelDetails, ok := raw["labels"].([]interface{}) + if ok && len(labelDetails) > 0 { + // We only want to change anything if we got label details. + if _, ok := labelDetails[0].(map[string]interface{}); !ok { + return json.Unmarshal(data, (*alias)(m)) + } + + labels := make([]interface{}, len(labelDetails)) + for i, details := range labelDetails { + labels[i] = details.(map[string]interface{})["name"] + } + + // Set the correct values + raw["labels"] = labels + raw["label_details"] = labelDetails + + data, err = json.Marshal(raw) + if err != nil { + return err + } + } + + return json.Unmarshal(data, (*alias)(m)) +} + // MergeRequestDiffVersion represents Gitlab merge request version. // // Gitlab API docs: