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

Add TriggerTestGroupHook #2046

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions group_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,25 @@ func (s *GroupsService) DeleteGroupHook(pid interface{}, hook int, options ...Re
return s.client.Do(req, nil)
}

// TriggerTestGroupHook triggers a test hook for a specified group.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_webhooks.html#trigger-a-test-group-hook
func (s *GroupsService) TriggerTestGroupHook(pid interface{}, hook int, trigger GroupHookTrigger, options ...RequestOptionFunc) (*Response, error) {
group, err := parseID(pid)
if err != nil {
return nil, err
}
u := fmt.Sprintf("groups/%s/hooks/%d/test/%s", PathEscape(group), hook, trigger)

req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
if err != nil {
return nil, err
}

return s.client.Do(req, nil)
}

// SetGroupCustomHeader creates or updates a group custom webhook header.
//
// GitLab API docs:
Expand Down
72 changes: 72 additions & 0 deletions group_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,78 @@ func TestEditGroupHook(t *testing.T) {
}
}

func TestTriggerTestGroupHook(t *testing.T) {
mux, client := setup(t)

mux.HandleFunc("/api/v4/groups/1/hooks/1/test/push_events", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.WriteHeader(http.StatusCreated)
fmt.Fprint(w, `{"message":"201 Created"}`)
})

mux.HandleFunc("/api/v4/groups/1/hooks/1/test/invalid_trigger", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, `{"error": "trigger does not have a valid value"}`)
})

tests := []struct {
name string
groupID interface{}
hookID int
trigger GroupHookTrigger
wantErr bool
wantStatus int
wantErrMsg string
}{
{
name: "Valid trigger",
groupID: 1,
hookID: 1,
trigger: GroupHookTriggerPush,
wantErr: false,
wantStatus: http.StatusCreated,
},
{
name: "Invalid group ID",
groupID: "invalid",
hookID: 1,
trigger: GroupHookTriggerPush,
wantErr: true,
wantStatus: http.StatusNotFound,
},
{
name: "Invalid trigger type",
groupID: 1,
hookID: 1,
trigger: "invalid_trigger",
wantErr: true,
wantStatus: http.StatusBadRequest,
wantErrMsg: "trigger does not have a valid value",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp, err := client.Groups.TriggerTestGroupHook(tt.groupID, tt.hookID, tt.trigger)

if tt.wantErr {
assert.Error(t, err)
if tt.wantStatus != 0 {
assert.Equal(t, tt.wantStatus, resp.StatusCode)
}
if tt.wantErrMsg != "" {
assert.Contains(t, err.Error(), tt.wantErrMsg)
}
} else {
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, tt.wantStatus, resp.StatusCode)
}
})
}
}

func TestDeleteGroupHook(t *testing.T) {
mux, client := setup(t)

Expand Down
20 changes: 20 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,26 @@ func GenericPackageStatus(v GenericPackageStatusValue) *GenericPackageStatusValu
return Ptr(v)
}

// GroupHookTrigger represents the type of event to trigger for a group
// hook test.
type GroupHookTrigger string

// List of available group hook trigger types.
const (
GroupHookTriggerPush GroupHookTrigger = "push_events"
GroupHookTriggerTagPush GroupHookTrigger = "tag_push_events"
GroupHookTriggerIssue GroupHookTrigger = "issues_events"
GroupHookTriggerConfidentialIssue GroupHookTrigger = "confidential_issues_events"
GroupHookTriggerNote GroupHookTrigger = "note_events"
GroupHookTriggerMergeRequest GroupHookTrigger = "merge_requests_events"
GroupHookTriggerJob GroupHookTrigger = "job_events"
GroupHookTriggerPipeline GroupHookTrigger = "pipeline_events"
GroupHookTriggerWikiPage GroupHookTrigger = "wiki_page_events"
GroupHookTriggerRelease GroupHookTrigger = "releases_events"
GroupHookTriggerEmoji GroupHookTrigger = "emoji_events"
GroupHookTriggerResourceAccessToken GroupHookTrigger = "resource_access_token_events"
)

// ISOTime represents an ISO 8601 formatted date.
type ISOTime time.Time

Expand Down