Skip to content

Commit

Permalink
For API attachments, use API URL (go-gitea#25639) (go-gitea#25814)
Browse files Browse the repository at this point in the history
Backport go-gitea#25639 by @lunny

Fix go-gitea#25257

Co-authored-by: Lunny Xiao <[email protected]>
  • Loading branch information
GiteaBot and lunny authored Jul 10, 2023
1 parent b4460cf commit 2b79d3f
Show file tree
Hide file tree
Showing 19 changed files with 108 additions and 67 deletions.
7 changes: 0 additions & 7 deletions models/repo/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ func (a *Attachment) DownloadURL() string {
return setting.AppURL + "attachments/" + url.PathEscape(a.UUID)
}

// _____ __ __ .__ __
// / _ \_/ |__/ |______ ____ | |__ _____ ____ _____/ |_
// / /_\ \ __\ __\__ \ _/ ___\| | \ / \_/ __ \ / \ __\
// / | \ | | | / __ \\ \___| Y \ Y Y \ ___/| | \ |
// \____|__ /__| |__| (____ /\___ >___| /__|_| /\___ >___| /__|
// \/ \/ \/ \/ \/ \/ \/

// ErrAttachmentNotExist represents a "AttachmentNotExist" kind of error.
type ErrAttachmentNotExist struct {
ID int64
Expand Down
6 changes: 3 additions & 3 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ func Routes(ctx gocontext.Context) *web.Route {
m.Group("/assets", func() {
m.Combo("").Get(repo.ListReleaseAttachments).
Post(reqToken(), reqRepoWriter(unit.TypeReleases), repo.CreateReleaseAttachment)
m.Combo("/{asset}").Get(repo.GetReleaseAttachment).
m.Combo("/{attachment_id}").Get(repo.GetReleaseAttachment).
Patch(reqToken(), reqRepoWriter(unit.TypeReleases), bind(api.EditAttachmentOptions{}), repo.EditReleaseAttachment).
Delete(reqToken(), reqRepoWriter(unit.TypeReleases), repo.DeleteReleaseAttachment)
})
Expand Down Expand Up @@ -1176,7 +1176,7 @@ func Routes(ctx gocontext.Context) *web.Route {
m.Combo("").
Get(repo.ListIssueCommentAttachments).
Post(reqToken(), mustNotBeArchived, repo.CreateIssueCommentAttachment)
m.Combo("/{asset}").
m.Combo("/{attachment_id}").
Get(repo.GetIssueCommentAttachment).
Patch(reqToken(), mustNotBeArchived, bind(api.EditAttachmentOptions{}), repo.EditIssueCommentAttachment).
Delete(reqToken(), mustNotBeArchived, repo.DeleteIssueCommentAttachment)
Expand Down Expand Up @@ -1228,7 +1228,7 @@ func Routes(ctx gocontext.Context) *web.Route {
m.Combo("").
Get(repo.ListIssueAttachments).
Post(reqToken(), mustNotBeArchived, repo.CreateIssueAttachment)
m.Combo("/{asset}").
m.Combo("/{attachment_id}").
Get(repo.GetIssueAttachment).
Patch(reqToken(), mustNotBeArchived, bind(api.EditAttachmentOptions{}), repo.EditIssueAttachment).
Delete(reqToken(), mustNotBeArchived, repo.DeleteIssueAttachment)
Expand Down
8 changes: 4 additions & 4 deletions routers/api/v1/repo/issue_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func GetIssueAttachment(ctx *context.APIContext) {
return
}

ctx.JSON(http.StatusOK, convert.ToAttachment(attach))
ctx.JSON(http.StatusOK, convert.ToAPIAttachment(ctx.Repo.Repository, attach))
}

// ListIssueAttachments lists all attachments of the issue
Expand Down Expand Up @@ -194,7 +194,7 @@ func CreateIssueAttachment(ctx *context.APIContext) {
return
}

ctx.JSON(http.StatusCreated, convert.ToAttachment(attachment))
ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attachment))
}

// EditIssueAttachment updates the given attachment
Expand Down Expand Up @@ -254,7 +254,7 @@ func EditIssueAttachment(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "UpdateAttachment", err)
}

ctx.JSON(http.StatusCreated, convert.ToAttachment(attachment))
ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attachment))
}

// DeleteIssueAttachment delete a given attachment
Expand Down Expand Up @@ -332,7 +332,7 @@ func getIssueAttachmentSafeWrite(ctx *context.APIContext) *repo_model.Attachment
}

