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

For API attachments, use API URL #25639

Merged
merged 15 commits into from
Jul 10, 2023
6 changes: 3 additions & 3 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.ToAttachment(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.ToAttachment(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.ToAttachment(ctx.Repo.Repository, attachment))
}

// DeleteIssueAttachment delete a given attachment
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
8 changes: 4 additions & 4 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.ToAttachment(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.ToAttachment(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.ToAttachment(ctx.Repo.Repository, attach))
}

// DeleteIssueCommentAttachment delete a given attachment
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
20 changes: 10 additions & 10 deletions routers/api/v1/repo/release_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

// GetReleaseAttachment gets a single attachment of the release
func GetReleaseAttachment(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id} repository repoGetReleaseAttachment
// swagger:operation GET /repos/{owner}/{repo}/releases/{id}/assets/{asset} repository repoGetReleaseAttachment
lunny marked this conversation as resolved.
Show resolved Hide resolved
// ---
// summary: Get a release attachment
// produces:
Expand All @@ -41,7 +41,7 @@ func GetReleaseAttachment(ctx *context.APIContext) {
// type: integer
// format: int64
// required: true
// - name: attachment_id
// - name: asset
// in: path
// description: id of the attachment to get
// type: integer
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.ToAttachment(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,12 +209,12 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
return
}

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

// EditReleaseAttachment updates the given attachment
func EditReleaseAttachment(ctx *context.APIContext) {
// swagger:operation PATCH /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id} repository repoEditReleaseAttachment
// swagger:operation PATCH /repos/{owner}/{repo}/releases/{id}/assets/{asset} repository repoEditReleaseAttachment
// ---
// summary: Edit a release attachment
// produces:
Expand All @@ -238,7 +238,7 @@ func EditReleaseAttachment(ctx *context.APIContext) {
// type: integer
// format: int64
// required: true
// - name: attachment_id
// - name: asset
// in: path
// description: id of the attachment to edit
// type: integer
Expand Down Expand Up @@ -279,12 +279,12 @@ 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.ToAttachment(ctx.Repo.Repository, attach))
}

// DeleteReleaseAttachment delete a given attachment
func DeleteReleaseAttachment(ctx *context.APIContext) {
// swagger:operation DELETE /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id} repository repoDeleteReleaseAttachment
// swagger:operation DELETE /repos/{owner}/{repo}/releases/{id}/assets/{asset} repository repoDeleteReleaseAttachment
// ---
// summary: Delete a release attachment
// produces:
Expand All @@ -306,7 +306,7 @@ func DeleteReleaseAttachment(ctx *context.APIContext) {
// type: integer
// format: int64
// required: true
// - name: attachment_id
// - name: asset
// in: path
// description: id of the attachment to delete
// type: integer
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
4 changes: 2 additions & 2 deletions routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3284,7 +3284,7 @@ func GetIssueAttachments(ctx *context.Context) {
issue := GetActionIssue(ctx)
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 @@ -3308,7 +3308,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 @@ -262,7 +262,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
31 changes: 28 additions & 3 deletions services/convert/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@
package convert

import (
"strconv"

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

func AssetDownloadURL(repo *repo_model.Repository, attach *repo_model.Attachment) string {
if attach.CustomDownloadURL != "" {
return attach.CustomDownloadURL
}

// /repos/{owner}/{repo}/releases/{id}/assets/{asset_id}
return setting.AppURL + "api/repos/" + repo.FullName() + "/releases/" + strconv.FormatInt(attach.ReleaseID, 10) + "/assets/" + strconv.FormatInt(attach.ID, 10)
}

// ToAttachment converts models.Attachment to api.Attachment
func ToAttachment(a *repo_model.Attachment) *api.Attachment {
func ToAttachment(repo *repo_model.Repository, a *repo_model.Attachment) *api.Attachment {
return &api.Attachment{
ID: a.ID,
Name: a.Name,
Expand All @@ -21,10 +33,23 @@ func ToAttachment(a *repo_model.Attachment) *api.Attachment {
}
}

func ToAttachments(attachments []*repo_model.Attachment) []*api.Attachment {
// ToAPIAttachment converts models.Attachment to api.Attachment for API usage
func ToAPIAttachment(repo *repo_model.Repository, 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: AssetDownloadURL(repo, a),
}
}

func ToAPIAttachments(repo *repo_model.Repository, attachments []*repo_model.Attachment) []*api.Attachment {
converted := make([]*api.Attachment, 0, len(attachments))
for _, attachment := range attachments {
converted = append(converted, ToAttachment(attachment))
converted = append(converted, ToAPIAttachment(repo, attachment))
}
return converted
}
2 changes: 1 addition & 1 deletion services/convert/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func ToAPIIssue(ctx context.Context, issue *issues_model.Issue) *api.Issue {
Poster: ToUser(ctx, issue.Poster, nil),
Title: issue.Title,
Body: issue.Content,
Attachments: ToAttachments(issue.Attachments),
Attachments: ToAPIAttachments(issue.Repo, issue.Attachments),
Ref: issue.Ref,
State: issue.State(),
IsLocked: issue.IsLocked,
Expand Down
Loading