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

[API] Add issue and comment attachments #14601

Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
f9595c3
[API] Add issue and comment attachments
andrebruch Feb 7, 2021
04abe85
Specify capacity for assets slice
andrebruch Feb 8, 2021
bef0c92
Make comment attachments accessible
andrebruch Mar 2, 2021
10d7f90
Update attachments in correct order
andrebruch Mar 2, 2021
806143c
Fix: Notifications for API attachments
andrebruch Mar 3, 2021
dc49872
Fix: new line in v1_json.tmpl
andrebruch Mar 13, 2021
7393e67
Fix import order
andrebruch Mar 13, 2021
488a072
Fix missed return
andrebruch Mar 13, 2021
7ac28eb
Merge branch 'master' into feature-add-issue-and-comment-attachments-api
zeripath Mar 13, 2021
6e3cf41
Merge remote-tracking branch 'origin/main' into feature-add-issue-and…
zeripath Jun 27, 2021
7dff40d
Merge branch 'main' into feature-add-issue-and-comment-attachments-api
6543 Jun 29, 2021
75785e1
partial correction of permissions checks
zeripath Jun 29, 2021
202ece1
Fix title of error
andrebruch Oct 2, 2021
587605e
Merge functions ToIssueAttachment and ToCommentAttachment
andrebruch Oct 2, 2021
036dd17
deduplicate convert.ToAttachment()
noerw Sep 30, 2021
9c433e5
refer to issue by index, not by ID..
noerw Sep 30, 2021
065d88f
Merge branch 'master' into feature-add-issue-and-comment-attachments-api
6543 Oct 3, 2021
d883f69
use attachment_service.UploadAttachment()
noerw Sep 30, 2021
e98bf3e
fix permissions of *IssueAttachment()
noerw Oct 2, 2021
f2a05e7
WIP: fix permissions of *IssueCommentAttachment()
noerw Oct 2, 2021
ad9a947
rm dublicate func
6543 Oct 3, 2021
f99ee82
update swagger
6543 Oct 3, 2021
5134ff6
fixup! fix permissions of *IssueAttachment()
noerw Oct 8, 2021
7557df1
fix permissions of *IssueCommentAttachment() (cont)
noerw Oct 8, 2021
170134a
move all asset APIs into issue / comment namespace
noerw Oct 8, 2021
a776dc2
fix swagger docs
noerw Oct 8, 2021
bfafd9d
fix updating issue
noerw Oct 8, 2021
ed6d55e
Merge branch 'main' into feature-add-issue-and-comment-attachments-api
6543 Oct 8, 2021
6b521f2
Correct the year of the code contribution
andrebruch Oct 15, 2021
e5633d8
Merge branch 'main' into feature-add-issue-and-comment-attachments-api
6543 Oct 19, 2021
32e8508
fix errors
noerw Oct 9, 2021
d32924d
Fix call of GetIssueByIndex and correction of grammar errors
andrebruch Oct 21, 2021
500d163
Merge branch 'main' into feature-add-issue-and-comment-attachments-api
andrebruch Oct 21, 2021
1459011
Remove redundant `return` statement
andrebruch Oct 21, 2021
6d6b858
make generate-swagger
andrebruch Oct 21, 2021
6517302
Add newline to v1_json.tmpl
andrebruch Oct 21, 2021
cbd1a77
[API] Add integration tests for issue attachments
andrebruch Oct 23, 2021
992b8ec
Merge branch 'main' into feature-add-issue-and-comment-attachments-api
6543 Oct 25, 2021
9227070
[API] Add integration tests for comment attachments
andrebruch Oct 29, 2021
d05625b
Merge branch 'main' into feature-add-issue-and-comment-attachments-api
6543 Oct 29, 2021
e39c3de
Merge branch 'master' into feature-add-issue-and-comment-attachments-api
6543 Apr 22, 2022
fbab446
adapt latest refactors
6543 Apr 22, 2022
c8cafc0
rm merge conflict resolve
6543 Apr 22, 2022
5bd533c
format & fix
6543 Apr 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,18 +770,21 @@ func updateCommentInfos(e *xorm.Session, opts *CreateCommentOptions, comment *Co
}

