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

Commit

Permalink
Merge pull request #1329 from sunny0826/slack-slash-commands
Browse files Browse the repository at this point in the history
add slack slash commands integration
  • Loading branch information
svanharmelen authored Dec 29, 2021
2 parents ce70510 + ac2a1df commit 5e9db4a
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
90 changes: 90 additions & 0 deletions services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -1331,6 +1332,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 `url:"token,omitempty" json:"token,omitempty"`
}

// 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:
Expand Down
51 changes: 51 additions & 0 deletions services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 5e9db4a

Please sign in to comment.