Skip to content

Commit

Permalink
add ReactionSummary to Commit and Issue API responce
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 committed Dec 25, 2019
1 parent 3e166bd commit 1113784
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 39 deletions.
24 changes: 13 additions & 11 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,18 +365,20 @@ func (issue *Issue) apiFormat(e Engine) *api.Issue {

issue.loadPoster(e)
issue.loadRepo(e)
issue.loadReactions(e)
apiIssue := &api.Issue{
ID: issue.ID,
URL: issue.APIURL(),
Index: issue.Index,
Poster: issue.Poster.APIFormat(),
Title: issue.Title,
Body: issue.Content,
Labels: apiLabels,
State: issue.State(),
Comments: issue.NumComments,
Created: issue.CreatedUnix.AsTime(),
Updated: issue.UpdatedUnix.AsTime(),
ID: issue.ID,
URL: issue.APIURL(),
Index: issue.Index,
Poster: issue.Poster.APIFormat(),
Title: issue.Title,
Body: issue.Content,
Labels: apiLabels,
State: issue.State(),
Comments: issue.NumComments,
Created: issue.CreatedUnix.AsTime(),
Updated: issue.UpdatedUnix.AsTime(),
Reactions: issue.Reactions.Summary(),
}

apiIssue.Repo = &api.RepositoryMeta{
Expand Down
18 changes: 10 additions & 8 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,17 @@ func (c *Comment) PRURL() string {

// APIFormat converts a Comment to the api.Comment format
func (c *Comment) APIFormat() *api.Comment {
_ = c.LoadReactions
return &api.Comment{
ID: c.ID,
Poster: c.Poster.APIFormat(),
HTMLURL: c.HTMLURL(),
IssueURL: c.IssueURL(),
PRURL: c.PRURL(),
Body: c.Content,
Created: c.CreatedUnix.AsTime(),
Updated: c.UpdatedUnix.AsTime(),
ID: c.ID,
Poster: c.Poster.APIFormat(),
HTMLURL: c.HTMLURL(),
IssueURL: c.IssueURL(),
PRURL: c.PRURL(),
Body: c.Content,
Created: c.CreatedUnix.AsTime(),
Updated: c.UpdatedUnix.AsTime(),
Reactions: c.Reactions.Summary(),
}
}

Expand Down
17 changes: 17 additions & 0 deletions models/issue_reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"

"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"

"xorm.io/builder"
Expand Down Expand Up @@ -216,6 +217,22 @@ func (list ReactionList) HasUser(userID int64) bool {
return false
}

// Summary return grouped reactions
func (list ReactionList) Summary() []*api.GroupedReaction {
rmap := make(map[string][]string)
var result []*api.GroupedReaction

for _, r := range list {
rmap[r.Type] = append(rmap[r.Type], r.User.Name)
}

for k, v := range rmap {
result = append(result, &api.GroupedReaction{Type: k, Users: v})
}

return result
}

// GroupByType returns reactions grouped by type
func (list ReactionList) GroupByType() map[string]ReactionList {
var reactions = make(map[string]ReactionList)
Expand Down
25 changes: 13 additions & 12 deletions modules/structs/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,19 @@ type RepositoryMeta struct {
// Issue represents an issue in a repository
// swagger:model
type Issue struct {
ID int64 `json:"id"`
URL string `json:"url"`
Index int64 `json:"number"`
Poster *User `json:"user"`
OriginalAuthor string `json:"original_author"`
OriginalAuthorID int64 `json:"original_author_id"`
Title string `json:"title"`
Body string `json:"body"`
Labels []*Label `json:"labels"`
Milestone *Milestone `json:"milestone"`
Assignee *User `json:"assignee"`
Assignees []*User `json:"assignees"`
ID int64 `json:"id"`
URL string `json:"url"`
Index int64 `json:"number"`
Poster *User `json:"user"`
OriginalAuthor string `json:"original_author"`
OriginalAuthorID int64 `json:"original_author_id"`
Title string `json:"title"`
Body string `json:"body"`
Labels []*Label `json:"labels"`
Milestone *Milestone `json:"milestone"`
Assignee *User `json:"assignee"`
Assignees []*User `json:"assignees"`
Reactions []*GroupedReaction `json:"reaction_summary"`
// Whether the issue is open or closed
//
// type: string
Expand Down
17 changes: 9 additions & 8 deletions modules/structs/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (

// Comment represents a comment on a commit or issue
type Comment struct {
ID int64 `json:"id"`
HTMLURL string `json:"html_url"`
PRURL string `json:"pull_request_url"`
IssueURL string `json:"issue_url"`
Poster *User `json:"user"`
OriginalAuthor string `json:"original_author"`
OriginalAuthorID int64 `json:"original_author_id"`
Body string `json:"body"`
ID int64 `json:"id"`
HTMLURL string `json:"html_url"`
PRURL string `json:"pull_request_url"`
IssueURL string `json:"issue_url"`
Poster *User `json:"user"`
OriginalAuthor string `json:"original_author"`
OriginalAuthorID int64 `json:"original_author_id"`
Body string `json:"body"`
Reactions []*GroupedReaction `json:"reaction_summary"`
// swagger:strfmt date-time
Created time.Time `json:"created_at"`
// swagger:strfmt date-time
Expand Down
10 changes: 10 additions & 0 deletions modules/structs/issue_reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ type ReactionResponse struct {
// swagger:strfmt date-time
Created time.Time `json:"created_at"`
}

// ReactionSummary return users who reacted grouped by type
// swagger:model
type ReactionSummary []*GroupedReaction

// GroupedReaction represents a item of ReactionSummary
type GroupedReaction struct {
Type string `json:"type"`
Users []string `json:"users"`
}
7 changes: 7 additions & 0 deletions routers/api/v1/swagger/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,10 @@ type swaggerReactionResponseList struct {
// in:body
Body []api.ReactionResponse `json:"body"`
}

// ReactionSummary
// swagger:response ReactionSummary
type swaggerReactionSummary struct {
// in:body
Body api.ReactionSummary `json:"body"`
}
46 changes: 46 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8227,6 +8227,13 @@
"type": "string",
"x-go-name": "PRURL"
},
"reaction_summary": {
"type": "array",
"items": {
"$ref": "#/definitions/GroupedReaction"
},
"x-go-name": "Reactions"
},
"updated_at": {
"type": "string",
"format": "date-time",
Expand Down Expand Up @@ -9930,6 +9937,24 @@
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"GroupedReaction": {
"description": "GroupedReaction represents a item of ReactionSummary",
"type": "object",
"properties": {
"type": {
"type": "string",
"x-go-name": "Type"
},
"users": {
"type": "array",
"items": {
"type": "string"
},
"x-go-name": "Users"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"Hook": {
"description": "Hook a hook is a web hook when one repository changed",
"type": "object",
Expand Down Expand Up @@ -10082,6 +10107,13 @@
"pull_request": {
"$ref": "#/definitions/PullRequestMeta"
},
"reaction_summary": {
"type": "array",
"items": {
"$ref": "#/definitions/GroupedReaction"
},
"x-go-name": "Reactions"
},
"repository": {
"$ref": "#/definitions/RepositoryMeta"
},
Expand Down Expand Up @@ -10718,6 +10750,14 @@
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"ReactionSummary": {
"description": "ReactionSummary return users who reacted grouped by type",
"type": "array",
"items": {
"$ref": "#/definitions/GroupedReaction"
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"Reference": {
"type": "object",
"title": "Reference represents a Git reference.",
Expand Down Expand Up @@ -11810,6 +11850,12 @@
}
}
},
"ReactionSummary": {
"description": "ReactionSummary",
"schema": {
"$ref": "#/definitions/ReactionSummary"
}
},
"Reference": {
"description": "Reference",
"schema": {
Expand Down

0 comments on commit 1113784

Please sign in to comment.