cli/api/reaction_groups.go
Mislav Marohnić 9bdc63c4ca Eliminate API overfetching in pr commands
This completely rewrites the PR lookup mechanism so that the caller
must specify the GraphQL fields to query for each PR. Additionally, this
fixes some export problems with `pr view --json`.

Features:

- Each pr command now gets assigned a concept of a Finder. This makes it
  easier to stub the PR in tests without having to stub the underlying
  HTTP calls or git invocations.

- `pr view --web` is much faster since it only fetches the "url" field.

- `pr diff 123` now skips a whole API call where a whole PR was
  unnecessarily preloaded just to access its diff in a subsequent call.

- PullRequestGraphQL query builder is now used to construct queries.

- A bunch of individual commands are now freed of having to know about
  concepts such as BaseRepo, Branch, Config, or Remotes.
2021-04-30 20:34:36 +02:00

59 lines
1.1 KiB
Go

package api
import (
"bytes"
"encoding/json"
)
type ReactionGroups []ReactionGroup
func (rg ReactionGroups) MarshalJSON() ([]byte, error) {
buf := bytes.Buffer{}
buf.WriteRune('[')
encoder := json.NewEncoder(&buf)
encoder.SetEscapeHTML(false)
hasPrev := false
for _, g := range rg {
if g.Users.TotalCount == 0 {
continue
}
if hasPrev {
buf.WriteRune(',')
}
if err := encoder.Encode(&g); err != nil {
return nil, err
}
hasPrev = true
}
buf.WriteRune(']')
return buf.Bytes(), nil
}
type ReactionGroup struct {
Content string `json:"content"`
Users ReactionGroupUsers `json:"users"`
}
type ReactionGroupUsers struct {
TotalCount int `json:"totalCount"`
}
func (rg ReactionGroup) Count() int {
return rg.Users.TotalCount
}
func (rg ReactionGroup) Emoji() string {
return reactionEmoji[rg.Content]
}
var reactionEmoji = map[string]string{
"THUMBS_UP": "\U0001f44d",
"THUMBS_DOWN": "\U0001f44e",
"LAUGH": "\U0001f604",
"HOORAY": "\U0001f389",
"CONFUSED": "\U0001f615",
"HEART": "\u2764\ufe0f",
"ROCKET": "\U0001f680",
"EYES": "\U0001f440",
}