boards.go

  1//
  2// Copyright 2015, 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)
 22
 23// IssueBoardsService handles communication with the issue board related
 24// methods of the GitLab API.
 25//
 26// GitLab API docs: https://docs.gitlab.com/ce/api/boards.html
 27type IssueBoardsService struct {
 28	client *Client
 29}
 30
 31// IssueBoard represents a GitLab issue board.
 32//
 33// GitLab API docs: https://docs.gitlab.com/ce/api/boards.html
 34type IssueBoard struct {
 35	ID        int          `json:"id"`
 36	Name      string       `json:"name"`
 37	Project   *Project     `json:"project"`
 38	Milestone *Milestone   `json:"milestone"`
 39	Lists     []*BoardList `json:"lists"`
 40}
 41
 42func (b IssueBoard) String() string {
 43	return Stringify(b)
 44}
 45
 46// BoardList represents a GitLab board list.
 47//
 48// GitLab API docs: https://docs.gitlab.com/ce/api/boards.html
 49type BoardList struct {
 50	ID       int    `json:"id"`
 51	Label    *Label `json:"label"`
 52	Position int    `json:"position"`
 53}
 54
 55func (b BoardList) String() string {
 56	return Stringify(b)
 57}
 58
 59// CreateIssueBoardOptions represents the available CreateIssueBoard() options.
 60//
 61// GitLab API docs: https://docs.gitlab.com/ee/api/boards.html#create-a-board-starter
 62type CreateIssueBoardOptions struct {
 63	Name *string `url:"name" json:"name"`
 64}
 65
 66// CreateIssueBoard creates a new issue board.
 67//
 68// GitLab API docs: https://docs.gitlab.com/ee/api/boards.html#create-a-board-starter
 69func (s *IssueBoardsService) CreateIssueBoard(pid interface{}, opt *CreateIssueBoardOptions, options ...OptionFunc) (*IssueBoard, *Response, error) {
 70	project, err := parseID(pid)
 71	if err != nil {
 72		return nil, nil, err
 73	}
 74	u := fmt.Sprintf("projects/%s/boards", pathEscape(project))
 75
 76	req, err := s.client.NewRequest("POST", u, opt, options)
 77	if err != nil {
 78		return nil, nil, err
 79	}
 80
 81	board := new(IssueBoard)
 82	resp, err := s.client.Do(req, board)
 83	if err != nil {
 84		return nil, resp, err
 85	}
 86
 87	return board, resp, err
 88}
 89
 90// UpdateIssueBoardOptions represents the available UpdateIssueBoard() options.
 91//
 92// GitLab API docs: https://docs.gitlab.com/ee/api/boards.html#update-a-board-starter
 93type UpdateIssueBoardOptions struct {
 94	Name        *string `url:"name,omitempty" json:"name,omitempty"`
 95	AssigneeID  *int    `url:"assignee_id,omitempty" json:"assignee_id,omitempty"`
 96	MilestoneID *int    `url:"milestone_id,omitempty" json:"milestone_id,omitempty"`
 97	Labels      Labels  `url:"labels,omitempty" json:"labels,omitempty"`
 98	Weight      *int    `url:"weight,omitempty" json:"weight,omitempty"`
 99}
