Skip to content

Commit 1ebd7e7

Browse files
falnyrghostsquad
authored andcommitted
feat(issues): Added support for AddWorklog and GetWorklogs
1 parent a9350ed commit 1ebd7e7

File tree

7 files changed

+210
-120
lines changed

7 files changed

+210
-120
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ language: go
33
sudo: false
44

55
go:
6-
- "1.7.x"
76
- "1.8.x"
87
- "1.9.x"
98
- "1.10.x"
109
- "1.11.x"
10+
- "1.12.x"
1111

1212
before_install:
1313
- go get -t ./...

Gopkg.lock

Lines changed: 0 additions & 36 deletions
This file was deleted.

Gopkg.toml

Lines changed: 0 additions & 46 deletions
This file was deleted.

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@
1717

1818
This package is not JIRA API complete (yet), but you can call every API endpoint you want. See [Call a not implemented API endpoint](#call-a-not-implemented-api-endpoint) how to do this. For all possible API endpoints of JIRA have a look at [latest JIRA REST API documentation](https://docs.atlassian.com/jira/REST/latest/).
1919

20-
## Compatible JIRA versions
20+
## Requirements
2121

22-
This package was tested against JIRA v6.3.4 and v7.1.2.
22+
* Go >= 1.8
23+
* JIRA v6.3.4 & v7.1.2.
2324

2425
## Installation
2526

2627
It is go gettable
2728

28-
$ go get github.com/andygrunwald/go-jira
29+
```bash
30+
go get github.com/andygrunwald/go-jira
31+
```
2932

3033
For stable versions you can use one of our tags with [gopkg.in](http://labix.org/gopkg.in). E.g.
3134

@@ -40,8 +43,10 @@ import (
4043

4144
(optional) to run unit / example tests:
4245

43-
$ cd $GOPATH/src/github.com/andygrunwald/go-jira
44-
$ go test -v ./...
46+
```bash
47+
cd $GOPATH/src/github.com/andygrunwald/go-jira
48+
go test -v ./...
49+
```
4550

4651
## API
4752

@@ -239,9 +244,9 @@ If you are new to pull requests, checkout [Collaborating on projects using issue
239244

240245
### Dependency management
241246

242-
`go-jira` uses `dep` for dependency management. After cloning the repo, it's easy to make sure you have the correct dependencies by running `dep ensure`.
247+
`go-jira` uses `go modules` for dependency management. After cloning the repo, it's easy to make sure you have the correct dependencies by running `go mod tidy`.
243248

244-
For adding new dependencies, updating dependencies, and other operations, the [Daily Dep](https://golang.github.io/dep/docs/daily-dep.html) is a good place to start.
249+
For adding new dependencies, updating dependencies, and other operations, the [Daily workflow](https://github.com/golang/go/wiki/Modules#daily-workflow) is a good place to start.
245250

246251
### Sandbox environment for testing
247252

go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/andygrunwald/go-jira
2+
3+
go 1.12
4+
5+
require (
6+
github.com/fatih/structs v1.0.0
7+
github.com/google/go-cmp v0.3.0
8+
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135
9+
github.com/pkg/errors v0.8.0
10+
github.com/trivago/tgo v1.0.1
11+
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734
12+
)

issue.go

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"io/ioutil"
99
"mime/multipart"
10+
"net/http"
1011
"net/url"
1112
"reflect"
1213
"strings"
@@ -295,6 +296,10 @@ type Parent struct {
295296
// Time represents the Time definition of JIRA as a time.Time of go
296297
type Time time.Time
297298

299+
func (t Time) Equal(u Time) bool {
300+
return time.Time(t).Equal(time.Time(u))
301+
}
302+
298303
// Date represents the Date definition of JIRA as a time.Time of go
299304
type Date time.Time
300305

@@ -394,17 +399,23 @@ type Worklog struct {
394399

395400
// WorklogRecord represents one entry of a Worklog
396401
type WorklogRecord struct {
397-
Self string `json:"self,omitempty" structs:"self,omitempty"`
398-
Author *User `json:"author,omitempty" structs:"author,omitempty"`
399-
UpdateAuthor *User `json:"updateAuthor,omitempty" structs:"updateAuthor,omitempty"`
400-
Comment string `json:"comment,omitempty" structs:"comment,omitempty"`
401-
Created *Time `json:"created,omitempty" structs:"created,omitempty"`
402-
Updated *Time `json:"updated,omitempty" structs:"updated,omitempty"`
403-
Started *Time `json:"started,omitempty" structs:"started,omitempty"`
404-
TimeSpent string `json:"timeSpent,omitempty" structs:"timeSpent,omitempty"`
405-
TimeSpentSeconds int `json:"timeSpentSeconds,omitempty" structs:"timeSpentSeconds,omitempty"`
406-
ID string `json:"id,omitempty" structs:"id,omitempty"`
407-
IssueID string `json:"issueId,omitempty" structs:"issueId,omitempty"`
402+
Self string `json:"self,omitempty" structs:"self,omitempty"`
403+
Author *User `json:"author,omitempty" structs:"author,omitempty"`
404+
UpdateAuthor *User `json:"updateAuthor,omitempty" structs:"updateAuthor,omitempty"`
405+
Comment string `json:"comment,omitempty" structs:"comment,omitempty"`
406+
Created *Time `json:"created,omitempty" structs:"created,omitempty"`
407+
Updated *Time `json:"updated,omitempty" structs:"updated,omitempty"`
408+
Started *Time `json:"started,omitempty" structs:"started,omitempty"`
409+
TimeSpent string `json:"timeSpent,omitempty" structs:"timeSpent,omitempty"`
410+
TimeSpentSeconds int `json:"timeSpentSeconds,omitempty" structs:"timeSpentSeconds,omitempty"`
411+
ID string `json:"id,omitempty" structs:"id,omitempty"`
412+
IssueID string `json:"issueId,omitempty" structs:"issueId,omitempty"`
413+
Properties []EntityProperty `json:"properties,omitempty"`
414+
}
415+
416+
type EntityProperty struct {
417+
Key string `json:"key"`
418+
Value interface{} `json:"value"`
408419
}
409420

410421
// TimeTracking represents the timetracking fields of a JIRA issue.
@@ -527,6 +538,22 @@ type GetQueryOptions struct {
527538
ProjectKeys string `url:"projectKeys,omitempty"`
528539
}
529540

541+
// GetWorklogsQueryOptions specifies the optional parameters for the Get Worklogs method
542+
type GetWorklogsQueryOptions struct {
543+
StartAt int64 `url:"startAt,omitempty"`
544+
MaxResults int32 `url:"maxResults,omitempty"`
545+
Expand string `url:"expand,omitempty"`
546+
}
547+
548+
type AddWorklogQueryOptions struct {
549+
NotifyUsers bool `url:"notifyUsers,omitempty"`
550+
AdjustEstimate string `url:"adjustEstimate,omitempty"`
551+
NewEstimate string `url:"newEstimate,omitempty"`
552+
ReduceBy string `url:"reduceBy,omitempty"`
553+
Expand string `url:"expand,omitempty"`
554+
OverrideEditableFlag bool `url:"overrideEditableFlag,omitempty"`
555+
}
556+
530557
// CustomFields represents custom fields of JIRA
531558
// This can heavily differ between JIRA instances
532559
type CustomFields map[string]string
@@ -626,19 +653,42 @@ func (s *IssueService) PostAttachment(issueID string, r io.Reader, attachmentNam
626653
// This method is especially important if you need to read all the worklogs, not just the first page.
627654
//
628655
// https://docs.atlassian.com/jira/REST/cloud/#api/2/issue/{issueIdOrKey}/worklog-getIssueWorklog
629-
func (s *IssueService) GetWorklogs(issueID string) (*Worklog, *Response, error) {
656+
func (s *IssueService) GetWorklogs(issueID string, options ...func(*http.Request) error) (*Worklog, *Response, error) {
630657
apiEndpoint := fmt.Sprintf("rest/api/2/issue/%s/worklog", issueID)
631658

632659
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
633660
if err != nil {
634661
return nil, nil, err
635662
}
636663

664+
for _, option := range options {
665+
err = option(req)
666+
if err != nil {
667+
return nil, nil, err
668+
}
669+
}
670+
637671
v := new(Worklog)
638672
resp, err := s.client.Do(req, v)
639673
return v, resp, err
640674
}
641675

676+
// Applies query options to http request.
677+
// This helper is meant to be used with all "QueryOptions" structs.
678+
func WithQueryOptions(options interface{}) func(*http.Request) error {
679+
q, err := query.Values(options)
680+
if err != nil {
681+
return func(*http.Request) error {
682+
return err
683+
}
684+
}
685+
686+
return func(r *http.Request) error {
687+
r.URL.RawQuery = q.Encode()
688+
return nil
689+
}
690+
}
691+
642692
// Create creates an issue or a sub-task from a JSON representation.
643693
// Creating a sub-task is similar to creating a regular issue, with two important differences:
644694
// The issueType field must correspond to a sub-task issue type and you must provide a parent field in the issue create request containing the id or key of the parent issue.
@@ -787,13 +837,20 @@ func (s *IssueService) DeleteComment(issueID, commentID string) error {
787837
// AddWorklogRecord adds a new worklog record to issueID.
788838
//
789839
// https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-issue-issueIdOrKey-worklog-post
790-
func (s *IssueService) AddWorklogRecord(issueID string, record *WorklogRecord) (*WorklogRecord, *Response, error) {
840+
func (s *IssueService) AddWorklogRecord(issueID string, record *WorklogRecord, options ...func(*http.Request) error) (*WorklogRecord, *Response, error) {
791841
apiEndpoint := fmt.Sprintf("rest/api/2/issue/%s/worklog", issueID)
792842
req, err := s.client.NewRequest("POST", apiEndpoint, record)
793843
if err != nil {
794844
return nil, nil, err
795845
}
796846

847+
for _, option := range options {
848+
err = option(req)
849+
if err != nil {
850+
return nil, nil, err
851+
}
852+
}
853+
797854
responseRecord := new(WorklogRecord)
798855
resp, err := s.client.Do(req, responseRecord)
799856
if err != nil {

0 commit comments

Comments
 (0)