Skip to content

Commit e7eb234

Browse files
author
Mitsuru Nakada
committed
Support Issues.Search()
1 parent 520add3 commit e7eb234

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

backlog/backlog.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import (
88
"io/ioutil"
99
"net/http"
1010
"net/url"
11+
"reflect"
1112
"strings"
13+
14+
"github.com/google/go-querystring/query"
1215
)
1316

1417
// A Client manages communication with the Backlog API.
@@ -179,3 +182,26 @@ func sanitizeURL(uri *url.URL) *url.URL {
179182
}
180183
return uri
181184
}
185+
186+
// addOptions adds the parameters in opt as URL query parameters to s. opt
187+
// must be a struct whose fields may contain "url" tags.
188+
// https://github.com/google/go-github/blob/99760a16213d6fdde13f4e477438f876b6c9c6eb/github/github.go#L212-L232
189+
func addOptions(s string, opt interface{}) (string, error) {
190+
v := reflect.ValueOf(opt)
191+
if v.Kind() == reflect.Ptr && v.IsNil() {
192+
return s, nil
193+
}
194+
195+
u, err := url.Parse(s)
196+
if err != nil {
197+
return s, err
198+
}
199+
200+
qs, err := query.Values(opt)
201+
if err != nil {
202+
return s, err
203+
}
204+
205+
u.RawQuery = qs.Encode()
206+
return u.String(), nil
207+
}

backlog/issues.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ type IssueRequest struct {
5959
DueDate *string
6060
}
6161

62+
// IssueSearchRequest represents a request to create/edit an issue.
63+
// https://developer.nulab-inc.com/ja/docs/backlog/api/2/get-issue-list/
64+
type IssueSearchRequest struct {
65+
IDs []int `url:"id[],omitempty"` // 課題のID
66+
ProjectIDs []int `url:"projectId[],omitempty"` // プロジェクトのID
67+
StatusIDs []int `url:"statusId[],omitempty"` // 状態のID
68+
PriorityIDs []int `url:"priorityId[],omitempty"` // 優先度のID
69+
CategoryIDs []int `url:"categoryId[],omitempty"` // カテゴリーのID
70+
IssueTypeIDs []int `url:"issueTypeId[],omitempty"` // 種別のID
71+
AssigneeIDs []int `url:"assigneeId[],omitempty"` // 担当者のID
72+
ParentIssueIDs []int `url:"parentIssueId[],omitempty"` // 親課題のID
73+
StartDateSince *string `url:"startDateSince[],omitempty"` // 開始日 (yyyy-MM-dd)
74+
DueDateSince *string `url:"dueDateSince,omitempty"` // 期限日 (yyyy-MM-dd)
75+
ParentChild *int `url:"parentChild,omitempty"` // 親子課題の条件
76+
Sort *string `url:"sort,omitempty"` // 課題一覧のソートに使用する属性名
77+
Order *string `url:"order,omitempty"` // `asc` または `desc` 指定が無い場合は `desc`
78+
Keyword *string `url:"keyword,omitempty"` // 検索キーワード
79+
Count *int `url:"count,omitempty"` // 取得上限 (1-100) 指定が無い場合は 20
80+
offset *int `url:"offset,omitempty"`
81+
}
82+
6283
// Get an issue.
6384
func (s *IssuesService) Get(issueKey string) (*Issue, *Response, error) {
6485
u := "issues/" + issueKey
@@ -124,6 +145,22 @@ func (s *IssuesService) Delete(issueKey string) (*Response, error) {
124145
return resp, nil
125146
}
126147

148+
// Search issues.
149+
func (s *IssuesService) Search(request IssueSearchRequest) ([]*Issue, *Response, error) {
150+
u, _ := addOptions("issues", request)
151+
req, err := s.client.NewRequest("GET", u, nil)
152+
if err != nil {
153+
return nil, nil, err
154+
}
155+
156+
issues := []*Issue{}
157+
resp, err := s.client.Do(req, &issues)
158+
if err != nil {
159+
return nil, resp, err
160+
}
161+
return issues, resp, nil
162+
}
163+
127164
func (r IssueRequest) makeValues() url.Values {
128165
v := url.Values{}
129166
if r.ProjectID != nil {

examples/search.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
8+
pointers "github.com/f2prateek/go-pointers"
9+
"github.com/mnkd/go-backlog/backlog"
10+
)
11+
12+
func main() {
13+
var space string
14+
flag.StringVar(&space, "s", "", "Backlog space (e.g. abc-inc)")
15+
flag.Parse()
16+
17+
if len(space) == 0 {
18+
fmt.Fprintf(os.Stderr, "Usage:\n\tgo run simple.go -s space_name -k issue_key\n")
19+
os.Exit(1)
20+
}
21+
22+
apiKey := os.Getenv("BACKLOG_API_KEY")
23+
if len(apiKey) == 0 {
24+
fmt.Fprintf(os.Stderr, "simple.go needs \"BACKLOG_API_KEY\" environment variable.\n")
25+
os.Exit(1)
26+
}
27+
28+
client := backlog.NewClient(nil, space, apiKey)
29+
30+
request := backlog.IssueSearchRequest{
31+
StatusIDs: []int{2},
32+
ParentChild: pointers.Int(4),
33+
Sort: pointers.String("created"),
34+
Order: pointers.String("asc"),
35+
Count: pointers.Int(10),
36+
}
37+
38+
issues, _, err := client.Issues.Search(request)
39+
if err != nil {
40+
fmt.Printf("Error: %v\n", err)
41+
os.Exit(1)
42+
}
43+
44+
fmt.Printf("Results: %d\n", len(issues))
45+
for _, issue := range issues {
46+
fmt.Printf("%v: %v (%v)\n", issue.IssueKey, issue.Summary, issue.CreatedUser.Name)
47+
}
48+
}

0 commit comments

Comments
 (0)