Skip to content

Commit 9d665ab

Browse files
authored
Merge pull request xanzy#628 from xanzy/svh/b-bool
Make a custom marshaller for merge params
2 parents 5847070 + c4ae36b commit 9d665ab

File tree

2 files changed

+76
-36
lines changed

2 files changed

+76
-36
lines changed

event_types.go

Lines changed: 75 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package gitlab
1818

1919
import (
20+
"encoding/json"
21+
"fmt"
22+
"strconv"
2023
"time"
2124
)
2225

@@ -512,41 +515,39 @@ type MergeEvent struct {
512515
Visibility VisibilityValue `json:"visibility"`
513516
} `json:"project"`
514517
ObjectAttributes struct {
515-
ID int `json:"id"`
516-
TargetBranch string `json:"target_branch"`
517-
SourceBranch string `json:"source_branch"`
518-
SourceProjectID int `json:"source_project_id"`
519-
AuthorID int `json:"author_id"`
520-
AssigneeID int `json:"assignee_id"`
521-
Title string `json:"title"`
522-
CreatedAt string `json:"created_at"` // Should be *time.Time (see Gitlab issue #21468)
523-
UpdatedAt string `json:"updated_at"` // Should be *time.Time (see Gitlab issue #21468)
524-
StCommits []*Commit `json:"st_commits"`
525-
StDiffs []*Diff `json:"st_diffs"`
526-
MilestoneID int `json:"milestone_id"`
527-
State string `json:"state"`
528-
MergeStatus string `json:"merge_status"`
529-
TargetProjectID int `json:"target_project_id"`
530-
IID int `json:"iid"`
531-
Description string `json:"description"`
532-
Position int `json:"position"`
533-
LockedAt string `json:"locked_at"`
534-
UpdatedByID int `json:"updated_by_id"`
535-
MergeError string `json:"merge_error"`
536-
MergeParams struct {
537-
ForceRemoveSourceBranch string `json:"force_remove_source_branch"`
538-
} `json:"merge_params"`
539-
MergeWhenBuildSucceeds bool `json:"merge_when_build_succeeds"`
540-
MergeUserID int `json:"merge_user_id"`
541-
MergeCommitSHA string `json:"merge_commit_sha"`
542-
DeletedAt string `json:"deleted_at"`
543-
ApprovalsBeforeMerge string `json:"approvals_before_merge"`
544-
RebaseCommitSHA string `json:"rebase_commit_sha"`
545-
InProgressMergeCommitSHA string `json:"in_progress_merge_commit_sha"`
546-
LockVersion int `json:"lock_version"`
547-
TimeEstimate int `json:"time_estimate"`
548-
Source *Repository `json:"source"`
549-
Target *Repository `json:"target"`
518+
ID int `json:"id"`
519+
TargetBranch string `json:"target_branch"`
520+
SourceBranch string `json:"source_branch"`
521+
SourceProjectID int `json:"source_project_id"`
522+
AuthorID int `json:"author_id"`
523+
AssigneeID int `json:"assignee_id"`
524+
Title string `json:"title"`
525+
CreatedAt string `json:"created_at"` // Should be *time.Time (see Gitlab issue #21468)
526+
UpdatedAt string `json:"updated_at"` // Should be *time.Time (see Gitlab issue #21468)
527+
StCommits []*Commit `json:"st_commits"`
528+
StDiffs []*Diff `json:"st_diffs"`
529+
MilestoneID int `json:"milestone_id"`
530+
State string `json:"state"`
531+
MergeStatus string `json:"merge_status"`
532+
TargetProjectID int `json:"target_project_id"`
533+
IID int `json:"iid"`
534+
Description string `json:"description"`
535+
Position int `json:"position"`
536+
LockedAt string `json:"locked_at"`
537+
UpdatedByID int `json:"updated_by_id"`
538+
MergeError string `json:"merge_error"`
539+
MergeParams *MergeParams `json:"merge_params"`
540+
MergeWhenBuildSucceeds bool `json:"merge_when_build_succeeds"`
541+
MergeUserID int `json:"merge_user_id"`
542+
MergeCommitSHA string `json:"merge_commit_sha"`
543+
DeletedAt string `json:"deleted_at"`
544+
ApprovalsBeforeMerge string `json:"approvals_before_merge"`
545+
RebaseCommitSHA string `json:"rebase_commit_sha"`
546+
InProgressMergeCommitSHA string `json:"in_progress_merge_commit_sha"`
547+
LockVersion int `json:"lock_version"`
548+
TimeEstimate int `json:"time_estimate"`
549+
Source *Repository `json:"source"`
550+
Target *Repository `json:"target"`
550551
LastCommit struct {
551552
ID string `json:"id"`
552553
Message string `json:"message"`
@@ -594,6 +595,45 @@ type MergeEvent struct {
594595
} `json:"changes"`
595596
}
596597

598+
// MergeParams represents the merge params.
599+
type MergeParams struct {
600+
ForceRemoveSourceBranch bool `json:"force_remove_source_branch"`
601+
}
602+
603+
// UnmarshalJSON decodes the merge parameters
604+
//
605+
// This allows support of ForceRemoveSourceBranch for both type bool (>11.9) and string (<11.9)
606+
func (p *MergeParams) UnmarshalJSON(b []byte) error {
607+
type Alias MergeParams
608+
raw := struct {
609+
*Alias
610+
ForceRemoveSourceBranch interface{} `json:"force_remove_source_branch"`
611+
}{
612+
Alias: (*Alias)(p),
613+
}
614+
615+
err := json.Unmarshal(b, &raw)
616+
if err != nil {
617+
return err
618+
}
619+
620+
switch v := raw.ForceRemoveSourceBranch.(type) {
621+
case nil:
622+
// No action needed.
623+
case bool:
624+
p.ForceRemoveSourceBranch = v
625+
case string:
626+
p.ForceRemoveSourceBranch, err = strconv.ParseBool(v)
627+
if err != nil {
628+
return err
629+
}
630+
default:
631+
return fmt.Errorf("failed to unmarshal ForceRemoveSourceBranch of type: %T", v)
632+
}
633+
634+
return nil
635+
}
636+
597637
// WikiPageEvent represents a wiki page event.
598638
//
599639
// GitLab API docs:

services.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ func (p *JiraServiceProperties) UnmarshalJSON(b []byte) error {
416416

417417
switch id := raw.JiraIssueTransitionID.(type) {
418418
case nil:
419-
p.JiraIssueTransitionID = ""
419+
// No action needed.
420420
case string:
421421
p.JiraIssueTransitionID = id
422422
case float64:

0 commit comments

Comments
 (0)