func getIssueAttachmentSafeRead(ctx *context.APIContext, issue *issues_model.Issue) *repo_model.Attachment {
attachment, err := repo_model.GetAttachmentByID(ctx, ctx.ParamsInt64("asset"))
attachment, err := repo_model.GetAttachmentByID(ctx, ctx.ParamsInt64("attachment_id"))
if err != nil {
ctx.NotFoundOrServerError("GetAttachmentByID", repo_model.IsErrAttachmentNotExist, err)
return nil
Expand Down
12 changes: 6 additions & 6 deletions routers/api/v1/repo/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func ListIssueComments(ctx *context.APIContext) {
apiComments := make([]*api.Comment, len(comments))
for i, comment := range comments {
comment.Issue = issue
apiComments[i] = convert.ToComment(ctx, comments[i])
apiComments[i] = convert.ToAPIComment(ctx, ctx.Repo.Repository, comments[i])
}

ctx.SetTotalCountHeader(totalCount)
Expand Down Expand Up @@ -191,7 +191,7 @@ func ListIssueCommentsAndTimeline(ctx *context.APIContext) {
for _, comment := range comments {
if comment.Type != issues_model.CommentTypeCode && isXRefCommentAccessible(ctx, ctx.Doer, comment, issue.RepoID) {
comment.Issue = issue
apiComments = append(apiComments, convert.ToTimelineComment(ctx, comment, ctx.Doer))
apiComments = append(apiComments, convert.ToTimelineComment(ctx, issue.Repo, comment, ctx.Doer))
}
}

Expand Down Expand Up @@ -308,7 +308,7 @@ func ListRepoIssueComments(ctx *context.APIContext) {
return
}
for i := range comments {
apiComments[i] = convert.ToComment(ctx, comments[i])
apiComments[i] = convert.ToAPIComment(ctx, ctx.Repo.Repository, comments[i])
}

ctx.SetTotalCountHeader(totalCount)
Expand Down Expand Up @@ -368,7 +368,7 @@ func CreateIssueComment(ctx *context.APIContext) {
return
}

ctx.JSON(http.StatusCreated, convert.ToComment(ctx, comment))
ctx.JSON(http.StatusCreated, convert.ToAPIComment(ctx, ctx.Repo.Repository, comment))
}

// GetIssueComment Get a comment by ID
Expand Down Expand Up @@ -436,7 +436,7 @@ func GetIssueComment(ctx *context.APIContext) {
return
}

ctx.JSON(http.StatusOK, convert.ToComment(ctx, comment))
ctx.JSON(http.StatusOK, convert.ToAPIComment(ctx, ctx.Repo.Repository, comment))
}

// EditIssueComment modify a comment of an issue
Expand Down Expand Up @@ -561,7 +561,7 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
return
}

ctx.JSON(http.StatusOK, convert.ToComment(ctx, comment))
ctx.JSON(http.StatusOK, convert.ToAPIComment(ctx, ctx.Repo.Repository, comment))
}

// DeleteIssueComment delete a comment from an issue
Expand Down
10 changes: 5 additions & 5 deletions routers/api/v1/repo/issue_comment_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func GetIssueCommentAttachment(ctx *context.APIContext) {
return
}

ctx.JSON(http.StatusOK, convert.ToAttachment(attachment))
ctx.JSON(http.StatusOK, convert.ToAPIAttachment(ctx.Repo.Repository, attachment))
}

// ListIssueCommentAttachments lists all attachments of the comment
Expand Down Expand Up @@ -110,7 +110,7 @@ func ListIssueCommentAttachments(ctx *context.APIContext) {
return
}

ctx.JSON(http.StatusOK, convert.ToAttachments(comment.Attachments))
ctx.JSON(http.StatusOK, convert.ToAPIAttachments(ctx.Repo.Repository, comment.Attachments))
}

// CreateIssueCommentAttachment creates an attachment and saves the given file
Expand Down Expand Up @@ -201,7 +201,7 @@ func CreateIssueCommentAttachment(ctx *context.APIContext) {
return
}

ctx.JSON(http.StatusCreated, convert.ToAttachment(attachment))
ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attachment))
}

// EditIssueCommentAttachment updates the given attachment
Expand Down Expand Up @@ -259,7 +259,7 @@ func EditIssueCommentAttachment(ctx *context.APIContext) {
if err := repo_model.UpdateAttachment(ctx, attach); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateAttachment", attach)
}
ctx.JSON(http.StatusCreated, convert.ToAttachment(attach))
ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attach))
}

// DeleteIssueCommentAttachment delete a given attachment
Expand Down Expand Up @@ -352,7 +352,7 @@ func canUserWriteIssueCommentAttachment(ctx *context.APIContext, comment *issues
}

