github.com/wtfutil/wtf@v0.43.0/modules/pivotal/view.go (about)

     1  package pivotal
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/wtfutil/wtf/utils"
     6  )
     7  
     8  type PivotalSource struct {
     9  	client    *PivotalClient
    10  	name      string
    11  	filter    string
    12  	widget    *Widget
    13  	Err       error
    14  	stories   []Story
    15  	max_items int
    16  }
    17  
    18  // NewPivotalSource returns a new Pivotal Filter source with a name
    19  func NewPivotalSource(name string, filter string, client *PivotalClient, widget *Widget) *PivotalSource {
    20  	source := PivotalSource{
    21  		name:   name,
    22  		filter: filter,
    23  		client: client,
    24  		widget: widget,
    25  	}
    26  	source.loadStories()
    27  	return &source
    28  }
    29  
    30  func (source *PivotalSource) loadStories() {
    31  	search, err := source.client.searchStories(source.filter)
    32  	if err != nil {
    33  		source.stories = nil
    34  		source.Err = err
    35  		source.setItemCount(0)
    36  	} else {
    37  		source.stories = search.Stories.Stories
    38  		source.Err = err
    39  		source.setItemCount(len(source.stories))
    40  	}
    41  }
    42  
    43  // Open: Will open Pivotal search url with filter applied using the utils helper
    44  func (source *PivotalSource) Open() {
    45  	sel := source.widget.GetSelected()
    46  	projectID := source.client.projectId
    47  	if sel >= 0 && sel < source.getItemCount() {
    48  		story := &source.stories[sel]
    49  		baseURL := "https://www.pivotaltracker.com/n/projects/"
    50  		ticketURL := fmt.Sprintf("%s%s/stories/%d", baseURL, projectID, story.ID)
    51  		utils.OpenFile(ticketURL)
    52  	}
    53  }
    54  
    55  // OpenPulls will open the GitHub Pull Requests URL using the utils helper
    56  func (source *PivotalSource) OpenPulls() {
    57  	sel := source.widget.GetSelected()
    58  	if sel >= 0 && sel < source.getItemCount() {
    59  		story := &source.stories[sel]
    60  		if len(story.PullRequests) > 0 {
    61  			pr := story.PullRequests[0]
    62  			ticketURL := fmt.Sprintf("%s%s/%s/pull/%d", pr.HostURL, pr.Owner, pr.Repo, pr.Number)
    63  			utils.OpenFile(ticketURL)
    64  		}
    65  	}
    66  }
    67  
    68  /* -------------------- Counts -------------------- */
    69  
    70  func (source *PivotalSource) getItemCount() int {
    71  	if source.stories == nil {
    72  		return 0
    73  	}
    74  	return len(source.stories)
    75  }
    76  func (source *PivotalSource) setItemCount(count int) {
    77  	source.max_items = count
    78  }
    79  
    80  /* -------------------- Unexported Functions -------------------- */