Commit 93c0833f authored by Igor Zibarev's avatar Igor Zibarev Committed by Sander van Harmelen

Supplement Slack service (#438)

* Supplement Slack service

Refs:
- https://docs.gitlab.com/ce/api/services.html#slack-notifications
- #142

* gitlab: move BoolValue type and associated methods to gitlab.go

* services: update slack service: add url tags, fix typo, remove pointers in properties

* Revert renaming webhook field
parent 537e392d
...@@ -842,3 +842,24 @@ func MergeMethod(v MergeMethodValue) *MergeMethodValue { ...@@ -842,3 +842,24 @@ func MergeMethod(v MergeMethodValue) *MergeMethodValue {
*p = v *p = v
return p return p
} }
// BoolValue is a boolean value with advanced json unmarshaling features.
type BoolValue bool
// UnmarshalJSON allows 1 and 0 to be considered as boolean values
// Needed for https://gitlab.com/gitlab-org/gitlab-ce/issues/50122
func (t *BoolValue) UnmarshalJSON(b []byte) error {
switch string(b) {
case `"1"`:
*t = true
return nil
case `"0"`:
*t = false
return nil
default:
var v bool
err := json.Unmarshal(b, &v)
*t = BoolValue(v)
return err
}
}
...@@ -2,6 +2,7 @@ package gitlab ...@@ -2,6 +2,7 @@ package gitlab
import ( import (
"context" "context"
"encoding/json"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
...@@ -125,3 +126,40 @@ func TestRequestWithContext(t *testing.T) { ...@@ -125,3 +126,40 @@ func TestRequestWithContext(t *testing.T) {
t.Fatal("Context was not set correctly") t.Fatal("Context was not set correctly")
} }
} }
func TestBoolValue(t *testing.T) {
testCases := map[string]struct {
data []byte
expected bool
}{
"should unmarshal true as true": {
data: []byte("true"),
expected: true,
},
"should unmarshal false as true": {
data: []byte("false"),
expected: false,
},
"should unmarshal \"1\" as true": {
data: []byte(`"1"`),
expected: true,
},
"should unmarshal \"0\" as false": {
data: []byte(`"0"`),
expected: false,
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
var b BoolValue
if err := json.Unmarshal(testCase.data, &b); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if bool(b) != testCase.expected {
t.Fatalf("Expected %v but got %v", testCase.expected, b)
}
})
}
}
...@@ -45,8 +45,10 @@ type Service struct { ...@@ -45,8 +45,10 @@ type Service struct {
MergeRequestsEvents bool `json:"merge_requests_events"` MergeRequestsEvents bool `json:"merge_requests_events"`
TagPushEvents bool `json:"tag_push_events"` TagPushEvents bool `json:"tag_push_events"`
NoteEvents bool `json:"note_events"` NoteEvents bool `json:"note_events"`
ConfidentialNoteEvents bool `json:"confidential_note_events"`
PipelineEvents bool `json:"pipeline_events"` PipelineEvents bool `json:"pipeline_events"`
JobEvents bool `json:"job_events"` JobEvents bool `json:"job_events"`
WikiPageEvents bool `json:"wiki_page_events"`
} }
// SetGitLabCIServiceOptions represents the available SetGitLabCIService() // SetGitLabCIServiceOptions represents the available SetGitLabCIService()
...@@ -252,7 +254,28 @@ type SlackService struct { ...@@ -252,7 +254,28 @@ type SlackService struct {
// GitLab API docs: // GitLab API docs:
// https://docs.gitlab.com/ce/api/services.html#slack // https://docs.gitlab.com/ce/api/services.html#slack
type SlackServiceProperties struct { type SlackServiceProperties struct {
NotifyOnlyBrokenPipelines bool `json:"notify_only_broken_pipelines"` // Note: NotifyOnlyBrokenPipelines and NotifyOnlyDefaultBranch are not
// just "bool" because in some cases gitlab returns
// "notify_only_broken_pipelines": true, and in other cases
// "notify_only_broken_pipelines": "1". The same is for
// "notify_only_default_branch" field.
// We need to handle this, until the bug will be fixed.
// Ref: https://gitlab.com/gitlab-org/gitlab-ce/issues/50122
NotifyOnlyBrokenPipelines BoolValue `url:"notify_only_broken_pipelines,omitempty" json:"notify_only_broken_pipelines,omitempty"`
NotifyOnlyDefaultBranch BoolValue `url:"notify_only_default_branch,omitempty" json:"notify_only_default_branch,omitempty"`
WebHook string `url:"webhook,omitempty" json:"webhook,omitempty"`
Username string `url:"username,omitempty" json:"username,omitempty"`
Channel string `url:"channel,omitempty" json:"channel,omitempty"`
PushChannel string `url:"push_channel,omitempty" json:"push_channel,omitempty"`
IssueChannel string `url:"issue_channel,omitempty" json:"issue_channel,omitempty"`
ConfidentialIssueChannel string `url:"confidential_issue_channel,omitempty" json:"confidential_issue_channel,omitempty"`
MergeRequestChannel string `url:"merge_request_channel,omitempty" json:"merge_request_channel,omitempty"`
NoteChannel string `url:"note_channel,omitempty" json:"note_channel,omitempty"`
ConfidentialNoteChannel string `url:"confidential_note_channel,omitempty" json:"confidential_note_channel,omitempty"`
TagPushChannel string `url:"tag_push_channel,omitempty" json:"tag_push_channel,omitempty"`
PipelineChannel string `url:"pipeline_channel,omitempty" json:"pipeline_channel,omitempty"`
WikiPageChannel string `url:"wiki_page_channel,omitempty" json:"wiki_page_channel,omitempty"`
} }
// GetSlackService gets Slack service settings for a project. // GetSlackService gets Slack service settings for a project.
...@@ -286,9 +309,32 @@ func (s *ServicesService) GetSlackService(pid interface{}, options ...OptionFunc ...@@ -286,9 +309,32 @@ func (s *ServicesService) GetSlackService(pid interface{}, options ...OptionFunc
// GitLab API docs: // GitLab API docs:
// https://docs.gitlab.com/ce/api/services.html#edit-slack-service // https://docs.gitlab.com/ce/api/services.html#edit-slack-service
type SetSlackServiceOptions struct { type SetSlackServiceOptions struct {
WebHook *string `url:"webhook,omitempty" json:"webhook,omitempty" ` WebHook *string `url:"webhook,omitempty" json:"webhook,omitempty"`
Username *string `url:"username,omitempty" json:"username,omitempty" ` Username *string `url:"username,omitempty" json:"username,omitempty"`
Channel *string `url:"channel,omitempty" json:"channel,omitempty"` Channel *string `url:"channel,omitempty" json:"channel,omitempty"`
NotifyOnlyBrokenPipelines *bool `url:"notify_only_broken_pipelines,omitempty" json:"notify_only_broken_pipelines,omitempty"`
NotifyOnlyDefaultBranch *bool `url:"notify_only_default_branch,omitempty" json:"notify_only_default_branch,omitempty"`
PushEvents *bool `url:"push_events,omitempty" json:"push_events,omitempty"`
PushChannel *string `url:"push_channel,omitempty" json:"push_channel,omitempty"`
IssuesEvents *bool `url:"issues_events,omitempty" json:"issues_events,omitempty"`
IssueChannel *string `url:"issue_channel,omitempty" json:"issue_channel,omitempty"`
ConfidentialIssuesEvents *bool `url:"confidential_issues_events,omitempty" json:"confidential_issues_events,omitempty"`
ConfidentialIssueChannel *string `url:"confidential_issue_channel,omitempty" json:"confidential_issue_channel,omitempty"`
MergeRequestsEvents *bool `url:"merge_requests_events,omitempty" json:"merge_requests_events,omitempty"`
MergeRequestChannel *string `url:"merge_request_channel,omitempty" json:"merge_request_channel,omitempty"`
TagPushEvents *bool `url:"tag_push_events,omitempty" json:"tag_push_events,omitempty"`
TagPushChannel *string `url:"tag_push_channel,omitempty" json:"tag_push_channel,omitempty"`
NoteEvents *bool `url:"note_events,omitempty" json:"note_events,omitempty"`
NoteChannel *string `url:"note_channel,omitempty" json:"note_channel,omitempty"`
ConfidentialNoteEvents *bool `url:"confidential_note_events" json:"confidential_note_events"`
// TODO: Currently, GitLab ignores this option (not implemented yet?), so
// there is no way to set it. Uncomment when this is fixed.
// See: https://gitlab.com/gitlab-org/gitlab-ce/issues/49730
//ConfidentialNoteChannel *string `json:"confidential_note_channel,omitempty"`
PipelineEvents *bool `url:"pipeline_events,omitempty" json:"pipeline_events,omitempty"`
PipelineChannel *string `url:"pipeline_channel,omitempty" json:"pipeline_channel,omitempty"`
WikiPageChannel *string `url:"wiki_page_channel,omitempty" json:"wiki_page_channel,omitempty"`
WikiPageEvents *bool `url:"wiki_page_events" json:"wiki_page_events"`
} }
// SetSlackService sets Slack service for a project // SetSlackService sets Slack service for a project
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment