Verified Commit 91cb5f32 authored by José M. Requena Plens's avatar José M. Requena Plens Committed by GitLab
Browse files

feat(system_hooks): add seven fields GitLab sends to the Hook struct

Changelog: Improvements
parent 40d95c61
Loading
Loading
Loading
Loading
+9 −0
Changes for system_hooks.go: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -98,7 +98,14 @@ type Hook struct {
	MergeRequestsEvents    bool               `json:"merge_requests_events"`
	RepositoryUpdateEvents bool               `json:"repository_update_events"`
	EnableSSLVerification  bool               `json:"enable_ssl_verification"`
	OrganizationID         int64              `json:"organization_id"`
	AlertStatus            string             `json:"alert_status"`
	DisabledUntil          *time.Time         `json:"disabled_until"`
	URLVariables           []HookURLVariable  `json:"url_variables"`
	PushEventsBranchFilter string             `json:"push_events_branch_filter"`
	BranchFilterStrategy   string             `json:"branch_filter_strategy"`
	CustomWebhookTemplate  string             `json:"custom_webhook_template"`
	CustomHeaders          []HookCustomHeader `json:"custom_headers"`
	TokenPresent           bool               `json:"token_present"`
	SigningTokenPresent    bool               `json:"signing_token_present"`
}
@@ -132,6 +139,7 @@ type AddHookOptions struct {
	PushEvents             *bool                 `url:"push_events,omitempty" json:"push_events,omitempty"`
	PushEventsBranchFilter *string               `url:"push_events_branch_filter,omitempty" json:"push_events_branch_filter,omitempty"`
	BranchFilterStrategy   *BranchFilterStrategy `url:"branch_filter_strategy,omitempty" json:"branch_filter_strategy,omitempty"`
	CustomWebhookTemplate  *string               `url:"custom_webhook_template,omitempty" json:"custom_webhook_template,omitempty"`
	TagPushEvents          *bool                 `url:"tag_push_events,omitempty" json:"tag_push_events,omitempty"`
	MergeRequestsEvents    *bool                 `url:"merge_requests_events,omitempty" json:"merge_requests_events,omitempty"`
	RepositoryUpdateEvents *bool                 `url:"repository_update_events,omitempty" json:"repository_update_events,omitempty"`
@@ -161,6 +169,7 @@ type EditHookOptions struct {
	PushEvents             *bool                 `url:"push_events,omitempty" json:"push_events,omitempty"`
	PushEventsBranchFilter *string               `url:"push_events_branch_filter,omitempty" json:"push_events_branch_filter,omitempty"`
	BranchFilterStrategy   *BranchFilterStrategy `url:"branch_filter_strategy,omitempty" json:"branch_filter_strategy,omitempty"`
	CustomWebhookTemplate  *string               `url:"custom_webhook_template,omitempty" json:"custom_webhook_template,omitempty"`
	TagPushEvents          *bool                 `url:"tag_push_events,omitempty" json:"tag_push_events,omitempty"`
	MergeRequestsEvents    *bool                 `url:"merge_requests_events,omitempty" json:"merge_requests_events,omitempty"`
	RepositoryUpdateEvents *bool                 `url:"repository_update_events,omitempty" json:"repository_update_events,omitempty"`
+38 −1
Changes for system_hooks_test.go: 38 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -71,7 +71,14 @@ func TestSystemHooksService_GetHook(t *testing.T) {
			"merge_requests_events": true,
			"repository_update_events": true,
			"enable_ssl_verification":true,
			"url_variables":[{"key":"abc","value":"def"}]
			"organization_id":1,
			"alert_status":"executable",
			"disabled_until":"2016-11-30T12:32:15.192Z",
			"url_variables":[{"key":"abc","value":"def"}],
			"push_events_branch_filter":"my-branch-*",
			"branch_filter_strategy":"wildcard",
			"custom_webhook_template":"{\"event\":\"{{object_kind}}\"}",
			"custom_headers":[{"key":"X-Custom-Header"}]
		}`)
	})

@@ -79,6 +86,7 @@ func TestSystemHooksService_GetHook(t *testing.T) {
	require.NoError(t, err)

	createdAt := time.Date(2016, time.October, 31, 12, 32, 15, 192000000, time.UTC)
	disabledUntil := time.Date(2016, time.November, 30, 12, 32, 15, 192000000, time.UTC)
	want := &Hook{
		ID:                     1,
		URL:                    "https://gitlab.example.com/hook",
@@ -90,7 +98,16 @@ func TestSystemHooksService_GetHook(t *testing.T) {
		MergeRequestsEvents:    true,
		RepositoryUpdateEvents: true,
		EnableSSLVerification:  true,
		OrganizationID:         1,
		AlertStatus:            "executable",
		DisabledUntil:          &disabledUntil,
		URLVariables:           []HookURLVariable{{Key: "abc", Value: "def"}},
		PushEventsBranchFilter: "my-branch-*",
		BranchFilterStrategy:   "wildcard",
		CustomWebhookTemplate:  `{"event":"{{object_kind}}"}`,
		// GitLab renders custom headers through masked_custom_headers, which
		// sends the key alone and no value, so Value stays empty here.
		CustomHeaders: []HookCustomHeader{{Key: "X-Custom-Header"}},
	}
	require.Equal(t, want, hooks)
}
@@ -101,11 +118,18 @@ func TestSystemHooksService_AddHook(t *testing.T) {

	mux.HandleFunc("/api/v4/hooks", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodPost)
		testBodyJSON(t, r, map[string]any{
			"url":                     "https://gitlab.example.com/hook",
			"name":                    "Test Hook",
			"description":             "A test hook",
			"custom_webhook_template": `{"event":"{{object_kind}}"}`,
		})
		fmt.Fprint(w, `{
			"id": 1,
			"url": "https://gitlab.example.com/hook",
			"name": "Test Hook",
			"description": "A test hook",
			"custom_webhook_template": "{\"event\":\"{{object_kind}}\"}",
			"url_variables": [{"key": "abc", "value": "def"}]
		}`)
	})
@@ -114,6 +138,7 @@ func TestSystemHooksService_AddHook(t *testing.T) {
		URL:                   new("https://gitlab.example.com/hook"),
		Name:                  new("Test Hook"),
		Description:           new("A test hook"),
		CustomWebhookTemplate: new(`{"event":"{{object_kind}}"}`),
	}

	hook, _, err := client.SystemHooks.AddHook(opt)
@@ -124,6 +149,7 @@ func TestSystemHooksService_AddHook(t *testing.T) {
		URL:                   "https://gitlab.example.com/hook",
		Name:                  "Test Hook",
		Description:           "A test hook",
		CustomWebhookTemplate: `{"event":"{{object_kind}}"}`,
		URLVariables:          []HookURLVariable{{Key: "abc", Value: "def"}},
	}
	require.Equal(t, want, hook)
@@ -135,11 +161,20 @@ func TestSystemHooksService_EditHook(t *testing.T) {

	mux.HandleFunc("/api/v4/hooks/1", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodPut)
		testBodyJSON(t, r, map[string]any{
			"url":                     "https://gitlab.example.com/hook",
			"name":                    "Test Hook",
			"description":             "An updated test hook",
			"custom_webhook_template": `{"event":"{{object_kind}}"}`,
			"token":                   "supersecret",
			"signing_token":           "whsec_abc",
		})
		fmt.Fprint(w, `{
			"id": 1,
			"url": "https://gitlab.example.com/hook",
			"name": "Test Hook",
			"description": "An updated test hook",
			"custom_webhook_template": "{\"event\":\"{{object_kind}}\"}",
			"token_present": true,
			"signing_token_present": true,
			"url_variables": [{"key": "abc", "value": "def"}]
@@ -150,6 +185,7 @@ func TestSystemHooksService_EditHook(t *testing.T) {
		URL:                   new("https://gitlab.example.com/hook"),
		Name:                  new("Test Hook"),
		Description:           new("An updated test hook"),
		CustomWebhookTemplate: new(`{"event":"{{object_kind}}"}`),
		Token:                 new("supersecret"),
		SigningToken:          new("whsec_abc"),
	}
@@ -162,6 +198,7 @@ func TestSystemHooksService_EditHook(t *testing.T) {
		URL:                   "https://gitlab.example.com/hook",
		Name:                  "Test Hook",
		Description:           "An updated test hook",
		CustomWebhookTemplate: `{"event":"{{object_kind}}"}`,
		TokenPresent:          true,
		SigningTokenPresent:   true,
		URLVariables:          []HookURLVariable{{Key: "abc", Value: "def"}},