...

Source file src/github.com/xanzy/go-gitlab/time_stats.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, 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  
    17  package gitlab
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  )
    23  
    24  // timeStatsService handles communication with the time tracking related
    25  // methods of the GitLab API.
    26  //
    27  // GitLab docs: https://docs.gitlab.com/ee/workflow/time_tracking.html
    28  type timeStatsService struct {
    29  	client *Client
    30  }
    31  
    32  // TimeStats represents the time estimates and time spent for an issue.
    33  //
    34  // GitLab docs: https://docs.gitlab.com/ee/workflow/time_tracking.html
    35  type TimeStats struct {
    36  	HumanTimeEstimate   string `json:"human_time_estimate"`
    37  	HumanTotalTimeSpent string `json:"human_total_time_spent"`
    38  	TimeEstimate        int    `json:"time_estimate"`
    39  	TotalTimeSpent      int    `json:"total_time_spent"`
    40  }
    41  
    42  func (t TimeStats) String() string {
    43  	return Stringify(t)
    44  }
    45  
    46  // SetTimeEstimateOptions represents the available SetTimeEstimate()
    47  // options.
    48  //
    49  // GitLab docs: https://docs.gitlab.com/ee/workflow/time_tracking.html
    50  type SetTimeEstimateOptions struct {
    51  	Duration *string `url:"duration,omitempty" json:"duration,omitempty"`
    52  }
    53  
    54  // setTimeEstimate sets the time estimate for a single project issue.
    55  //
    56  // GitLab docs: https://docs.gitlab.com/ee/workflow/time_tracking.html
    57  func (s *timeStatsService) setTimeEstimate(pid interface{}, entity string, issue int, opt *SetTimeEstimateOptions, options ...RequestOptionFunc) (*TimeStats, *Response, error) {
    58  	project, err := parseID(pid)
    59  	if err != nil {
    60  		return nil, nil, err
    61  	}
    62  	u := fmt.Sprintf("projects/%s/%s/%d/time_estimate", PathEscape(project), entity, issue)
    63  
    64  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
    65  	if err != nil {
    66  		return nil, nil, err
    67  	}
    68  
    69  	t := new(TimeStats)
    70  	resp, err := s.client.Do(req, t)
    71  	if err != nil {
    72  		return nil, resp, err
    73  	}
    74  
    75  	return t, resp, nil
    76  }
    77  
    78  // resetTimeEstimate resets the time estimate for a single project issue.
    79  //
    80  // GitLab docs: https://docs.gitlab.com/ee/workflow/time_tracking.html
    81  func (s *timeStatsService) resetTimeEstimate(pid interface{}, entity string, issue int, options ...RequestOptionFunc) (*TimeStats, *Response, error) {
    82  	project, err := parseID(pid)
    83  	if err != nil {
    84  		return nil, nil, err
    85  	}
    86  	u := fmt.Sprintf("projects/%s/%s/%d/reset_time_estimate", PathEscape(project), entity, issue)
    87  
    88  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
    89  	if err != nil {
    90  		return nil, nil, err
    91  	}
    92  
    93  	t := new(TimeStats)
    94  	resp, err := s.client.Do(req, t)
    95  	if err != nil {
    96  		return nil, resp, err
    97  	}
    98  
    99  	return t, resp, nil
   100  }
   101  
   102  // AddSpentTimeOptions represents the available AddSpentTime() options.
   103  //
   104  // GitLab docs: https://docs.gitlab.com/ee/workflow/time_tracking.html
   105  type AddSpentTimeOptions struct {
   106  	Duration *string `url:"duration,omitempty" json:"duration,omitempty"`
   107  	Summary  *string `url:"summary,omitempty" json:"summary,omitempty"`
   108  }
   109  
   110  // addSpentTime adds spent time for a single project issue.
   111  //
   112  // GitLab docs: https://docs.gitlab.com/ee/workflow/time_tracking.html
   113  func (s *timeStatsService) addSpentTime(pid interface{}, entity string, issue int, opt *AddSpentTimeOptions, options ...RequestOptionFunc) (*TimeStats, *Response, error) {
   114  	project, err := parseID(pid)
   115  	if err != nil {
   116  		return nil, nil, err
   117  	}
   118  	u := fmt.Sprintf("projects/%s/%s/%d/add_spent_time", PathEscape(project), entity, issue)
   119  
   120  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   121  	if err != nil {
   122  		return nil, nil, err
   123  	}
   124  
   125  	t := new(TimeStats)
   126  	resp, err := s.client.Do(req, t)
   127  	if err != nil {
   128  		return nil, resp, err
   129  	}
   130  
   131  	return t, resp, nil
   132  }
   133  
   134  // resetSpentTime resets the spent time for a single project issue.
   135  //
   136  // GitLab docs: https://docs.gitlab.com/ee/workflow/time_tracking.html
   137  func (s *timeStatsService) resetSpentTime(pid interface{}, entity string, issue int, options ...RequestOptionFunc) (*TimeStats, *Response, error) {
   138  	project, err := parseID(pid)
   139  	if err != nil {
   140  		return nil, nil, err
   141  	}
   142  	u := fmt.Sprintf("projects/%s/%s/%d/reset_spent_time", PathEscape(project), entity, issue)
   143  
   144  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
   145  	if err != nil {
   146  		return nil, nil, err
   147  	}
   148  
   149  	t := new(TimeStats)
   150  	resp, err := s.client.Do(req, t)
   151  	if err != nil {
   152  		return nil, resp, err
   153  	}
   154  
   155  	return t, resp, nil
   156  }
   157  
   158  // getTimeSpent gets the spent time for a single project issue.
   159  //
   160  // GitLab docs: https://docs.gitlab.com/ee/workflow/time_tracking.html
   161  func (s *timeStatsService) getTimeSpent(pid interface{}, entity string, issue int, options ...RequestOptionFunc) (*TimeStats, *Response, error) {
   162  	project, err := parseID(pid)
   163  	if err != nil {
   164  		return nil, nil, err
   165  	}
   166  	u := fmt.Sprintf("projects/%s/%s/%d/time_stats", PathEscape(project), entity, issue)
   167  
   168  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   169  	if err != nil {
   170  		return nil, nil, err
   171  	}
   172  
   173  	t := new(TimeStats)
   174  	resp, err := s.client.Do(req, t)
   175  	if err != nil {
   176  		return nil, resp, err
   177  	}
   178  
   179  	return t, resp, nil
   180  }
   181  

View as plain text