Skip to content

Commit 1113784

Browse files
committed
add ReactionSummary to Commit and Issue API responce
1 parent 3e166bd commit 1113784

File tree

8 files changed

+125
-39
lines changed

8 files changed

+125
-39
lines changed

models/issue.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -365,18 +365,20 @@ func (issue *Issue) apiFormat(e Engine) *api.Issue {
365365

366366
issue.loadPoster(e)
367367
issue.loadRepo(e)
368+
issue.loadReactions(e)
368369
apiIssue := &api.Issue{
369-
ID: issue.ID,
370-
URL: issue.APIURL(),
371-
Index: issue.Index,
372-
Poster: issue.Poster.APIFormat(),
373-
Title: issue.Title,
374-
Body: issue.Content,
375-
Labels: apiLabels,
376-
State: issue.State(),
377-
Comments: issue.NumComments,
378-
Created: issue.CreatedUnix.AsTime(),
379-
Updated: issue.UpdatedUnix.AsTime(),
370+
ID: issue.ID,
371+
URL: issue.APIURL(),
372+
Index: issue.Index,
373+
Poster: issue.Poster.APIFormat(),
374+
Title: issue.Title,
375+
Body: issue.Content,
376+
Labels: apiLabels,
377+
State: issue.State(),
378+
Comments: issue.NumComments,
379+
Created: issue.CreatedUnix.AsTime(),
380+
Updated: issue.UpdatedUnix.AsTime(),
381+
Reactions: issue.Reactions.Summary(),
380382
}
381383

382384
apiIssue.Repo = &api.RepositoryMeta{

models/issue_comment.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,17 @@ func (c *Comment) PRURL() string {
275275

276276
// APIFormat converts a Comment to the api.Comment format
277277
func (c *Comment) APIFormat() *api.Comment {
278+
_ = c.LoadReactions
278279
return &api.Comment{
279-
ID: c.ID,
280-
Poster: c.Poster.APIFormat(),
281-
HTMLURL: c.HTMLURL(),
282-
IssueURL: c.IssueURL(),
283-
PRURL: c.PRURL(),
284-
Body: c.Content,
285-
Created: c.CreatedUnix.AsTime(),
286-
Updated: c.UpdatedUnix.AsTime(),
280+
ID: c.ID,
281+
Poster: c.Poster.APIFormat(),
282+
HTMLURL: c.HTMLURL(),
283+
IssueURL: c.IssueURL(),
284+
PRURL: c.PRURL(),
285+
Body: c.Content,
286+
Created: c.CreatedUnix.AsTime(),
287+
Updated: c.UpdatedUnix.AsTime(),
288+
Reactions: c.Reactions.Summary(),
287289
}
288290
}
289291

models/issue_reaction.go

+17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010

1111
"code.gitea.io/gitea/modules/setting"
12+
api "code.gitea.io/gitea/modules/structs"
1213
"code.gitea.io/gitea/modules/timeutil"
1314

1415
"xorm.io/builder"
@@ -216,6 +217,22 @@ func (list ReactionList) HasUser(userID int64) bool {
216217
return false
217218
}
218219

220+
// Summary return grouped reactions
221+
func (list ReactionList) Summary() []*api.GroupedReaction {
222+
rmap := make(map[string][]string)
223+
var result []*api.GroupedReaction
224+
225+
for _, r := range list {
226+
rmap[r.Type] = append(rmap[r.Type], r.User.Name)
227+
}
228+
229+
for k, v := range rmap {
230+
result = append(result, &api.GroupedReaction{Type: k, Users: v})
231+
}
232+
233+
return result
234+
}
235+
219236
// GroupByType returns reactions grouped by type
220237
func (list ReactionList) GroupByType() map[string]ReactionList {
221238
var reactions = make(map[string]ReactionList)

modules/structs/issue.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,19 @@ type RepositoryMeta struct {
3636
// Issue represents an issue in a repository
3737
// swagger:model
3838
type Issue struct {
39-
ID int64 `json:"id"`
40-
URL string `json:"url"`
41-
Index int64 `json:"number"`
42-
Poster *User `json:"user"`
43-
OriginalAuthor string `json:"original_author"`
44-
OriginalAuthorID int64 `json:"original_author_id"`
45-
Title string `json:"title"`
46-
Body string `json:"body"`
47-
Labels []*Label `json:"labels"`
48-
Milestone *Milestone `json:"milestone"`
49-
Assignee *User `json:"assignee"`
50-
Assignees []*User `json:"assignees"`
39+
ID int64 `json:"id"`
40+
URL string `json:"url"`
41+
Index int64 `json:"number"`
42+
Poster *User `json:"user"`
43+
OriginalAuthor string `json:"original_author"`
44+
OriginalAuthorID int64 `json:"original_author_id"`
45+
Title string `json:"title"`
46+
Body string `json:"body"`
47+
Labels []*Label `json:"labels"`
48+
Milestone *Milestone `json:"milestone"`
49+
Assignee *User `json:"assignee"`
50+
Assignees []*User `json:"assignees"`
51+
Reactions []*GroupedReaction `json:"reaction_summary"`
5152
// Whether the issue is open or closed
5253
//
5354
// type: string

modules/structs/issue_comment.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import (
1010

1111
// Comment represents a comment on a commit or issue
1212
type Comment struct {
13-
ID int64 `json:"id"`
14-
HTMLURL string `json:"html_url"`
15-
PRURL string `json:"pull_request_url"`
16-
IssueURL string `json:"issue_url"`
17-
Poster *User `json:"user"`
18-
OriginalAuthor string `json:"original_author"`
19-
OriginalAuthorID int64 `json:"original_author_id"`
20-
Body string `json:"body"`
13+
ID int64 `json:"id"`
14+
HTMLURL string `json:"html_url"`
15+
PRURL string `json:"pull_request_url"`
16+
IssueURL string `json:"issue_url"`
17+
Poster *User `json:"user"`
18+
OriginalAuthor string `json:"original_author"`
19+
OriginalAuthorID int64 `json:"original_author_id"`
20+
Body string `json:"body"`
21+
Reactions []*GroupedReaction `json:"reaction_summary"`
2122
// swagger:strfmt date-time
2223
Created time.Time `json:"created_at"`
2324
// swagger:strfmt date-time

modules/structs/issue_reaction.go

+10
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,13 @@ type ReactionResponse struct {
2020
// swagger:strfmt date-time
2121
Created time.Time `json:"created_at"`
2222
}
23+
24+
// ReactionSummary return users who reacted grouped by type
25+
// swagger:model
26+
type ReactionSummary []*GroupedReaction
27+
28+
// GroupedReaction represents a item of ReactionSummary
29+
type GroupedReaction struct {
30+
Type string `json:"type"`
31+
Users []string `json:"users"`
32+
}

routers/api/v1/swagger/issue.go

+7
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,10 @@ type swaggerReactionResponseList struct {
119119
// in:body
120120
Body []api.ReactionResponse `json:"body"`
121121
}
122+
123+
// ReactionSummary
124+
// swagger:response ReactionSummary
125+
type swaggerReactionSummary struct {
126+
// in:body
127+
Body api.ReactionSummary `json:"body"`
128+
}

templates/swagger/v1_json.tmpl

+46
Original file line numberDiff line numberDiff line change
@@ -8227,6 +8227,13 @@
82278227
"type": "string",
82288228
"x-go-name": "PRURL"
82298229
},
8230+
"reaction_summary": {
8231+
"type": "array",
8232+
"items": {
8233+
"$ref": "#/definitions/GroupedReaction"
8234+
},
8235+
"x-go-name": "Reactions"
8236+
},
82308237
"updated_at": {
82318238
"type": "string",
82328239
"format": "date-time",
@@ -9930,6 +9937,24 @@
99309937
},
99319938
"x-go-package": "code.gitea.io/gitea/modules/structs"
99329939
},
9940+
"GroupedReaction": {
9941+
"description": "GroupedReaction represents a item of ReactionSummary",
9942+
"type": "object",
9943+
"properties": {
9944+
"type": {
9945+
"type": "string",
9946+
"x-go-name": "Type"
9947+
},
9948+
"users": {
9949+
"type": "array",
9950+
"items": {
9951+
"type": "string"
9952+
},
9953+
"x-go-name": "Users"
9954+
}
9955+
},
9956+
"x-go-package": "code.gitea.io/gitea/modules/structs"
9957+
},
99339958
"Hook": {
99349959
"description": "Hook a hook is a web hook when one repository changed",
99359960
"type": "object",
@@ -10082,6 +10107,13 @@
1008210107
"pull_request": {
1008310108
"$ref": "#/definitions/PullRequestMeta"
1008410109
},
10110+
"reaction_summary": {
10111+
"type": "array",
10112+
"items": {
10113+
"$ref": "#/definitions/GroupedReaction"
10114+
},
10115+
"x-go-name": "Reactions"
10116+
},
1008510117
"repository": {
1008610118
"$ref": "#/definitions/RepositoryMeta"
1008710119
},
@@ -10718,6 +10750,14 @@
1071810750
},
1071910751
"x-go-package": "code.gitea.io/gitea/modules/structs"
1072010752
},
10753+
"ReactionSummary": {
10754+
"description": "ReactionSummary return users who reacted grouped by type",
10755+
"type": "array",
10756+
"items": {
10757+
"$ref": "#/definitions/GroupedReaction"
10758+
},
10759+
"x-go-package": "code.gitea.io/gitea/modules/structs"
10760+
},
1072110761
"Reference": {
1072210762
"type": "object",
1072310763
"title": "Reference represents a Git reference.",
@@ -11810,6 +11850,12 @@
1181011850
}
1181111851
}
1181211852
},
11853+
"ReactionSummary": {
11854+
"description": "ReactionSummary",
11855+
"schema": {
11856+
"$ref": "#/definitions/ReactionSummary"
11857+
}
11858+
},
1181311859
"Reference": {
1181411860
"description": "Reference",
1181511861
"schema": {

0 commit comments

Comments
 (0)