diff --git a/core/section/trello/labels_template.go b/core/section/trello/labels_template.go index ea592eb2..a1b5a160 100644 --- a/core/section/trello/labels_template.go +++ b/core/section/trello/labels_template.go @@ -25,7 +25,7 @@ const labelsTemplate = ` {{ $l.Name }} ({{len $l.Boards}}) - {{range $idx, $brd := $l.Boards}}{{if gt $idx 0}}, {{end}}{{ $brd }}{{end}}. + {{range $idx, $brd := $l.Boards}}{{if gt $idx 0}}, {{end}}{{$brd.OrgName}}::{{$brd.Name}}{{end}}. {{end}} diff --git a/core/section/trello/model.go b/core/section/trello/model.go index d49c6d92..c583d442 100644 --- a/core/section/trello/model.go +++ b/core/section/trello/model.go @@ -12,7 +12,6 @@ package trello import ( - "html/template" "strings" "time" ) @@ -29,6 +28,8 @@ type trelloConfig struct { Boards []trelloBoard `json:"boards"` Since string `json:"since,omitempty"` SincePtr *time.Time `json:"-"` + + OrgByID map[string]trelloOrganization `json:"-"` } func (c *trelloConfig) Clean() { @@ -38,52 +39,65 @@ func (c *trelloConfig) Clean() { // Trello objects based upon https://github.com/VojtechVitek/go-trello +type trelloOrganization struct { + ID string `json:"id"` + Name string `json:"name"` + DisplayName string `json:"displayName"` + Desc string `json:"desc"` + DescData string `json:"descData"` + URL string `json:"url"` + Website string `json:"website"` + LogoHash string `json:"logoHash"` + Products []string `json:"products"` + PowerUps []string `json:"powerUps"` +} + type trelloAction struct { - Id string `json:"id"` - IdMemberCreator string `json:"idMemberCreator"` + ID string `json:"id"` + IDMemberCreator string `json:"idMemberCreator"` Data struct { DateLastEdited string `json:"dateLastEdited"` ListBefore struct { - Id string `json:"id"` + ID string `json:"id"` Name string `json:"name"` } `json:"listBefore"` ListAfter struct { - Id string `json:"id"` + ID string `json:"id"` Name string `json:"name"` } `json:"listAfter"` CheckItem struct { - Id string `json:"id"` + ID string `json:"id"` State string `json:"state"` Name string `json:"name"` } `json:"checkItem"` CheckList struct { - Id string `json:"id"` + ID string `json:"id"` Name string `json:"name"` } `json:"checklist"` List struct { - Id string `json:"id"` + ID string `json:"id"` Name string `json:"name"` } `json:"list"` TextData struct { Emoji struct{} `json:"emoji"` } `json:"textData"` Board struct { - Id string `json:"id"` + ID string `json:"id"` Name string `json:"name"` ShortLink string `json:"shortLink"` } `json:"board"` Card struct { - Id string `json:"id"` + ID string `json:"id"` Name string `json:"name"` ShortLink string `json:"shortLink"` - IdShort int `json:"idShort"` + IDShort int `json:"idShort"` } `json:"card"` Text string `json:"text"` } `json:"data"` Type string `json:"type"` Date string `json:"date"` MemberCreator struct { - Id string `json:"id"` + ID string `json:"id"` AvatarHash string `json:"avatarHash"` FullName string `json:"fullName"` Initials string `json:"initials"` @@ -133,6 +147,7 @@ type trelloBoard struct { Name string `json:"name"` Closed bool `json:"closed"` OrganizationID string `json:"idOrganization"` + OrgName string `json:"orgName"` Pinned bool `json:"pinned"` URL string `json:"url"` ShortURL string `json:"shortUrl"` @@ -250,7 +265,7 @@ type trelloRenderBoard struct { type trelloSharedLabel struct { Name string Color string - Boards []template.HTML + Boards []trelloBoard } type trelloBoardAssignCount struct { diff --git a/core/section/trello/trello.go b/core/section/trello/trello.go index 163d6432..5dc86502 100644 --- a/core/section/trello/trello.go +++ b/core/section/trello/trello.go @@ -101,7 +101,7 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R provider.WriteJSON(w, render) case "boards": - render, err := getBoards(config) + render, err := getBoards(&config) if err != nil { log.IfErr(err) @@ -267,7 +267,44 @@ func (*Provider) Refresh(ctx *provider.Context, config, data string) string { } // Helpers -func getBoards(config trelloConfig) (boards []trelloBoard, err error) { + +func getOrg(config *trelloConfig, orgID string) (*trelloOrganization, error) { + if config.OrgByID == nil { + config.OrgByID = make(map[string]trelloOrganization) + } + if org, found := config.OrgByID[orgID]; found { + return &org, nil + } + req, err := http.NewRequest("GET", fmt.Sprintf( + "https://api.trello.com/1/organizations/%s?fields=name,desc&key=%s&token=%s", + orgID, config.AppKey, config.Token), nil) + log.IfErr(err) + client := &http.Client{} + res, err := client.Do(req) + + if err != nil { + return nil, err + } + + if res.StatusCode != http.StatusOK { + return nil, fmt.Errorf("error: HTTP status code %d", res.StatusCode) + } + + b := trelloOrganization{} + + defer res.Body.Close() + dec := json.NewDecoder(res.Body) + err = dec.Decode(&b) + if err != nil { + fmt.Println(err) + return nil, err + } + + config.OrgByID[orgID] = b + return &b, nil +} + +func getBoards(config *trelloConfig) (boards []trelloBoard, err error) { req, err := http.NewRequest("GET", fmt.Sprintf( "https://api.trello.com/1/members/me/boards?fields=id,name,url,closed,prefs,idOrganization&key=%s&token=%s", config.AppKey, config.Token), nil) @@ -296,6 +333,11 @@ func getBoards(config trelloConfig) (boards []trelloBoard, err error) { // we only show open, team boards (not personal) for _, b := range b { if !b.Closed && len(b.OrganizationID) > 0 { + if o, e := getOrg(config, b.OrganizationID); e == nil { + b.OrgName = o.Name + } else { + log.Error("failed to get organisation infomation", e) + } boards = append(boards, b) } } @@ -521,7 +563,7 @@ func buildPayloadAnalysis(config *trelloConfig, render *trelloRender) { // pre-process labels type labT struct { color string - boards map[string]bool + boards map[string]trelloBoard } labels := make(map[string]labT) @@ -541,10 +583,9 @@ func buildPayloadAnalysis(config *trelloConfig, render *trelloRender) { // process labels for _, lab := range crd.Labels { if _, exists := labels[lab.Name]; !exists { - labels[lab.Name] = labT{color: lab.Color, boards: make(map[string]bool)} + labels[lab.Name] = labT{color: lab.Color, boards: make(map[string]trelloBoard)} } - labels[lab.Name].boards[fmt.Sprintf(`%s`, - brd.Board.URL, brd.Board.Name)] = true + labels[lab.Name].boards[brd.Board.URL+"::"+brd.Board.Name] = brd.Board } // process member stats @@ -589,12 +630,12 @@ func buildPayloadAnalysis(config *trelloConfig, render *trelloRender) { brds = append(brds, bname) } sort.Strings(brds) - htm := []template.HTML{} + lbrds := []trelloBoard{} for _, h := range brds { - htm = append(htm, template.HTML(h)) + lbrds = append(lbrds, labels[lname].boards[h]) } render.SharedLabels = append(render.SharedLabels, trelloSharedLabel{ - Name: lname, Color: labels[lname].color, Boards: htm, + Name: lname, Color: labels[lname].color, Boards: lbrds, }) } }