forked from justmiles/go-confluence
-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.go
109 lines (100 loc) · 3.55 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package confluence
import (
"encoding/json"
"errors"
"time"
"github.com/google/go-querystring/query"
log "github.com/sirupsen/logrus"
)
// Search searches for content using the Confluence Query Language (CQL)
// https://developer.atlassian.com/cloud/confluence/rest/#api-search-get
//
// Example:
// searchResults, err := client.Search(&confluence.SearchQueryParameters{
// CQL: "space = PE",
// Limit: 1,
// })
//
// if err != nil {
// errorAndExit(err)
// }
//
// for _, searchResult := range searchResults {
// fmt.Println(searchResult.Title)
// }
func (client *Client) Search(qp *SearchQueryParameters) ([]SearchResult, error) {
var queryParams string
if qp != nil {
v, _ := query.Values(qp)
queryParams = v.Encode()
}
var searchResponse SearchResponse
body, err := client.request("GET", "/rest/api/search", queryParams, "")
if err != nil {
return searchResponse.Results, err
}
err = json.Unmarshal(body, &searchResponse)
if err != nil {
log.Error("Unable to unmarshal SearchResponse. Received: '", string(body), "'")
}
if searchResponse.Message != "" {
err = errors.New(searchResponse.Message)
}
return searchResponse.Results, err
}
// SearchQueryParameters query parameters for Search
type SearchQueryParameters struct {
CQL string `url:"cql"`
CQLContext string `url:"cqlcontext,omitempty"`
IncludeArchivedSpaces bool `url:"includeArchivedSpaces,omitempty"`
Limit int `url:"limit,omitempty"`
Start int `url:"start,omitempty"`
}
// SearchResponse represents the data returned from the Confluence API
type SearchResponse struct {
APIResponse
Results []SearchResult `json:"results,omitempty"`
Start int `json:"start,omitempty"`
Limit int `json:"limit,omitempty"`
Size int `json:"size,omitempty"`
TotalSize int `json:"totalSize,omitempty"`
CqlQuery string `json:"cqlQuery,omitempty"`
SearchDuration int `json:"searchDuration,omitempty"`
Links struct {
Base string `json:"base,omitempty"`
Context string `json:"context,omitempty"`
} `json:"_links,omitempty"`
}
// SearchResult results from Search
type SearchResult struct {
Space struct {
Key string `json:"key,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Metadata struct {
} `json:"metadata,omitempty"`
Status string `json:"status,omitempty"`
Expandable struct {
Operations string `json:"operations,omitempty"`
Permissions string `json:"permissions,omitempty"`
Description string `json:"description,omitempty"`
} `json:"_expandable,omitempty"`
Links struct {
Self string `json:"self,omitempty"`
} `json:"_links,omitempty"`
} `json:"space,omitempty"`
Title string `json:"title,omitempty"`
Excerpt string `json:"excerpt,omitempty"`
URL string `json:"url,omitempty"`
ResultGlobalContainer struct {
Title string `json:"title"`
DisplayURL string `json:"displayUrl"`
} `json:"resultGlobalContainer"`
Breadcrumbs []interface{} `json:"breadcrumbs,omitempty"`
EntityType string `json:"entityType,omitempty"`
IconCSSClass string `json:"iconCssClass,omitempty"`
LastModified time.Time `json:"lastModified,omitempty"`
FriendlyLastModified string `json:"friendlyLastModified,omitempty"`
Score float64 `json:"score,omitempty"`
Content `json:"content,omitempty"`
}