Skip to content

Commit

Permalink
Minor tweaks for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
svanharmelen committed Apr 18, 2024
1 parent fe564bb commit c187a2d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
12 changes: 8 additions & 4 deletions event_parsing.go
Expand Up @@ -260,11 +260,15 @@ func ParseWebhook(eventType EventType, payload []byte) (event interface{}, err e
return nil, err
}

if _, ok := data["project"]; ok {
event = &ProjectResourceAccessTokenEvent{}
} else if _, ok := data["group"]; ok {
_, groupEvent := data["group"]
_, projectEvent := data["project"]

switch {
case groupEvent:
event = &GroupResourceAccessTokenEvent{}
} else {
case projectEvent:
event = &ProjectResourceAccessTokenEvent{}
default:
return nil, fmt.Errorf("unexpected resource access token payload")
}
case EventTypeServiceHook:
Expand Down
41 changes: 24 additions & 17 deletions event_webhook_types.go
Expand Up @@ -131,7 +131,7 @@ type CommitCommentEvent struct {
} `json:"commit"`
}

// DeploymentEvent represents a deployment event
// DeploymentEvent represents a deployment event.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#deployment-events
Expand Down Expand Up @@ -171,7 +171,7 @@ type DeploymentEvent struct {
CommitTitle string `json:"commit_title"`
}

// FeatureFlagEvent represents a feature flag event
// FeatureFlagEvent represents a feature flag event.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#feature-flag-events
Expand Down Expand Up @@ -205,7 +205,8 @@ type FeatureFlagEvent struct {
} `json:"object_attributes"`
}

// GroupResourceAccessTokenEvent represents a resource access token event for a group
// GroupResourceAccessTokenEvent represents a resource access token event for a
// group.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#project-and-group-access-token-events
Expand All @@ -219,16 +220,13 @@ type GroupResourceAccessTokenEvent struct {
GroupPath string `json:"group_path"`
FullPath string `json:"full_path"`
} `json:"group"`
ObjectAttributes *ResourceAccessToken `json:"object_attributes"`
}

// ResourceAccessToken represents an access token record.
type ResourceAccessToken struct {
ID int `json:"id"`
UserID int `json:"user_id"`
Name string `json:"name"`
CreatedAt *time.Time `json:"created_at"`
ExpiresAt *ISOTime `json:"expires_at"`
ObjectAttributes struct {
ID int `json:"id"`
UserID int `json:"user_id"`
Name string `json:"name"`
CreatedAt *time.Time `json:"created_at"`
ExpiresAt *ISOTime `json:"expires_at"`
} `json:"object_attributes"`
}

// IssueCommentEvent represents a comment on an issue event.
Expand Down Expand Up @@ -763,7 +761,8 @@ type MergeEvent struct {
Reviewers []*EventUser `json:"reviewers"`
}

// EventUser represents a user record in an event and is used as an even initiator or a merge assignee.
// EventUser represents a user record in an event and is used as an even
// initiator or a merge assignee.
type EventUser struct {
ID int `json:"id"`
Name string `json:"name"`
Expand All @@ -779,7 +778,8 @@ type MergeParams struct {

// UnmarshalJSON decodes the merge parameters
//
// This allows support of ForceRemoveSourceBranch for both type bool (>11.9) and string (<11.9)
// This allows support of ForceRemoveSourceBranch for both type
// bool (>11.9) and string (<11.9)
func (p *MergeParams) UnmarshalJSON(b []byte) error {
type Alias MergeParams
raw := struct {
Expand Down Expand Up @@ -925,7 +925,8 @@ type PipelineEvent struct {
} `json:"builds"`
}

// ProjectResourceAccessTokenEvent represents a resource access token event for a project
// ProjectResourceAccessTokenEvent represents a resource access token event for
// a project.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#project-and-group-access-token-events
Expand All @@ -951,7 +952,13 @@ type ProjectResourceAccessTokenEvent struct {
SSHURL string `json:"ssh_url"`
HTTPURL string `json:"http_url"`
} `json:"project"`
ObjectAttributes ResourceAccessToken `json:"object_attributes"`
ObjectAttributes struct {
ID int `json:"id"`
UserID int `json:"user_id"`
Name string `json:"name"`
CreatedAt *time.Time `json:"created_at"`
ExpiresAt *ISOTime `json:"expires_at"`
} `json:"object_attributes"`
}

// PushEvent represents a push event.
Expand Down
26 changes: 12 additions & 14 deletions event_webhook_types_test.go
Expand Up @@ -312,20 +312,19 @@ func TestGroupResourceAccessTokenEventUnmarshal(t *testing.T) {
GroupID: 35,
ObjectKind: "access_token",
EventName: "expiring_access_token",
ObjectAttributes: &ResourceAccessToken{
ID: 25,
UserID: 90,
Name: "acd",
CreatedAt: &createdAt,
ExpiresAt: &expiresAt,
},
}

expected.Group.GroupID = 35
expected.Group.GroupName = "Twitter"
expected.Group.GroupPath = "twitter"
expected.Group.FullPath = "twitter"

expected.ObjectAttributes.ID = 25
expected.ObjectAttributes.UserID = 90
expected.ObjectAttributes.Name = "acd"
expected.ObjectAttributes.CreatedAt = &createdAt
expected.ObjectAttributes.ExpiresAt = &expiresAt

assert.Equal(t, expected, event)
}

Expand Down Expand Up @@ -1083,15 +1082,14 @@ func TestProjectResourceAccessTokenEventUnmarshal(t *testing.T) {
ProjectID: 7,
ObjectKind: "access_token",
EventName: "expiring_access_token",
ObjectAttributes: ResourceAccessToken{
ID: 25,
UserID: 90,
Name: "acd",
CreatedAt: &createdAt,
ExpiresAt: &expiresAt,
},
}

expected.ObjectAttributes.ID = 25
expected.ObjectAttributes.UserID = 90
expected.ObjectAttributes.Name = "acd"
expected.ObjectAttributes.CreatedAt = &createdAt
expected.ObjectAttributes.ExpiresAt = &expiresAt

expected.Project.ID = 7
expected.Project.Name = "Flight"
expected.Project.Description = "Eum dolore maxime atque reprehenderit voluptatem."
Expand Down

0 comments on commit c187a2d

Please sign in to comment.