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

Add API management for issue/pull and comment attachments #21783

Merged
merged 60 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
60 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
92d527b
Merge remote-tracking branch 'andre/feature-add-issue-and-comment-att…
KN4CK3R Nov 12, 2022
91c93ed
Add issue/pull permission check.
KN4CK3R Nov 12, 2022
b0461f4
Add issue/comment read access check.
KN4CK3R Nov 12, 2022
27e324a
Fix error log.
KN4CK3R Nov 12, 2022
beee20b
lint
KN4CK3R Nov 12, 2022
7fd332e
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R Nov 23, 2022
57b5f31
Merge remote-tracking branch 'origin/main' into feature-api-attachment
zeripath Dec 4, 2022
cf93210
placate spdx
zeripath Dec 4, 2022
5687791
reduce duplication and remove old ToIssueAttachment
zeripath Dec 4, 2022
8848e0b
attachment not attachEment
zeripath Dec 4, 2022
5438207
use ToAttachments instead
zeripath Dec 4, 2022
4f5e61c
wrap errors too
zeripath Dec 4, 2022
7e49218
Merge branch 'main' into feature-api-attachment
zeripath Dec 4, 2022
5fa0b6b
Merge branch 'main' into feature-api-attachment
lunny Dec 6, 2022
7738345
Merge branch 'main' into feature-api-attachment
lunny Dec 6, 2022
f839f8f
Merge branch 'main' into feature-api-attachment
lunny Dec 8, 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
2 changes: 2 additions & 0 deletions models/issues/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,8 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
return fmt.Errorf("update attachment [%d]: %w", attachments[i].ID, err)
}
}

