broadcast_messages.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. package gitlab
  17. import (
  18. "fmt"
  19. "time"
  20. )
  21. // BroadcastMessagesService handles communication with the broadcast
  22. // messages methods of the GitLab API.
  23. //
  24. // GitLab API docs: https://docs.gitlab.com/ce/api/broadcast_messages.html
  25. type BroadcastMessagesService struct {
  26. client *Client
  27. }
  28. // BroadcastMessage represents a GitLab issue board.
  29. //
  30. // GitLab API docs:
  31. // https://docs.gitlab.com/ce/api/broadcast_messages.html#get-all-broadcast-messages
  32. type BroadcastMessage struct {
  33. Message string `json:"message"`
  34. StartsAt *time.Time `json:"starts_at"`
  35. EndsAt *time.Time `json:"ends_at"`
  36. Color string `json:"color"`
  37. Font string `json:"font"`
  38. ID int `json:"id"`
  39. Active bool `json:"active"`
  40. }
  41. // ListBroadcastMessagesOptions represents the available ListBroadcastMessages()
  42. // options.
  43. //
  44. // GitLab API docs:
  45. // https://docs.gitlab.com/ce/api/broadcast_messages.html#get-all-broadcast-messages
  46. type ListBroadcastMessagesOptions ListOptions
  47. // ListBroadcastMessages gets a list of all broadcasted messages.
  48. //
  49. // GitLab API docs:
  50. // https://docs.gitlab.com/ce/api/broadcast_messages.html#get-all-broadcast-messages
  51. func (s *BroadcastMessagesService) ListBroadcastMessages(opt *ListBroadcastMessagesOptions, options ...OptionFunc) ([]*BroadcastMessage, *Response, error) {
  52. req, err := s.client.NewRequest("GET", "broadcast_messages", opt, options)
  53. if err != nil {
  54. return nil, nil, err
  55. }
  56. var bs []*BroadcastMessage
  57. resp, err := s.client.Do(req, &bs)
  58. if err != nil {
  59. return nil, resp, err
  60. }
  61. return bs, resp, err
  62. }
  63. // GetBroadcastMessage gets a single broadcast message.
  64. //
  65. // GitLab API docs:
  66. // https://docs.gitlab.com/ce/api/broadcast_messages.html#get-a-specific-broadcast-message
  67. func (s *BroadcastMessagesService) GetBroadcastMessage(broadcast int, options ...OptionFunc) (*BroadcastMessage, *Response, error) {
  68. u := fmt.Sprintf("broadcast_messages/%d", broadcast)
  69. req, err := s.client.NewRequest("GET", u, nil, options)
  70. if err != nil {
  71. return nil, nil, err
  72. }
  73. b := new(BroadcastMessage)
  74. resp, err := s.client.Do(req, &b)
  75. if err != nil {
  76. return nil, resp, err
  77. }
  78. return b, resp, err
  79. }
  80. // CreateBroadcastMessageOptions represents the available CreateBroadcastMessage()
  81. // options.
  82. //
  83. // GitLab API docs:
  84. // https://docs.gitlab.com/ce/api/broadcast_messages.html#create-a-broadcast-message
  85. type CreateBroadcastMessageOptions struct {
  86. Message *string `url:"message" json:"message"`
  87. StartsAt *time.Time `url:"starts_at,omitempty" json:"starts_at,omitempty"`
  88. EndsAt *time.Time `url:"ends_at,omitempty" json:"ends_at,omitempty"`
  89. Color *string `url:"color,omitempty" json:"color,omitempty"`
  90. Font *string `url:"font,omitempty" json:"font,omitempty"`
  91. }
  92. // CreateBroadcastMessage creates a message to broadcast.
  93. //
  94. // GitLab API docs:
  95. // https://docs.gitlab.com/ce/api/broadcast_messages.html#create-a-broadcast-message
  96. func (s *BroadcastMessagesService) CreateBroadcastMessage(opt *CreateBroadcastMessageOptions, options ...OptionFunc) (*BroadcastMessage, *Response, error) {
  97. req, err := s.client.NewRequest("POST", "broadcast_messages", opt, options)
  98. if err != nil {
  99. return nil, nil, err
  100. }
  101. b := new(BroadcastMessage)
  102. resp, err := s.client.Do(req, &b)
  103. if err != nil {
  104. return nil, resp, err
  105. }
  106. return b, resp, err
  107. }
  108. // UpdateBroadcastMessageOptions represents the available CreateBroadcastMessage()
  109. // options.
  110. //
  111. // GitLab API docs:
  112. // https://docs.gitlab.com/ce/api/broadcast_messages.html#update-a-broadcast-message
  113. type UpdateBroadcastMessageOptions struct {
  114. Message *string `url:"message,omitempty" json:"message,omitempty"`
  115. StartsAt *time.Time `url:"starts_at,omitempty" json:"starts_at,omitempty"`
  116. EndsAt *time.Time `url:"ends_at,omitempty" json:"ends_at,omitempty"`
  117. Color *string `url:"color,omitempty" json:"color,omitempty"`
  118. Font *string `url:"font,omitempty" json:"font,omitempty"`
  119. }
  120. // UpdateBroadcastMessage update a broadcasted message.
  121. //
  122. // GitLab API docs:
  123. // https://docs.gitlab.com/ce/api/broadcast_messages.html#update-a-broadcast-message
  124. func (s *BroadcastMessagesService) UpdateBroadcastMessage(broadcast int, opt *UpdateBroadcastMessageOptions, options ...OptionFunc) (*BroadcastMessage, *Response, error) {
  125. u := fmt.Sprintf("broadcast_messages/%d", broadcast)
  126. req, err := s.client.NewRequest("PUT", u, opt, options)
  127. if err != nil {
  128. return nil, nil, err
  129. }
  130. b := new(BroadcastMessage)
  131. resp, err := s.client.Do(req, &b)
  132. if err != nil {
  133. return nil, resp, err
  134. }
  135. return b, resp, err
  136. }
  137. // DeleteBroadcastMessage deletes a broadcasted message.
  138. //
  139. // GitLab API docs:
  140. // https://docs.gitlab.com/ce/api/broadcast_messages.html#delete-a-broadcast-message
  141. func (s *BroadcastMessagesService) DeleteBroadcastMessage(broadcast int, options ...OptionFunc) (*Response, error) {
  142. u := fmt.Sprintf("broadcast_messages/%d", broadcast)
  143. req, err := s.client.NewRequest("DELETE", u, nil, options)
  144. if err != nil {
  145. return nil, err
  146. }
  147. return s.client.Do(req, nil)
  148. }