Skip to content

Commit

Permalink
feat: expore more fields on the JSON API
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Oct 17, 2018
1 parent eddb42b commit 9fd33be
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
4 changes: 3 additions & 1 deletion cmd_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func webListIssues(w http.ResponseWriter, r *http.Request) {
return
}

issues.prepare(true)

targets := strings.Split(r.URL.Query().Get("targets"), ",")
issues.filterByTargets(targets)

Expand All @@ -79,7 +81,7 @@ func webListIssues(w http.ResponseWriter, r *http.Request) {
if issue.Hidden {
continue
}
list = append(list, issue)
list = append(list, issue.WithJSONFields())
}

if err := render.RenderList(w, r, list); err != nil {
Expand Down
76 changes: 49 additions & 27 deletions issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,43 @@ type Issue struct {
GitLab *gitlab.Issue `json:"-" gorm:"-"`

// internal
Provider Provider
Provider Provider `json:"provider"`
DependsOn IssueSlice `json:"-" gorm:"-"`
Blocks IssueSlice `json:"-" gorm:"-"`
weightMultiplier int `gorm:"-"`
weightMultiplier int `gorm:"-" json:"-"`
BaseWeight int `json:"-" gorm:"-"`
IsOrphan bool `json:"-" gorm:"-"`
Hidden bool `json:"-" gorm:"-"`
Duplicates []string `json:"-" gorm:"-"`
LinkedWithEpic bool `json:"-" gorm:"-"`
Errors []error `json:"-" gorm:"-"`
IsOrphan bool `json:"is-orphan" gorm:"-"`
Hidden bool `json:"is-hidden" gorm:"-"`
Duplicates []string `json:"duplicates" gorm:"-"`
LinkedWithEpic bool `json:"is-linked-with-an-epic" gorm:"-"`
Errors []error `json:"errors" gorm:"-"`

// mapping
CreatedAt time.Time
UpdatedAt time.Time
CompletedAt time.Time
Number int
Title string
State string
Body string
RepoURL string
URL string `gorm:"primary_key"`
Labels []*IssueLabel `gorm:"many2many:issue_labels;"`
Assignees []*Profile `gorm:"many2many:issue_assignees;"`
IsPR bool

Locked bool
Author Profile
AuthorID string
Comments int
Milestone string
Upvotes int
Downvotes int
CreatedAt time.Time `json:"created-at"`
UpdatedAt time.Time `json:"updated-at"`
CompletedAt time.Time `json:"completed-at"`
Number int `json:"number"`
Title string `json:"title"`
State string `json:"state"`
Body string `json:"body"`
RepoURL string `json:"repo-url"`
URL string `gorm:"primary_key" json:"url"`
Labels []*IssueLabel `gorm:"many2many:issue_labels;" json:"labels"`
Assignees []*Profile `gorm:"many2many:issue_assignees;" json:"assignees"`
IsPR bool `json:"is-pr"`

// json export fields
JSONChildren []string `gorm:"-" json:"children"`
JSONParents []string `gorm:"-" json:"parents"`
JSONWeight int `gorm:"-" json:"weight"`

Locked bool `json:"is-locked"`
Author Profile `json:"author"`
AuthorID string `json:"-"`
Comments int `json:"comments"`
Milestone string `json:"milestone"`
Upvotes int `json:"upvotes"`
Downvotes int `json:"downvotes"`
}

type IssueLabel struct {
Expand Down Expand Up @@ -174,6 +179,23 @@ func FromGitLabIssue(input *gitlab.Issue) *Issue {
return issue
}

func (i *Issue) WithJSONFields() *Issue {
i.JSONWeight = i.Weight()
if len(i.Blocks) > 0 {
i.JSONParents = []string{}
for _, rel := range i.Blocks {
i.JSONParents = append(i.JSONParents, rel.URL)
}
}
if len(i.DependsOn) > 0 {
i.JSONChildren = []string{}
for _, rel := range i.DependsOn {
i.JSONChildren = append(i.JSONChildren, rel.URL)
}
}
return i
}

func (i Issue) Path() string {
u, err := url.Parse(i.URL)
if err != nil {
Expand Down

0 comments on commit 9fd33be

Please sign in to comment.