/
view.go
80 lines (71 loc) · 2 KB
/
view.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
package pivotal
import (
"fmt"
"github.com/wtfutil/wtf/utils"
)
type PivotalSource struct {
client *PivotalClient
name string
filter string
widget *Widget
Err error
stories []Story
max_items int
}
// NewPivotalSource returns a new Pivotal Filter source with a name
func NewPivotalSource(name string, filter string, client *PivotalClient, widget *Widget) *PivotalSource {
source := PivotalSource{
name: name,
filter: filter,
client: client,
widget: widget,
}
source.loadStories()
return &source
}
func (source *PivotalSource) loadStories() {
search, err := source.client.searchStories(source.filter)
if err != nil {
source.stories = nil
source.Err = err
source.setItemCount(0)
} else {
source.stories = search.Stories.Stories
source.Err = err
source.setItemCount(len(source.stories))
}
}
// Open: Will open Pivotal search url with filter applied using the utils helper
func (source *PivotalSource) Open() {
sel := source.widget.GetSelected()
projectID := source.client.projectId
if sel >= 0 && sel < source.getItemCount() {
story := &source.stories[sel]
baseURL := "https://www.pivotaltracker.com/n/projects/"
ticketURL := fmt.Sprintf("%s%s/stories/%d", baseURL, projectID, story.ID)
utils.OpenFile(ticketURL)
}
}
// OpenPulls will open the GitHub Pull Requests URL using the utils helper
func (source *PivotalSource) OpenPulls() {
sel := source.widget.GetSelected()
if sel >= 0 && sel < source.getItemCount() {
story := &source.stories[sel]
if len(story.PullRequests) > 0 {
pr := story.PullRequests[0]
ticketURL := fmt.Sprintf("%s%s/%s/pull/%d", pr.HostURL, pr.Owner, pr.Repo, pr.Number)
utils.OpenFile(ticketURL)
}
}
}
/* -------------------- Counts -------------------- */
func (source *PivotalSource) getItemCount() int {
if source.stories == nil {
return 0
}
return len(source.stories)
}
func (source *PivotalSource) setItemCount(count int) {
source.max_items = count
}
/* -------------------- Unexported Functions -------------------- */