2019-04-25 13:06:53 -04:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TrackedTime worked time for an issue / pr
|
|
|
|
type TrackedTime struct {
|
2019-10-13 11:13:33 -04:00
|
|
|
ID int64 `json:"id"`
|
2019-04-25 13:06:53 -04:00
|
|
|
Created time.Time `json:"created"`
|
|
|
|
// Time in seconds
|
2020-01-03 20:44:33 -05:00
|
|
|
Time int64 `json:"time"`
|
|
|
|
// deprecated (only for backwards compatibility)
|
|
|
|
UserID int64 `json:"user_id"`
|
|
|
|
UserName string `json:"user_name"`
|
|
|
|
// deprecated (only for backwards compatibility)
|
|
|
|
IssueID int64 `json:"issue_id"`
|
|
|
|
Issue *Issue `json:"issue"`
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserTrackedTimes list tracked times of a user
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) GetUserTrackedTimes(owner, repo, user string) ([]*TrackedTime, *Response, error) {
|
2019-10-13 11:13:33 -04:00
|
|
|
times := make([]*TrackedTime, 0, 10)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/times/%s", owner, repo, user), nil, nil, ×)
|
|
|
|
return times, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepoTrackedTimes list tracked times of a repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) GetRepoTrackedTimes(owner, repo string) ([]*TrackedTime, *Response, error) {
|
2019-10-13 11:13:33 -04:00
|
|
|
times := make([]*TrackedTime, 0, 10)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/times", owner, repo), nil, nil, ×)
|
|
|
|
return times, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetMyTrackedTimes list tracked times of the current user
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) GetMyTrackedTimes() ([]*TrackedTime, *Response, error) {
|
2019-10-13 11:13:33 -04:00
|
|
|
times := make([]*TrackedTime, 0, 10)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", "/user/times", nil, nil, ×)
|
|
|
|
return times, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddTimeOption options for adding time to an issue
|
|
|
|
type AddTimeOption struct {
|
|
|
|
// time in seconds
|
2020-09-15 22:01:41 -04:00
|
|
|
Time int64 `json:"time"`
|
2020-01-03 20:44:33 -05:00
|
|
|
// optional
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
// optional
|
|
|
|
User string `json:"user_name"`
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2020-09-15 22:01:41 -04:00
|
|
|
// Validate the AddTimeOption struct
|
|
|
|
func (opt AddTimeOption) Validate() error {
|
|
|
|
if opt.Time == 0 {
|
|
|
|
return fmt.Errorf("no time to add")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:06:53 -04:00
|
|
|
// AddTime adds time to issue with the given index
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) AddTime(owner, repo string, index int64, opt AddTimeOption) (*TrackedTime, *Response, error) {
|
|
|
|
if err := opt.Validate(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-04-25 13:06:53 -04:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
t := new(TrackedTime)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("POST",
|
|
|
|
fmt.Sprintf("/repos/%s/%s/issues/%d/times", owner, repo, index),
|
2019-04-25 13:06:53 -04:00
|
|
|
jsonHeader, bytes.NewReader(body), t)
|
2020-09-15 22:01:41 -04:00
|
|
|
return t, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2020-05-27 09:48:09 -04:00
|
|
|
// ListTrackedTimesOptions options for listing repository's tracked times
|
|
|
|
type ListTrackedTimesOptions struct {
|
|
|
|
ListOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListTrackedTimes list tracked times of a single issue for a given repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) ListTrackedTimes(owner, repo string, index int64, opt ListTrackedTimesOptions) ([]*TrackedTime, *Response, error) {
|
2020-05-27 09:48:09 -04:00
|
|
|
opt.setDefaults()
|
|
|
|
times := make([]*TrackedTime, 0, opt.PageSize)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/times?%s", owner, repo, index, opt.getURLQuery().Encode()), nil, nil, ×)
|
|
|
|
return times, resp, err
|
2020-05-27 09:48:09 -04:00
|
|
|
}
|
|
|
|
|
2020-02-07 08:53:15 -05:00
|
|
|
// ResetIssueTime reset tracked time of a single issue for a given repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) ResetIssueTime(owner, repo string, index int64) (*Response, error) {
|
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/times", owner, repo, index), nil, nil)
|
|
|
|
return resp, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTime delete a specific tracked time by id of a single issue for a given repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) DeleteTime(owner, repo string, index, timeID int64) (*Response, error) {
|
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/times/%d", owner, repo, index, timeID), nil, nil)
|
|
|
|
return resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|