1//
2// Copyright 2018, Sander van Harmelen
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17package gitlab
18
19import (
20 "fmt"
21 "time"
22)
23
24// BroadcastMessagesService handles communication with the broadcast
25// messages methods of the GitLab API.
26//
27// GitLab API docs: https://docs.gitlab.com/ce/api/broadcast_messages.html
28type BroadcastMessagesService struct {
29 client *Client
30}
31
32// BroadcastMessage represents a GitLab issue board.
33//
34// GitLab API docs:
35// https://docs.gitlab.com/ce/api/broadcast_messages.html#get-all-broadcast-messages
36type BroadcastMessage struct {
37 Message string `json:"message"`
38 StartsAt *time.Time `json:"starts_at"`
39 EndsAt *time.Time `json:"ends_at"`
40 Color string `json:"color"`
41 Font string `json:"font"`
42 ID int `json:"id"`
43 Active bool `json:"active"`
44}
45
46// ListBroadcastMessagesOptions represents the available ListBroadcastMessages()
47// options.
48//
49// GitLab API docs:
50// https://docs.gitlab.com/ce/api/broadcast_messages.html#get-all-broadcast-messages
51type ListBroadcastMessagesOptions ListOptions
52
53// ListBroadcastMessages gets a list of all broadcasted messages.
54//
55// GitLab API docs:
56// https://docs.gitlab.com/ce/api/broadcast_messages.html#get-all-broadcast-messages
57func (s *BroadcastMessagesService) ListBroadcastMessages(opt *ListBroadcastMessagesOptions, options ...OptionFunc) ([]*BroadcastMessage, *Response, error) {
58 req, err := s.client.NewRequest("GET", "broadcast_messages", opt, options)
59 if err != nil {
60 return nil, nil, err
61 }
62
63 var bs []*BroadcastMessage
64 resp, err := s.client.Do(req, &bs)
65 if err != nil {
66 return nil, resp, err
67 }
68
69 return bs, resp, err
70}
71
72// GetBroadcastMessage gets a single broadcast message.
73//
74// GitLab API docs:
75// https://docs.gitlab.com/ce/api/broadcast_messages.html#get-a-specific-broadcast-message
76func (s *BroadcastMessagesService) GetBroadcastMessage(broadcast int, options ...OptionFunc) (*BroadcastMessage, *Response, error) {
77 u := fmt.Sprintf("broadcast_messages/%d", broadcast)
78
79 req, err := s.client.NewRequest("GET", u, nil, options)
80 if err != nil {
81 return nil, nil, err
82 }
83
84 b := new(BroadcastMessage)
85 resp, err := s.client.Do(req, &b)
86 if err != nil {
87 return nil, resp, err
88 }
89
90 return b, resp, err
91}
92
93// CreateBroadcastMessageOptions represents the available CreateBroadcastMessage()
94// options.
95//
96// GitLab API docs:
97// https://docs.gitlab.com/ce/api/broadcast_messages.html#create-a-broadcast-message
98type CreateBroadcastMessageOptions struct {
99 Message *string `url:"message" json:"message"`
100 StartsAt *time.Time `url:"starts_at,omitempty" json:"starts_at,omitempty"`
101 EndsAt *time.Time `url:"ends_at,omitempty" json:"ends_at,omitempty"`
102 Color *string `url:"color,omitempty" json:"color,omitempty"`
103 Font *string `url:"font,omitempty" json:"font,omitempty"`
104}
105
106// CreateBroadcastMessage creates a message to broadcast.
107//
108// GitLab API docs:
109// https://docs.gitlab.com/ce/api/broadcast_messages.html#create-a-broadcast-message
110func (s *BroadcastMessagesService) CreateBroadcastMessage(opt *CreateBroadcastMessageOptions, options ...OptionFunc) (*BroadcastMessage, *Response, error) {
111 req, err := s.client.NewRequest("POST", "broadcast_messages", opt, options)
112 if err != nil {
113 return nil, nil, err
114 }
115
116 b := new(BroadcastMessage)
117 resp, err := s.client.Do(req, &b)
118 if err != nil {
119 return nil, resp, err
120 }
121
122 return b, resp, err
123}
124
125// UpdateBroadcastMessageOptions represents the available CreateBroadcastMessage()
126// options.
127//
128// GitLab API docs:
129// https://docs.gitlab.com/ce/api/broadcast_messages.html#update-a-broadcast-message
130type UpdateBroadcastMessageOptions struct {
131 Message *string `url:"message,omitempty" json:"message,omitempty"`
132 StartsAt *time.Time `url:"starts_at,omitempty" json:"starts_at,omitempty"`
133 EndsAt *time.Time `url:"ends_at,omitempty" json:"ends_at,omitempty"`
134 Color *string `url:"color,omitempty" json:"color,omitempty"`
135 Font *string `url:"font,omitempty" json:"font,omitempty"`
136}
137
138// UpdateBroadcastMessage update a broadcasted message.
139//
140// GitLab API docs:
141// https://docs.gitlab.com/ce/api/broadcast_messages.html#update-a-broadcast-message
142func (s *BroadcastMessagesService) UpdateBroadcastMessage(broadcast int, opt *UpdateBroadcastMessageOptions, options ...OptionFunc) (*BroadcastMessage, *Response, error) {
143 u := fmt.Sprintf("broadcast_messages/%d", broadcast)
144
145 req, err := s.client.NewRequest("PUT", u, opt, options)
146 if err != nil {
147 return nil, nil, err
148 }
149
150 b := new(BroadcastMessage)
151 resp, err := s.client.Do(req, &b)
152 if err != nil {
153 return nil, resp, err
154 }
155
156 return b, resp, err
157}
158
159// DeleteBroadcastMessage deletes a broadcasted message.
160//
161// GitLab API docs:
162// https://docs.gitlab.com/ce/api/broadcast_messages.html#delete-a-broadcast-message
163func (s *BroadcastMessagesService) DeleteBroadcastMessage(broadcast int, options ...OptionFunc) (*Response, error) {
164 u := fmt.Sprintf("broadcast_messages/%d", broadcast)
165
166 req, err := s.client.NewRequest("DELETE", u, nil, options)
167 if err != nil {
168 return nil, err
169 }
170
171 return s.client.Do(req, nil)
172}