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

Commit

Permalink
Add Telegram service
Browse files Browse the repository at this point in the history
  • Loading branch information
alxrem committed Dec 16, 2023
1 parent e69c57e commit 3ed1f99
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 0 deletions.
102 changes: 102 additions & 0 deletions services.go
Original file line number Diff line number Diff line change
Expand Up @@ -1910,3 +1910,105 @@ func (s *ServicesService) DeleteYouTrackService(pid interface{}, options ...Requ

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

// TelegramService represents Telegram service settings.
//
// Gitlab API docs:
// https://docs.gitlab.com/ee/api/integrations.html#telegram
type TelegramService struct {
Service
Properties *TelegramServiceProperties `json:"properties"`
}

// TelegramServiceProperties represents Telegram specific properties.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/integrations.html#set-up-telegram
type TelegramServiceProperties struct {
Room string `json:"room"`
NotifyOnlyBrokenPipelines bool `json:"notify_only_broken_pipelines"`
BranchesToBeNotified string `json:"branches_to_be_notified"`
}

// GetTelegramService gets MicrosoftTeams service settings for a project.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/integrations.html#get-telegram-settings
func (s *ServicesService) GetTelegramService(pid interface{}, options ...RequestOptionFunc) (*TelegramService, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/services/telegram", PathEscape(project))

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

svc := new(TelegramService)
resp, err := s.client.Do(req, svc)
if err != nil {
return nil, resp, err
}

return svc, resp, nil
}

// SetTelegramServiceOptions represents the available SetTelegramService()
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/integrations.html#set-up-telegram
type SetTelegramServiceOptions struct {
Token *string `url:"token,omitempty" json:"token,omitempty"`
Room *string `url:"room,omitempty" json:"room,omitempty"`
NotifyOnlyBrokenPipelines *bool `url:"notify_only_broken_pipelines,omitempty" json:"notify_only_broken_pipelines,omitempty"`
BranchesToBeNotified *string `url:"branches_to_be_notified,omitempty" json:"branches_to_be_notified,omitempty"`
PushEvents *bool `url:"push_events,omitempty" json:"push_events,omitempty"`
IssuesEvents *bool `url:"issues_events,omitempty" json:"issues_events,omitempty"`
ConfidentialIssuesEvents *bool `url:"confidential_issues_events,omitempty" json:"confidential_issues_events,omitempty"`
MergeRequestsEvents *bool `url:"merge_requests_events,omitempty" json:"merge_requests_events,omitempty"`
TagPushEvents *bool `url:"tag_push_events,omitempty" json:"tag_push_events,omitempty"`
NoteEvents *bool `url:"note_events,omitempty" json:"note_events,omitempty"`
ConfidentialNoteEvents *bool `url:"confidential_note_events,omitempty" json:"confidential_note_events,omitempty"`
PipelineEvents *bool `url:"pipeline_events,omitempty" json:"pipeline_events,omitempty"`
WikiPageEvents *bool `url:"wiki_page_events,omitempty" json:"wiki_page_events,omitempty"`
}

// SetTelegramService sets Telegram service for a project
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/integrations.html#set-up-telegram
func (s *ServicesService) SetTelegramService(pid interface{}, opt *SetTelegramServiceOptions, options ...RequestOptionFunc) (*Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, err
}
u := fmt.Sprintf("projects/%s/services/telegram", PathEscape(project))

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

// DeleteTelegramService deletes Telegram service for project.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/integrations.html#disable-telegram
func (s *ServicesService) DeleteTelegramService(pid interface{}, options ...RequestOptionFunc) (*Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, err
}
u := fmt.Sprintf("projects/%s/services/telegram", PathEscape(project))

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

return s.client.Do(req, nil)
}
122 changes: 122 additions & 0 deletions services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
"reflect"
"testing"
"time"
)

func TestListServices(t *testing.T) {
Expand Down Expand Up @@ -774,3 +775,124 @@ func TestDeleteMattermostSlashCommandsService(t *testing.T) {
t.Fatalf("Services.DeleteMattermostSlashCommandsService returns an error: %v", err)
}
}

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

mux.HandleFunc("/api/v4/projects/1/services/telegram", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `
{
"id": 1,
"title": "Telegram",
"slug": "telegram",
"created_at": "2023-12-16T20:21:03.117Z",
"updated_at": "2023-12-16T20:22:19.140Z",
"active": true,
"commit_events": true,
"push_events": false,
"issues_events": false,
"incident_events": false,
"alert_events": true,
"confidential_issues_events": false,
"merge_requests_events": false,
"tag_push_events": false,
"deployment_events": false,
"note_events": false,
"confidential_note_events": false,
"pipeline_events": true,
"wiki_page_events": false,
"job_events": true,
"comment_on_event_enabled": true,
"vulnerability_events": false,
"properties": {
"room": "-1000000000000",
"notify_only_broken_pipelines": false,
"branches_to_be_notified": "all"
}
}
`)
})
wantCreatedAt, _ := time.Parse(time.RFC3339, "2023-12-16T20:21:03.117Z")
wantUpdatedAt, _ := time.Parse(time.RFC3339, "2023-12-16T20:22:19.140Z")
want := &TelegramService{
Service: Service{
ID: 1,
Title: "Telegram",
Slug: "telegram",
CreatedAt: &wantCreatedAt,
UpdatedAt: &wantUpdatedAt,
Active: true,
CommitEvents: true,
PushEvents: false,
IssuesEvents: false,
AlertEvents: true,
ConfidentialIssuesEvents: false,
MergeRequestsEvents: false,
TagPushEvents: false,
DeploymentEvents: false,
NoteEvents: false,
ConfidentialNoteEvents: false,
PipelineEvents: true,
WikiPageEvents: false,
JobEvents: true,
CommentOnEventEnabled: true,
VulnerabilityEvents: false,
},
Properties: &TelegramServiceProperties{
Room: "-1000000000000",
NotifyOnlyBrokenPipelines: false,
BranchesToBeNotified: "all",
},
}

service, _, err := client.Services.GetTelegramService(1)
if err != nil {
t.Fatalf("Services.GetTelegramService returns an error: %v", err)
}
if !reflect.DeepEqual(want, service) {
t.Errorf("Services.GetTelegramService returned %+v, want %+v", service, want)
}
}

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

mux.HandleFunc("/api/v4/projects/1/services/telegram", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)
})

opt := &SetTelegramServiceOptions{
Token: Ptr("token"),
Room: Ptr("-1000"),
NotifyOnlyBrokenPipelines: Ptr(true),
BranchesToBeNotified: Ptr("all"),
PushEvents: Ptr(true),
IssuesEvents: Ptr(true),
ConfidentialIssuesEvents: Ptr(true),
MergeRequestsEvents: Ptr(true),
TagPushEvents: Ptr(true),
NoteEvents: Ptr(true),
ConfidentialNoteEvents: Ptr(true),
PipelineEvents: Ptr(true),
WikiPageEvents: Ptr(true),
}

_, err := client.Services.SetTelegramService(1, opt)
if err != nil {
t.Fatalf("Services.SetTelegramService returns an error: %v", err)
}
}

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

mux.HandleFunc("/api/v4/projects/1/services/telegram", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})

_, err := client.Services.DeleteTelegramService(1)
if err != nil {
t.Fatalf("Services.DeleteTelegramService returns an error: %v", err)
}
}

0 comments on commit 3ed1f99

Please sign in to comment.