// Check attachments
attachments, err := getAttachmentsByUUIDs(e, opts.Attachments)
if err != nil {
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %v", opts.Attachments, err)
}
if len(opts.Attachments) > 0 {
attachments, err := getAttachmentsByUUIDs(e, opts.Attachments)
if err != nil {
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %v", opts.Attachments, err)
}

for i := range attachments {
attachments[i].IssueID = opts.Issue.ID
attachments[i].CommentID = comment.ID
// No assign value could be 0, so ignore AllCols().
if _, err = e.ID(attachments[i].ID).Update(attachments[i]); err != nil {
return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
for i := range attachments {
attachments[i].IssueID = opts.Issue.ID
attachments[i].CommentID = comment.ID
// No assign value could be 0, so ignore AllCols().
if _, err = e.ID(attachments[i].ID).Update(attachments[i]); err != nil {
return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
}
}
comment.Attachments = attachments
}
case CommentTypeReopen, CommentTypeClose:
if err = opts.Issue.updateClosedNum(e); err != nil {
Expand Down
47 changes: 33 additions & 14 deletions modules/convert/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,27 @@ func ToAPIIssue(issue *models.Issue) *api.Issue {
return &api.Issue{}
}

assets := make([]*api.Attachment, 0, len(issue.Attachments))
for _, att := range issue.Attachments {
assets = append(assets, ToIssueAttachment(att))
}

apiIssue := &api.Issue{
ID: issue.ID,
URL: issue.APIURL(),
HTMLURL: issue.HTMLURL(),
Index: issue.Index,
Poster: ToUser(issue.Poster, nil),
Title: issue.Title,
Body: issue.Content,
Ref: issue.Ref,
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: ToUser(issue.Poster, nil),
Title: issue.Title,
Body: issue.Content,
Attachments: assets,
Ref: issue.Ref,
Labels: ToLabelList(issue.Labels),
State: issue.State(),
IsLocked: issue.IsLocked,
Comments: issue.NumComments,
Created: issue.CreatedUnix.AsTime(),
Updated: issue.UpdatedUnix.AsTime(),
}

apiIssue.Repo = &api.RepositoryMeta{
Expand Down Expand Up @@ -206,3 +212,16 @@ func ToAPIMilestone(m *models.Milestone) *api.Milestone {
}
return apiMilestone
}

// ToIssueAttachment converts models.Attachment to api.Attachment
func ToIssueAttachment(a *models.Attachment) *api.Attachment {
return &api.Attachment{
ID: a.ID,
Name: a.Name,
Created: a.CreatedUnix.AsTime(),
DownloadCount: a.DownloadCount,
Size: a.Size,
UUID: a.UUID,
DownloadURL: a.DownloadURL(),
}
}
34 changes: 26 additions & 8 deletions modules/convert/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,32 @@ import (

// ToComment converts a models.Comment to the api.Comment format
func ToComment(c *models.Comment) *api.Comment {
assets := make([]*api.Attachment, 0, len(c.Attachments))
for _, att := range c.Attachments {
assets = append(assets, ToCommentAttachment(att))
}
return &api.Comment{
ID: c.ID,
Poster: ToUser(c.Poster, nil),
HTMLURL: c.HTMLURL(),
IssueURL: c.IssueURL(),
PRURL: c.PRURL(),
Body: c.Content,
Created: c.CreatedUnix.AsTime(),
Updated: c.UpdatedUnix.AsTime(),
ID: c.ID,
Poster: ToUser(c.Poster, nil),
HTMLURL: c.HTMLURL(),
IssueURL: c.IssueURL(),
PRURL: c.PRURL(),
Body: c.Content,
Attachments: assets,
Created: c.CreatedUnix.AsTime(),
Updated: c.UpdatedUnix.AsTime(),
}
}

// ToCommentAttachment converts models.Attachment to api.Attachment
func ToCommentAttachment(a *models.Attachment) *api.Attachment {
andrebruch marked this conversation as resolved.
Show resolved Hide resolved
return &api.Attachment{
ID: a.ID,
Name: a.Name,
Created: a.CreatedUnix.AsTime(),
DownloadCount: a.DownloadCount,
Size: a.Size,
UUID: a.UUID,
DownloadURL: a.DownloadURL(),
}
}
25 changes: 13 additions & 12 deletions modules/structs/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,19 @@ 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"`
Ref string `json:"ref"`
Labels []*Label `json:"labels"`
Milestone *Milestone `json:"milestone"`
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"`
Ref string `json:"ref"`
Attachments []*Attachment `json:"assets"`
Labels []*Label `json:"labels"`
Milestone *Milestone `json:"milestone"`
// deprecated
Assignee *User `json:"assignee"`
Assignees []*User `json:"assignees"`
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"`
Attachments []*Attachment `json:"assets"`
// swagger:strfmt date-time
Created time.Time `json:"created_at"`
// swagger:strfmt date-time
Expand Down
16 changes: 16 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,13 @@ func Routes() *web.Route {
Get(repo.GetIssueCommentReactions).
Post(reqToken(), bind(api.EditReactionOption{}), repo.PostIssueCommentReaction).
Delete(reqToken(), bind(api.EditReactionOption{}), repo.DeleteIssueCommentReaction)
m.Combo("/assets").Get(repo.ListIssueCommentAttachments).
Post(reqToken(), reqRepoWriter(models.UnitTypeIssues), repo.CreateIssueCommentAttachment)
})
m.Group("/assets", func() {
m.Combo("/{asset}").Get(repo.GetIssueCommentAttachment).
Patch(reqToken(), reqRepoWriter(models.UnitTypeIssues), bind(api.EditAttachmentOptions{}), repo.EditIssueCommentAttachment).
Delete(reqToken(), reqRepoWriter(models.UnitTypeIssues), repo.DeleteIssueCommentAttachment)
6543 marked this conversation as resolved.
Show resolved Hide resolved
})
})
m.Group("/{index}", func() {
Expand Down Expand Up @@ -848,6 +855,15 @@ func Routes() *web.Route {
Get(repo.GetIssueReactions).
Post(reqToken(), bind(api.EditReactionOption{}), repo.PostIssueReaction).
Delete(reqToken(), bind(api.EditReactionOption{}), repo.DeleteIssueReaction)
m.Group("/assets", func() {
m.Combo("").Get(repo.ListIssueAttachments).
Post(reqToken(), reqRepoWriter(models.UnitTypeIssues), repo.CreateIssueAttachment)
})
})
m.Group("/assets", func() {
m.Combo("/{asset}").Get(repo.GetIssueAttachment).
Patch(reqToken(), reqRepoWriter(models.UnitTypeIssues), bind(api.EditAttachmentOptions{}), repo.EditIssueAttachment).
Delete(reqToken(), reqRepoWriter(models.UnitTypeIssues), repo.DeleteIssueAttachment)
6543 marked this conversation as resolved.
Show resolved Hide resolved
})
}, mustEnableIssuesOrPulls)
m.Group("/labels", func() {
Expand Down
Loading