100
101// UpdateIssueBoard update an issue board.
102//
103// GitLab API docs: https://docs.gitlab.com/ee/api/boards.html#create-a-board-starter
104func (s *IssueBoardsService) UpdateIssueBoard(pid interface{}, board int, opt *UpdateIssueBoardOptions, options ...OptionFunc) (*IssueBoard, *Response, error) {
105	project, err := parseID(pid)
106	if err != nil {
107		return nil, nil, err
108	}
109	u := fmt.Sprintf("projects/%s/boards/%d", pathEscape(project), board)
110
111	req, err := s.client.NewRequest("PUT", u, opt, options)
112	if err != nil {
113		return nil, nil, err
114	}
115
116	is := new(IssueBoard)
117	resp, err := s.client.Do(req, is)
118	if err != nil {
119		return nil, resp, err
120	}
121
122	return is, resp, err
123}
124
125// DeleteIssueBoard deletes an issue board.
126//
127// GitLab API docs: https://docs.gitlab.com/ee/api/boards.html#delete-a-board-starter
128func (s *IssueBoardsService) DeleteIssueBoard(pid interface{}, board int, options ...OptionFunc) (*Response, error) {
129	project, err := parseID(pid)
130	if err != nil {
131		return nil, err
132	}
133	u := fmt.Sprintf("projects/%s/boards/%d", pathEscape(project), board)
134
135	req, err := s.client.NewRequest("DELETE", u, nil, options)
136	if err != nil {
137		return nil, err
138	}
139
140	return s.client.Do(req, nil)
141}
142
143// ListIssueBoardsOptions represents the available ListIssueBoards() options.
144//
145// GitLab API docs: https://docs.gitlab.com/ce/api/boards.html#project-board
146type ListIssueBoardsOptions ListOptions
147
148// ListIssueBoards gets a list of all issue boards in a project.
149//
150// GitLab API docs: https://docs.gitlab.com/ce/api/boards.html#project-board
151func (s *IssueBoardsService) ListIssueBoards(pid interface{}, opt *ListIssueBoardsOptions, options ...OptionFunc) ([]*IssueBoard, *Response, error) {
152	project, err := parseID(pid)
153	if err != nil {
154		return nil, nil, err
155	}
156	u := fmt.Sprintf("projects/%s/boards", pathEscape(project))
157
158	req, err := s.client.NewRequest("GET", u, opt, options)
159	if err != nil {
160		return nil, nil, err
161	}
162
163	var is []*IssueBoard
164	resp, err := s.client.Do(req, &is)
165	if err != nil {
166		return nil, resp, err
167	}
168
169	return is, resp, err
170}
171
172// GetIssueBoard gets a single issue board of a project.
173//
174// GitLab API docs: https://docs.gitlab.com/ce/api/boards.html#single-board
175func (s *IssueBoardsService) GetIssueBoard(pid interface{}, board int, options ...OptionFunc) (*IssueBoard, *Response, error) {
176	project, err := parseID(pid)
177	if err != nil {
178		return nil, nil, err
179	}
180	u := fmt.Sprintf("projects/%s/boards/%d", pathEscape(project), board)
181
182	req, err := s.client.NewRequest("GET", u, nil, options)
183	if err != nil {
184		return nil, nil, err
185	}
186
187	ib := new(IssueBoard)
188	resp, err := s.client.Do(req, ib)
189	if err != nil {
190		return nil, resp, err
191	}
192
193	return ib, resp, err
194}
195
196// GetIssueBoardListsOptions represents the available GetIssueBoardLists() options.
197//
198// GitLab API docs: https://docs.gitlab.com/ce/api/boards.html#list-board-lists
199type GetIssueBoardListsOptions ListOptions
200
201// GetIssueBoardLists gets a list of the issue board's lists. Does not include
202// backlog and closed lists.
203//
204// GitLab API docs: https://docs.gitlab.com/ce/api/boards.html#list-board-lists
205func (s *IssueBoardsService) GetIssueBoardLists(pid interface{}, board int, opt *GetIssueBoardListsOptions, options ...OptionFunc) ([]*BoardList, *Response, error) {
206	project, err := parseID(pid)
207	if err != nil {
208		return nil, nil, err
209	}
210	u := fmt.Sprintf("projects/%s/boards/%d/lists", pathEscape(project), board)
211
212	req, err := s.client.NewRequest("GET", u, opt, options)
213	if err != nil {
214		return nil, nil, err
215	}
216
217	var bl []*BoardList
218	resp, err := s.client.Do(req, &bl)
219	if err != nil {
220		return nil, resp, err
221	}
222
223	return bl, resp, err
224}
225
226// GetIssueBoardList gets a single issue board list.
227//
228// GitLab API docs: https://docs.gitlab.com/ce/api/boards.html#single-board-list
229func (s *IssueBoardsService) GetIssueBoardList(pid interface{}, board, list int, options ...OptionFunc) (*BoardList, *Response, error) {
230	project, err := parseID(pid)
231	if err != nil {
232		return nil, nil, err
233	}
234	u := fmt.Sprintf("projects/%s/boards/%d/lists/%d",
235		pathEscape(project),
236		board,
237		list,
238	)
239
240	req, err := s.client.NewRequest("GET", u, nil, options)
241	if err != nil {
242		return nil, nil, err
243	}
244
245	bl := new(BoardList)
246	resp, err := s.client.Do(req, bl)
247	if err != nil {
248		return nil, resp, err
249	}
250
251	return bl, resp, err
252}
253
254// CreateIssueBoardListOptions represents the available CreateIssueBoardList()
255// options.
256//
257// GitLab API docs: https://docs.gitlab.com/ce/api/boards.html#new-board-list
258type CreateIssueBoardListOptions struct {
259	LabelID *int `url:"label_id" json:"label_id"`
260}
261
262// CreateIssueBoardList creates a new issue board list.
263//
264// GitLab API docs: https://docs.gitlab.com/ce/api/boards.html#new-board-list
265func (s *IssueBoardsService) CreateIssueBoardList(pid interface{}, board int, opt *CreateIssueBoardListOptions, options ...OptionFunc) (*BoardList, *Response, error) {
266	project, err := parseID(pid)
267	if err != nil {
268		return nil, nil, err
269	}
270	u := fmt.Sprintf("projects/%s/boards/%d/lists", pathEscape(project), board)
271
272	req, err := s.client.NewRequest("POST", u, opt, options)
273	if err != nil {
274		return nil, nil, err
275	}
276
277	bl := new(BoardList)
278	resp, err := s.client.Do(req, bl)
279	if err != nil {
280		return nil, resp, err
281	}
282
283	return bl, resp, err
284}
285
286// UpdateIssueBoardListOptions represents the available UpdateIssueBoardList()
287// options.
288//
289// GitLab API docs: https://docs.gitlab.com/ce/api/boards.html#edit-board-list
290type UpdateIssueBoardListOptions struct {
291	Position *int `url:"position" json:"position"`
292}
293
294// UpdateIssueBoardList updates the position of an existing issue board list.
295//
296// GitLab API docs: https://docs.gitlab.com/ce/api/boards.html#edit-board-list
297func (s *IssueBoardsService) UpdateIssueBoardList(pid interface{}, board, list int, opt *UpdateIssueBoardListOptions, options ...OptionFunc) (*BoardList, *Response, error) {
298	project, err := parseID(pid)
299	if err != nil {
300		return nil, nil, err
301	}
302	u := fmt.Sprintf("projects/%s/boards/%d/lists/%d",
303		pathEscape(project),
304		board,
305		list,
306	)
307
308	req, err := s.client.NewRequest("PUT", u, opt, options)
309	if err != nil {
310		return nil, nil, err
311	}
312
313	bl := new(BoardList)
314	resp, err := s.client.Do(req, bl)
315	if err != nil {
316		return nil, resp, err
317	}
318
319	return bl, resp, err
320}
321
322// DeleteIssueBoardList soft deletes an issue board list. Only for admins and
323// project owners.
324//
325// GitLab API docs:
326// https://docs.gitlab.com/ce/api/boards.html#delete-a-board-list
327func (s *IssueBoardsService) DeleteIssueBoardList(pid interface{}, board, list int, options ...OptionFunc) (*Response, error) {
328	project, err := parseID(pid)
329	if err != nil {
330		return nil, err
331	}
332	u := fmt.Sprintf("projects/%s/boards/%d/lists/%d",
333		pathEscape(project),
334		board,
335		list,
336	)
337
338	req, err := s.client.NewRequest("DELETE", u, nil, options)
339	if err != nil {
340		return nil, err
341	}
342
343	return s.client.Do(req, nil)
344}