func getIssueCommentAttachmentSafeRead(ctx *context.APIContext, comment *issues_model.Comment) *repo_model.Attachment {
attachment, err := repo_model.GetAttachmentByID(ctx, ctx.ParamsInt64("asset"))
attachment, err := repo_model.GetAttachmentByID(ctx, ctx.ParamsInt64("attachment_id"))
if err != nil {
ctx.NotFoundOrServerError("GetAttachmentByID", repo_model.IsErrAttachmentNotExist, err)
return nil
Expand Down
10 changes: 5 additions & 5 deletions routers/api/v1/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func GetRelease(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
return
}
ctx.JSON(http.StatusOK, convert.ToRelease(ctx, release))
ctx.JSON(http.StatusOK, convert.ToAPIRelease(ctx, ctx.Repo.Repository, release))
}

// GetLatestRelease gets the most recent non-prerelease, non-draft release of a repository, sorted by created_at
Expand Down Expand Up @@ -105,7 +105,7 @@ func GetLatestRelease(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
return
}
ctx.JSON(http.StatusOK, convert.ToRelease(ctx, release))
ctx.JSON(http.StatusOK, convert.ToAPIRelease(ctx, ctx.Repo.Repository, release))
}

// ListReleases list a repository's releases
Expand Down Expand Up @@ -174,7 +174,7 @@ func ListReleases(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
return
}
rels[i] = convert.ToRelease(ctx, release)
rels[i] = convert.ToAPIRelease(ctx, ctx.Repo.Repository, release)
}

filteredCount, err := repo_model.CountReleasesByRepoID(ctx.Repo.Repository.ID, opts)
Expand Down Expand Up @@ -272,7 +272,7 @@ func CreateRelease(ctx *context.APIContext) {
return
}
}
ctx.JSON(http.StatusCreated, convert.ToRelease(ctx, rel))
ctx.JSON(http.StatusCreated, convert.ToAPIRelease(ctx, ctx.Repo.Repository, rel))
}

// EditRelease edit a release
Expand Down Expand Up @@ -357,7 +357,7 @@ func EditRelease(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
return
}
ctx.JSON(http.StatusOK, convert.ToRelease(ctx, rel))
ctx.JSON(http.StatusOK, convert.ToAPIRelease(ctx, ctx.Repo.Repository, rel))
}

// DeleteRelease delete a release from a repository
Expand Down
14 changes: 7 additions & 7 deletions routers/api/v1/repo/release_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func GetReleaseAttachment(ctx *context.APIContext) {
// "$ref": "#/responses/Attachment"

releaseID := ctx.ParamsInt64(":id")
attachID := ctx.ParamsInt64(":asset")
attachID := ctx.ParamsInt64(":attachment_id")
attach, err := repo_model.GetAttachmentByID(ctx, attachID)
if err != nil {
if repo_model.IsErrAttachmentNotExist(err) {
Expand All @@ -68,7 +68,7 @@ func GetReleaseAttachment(ctx *context.APIContext) {
return
}
// FIXME Should prove the existence of the given repo, but results in unnecessary database requests
ctx.JSON(http.StatusOK, convert.ToAttachment(attach))
ctx.JSON(http.StatusOK, convert.ToAPIAttachment(ctx.Repo.Repository, attach))
}

// ListReleaseAttachments lists all attachments of the release
Expand Down Expand Up @@ -117,7 +117,7 @@ func ListReleaseAttachments(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
return
}
ctx.JSON(http.StatusOK, convert.ToRelease(ctx, release).Attachments)
ctx.JSON(http.StatusOK, convert.ToAPIRelease(ctx, ctx.Repo.Repository, release).Attachments)
}

// CreateReleaseAttachment creates an attachment and saves the given file
Expand Down Expand Up @@ -209,7 +209,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
return
}

ctx.JSON(http.StatusCreated, convert.ToAttachment(attach))
ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attach))
}

// EditReleaseAttachment updates the given attachment
Expand Down Expand Up @@ -256,7 +256,7 @@ func EditReleaseAttachment(ctx *context.APIContext) {

// Check if release exists an load release
releaseID := ctx.ParamsInt64(":id")
attachID := ctx.ParamsInt64(":asset")
attachID := ctx.ParamsInt64(":attachment_id")
attach, err := repo_model.GetAttachmentByID(ctx, attachID)
if err != nil {
if repo_model.IsErrAttachmentNotExist(err) {
Expand All @@ -279,7 +279,7 @@ func EditReleaseAttachment(ctx *context.APIContext) {
if err := repo_model.UpdateAttachment(ctx, attach); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateAttachment", attach)
}
ctx.JSON(http.StatusCreated, convert.ToAttachment(attach))
ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attach))
}

