From ac88b88cbb64753ec0f5d4c7f68feb657f050d99 Mon Sep 17 00:00:00 2001 From: guoxudong Date: Tue, 21 Dec 2021 20:51:48 +0800 Subject: [PATCH 1/2] add slack slash commands integration --- services.go | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ services_test.go | 51 +++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) diff --git a/services.go b/services.go index 67b221f95..57be2cbba 100644 --- a/services.go +++ b/services.go @@ -1331,6 +1331,95 @@ func (s *ServicesService) DeleteSlackService(pid interface{}, options ...Request return s.client.Do(req, nil) } +// SlackSlashCommandsService represents Slack slash commands settings. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/integrations.html#slack-slash-commands +type SlackSlashCommandsService struct { + Service + Properties *SlackSlashCommandsProperties `json:"properties"` +} + +// SlackSlashCommandsProperties represents Slack slash commands specific properties. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/integrations.html#slack-slash-commands +type SlackSlashCommandsProperties struct { + Token string `json:"token"` +} + +// GetSlackSlashCommandsService gets Slack slash commands service settings for a project. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/integrations.html#get-slack-slash-command-integration-settings +func (s *ServicesService) GetSlackSlashCommandsService(pid interface{}, options ...RequestOptionFunc) (*SlackSlashCommandsService, *Response, error) { + project, err := parseID(pid) + if err != nil { + return nil, nil, err + } + u := fmt.Sprintf("projects/%s/services/slack-slash-commands", PathEscape(project)) + + req, err := s.client.NewRequest(http.MethodGet, u, nil, options) + if err != nil { + return nil, nil, err + } + + svc := new(SlackSlashCommandsService) + resp, err := s.client.Do(req, svc) + if err != nil { + return nil, resp, err + } + + return svc, resp, err +} + +// SetSlackSlashCommandsServiceOptions represents the available SetSlackSlashCommandsService() +// options. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/services.html#createedit-slack-slash-command-service +type SetSlackSlashCommandsServiceOptions struct { + Token *string `json:"token"` +} + +// SetSlackSlashCommandsService sets Slack slash commands service for a project +// +// GitLab API docs: +// https://docs.gitlab.com/13.12/ee/api/services.html#createedit-slack-slash-command-service +func (s *ServicesService) SetSlackSlashCommandsService(pid interface{}, opt *SetSlackSlashCommandsServiceOptions, options ...RequestOptionFunc) (*Response, error) { + project, err := parseID(pid) + if err != nil { + return nil, err + } + u := fmt.Sprintf("projects/%s/services/slack-slash-commands", PathEscape(project)) + + req, err := s.client.NewRequest(http.MethodPut, u, opt, options) + if err != nil { + return nil, err + } + + return s.client.Do(req, nil) +} + +// DeleteSlackSlashCommandsService deletes Slack slash commands service for project. +// +// GitLab API docs: +// https://docs.gitlab.com/13.12/ee/api/services.html#delete-slack-slash-command-service +func (s *ServicesService) DeleteSlackSlashCommandsService(pid interface{}, options ...RequestOptionFunc) (*Response, error) { + project, err := parseID(pid) + if err != nil { + return nil, err + } + u := fmt.Sprintf("projects/%s/services/slack-slash-commands", PathEscape(project)) + + req, err := s.client.NewRequest(http.MethodDelete, u, nil, options) + if err != nil { + return nil, err + } + + return s.client.Do(req, nil) +} + // YouTrackService represents YouTrack service settings. // // GitLab API docs: diff --git a/services_test.go b/services_test.go index 7c49428e9..02d41876d 100644 --- a/services_test.go +++ b/services_test.go @@ -524,3 +524,54 @@ func TestDeleteYouTrackService(t *testing.T) { t.Fatalf("Services.DeleteYouTrackService returns an error: %v", err) } } + +func TestGetSlackSlashCommandsService(t *testing.T) { + mux, server, client := setup(t) + defer teardown(server) + + mux.HandleFunc("/api/v4/projects/1/services/slack-slash-commands", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodGet) + fmt.Fprint(w, `{"id":1}`) + }) + want := &SlackSlashCommandsService{Service: Service{ID: 1}} + + service, _, err := client.Services.GetSlackSlashCommandsService(1) + if err != nil { + t.Fatalf("Services.GetSlackSlashCommandsService returns an error: %v", err) + } + if !reflect.DeepEqual(want, service) { + t.Errorf("Services.GetSlackSlashCommandsService returned %+v, want %+v", service, want) + } +} + +func TestSetSlackSlashCommandsService(t *testing.T) { + mux, server, client := setup(t) + defer teardown(server) + + mux.HandleFunc("/api/v4/projects/1/services/slack-slash-commands", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPut) + }) + + opt := &SetSlackSlashCommandsServiceOptions{ + Token: String("token"), + } + + _, err := client.Services.SetSlackSlashCommandsService(1, opt) + if err != nil { + t.Fatalf("Services.SetSlackSlashCommandsService returns an error: %v", err) + } +} + +func TestDeleteSlackSlashCommandsService(t *testing.T) { + mux, server, client := setup(t) + defer teardown(server) + + mux.HandleFunc("/api/v4/projects/1/services/slack-slash-commands", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodDelete) + }) + + _, err := client.Services.DeleteSlackSlashCommandsService(1) + if err != nil { + t.Fatalf("Services.DeleteSlackSlashCommandsService returns an error: %v", err) + } +} From ac2a1dfbd54422df06b2bc53d584d7f4d95428d9 Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Wed, 29 Dec 2021 10:55:32 +0100 Subject: [PATCH 2/2] Add missing option tags --- services.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services.go b/services.go index 57be2cbba..2abcdc737 100644 --- a/services.go +++ b/services.go @@ -38,6 +38,7 @@ type ServicesService struct { type Service struct { ID int `json:"id"` Title string `json:"title"` + Slug string `json:"slug"` CreatedAt *time.Time `json:"created_at"` UpdatedAt *time.Time `json:"updated_at"` Active bool `json:"active"` @@ -1379,7 +1380,7 @@ func (s *ServicesService) GetSlackSlashCommandsService(pid interface{}, options // GitLab API docs: // https://docs.gitlab.com/ee/api/services.html#createedit-slack-slash-command-service type SetSlackSlashCommandsServiceOptions struct { - Token *string `json:"token"` + Token *string `url:"token,omitempty" json:"token,omitempty"` } // SetSlackSlashCommandsService sets Slack slash commands service for a project