| OLD | NEW |
| 1 package rietveld | 1 package rietveld |
| 2 | 2 |
| 3 import ( | 3 import ( |
| 4 "encoding/json" | 4 "encoding/json" |
| 5 "fmt" | 5 "fmt" |
| 6 "net/http" | 6 "net/http" |
| 7 "net/url" | 7 "net/url" |
| 8 "regexp" | 8 "regexp" |
| 9 "sort" | 9 "sort" |
| 10 "strings" | 10 "strings" |
| 11 "time" | 11 "time" |
| 12 ) | 12 ) |
| 13 | 13 |
| 14 import ( | 14 import ( |
| 15 "github.com/skia-dev/glog" | 15 "github.com/skia-dev/glog" |
| 16 "go.skia.org/infra/go/util" | 16 "go.skia.org/infra/go/util" |
| 17 ) | 17 ) |
| 18 | 18 |
| 19 var ( | 19 var ( |
| 20 committedIssueRegexp []string = []string{ | 20 committedIssueRegexp []string = []string{ |
| 21 "Committed patchset #[0-9]+ \\((id:)?[0-9]+\\) as [0-9a-f]{2,40}
", | 21 "Committed patchset #[0-9]+ \\((id:)?[0-9]+\\) as [0-9a-f]{2,40}
", |
| 22 "Committed patchset #[0-9]+", |
| 22 "Change committed as [0-9]+", | 23 "Change committed as [0-9]+", |
| 23 } | 24 } |
| 24 ) | 25 ) |
| 25 | 26 |
| 26 // Issue contains information about a Rietveld issue. | 27 // Issue contains information about a Rietveld issue. |
| 27 type Issue struct { | 28 type Issue struct { |
| 28 CC []string | 29 CC []string |
| 29 Closed bool | 30 Closed bool |
| 30 Committed bool | 31 Committed bool |
| 31 Created time.Time | 32 Created time.Time |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 value := "2" | 108 value := "2" |
| 108 if open { | 109 if open { |
| 109 value = "3" | 110 value = "3" |
| 110 } | 111 } |
| 111 return &SearchTerm{ | 112 return &SearchTerm{ |
| 112 Key: "closed", | 113 Key: "closed", |
| 113 Value: value, | 114 Value: value, |
| 114 } | 115 } |
| 115 } | 116 } |
| 116 | 117 |
| 117 func SearchLimit(limit int) *SearchTerm { | |
| 118 return &SearchTerm{ | |
| 119 Key: "limit", | |
| 120 Value: fmt.Sprintf("%d", limit), | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 // Search returns a slice of Issues which fit the given criteria. | 118 // Search returns a slice of Issues which fit the given criteria. |
| 125 func (r Rietveld) Search(terms ...*SearchTerm) ([]*Issue, error) { | 119 func (r Rietveld) Search(limit int, terms ...*SearchTerm) ([]*Issue, error) { |
| 126 » searchUrl := "/search?format=json" | 120 » searchUrl := fmt.Sprintf("/search?format=json&limit=%d", limit) |
| 127 for _, term := range terms { | 121 for _, term := range terms { |
| 128 searchUrl += fmt.Sprintf("&%s=%s", term.Key, term.Value) | 122 searchUrl += fmt.Sprintf("&%s=%s", term.Key, term.Value) |
| 129 } | 123 } |
| 130 | 124 |
| 131 var issues issueListSortable | 125 var issues issueListSortable |
| 132 cursor := "" | 126 cursor := "" |
| 133 for { | 127 for { |
| 134 var data rietveldResults | 128 var data rietveldResults |
| 135 err := r.get(searchUrl+cursor, &data) | 129 err := r.get(searchUrl+cursor, &data) |
| 136 if err != nil { | 130 if err != nil { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 159 glog.Error(err) | 153 glog.Error(err) |
| 160 continue | 154 continue |
| 161 } | 155 } |
| 162 if committed { | 156 if committed { |
| 163 fullIssue.Committed = true | 157 fullIssue.Committed = true |
| 164 } | 158 } |
| 165 } | 159 } |
| 166 issues = append(issues, &fullIssue) | 160 issues = append(issues, &fullIssue) |
| 167 } | 161 } |
| 168 } | 162 } |
| 163 if len(issues) >= limit { |
| 164 break |
| 165 } |
| 169 cursor = "&cursor=" + data.Cursor | 166 cursor = "&cursor=" + data.Cursor |
| 170 } | 167 } |
| 171 sort.Sort(issues) | 168 sort.Sort(issues) |
| 172 return issues, nil | 169 return issues, nil |
| 173 } | 170 } |
| 174 | 171 |
| 175 // getIssueProperties returns a fully filled-in Issue object, as opposed to | 172 // getIssueProperties returns a fully filled-in Issue object, as opposed to |
| 176 // the partial data returned by Rietveld's search endpoint. | 173 // the partial data returned by Rietveld's search endpoint. |
| 177 func (r Rietveld) getIssueProperties(issue int, messages bool) (Issue, error) { | 174 func (r Rietveld) getIssueProperties(issue int, messages bool) (Issue, error) { |
| 178 url := fmt.Sprintf("/api/%v", issue) | 175 url := fmt.Sprintf("/api/%v", issue) |
| 179 if messages { | 176 if messages { |
| 180 url += "?messages=true" | 177 url += "?messages=true" |
| 181 } | 178 } |
| 182 var res Issue | 179 var res Issue |
| 183 err := r.get(url, &res) | 180 err := r.get(url, &res) |
| 184 if err != nil { | 181 if err != nil { |
| 185 return Issue{}, fmt.Errorf("Failed to load details for issue %d:
%v", issue, err) | 182 return Issue{}, fmt.Errorf("Failed to load details for issue %d:
%v", issue, err) |
| 186 } | 183 } |
| 187 return res, nil | 184 return res, nil |
| 188 } | 185 } |
| 189 | 186 |
| 190 // New returns a new Rietveld instance. | 187 // New returns a new Rietveld instance. |
| 191 func New(url string) Rietveld { | 188 func New(url string) Rietveld { |
| 192 url = strings.TrimRight(url, "/") | 189 url = strings.TrimRight(url, "/") |
| 193 return Rietveld{url} | 190 return Rietveld{url} |
| 194 } | 191 } |
| OLD | NEW |