comment.Attachments = attachments
case CommentTypeReopen, CommentTypeClose:
if err = repo_model.UpdateRepoIssueNumbers(ctx, opts.Issue.RepoID, opts.Issue.IsPull, true); err != nil {
return err
Expand Down
12 changes: 6 additions & 6 deletions models/migrations/v1_10/v96.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ func DeleteOrphanedAttachments(x *xorm.Engine) error {
}

for {
attachements := make([]Attachment, 0, limit)
attachments := make([]Attachment, 0, limit)
if err := sess.Where("`issue_id` = 0 and (`release_id` = 0 or `release_id` not in (select `id` from `release`))").
Cols("id, uuid").Limit(limit).
Asc("id").
Find(&attachements); err != nil {
Find(&attachments); err != nil {
return err
}
if len(attachements) == 0 {
if len(attachments) == 0 {
return nil
}

ids := make([]int64, 0, limit)
for _, attachment := range attachements {
for _, attachment := range attachments {
ids = append(ids, attachment.ID)
}
if len(ids) > 0 {
Expand All @@ -51,13 +51,13 @@ func DeleteOrphanedAttachments(x *xorm.Engine) error {
}
}

for _, attachment := range attachements {
for _, attachment := range attachments {
uuid := attachment.UUID
if err := util.RemoveAll(filepath.Join(setting.Attachment.Path, uuid[0:1], uuid[1:2], uuid)); err != nil {
return err
}
}
if len(attachements) < limit {
if len(attachments) < limit {
return nil
}
}
Expand Down
30 changes: 30 additions & 0 deletions modules/convert/attachment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package convert

import (
repo_model "code.gitea.io/gitea/models/repo"
api "code.gitea.io/gitea/modules/structs"
)

// ToAttachment converts models.Attachment to api.Attachment
func ToAttachment(a *repo_model.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(),
}
}

func ToAttachments(attachments []*repo_model.Attachment) []*api.Attachment {
converted := make([]*api.Attachment, 0, len(attachments))
for _, attachment := range attachments {
converted = append(converted, ToAttachment(attachment))
}
return converted
}
29 changes: 15 additions & 14 deletions modules/convert/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,21 @@ func ToAPIIssue(ctx context.Context, issue *issues_model.Issue) *api.Issue {
}

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, issue.Repo, issue.Repo.Owner),
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: ToAttachments(issue.Attachments),
Ref: issue.Ref,
Labels: ToLabelList(issue.Labels, issue.Repo, issue.Repo.Owner),
State: issue.State(),
IsLocked: issue.IsLocked,
Comments: issue.NumComments,
Created: issue.CreatedUnix.AsTime(),
Updated: issue.UpdatedUnix.AsTime(),
}

apiIssue.Repo = &api.RepositoryMeta{
Expand Down
17 changes: 9 additions & 8 deletions modules/convert/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import (
// ToComment converts a issues_model.Comment to the api.Comment format
func ToComment(c *issues_model.Comment) *api.Comment {
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: ToAttachments(c.Attachments),
Created: c.CreatedUnix.AsTime(),
Updated: c.UpdatedUnix.AsTime(),
}
}

Expand Down
19 changes: 1 addition & 18 deletions modules/convert/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import (

// ToRelease convert a repo_model.Release to api.Release
func ToRelease(r *repo_model.Release) *api.Release {
assets := make([]*api.Attachment, 0)
for _, att := range r.Attachments {
assets = append(assets, ToReleaseAttachment(att))
}
return &api.Release{
ID: r.ID,
TagName: r.TagName,
Expand All @@ -29,19 +25,6 @@ func ToRelease(r *repo_model.Release) *api.Release {
CreatedAt: r.CreatedUnix.AsTime(),
PublishedAt: r.CreatedUnix.AsTime(),
Publisher: ToUser(r.Publisher, nil),
Attachments: assets,
}
}

// ToReleaseAttachment converts models.Attachment to api.Attachment
func ToReleaseAttachment(a *repo_model.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(),
Attachments: ToAttachments(r.Attachments),
}
}
5 changes: 5 additions & 0 deletions modules/notification/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ func (m *webhookNotifier) NotifyNewPullRequest(ctx context.Context, pull *issues
}

func (m *webhookNotifier) NotifyIssueChangeContent(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldContent string) {
if err := issue.LoadRepo(ctx); err != nil {
log.Error("LoadRepo: %v", err)
return
}

mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
var err error
if issue.IsPull {
Expand Down
25 changes: 13 additions & 12 deletions modules/structs/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,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"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little curious, why it's called assets in JSON (and route path), it introduces unnecessary inconsistency IMO.

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 @@ -9,14 +9,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
25 changes: 25 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,13 @@ func mustNotBeArchived(ctx *context.APIContext) {
}
}

func mustEnableAttachments(ctx *context.APIContext) {
if !setting.Attachment.Enabled {
ctx.NotFound()
return
}
}

// bind binding an obj to a func(ctx *context.APIContext)
func bind(obj interface{}) http.HandlerFunc {
tp := reflect.TypeOf(obj)
Expand Down Expand Up @@ -892,6 +899,15 @@ func Routes(ctx gocontext.Context) *web.Route {
Get(repo.GetIssueCommentReactions).
Post(reqToken(), bind(api.EditReactionOption{}), repo.PostIssueCommentReaction).
Delete(reqToken(), bind(api.EditReactionOption{}), repo.DeleteIssueCommentReaction)
m.Group("/assets", func() {
m.Combo("").
Get(repo.ListIssueCommentAttachments).
Post(reqToken(), mustNotBeArchived, repo.CreateIssueCommentAttachment)
m.Combo("/{asset}").
Get(repo.GetIssueCommentAttachment).
Patch(reqToken(), mustNotBeArchived, bind(api.EditAttachmentOptions{}), repo.EditIssueCommentAttachment).
Delete(reqToken(), mustNotBeArchived, repo.DeleteIssueCommentAttachment)
}, mustEnableAttachments)
})
})
m.Group("/{index}", func() {
Expand Down Expand Up @@ -935,6 +951,15 @@ func Routes(ctx gocontext.Context) *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(), mustNotBeArchived, repo.CreateIssueAttachment)
m.Combo("/{asset}").
Get(repo.GetIssueAttachment).
Patch(reqToken(), mustNotBeArchived, bind(api.EditAttachmentOptions{}), repo.EditIssueAttachment).
Delete(reqToken(), mustNotBeArchived, repo.DeleteIssueAttachment)
}, mustEnableAttachments)
})
}, mustEnableIssuesOrPulls)
m.Group("/labels", func() {
Expand Down
Loading