Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1786 from xanzy/bugfix/label-details
Browse files Browse the repository at this point in the history
Add custom MergRequest unmarshal logic for label details
  • Loading branch information
svanharmelen authored Sep 2, 2023
2 parents cf34774 + 2205d5b commit da51eb1
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions merge_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package gitlab

import (
"encoding/json"
"fmt"
"net/http"
"time"
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit da51eb1

Please sign in to comment.