// DeleteReleaseAttachment delete a given attachment
Expand Down Expand Up @@ -318,7 +318,7 @@ func DeleteReleaseAttachment(ctx *context.APIContext) {

// Check if release exists an load release
releaseID := ctx.ParamsInt64(":id")
attachID := ctx.ParamsInt64(":asset")
attachID := ctx.ParamsInt64(":attachment_id")
attach, err := repo_model.GetAttachmentByID(ctx, attachID)
if err != nil {
if repo_model.IsErrAttachmentNotExist(err) {
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/release_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func GetReleaseByTag(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
return
}
ctx.JSON(http.StatusOK, convert.ToRelease(ctx, release))
ctx.JSON(http.StatusOK, convert.ToAPIRelease(ctx, ctx.Repo.Repository, release))
}

// DeleteReleaseByTag delete a release from a repository by tag name
Expand Down
10 changes: 5 additions & 5 deletions routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -2036,7 +2036,7 @@ func GetIssueInfo(ctx *context.Context) {
}
}

ctx.JSON(http.StatusOK, convert.ToAPIIssue(ctx, issue))
ctx.JSON(http.StatusOK, convert.ToIssue(ctx, issue))
}

// UpdateIssueTitle change issue's title
Expand Down Expand Up @@ -2541,7 +2541,7 @@ func SearchIssues(ctx *context.Context) {
}

ctx.SetTotalCountHeader(filteredCount)
ctx.JSON(http.StatusOK, convert.ToAPIIssueList(ctx, issues))
ctx.JSON(http.StatusOK, convert.ToIssueList(ctx, issues))
}

func getUserIDForFilter(ctx *context.Context, queryName string) int64 {
Expand Down Expand Up @@ -2702,7 +2702,7 @@ func ListIssues(ctx *context.Context) {
}

ctx.SetTotalCountHeader(filteredCount)
ctx.JSON(http.StatusOK, convert.ToAPIIssueList(ctx, issues))
ctx.JSON(http.StatusOK, convert.ToIssueList(ctx, issues))
}

// UpdateIssueStatus change issue's status
Expand Down Expand Up @@ -3263,7 +3263,7 @@ func GetIssueAttachments(ctx *context.Context) {
}
attachments := make([]*api.Attachment, len(issue.Attachments))
for i := 0; i < len(issue.Attachments); i++ {
attachments[i] = convert.ToAttachment(issue.Attachments[i])
attachments[i] = convert.ToAttachment(ctx.Repo.Repository, issue.Attachments[i])
}
ctx.JSON(http.StatusOK, attachments)
}
Expand All @@ -3287,7 +3287,7 @@ func GetCommentAttachments(ctx *context.Context) {
return
}
for i := 0; i < len(comment.Attachments); i++ {
attachments = append(attachments, convert.ToAttachment(comment.Attachments[i]))
attachments = append(attachments, convert.ToAttachment(ctx.Repo.Repository, comment.Attachments[i]))
}
ctx.JSON(http.StatusOK, attachments)
}
Expand Down
4 changes: 2 additions & 2 deletions services/actions/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (n *actionsNotifier) NotifyCreateIssueComment(ctx context.Context, doer *us
WithPayload(&api.IssueCommentPayload{
Action: api.HookIssueCommentCreated,
Issue: convert.ToAPIIssue(ctx, issue),
Comment: convert.ToComment(ctx, comment),
Comment: convert.ToAPIComment(ctx, repo, comment),
Repository: convert.ToRepo(ctx, repo, permission),
Sender: convert.ToUser(ctx, doer, nil),
IsPull: true,
Expand All @@ -185,7 +185,7 @@ func (n *actionsNotifier) NotifyCreateIssueComment(ctx context.Context, doer *us
WithPayload(&api.IssueCommentPayload{
Action: api.HookIssueCommentCreated,
Issue: convert.ToAPIIssue(ctx, issue),
Comment: convert.ToComment(ctx, comment),
Comment: convert.ToAPIComment(ctx, repo, comment),
Repository: convert.ToRepo(ctx, repo, permission),
Sender: convert.ToUser(ctx, doer, nil),
IsPull: false,
Expand Down
2 changes: 1 addition & 1 deletion services/actions/notifier_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func notifyRelease(ctx context.Context, doer *user_model.User, rel *repo_model.R
WithRef(git.RefNameFromTag(rel.TagName).String()).
WithPayload(&api.ReleasePayload{
Action: action,
Release: convert.ToRelease(ctx, rel),
Release: convert.ToAPIRelease(ctx, rel.Repo, rel),
Repository: convert.ToRepo(ctx, rel.Repo, permission),
Sender: convert.ToUser(ctx, doer, nil),
}).
Expand Down
2 changes: 1 addition & 1 deletion services/convert/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func ToActivity(ctx context.Context, ac *activities_model.Action, doer *user_mod

if ac.Comment != nil {
result.CommentID = ac.CommentID
result.Comment = ToComment(ctx, ac.Comment)
result.Comment = ToAPIComment(ctx, ac.Repo, ac.Comment)
}

return result
Expand Down
Loading

0 comments on commit 2b79d3f

Please sign in to comment.