Commit 0e5c83e8 authored by Lorac's avatar Lorac Committed by Sander van Harmelen

Add broadcast messages API (#333)

parent c77a8230
......@@ -22,7 +22,7 @@ to add new and/or missing endpoints. Currently the following services are suppor
- [x] Award Emojis
- [x] Branches
- [ ] Broadcast Messages
- [x] Broadcast Messages
- [ ] Project-level Variables
- [ ] Group-level Variables
- [x] Commits
......
//
// Copyright 2018, Sander van Harmelen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package gitlab
import (
"fmt"
"time"
)
// BroadcastMessagesService handles communication with the broadcast
// messages methods of the GitLab API.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/broadcast_messages.html
type BroadcastMessagesService struct {
client *Client
}
// BroadcastMessage represents a GitLab issue board.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/broadcast_messages.html#get-all-broadcast-messages
type BroadcastMessage struct {
Message string `json:"message"`
StartsAt *time.Time `json:"starts_at"`
EndsAt *time.Time `json:"ends_at"`
Color string `json:"color"`
Font string `json:"font"`
ID int `json:"id"`
Active bool `json:"active"`
}
// ListBroadcastMessagesOptions represents the available ListBroadcastMessages()
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/broadcast_messages.html#get-all-broadcast-messages
type ListBroadcastMessagesOptions struct {
ListOptions
}
// ListBroadcastMessages gets a list of all broadcasted messages.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/broadcast_messages.html#get-all-broadcast-messages
func (s *BroadcastMessagesService) ListBroadcastMessages(opt *ListBroadcastMessagesOptions, options ...OptionFunc) ([]*BroadcastMessage, *Response, error) {
req, err := s.client.NewRequest("GET", "broadcast_messages", opt, options)
if err != nil {
return nil, nil, err
}
var bs []*BroadcastMessage
resp, err := s.client.Do(req, &bs)
if err != nil {
return nil, resp, err
}
return bs, resp, err
}
// GetBroadcastMessage gets a single broadcast message.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/broadcast_messages.html#get-a-specific-broadcast-message
func (s *BroadcastMessagesService) GetBroadcastMessage(broadcast int, options ...OptionFunc) (*BroadcastMessage, *Response, error) {
u := fmt.Sprintf("broadcast_messages/%d", broadcast)
req, err := s.client.NewRequest("GET", u, nil, options)
if err != nil {
return nil, nil, err
}
b := new(BroadcastMessage)
resp, err := s.client.Do(req, &b)
if err != nil {
return nil, resp, err
}
return b, resp, err
}
// CreateBroadcastMessageOptions represents the available CreateBroadcastMessage()
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/broadcast_messages.html#create-a-broadcast-message
type CreateBroadcastMessageOptions struct {
Message *string `url:"message" json:"message"`
StartsAt *time.Time `url:"starts_at,omitempty" json:"starts_at,omitempty"`
EndsAt *time.Time `url:"ends_at,omitempty" json:"ends_at,omitempty"`
Color *string `url:"color,omitempty" json:"color,omitempty"`
Font *string `url:"font,omitempty" json:"font,omitempty"`
}
// CreateBroadcastMessage creates a message to broadcast.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/broadcast_messages.html#create-a-broadcast-message
func (s *BroadcastMessagesService) CreateBroadcastMessage(opt *CreateBroadcastMessageOptions, options ...OptionFunc) (*BroadcastMessage, *Response, error) {
req, err := s.client.NewRequest("POST", "broadcast_messages", opt, options)
if err != nil {
return nil, nil, err
}
b := new(BroadcastMessage)
resp, err := s.client.Do(req, &b)
if err != nil {
return nil, resp, err
}
return b, resp, err
}
// UpdateBroadcastMessageOptions represents the available CreateBroadcastMessage()
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/broadcast_messages.html#update-a-broadcast-message
type UpdateBroadcastMessageOptions struct {
Message *string `url:"message,omitempty" json:"message,omitempty"`
StartsAt *time.Time `url:"starts_at,omitempty" json:"starts_at,omitempty"`
EndsAt *time.Time `url:"ends_at,omitempty" json:"ends_at,omitempty"`
Color *string `url:"color,omitempty" json:"color,omitempty"`
Font *string `url:"font,omitempty" json:"font,omitempty"`
}
// UpdateBroadcastMessage update a broadcasted message.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/broadcast_messages.html#update-a-broadcast-message
func (s *BroadcastMessagesService) UpdateBroadcastMessage(broadcast int, opt *UpdateBroadcastMessageOptions, options ...OptionFunc) (*BroadcastMessage, *Response, error) {
u := fmt.Sprintf("broadcast_messages/%d", broadcast)
req, err := s.client.NewRequest("PUT", u, opt, options)
if err != nil {
return nil, nil, err
}
b := new(BroadcastMessage)
resp, err := s.client.Do(req, &b)
if err != nil {
return nil, resp, err
}
return b, resp, err
}
// DeleteBroadcastMessage deletes a broadcasted message.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/broadcast_messages.html#delete-a-broadcast-message
func (s *BroadcastMessagesService) DeleteBroadcastMessage(broadcast int, options ...OptionFunc) (*Response, error) {
u := fmt.Sprintf("broadcast_messages/%d", broadcast)
req, err := s.client.NewRequest("DELETE", u, nil, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
package gitlab
import (
"fmt"
"net/http"
"reflect"
"testing"
"time"
)
func TestListBroadcastMessages(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/broadcast_messages", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprintf(w, `[{
"message": "Some Message",
"starts_at": "2017-06-26T06:00:00.000Z",
"ends_at": "2017-06-27T12:59:00.000Z",
"color": "#E75E40",
"font": "#FFFFFF",
"id": 1,
"active": false
},{
"message": "SomeMessage2",
"starts_at": "2015-04-27T06:43:00.000Z",
"ends_at": "2015-04-28T20:43:00.000Z",
"color": "#AA33EE",
"font": "#224466",
"id": 2,
"active": true
}]`)
})
got, _, err := client.BroadcastMessage.ListBroadcastMessages(nil, nil)
if err != nil {
t.Errorf("ListBroadcastMessages returned error: %v", err)
}
wantedFirstStartsAt := time.Date(2017, 06, 26, 6, 0, 0, 0, time.UTC)
wantedFirstEndsAt := time.Date(2017, 06, 27, 12, 59, 0, 0, time.UTC)
wantedSecondStartsAt := time.Date(2015, 04, 27, 6, 43, 0, 0, time.UTC)
wantedSecondEndsAt := time.Date(2015, 04, 28, 20, 43, 0, 0, time.UTC)
want := []*BroadcastMessage{{
Message: "Some Message",
StartsAt: &wantedFirstStartsAt,
EndsAt: &wantedFirstEndsAt,
Color: "#E75E40",
Font: "#FFFFFF",
ID: 1,
Active: false,
}, {
Message: "SomeMessage2",
StartsAt: &wantedSecondStartsAt,
EndsAt: &wantedSecondEndsAt,
Color: "#AA33EE",
Font: "#224466",
ID: 2,
Active: true,
}}
if !reflect.DeepEqual(got, want) {
t.Errorf("ListBroadcastMessages returned \ngot:\n%v\nwant:\n%v", Stringify(got), Stringify(want))
}
}
func TestGetBroadcastMessages(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/broadcast_messages/1/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprintf(w, `{
"message": "Some Message",
"starts_at": "2017-06-26T06:00:00.000Z",
"ends_at": "2017-06-27T12:59:00.000Z",
"color": "#E75E40",
"font": "#FFFFFF",
"id": 1,
"active": false
}`)
})
got, _, err := client.BroadcastMessage.GetBroadcastMessage(1)
if err != nil {
t.Errorf("GetBroadcastMessage returned error: %v", err)
}
wantedStartsAt := time.Date(2017, time.June, 26, 6, 0, 0, 0, time.UTC)
wantedEndsAt := time.Date(2017, time.June, 27, 12, 59, 0, 0, time.UTC)
want := &BroadcastMessage{
Message: "Some Message",
StartsAt: &wantedStartsAt,
EndsAt: &wantedEndsAt,
Color: "#E75E40",
Font: "#FFFFFF",
ID: 1,
Active: false,
}
if !reflect.DeepEqual(got, want) {
t.Errorf("GetBroadcastMessage returned \ngot:\n%v\nwant:\n%v", Stringify(got), Stringify(want))
}
}
func TestCreateBroadcastMessages(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
wantedStartsAt := time.Date(2017, time.June, 26, 6, 0, 0, 0, time.UTC)
wantedEndsAt := time.Date(2017, time.June, 27, 12, 59, 0, 0, time.UTC)
mux.HandleFunc("/broadcast_messages", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
fmt.Fprintf(w, `{
"message": "Some Message",
"starts_at": "2017-06-26T06:00:00.000Z",
"ends_at": "2017-06-27T12:59:00.000Z",
"color": "#E75E40",
"font": "#FFFFFF",
"id": 42,
"active": false
}`)
})
opt := &CreateBroadcastMessageOptions{
Message: String("Some Message"),
StartsAt: &wantedStartsAt,
EndsAt: &wantedEndsAt,
Color: String("#E75E40"),
Font: String("#FFFFFF"),
}
got, _, err := client.BroadcastMessage.CreateBroadcastMessage(opt)
if err != nil {
t.Errorf("CreateBroadcastMessage returned error: %v", err)
}
want := &BroadcastMessage{
Message: "Some Message",
StartsAt: &wantedStartsAt,
EndsAt: &wantedEndsAt,
Color: "#E75E40",
Font: "#FFFFFF",
ID: 42,
Active: false,
}
if !reflect.DeepEqual(got, want) {
t.Errorf("CreateBroadcastMessage returned \ngot:\n%v\nwant:\n%v", Stringify(got), Stringify(want))
}
}
func TestUpdateBroadcastMessages(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
wantedStartsAt := time.Date(2017, time.June, 26, 6, 0, 0, 0, time.UTC)
wantedEndsAt := time.Date(2017, time.June, 27, 12, 59, 0, 0, time.UTC)
mux.HandleFunc("/broadcast_messages/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
fmt.Fprintf(w, `{
"message": "Some Message Updated",
"starts_at": "2017-06-26T06:00:00.000Z",
"ends_at": "2017-06-27T12:59:00.000Z",
"color": "#E75E40",
"font": "#FFFFFF",
"id": 42,
"active": false
}`)
})
opt := &UpdateBroadcastMessageOptions{
Message: String("Some Message Updated"),
StartsAt: &wantedStartsAt,
EndsAt: &wantedEndsAt,
Color: String("#E75E40"),
Font: String("#FFFFFF"),
}
got, _, err := client.BroadcastMessage.UpdateBroadcastMessage(1, opt)
if err != nil {
t.Errorf("UpdateBroadcastMessage returned error: %v", err)
}
want := &BroadcastMessage{
Message: "Some Message Updated",
StartsAt: &wantedStartsAt,
EndsAt: &wantedEndsAt,
Color: "#E75E40",
Font: "#FFFFFF",
ID: 42,
Active: false,
}
if !reflect.DeepEqual(got, want) {
t.Errorf("UpdateBroadcastMessage returned \ngot:\n%v\nwant:\n%v", Stringify(got), Stringify(want))
}
}
func TestDeleteBroadcastMessages(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/broadcast_messages/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
})
_, err := client.BroadcastMessage.DeleteBroadcastMessage(1)
if err != nil {
t.Errorf("UpdateBroadcastMessage returned error: %v", err)
}
}
......@@ -265,6 +265,7 @@ type Client struct {
AwardEmoji *AwardEmojiService
Branches *BranchesService
BuildVariables *BuildVariablesService
BroadcastMessage *BroadcastMessagesService
Commits *CommitsService
DeployKeys *DeployKeysService
Deployments *DeploymentsService
......@@ -349,6 +350,7 @@ func newClient(httpClient *http.Client, tokenType tokenType, token string) *Clie
c.AwardEmoji = &AwardEmojiService{client: c}
c.Branches = &BranchesService{client: c}
c.BuildVariables = &BuildVariablesService{client: c}
c.BroadcastMessage = &BroadcastMessagesService{client: c}
c.Commits = &CommitsService{client: c}
c.DeployKeys = &DeployKeysService{client: c}
c.Deployments = &DeploymentsService{client: c}
......
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