Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] [API] add ReactionSummary to Commit and Issue #9497

Closed
wants to merge 10 commits into from
12 changes: 11 additions & 1 deletion integrations/api_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ func TestAPIListIssues(t *testing.T) {
DecodeJSON(t, resp, &apiIssues)
assert.Len(t, apiIssues, models.GetCount(t, &models.Issue{RepoID: repo.ID}))
for _, apiIssue := range apiIssues {
models.AssertExistsAndLoadBean(t, &models.Issue{ID: apiIssue.ID, RepoID: repo.ID})
issue, err := models.GetIssueByID(apiIssue.ID)
assert.NoError(t, err)
assert.NoError(t, issue.LoadAttributes())
assert.Equal(t, repo.ID, issue.RepoID)
assert.Equal(t, issue.PosterID, apiIssue.Poster.ID)
assert.Equal(t, int64(issue.UpdatedUnix), apiIssue.Updated.Unix())
summary := issue.Reactions.Summary()
for i, apiR := range apiIssue.Reactions {
assert.Equal(t, summary[i].Type, apiR.Type)
assert.EqualValues(t, summary[i].Users, apiR.Users)
}
}

// test milestone filter
Expand Down
5 changes: 5 additions & 0 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ func (issue *Issue) loadCommentsByType(e Engine, tp CommentType) (err error) {
return err
}

// LoadReactions loads reactions
func (issue *Issue) LoadReactions() (err error) {
return issue.loadReactions(x)
}

func (issue *Issue) loadReactions(e Engine) (err error) {
if issue.Reactions != nil {
return nil
Expand Down
18 changes: 10 additions & 8 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,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 @@ -254,6 +255,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
31 changes: 18 additions & 13 deletions modules/convert/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,25 @@ func ToAPIIssue(issue *models.Issue) *api.Issue {
return &api.Issue{}
}

if err := issue.LoadReactions(); err != nil {
return &api.Issue{}
}

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

apiIssue.Repo = &api.RepositoryMeta{
Expand Down
27 changes: 14 additions & 13 deletions modules/structs/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,20 @@ type RepositoryMeta struct {
// Issue represents an issue in a repository
// swagger:model
type Issue struct {
ID int64 `json:"id"`
URL string `json:"url"`
HTMLURL string `json:"html_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"`
HTMLURL string `json:"html_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 ReactionSummary `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 ReactionSummary `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 Reaction 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 swaggerReactionList struct {
// in:body
Body []api.Reaction `json:"body"`
}

// ReactionSummary
// swagger:response ReactionSummary
type swaggerReactionSummary struct {
// in:body
Body api.ReactionSummary `json:"body"`
}
38 changes: 38 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11273,6 +11273,9 @@
"type": "string",
"x-go-name": "PRURL"
},
"reaction_summary": {
"$ref": "#/definitions/ReactionSummary"
},
"updated_at": {
"type": "string",
"format": "date-time",
Expand Down Expand Up @@ -13461,6 +13464,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 @@ -13621,6 +13642,9 @@
"pull_request": {
"$ref": "#/definitions/PullRequestMeta"
},
"reaction_summary": {
"$ref": "#/definitions/ReactionSummary"
},
"repository": {
"$ref": "#/definitions/RepositoryMeta"
},
Expand Down Expand Up @@ -14633,6 +14657,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 @@ -15931,6 +15963,12 @@
}
}
},
"ReactionSummary": {
"description": "ReactionSummary",
"schema": {
"$ref": "#/definitions/ReactionSummary"
}
},
"Reference": {
"description": "Reference",
"schema": {
Expand Down