From 9fd33be61000440bca4b251838c340ce88fc3a91 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Wed, 17 Oct 2018 16:55:15 +0200 Subject: [PATCH] feat: expore more fields on the JSON API --- cmd_web.go | 4 ++- issue.go | 76 +++++++++++++++++++++++++++++++++++------------------- 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/cmd_web.go b/cmd_web.go index 8f7517a16..cf01fedbb 100644 --- a/cmd_web.go +++ b/cmd_web.go @@ -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) @@ -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 { diff --git a/issue.go b/issue.go index a4c9fc994..e3a619c42 100644 --- a/issue.go +++ b/issue.go @@ -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 { @@ -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 {