Repository: xanzy/go-gitlab
Pull request #1395 (2022-02-28 05:02:18)

feat(gitlab integration): add integration of mattermost slash command…
re #1132


Patch:
diff --git a/services.go b/services.go
index 2abcdc7..99688da 100644
--- a/services.go
+++ b/services.go
@@ -1421,6 +1421,97 @@ func (s *ServicesService) DeleteSlackSlashCommandsService(pid interface{}, optio
 	return s.client.Do(req, nil)
 }
 
+// MattermostSlashCommandsService represents Mattermost slash commands settings.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/integrations.html#mattermost-slash-commands
+type MattermostSlashCommandsService struct {
+	Service
+	Properties *MattermostSlashCommandsProperties `json:"properties"`
+}
+
+// MattermostSlashCommandsProperties represents Mattermost slash commands specific properties.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/integrations.html#mattermost-slash-commands
+type MattermostSlashCommandsProperties struct {
+	Token    string `json:"token"`
+	Username string `json:"username,omitempty"`
+}
+
+// GetMattermostSlashCommandsService gets Slack Mattermost commands service settings for a project.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/integrations.html#get-mattermost-slash-command-integration-settings
+func (s *ServicesService) GetMattermostSlashCommandsService(pid interface{}, options ...RequestOptionFunc) (*MattermostSlashCommandsService, *Response, error) {
+	project, err := parseID(pid)
+	if err != nil {
+		return nil, nil, err
+	}
+	u := fmt.Sprintf("projects/%s/services/mattermost-slash-commands", PathEscape(project))
+
+	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	svc := new(MattermostSlashCommandsService)
+	resp, err := s.client.Do(req, svc)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return svc, resp, err
+}
+
+// SetMattermostSlashCommandsServiceOptions represents the available SetSlackSlashCommandsService()
+// options.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/integrations.html#get-mattermost-slash-command-integration-settings
+type SetMattermostSlashCommandsServiceOptions struct {
+	Token    *string `url:"token,omitempty" json:"token,omitempty"`
+	Username *string `url:"username,omitempty" json:"username,omitempty"`
+}
+
+// SetMattermostSlashCommandsService sets Mattermost slash commands service for a project
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/integrations.html#createedit-mattermost-slash-command-integration
+func (s *ServicesService) SetMattermostSlashCommandsService(pid interface{}, opt *SetMattermostSlashCommandsServiceOptions, options ...RequestOptionFunc) (*Response, error) {
+	project, err := parseID(pid)
+	if err != nil {
+		return nil, err
+	}
+	u := fmt.Sprintf("projects/%s/services/mattermost-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)
+}
+
+// DeleteMattermostSlashCommandsService deletes Mattermost slash commands service for project.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/integrations.html#disable-mattermost-slash-command-integration
+func (s *ServicesService) DeleteMattermostSlashCommandsService(pid interface{}, options ...RequestOptionFunc) (*Response, error) {
+	project, err := parseID(pid)
+	if err != nil {
+		return nil, err
+	}
+	u := fmt.Sprintf("projects/%s/services/mattermost-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:


Test patch:
diff --git a/services_test.go b/services_test.go
index 02d4187..864964a 100644
--- a/services_test.go
+++ b/services_test.go
@@ -575,3 +575,55 @@ func TestDeleteSlackSlashCommandsService(t *testing.T) {
 		t.Fatalf("Services.DeleteSlackSlashCommandsService returns an error: %v", err)
 	}
 }
+
+func TestGetMattermostSlashCommandsService(t *testing.T) {
+	mux, server, client := setup(t)
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/projects/1/services/mattermost-slash-commands", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, http.MethodGet)
+		fmt.Fprint(w, `{"id":1}`)
+	})
+	want := &MattermostSlashCommandsService{Service: Service{ID: 1}}
+
+	service, _, err := client.Services.GetMattermostSlashCommandsService(1)
+	if err != nil {
+		t.Fatalf("Services.mattermost-slash-commands returns an error: %v", err)
+	}
+	if !reflect.DeepEqual(want, service) {
+		t.Errorf("Services.mattermost-slash-commands returned %+v, want %+v", service, want)
+	}
+}
+
+func TestSetMattermostSlashCommandsService(t *testing.T) {
+	mux, server, client := setup(t)
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/projects/1/services/mattermost-slash-commands", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, http.MethodPut)
+	})
+
+	opt := &SetMattermostSlashCommandsServiceOptions{
+		Token:    String("token"),
+		Username: String("username"),
+	}
+
+	_, err := client.Services.SetMattermostSlashCommandsService(1, opt)
+	if err != nil {
+		t.Fatalf("Services.SetMattermostSlashCommandsService returns an error: %v", err)
+	}
+}
+
+func TestDeleteMattermostSlashCommandsService(t *testing.T) {
+	mux, server, client := setup(t)
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/projects/1/services/mattermost-slash-commands", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, http.MethodDelete)
+	})
+
+	_, err := client.Services.DeleteMattermostSlashCommandsService(1)
+	if err != nil {
+		t.Fatalf("Services.DeleteMattermostSlashCommandsService returns an error: %v", err)
+	}
+}