aboutsummaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2020-10-17 06:23:08 +0200
committerGitHub <noreply@github.com>2020-10-17 00:23:08 -0400
commitd453533beb197d25b25530dc5f0e3c3c0742d8d1 (patch)
tree6d66e1163333344d91af63e9ab50a82367470b8f /routers
parent131278ff222f1a8580f20e6bbdff405341403042 (diff)
downloadgitea-d453533beb197d25b25530dc5f0e3c3c0742d8d1.tar.gz
gitea-d453533beb197d25b25530dc5f0e3c3c0742d8d1.zip
[Refactor] Move APIFormat functions into convert package (#12856)
* USER APIFormat -> ToUser * Migrate more and mark APIFormat deprecated * models.Comment APIFormat() -> convert.ToComment * models.Release APIFormat() -> convert.ToRelease * models.Attachments APIFormat() -> convert.ToReleaseAttachments * models.CommitStatus APIFormat() -> convert.ToCommitStatus * finish migration to convert.ToUser * Move Test * Imprufe Test * fix test Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'routers')
-rw-r--r--routers/api/v1/repo/issue_comment.go11
-rw-r--r--routers/api/v1/repo/issue_reaction.go13
-rw-r--r--routers/api/v1/repo/issue_subscription.go7
-rw-r--r--routers/api/v1/repo/release.go9
-rw-r--r--routers/api/v1/repo/release_attachment.go9
-rw-r--r--routers/api/v1/repo/release_tags.go3
-rw-r--r--routers/api/v1/repo/status.go7
-rw-r--r--routers/repo/issue.go5
-rw-r--r--routers/repo/webhook.go3
9 files changed, 40 insertions, 27 deletions
diff --git a/routers/api/v1/repo/issue_comment.go b/routers/api/v1/repo/issue_comment.go
index bf86b42402..2da2f29063 100644
--- a/routers/api/v1/repo/issue_comment.go
+++ b/routers/api/v1/repo/issue_comment.go
@@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
comment_service "code.gitea.io/gitea/services/comments"
@@ -85,7 +86,7 @@ func ListIssueComments(ctx *context.APIContext) {
apiComments := make([]*api.Comment, len(comments))
for i, comment := range comments {
comment.Issue = issue
- apiComments[i] = comments[i].APIFormat()
+ apiComments[i] = convert.ToComment(comments[i])
}
ctx.JSON(http.StatusOK, &apiComments)
}
@@ -167,7 +168,7 @@ func ListRepoIssueComments(ctx *context.APIContext) {
return
}
for i := range comments {
- apiComments[i] = comments[i].APIFormat()
+ apiComments[i] = convert.ToComment(comments[i])
}
ctx.JSON(http.StatusOK, &apiComments)
}
@@ -225,7 +226,7 @@ func CreateIssueComment(ctx *context.APIContext, form api.CreateIssueCommentOpti
return
}
- ctx.JSON(http.StatusCreated, comment.APIFormat())
+ ctx.JSON(http.StatusCreated, convert.ToComment(comment))
}
// GetIssueComment Get a comment by ID
@@ -293,7 +294,7 @@ func GetIssueComment(ctx *context.APIContext) {
return
}
- ctx.JSON(http.StatusOK, comment.APIFormat())
+ ctx.JSON(http.StatusOK, convert.ToComment(comment))
}
// EditIssueComment modify a comment of an issue
@@ -414,7 +415,7 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
return
}
- ctx.JSON(http.StatusOK, comment.APIFormat())
+ ctx.JSON(http.StatusOK, convert.ToComment(comment))
}
// DeleteIssueComment delete a comment from an issue
diff --git a/routers/api/v1/repo/issue_reaction.go b/routers/api/v1/repo/issue_reaction.go
index 564f67e493..f5387fdd1a 100644
--- a/routers/api/v1/repo/issue_reaction.go
+++ b/routers/api/v1/repo/issue_reaction.go
@@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
)
@@ -75,7 +76,7 @@ func GetIssueCommentReactions(ctx *context.APIContext) {
var result []api.Reaction
for _, r := range reactions {
result = append(result, api.Reaction{
- User: r.User.APIFormat(),
+ User: convert.ToUser(r.User, ctx.IsSigned, false),
Reaction: r.Type,
Created: r.CreatedUnix.AsTime(),
})
@@ -193,7 +194,7 @@ func changeIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOp
ctx.Error(http.StatusForbidden, err.Error(), err)
} else if models.IsErrReactionAlreadyExist(err) {
ctx.JSON(http.StatusOK, api.Reaction{
- User: ctx.User.APIFormat(),
+ User: convert.ToUser(ctx.User, true, true),
Reaction: reaction.Type,
Created: reaction.CreatedUnix.AsTime(),
})
@@ -204,7 +205,7 @@ func changeIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOp
}
ctx.JSON(http.StatusCreated, api.Reaction{
- User: ctx.User.APIFormat(),
+ User: convert.ToUser(ctx.User, true, true),
Reaction: reaction.Type,
Created: reaction.CreatedUnix.AsTime(),
})
@@ -289,7 +290,7 @@ func GetIssueReactions(ctx *context.APIContext) {
var result []api.Reaction
for _, r := range reactions {
result = append(result, api.Reaction{
- User: r.User.APIFormat(),
+ User: convert.ToUser(r.User, ctx.IsSigned, false),
Reaction: r.Type,
Created: r.CreatedUnix.AsTime(),
})
@@ -402,7 +403,7 @@ func changeIssueReaction(ctx *context.APIContext, form api.EditReactionOption, i
ctx.Error(http.StatusForbidden, err.Error(), err)
} else if models.IsErrReactionAlreadyExist(err) {
ctx.JSON(http.StatusOK, api.Reaction{
- User: ctx.User.APIFormat(),
+ User: convert.ToUser(ctx.User, true, true),
Reaction: reaction.Type,
Created: reaction.CreatedUnix.AsTime(),
})
@@ -413,7 +414,7 @@ func changeIssueReaction(ctx *context.APIContext, form api.EditReactionOption, i
}
ctx.JSON(http.StatusCreated, api.Reaction{
- User: ctx.User.APIFormat(),
+ User: convert.ToUser(ctx.User, true, true),
Reaction: reaction.Type,
Created: reaction.CreatedUnix.AsTime(),
})
diff --git a/routers/api/v1/repo/issue_subscription.go b/routers/api/v1/repo/issue_subscription.go
index f0df0976fb..2bbd72299a 100644
--- a/routers/api/v1/repo/issue_subscription.go
+++ b/routers/api/v1/repo/issue_subscription.go
@@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
)
@@ -276,6 +277,10 @@ func GetIssueSubscribers(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "GetUsersByIDs", err)
return
}
+ apiUsers := make([]*api.User, 0, len(users))
+ for i := range users {
+ apiUsers[i] = convert.ToUser(users[i], ctx.IsSigned, false)
+ }
- ctx.JSON(http.StatusOK, users.APIFormat())
+ ctx.JSON(http.StatusOK, apiUsers)
}
diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go
index c2ed1fe024..358cc01143 100644
--- a/routers/api/v1/repo/release.go
+++ b/routers/api/v1/repo/release.go
@@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
releaseservice "code.gitea.io/gitea/services/release"
@@ -60,7 +61,7 @@ func GetRelease(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
return
}
- ctx.JSON(http.StatusOK, release.APIFormat())
+ ctx.JSON(http.StatusOK, convert.ToRelease(release))
}
// ListReleases list a repository's releases
@@ -117,7 +118,7 @@ func ListReleases(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
return
}
- rels[i] = release.APIFormat()
+ rels[i] = convert.ToRelease(release)
}
ctx.JSON(http.StatusOK, rels)
}
@@ -205,7 +206,7 @@ func CreateRelease(ctx *context.APIContext, form api.CreateReleaseOption) {
return
}
}
- ctx.JSON(http.StatusCreated, rel.APIFormat())
+ ctx.JSON(http.StatusCreated, convert.ToRelease(rel))
}
// EditRelease edit a release
@@ -288,7 +289,7 @@ func EditRelease(ctx *context.APIContext, form api.EditReleaseOption) {
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
return
}
- ctx.JSON(http.StatusOK, rel.APIFormat())
+ ctx.JSON(http.StatusOK, convert.ToRelease(rel))
}
// DeleteRelease delete a release from a repository
diff --git a/routers/api/v1/repo/release_attachment.go b/routers/api/v1/repo/release_attachment.go
index f352c10829..51e1b160da 100644
--- a/routers/api/v1/repo/release_attachment.go
+++ b/routers/api/v1/repo/release_attachment.go
@@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
@@ -62,7 +63,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, attach.APIFormat())
+ ctx.JSON(http.StatusOK, convert.ToReleaseAttachment(attach))
}
// ListReleaseAttachments lists all attachments of the release
@@ -107,7 +108,7 @@ func ListReleaseAttachments(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
return
}
- ctx.JSON(http.StatusOK, release.APIFormat().Attachments)
+ ctx.JSON(http.StatusOK, convert.ToRelease(release).Attachments)
}
// CreateReleaseAttachment creates an attachment and saves the given file
@@ -203,7 +204,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
return
}
- ctx.JSON(http.StatusCreated, attach.APIFormat())
+ ctx.JSON(http.StatusCreated, convert.ToReleaseAttachment(attach))
}
// EditReleaseAttachment updates the given attachment
@@ -267,7 +268,7 @@ func EditReleaseAttachment(ctx *context.APIContext, form api.EditAttachmentOptio
if err := models.UpdateAttachment(attach); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateAttachment", attach)
}
- ctx.JSON(http.StatusCreated, attach.APIFormat())
+ ctx.JSON(http.StatusCreated, convert.ToReleaseAttachment(attach))
}
// DeleteReleaseAttachment delete a given attachment
diff --git a/routers/api/v1/repo/release_tags.go b/routers/api/v1/repo/release_tags.go
index bde3251ba2..2a72e0000e 100644
--- a/routers/api/v1/repo/release_tags.go
+++ b/routers/api/v1/repo/release_tags.go
@@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/convert"
)
// GetReleaseTag get a single release of a repository by its tagname
@@ -56,5 +57,5 @@ func GetReleaseTag(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
return
}
- ctx.JSON(http.StatusOK, release.APIFormat())
+ ctx.JSON(http.StatusOK, convert.ToRelease(release))
}
diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go
index 69661dca91..e7318edaa5 100644
--- a/routers/api/v1/repo/status.go
+++ b/routers/api/v1/repo/status.go
@@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/repofiles"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
@@ -64,7 +65,7 @@ func NewCommitStatus(ctx *context.APIContext, form api.CreateStatusOption) {
return
}
- ctx.JSON(http.StatusCreated, status.APIFormat())
+ ctx.JSON(http.StatusCreated, convert.ToCommitStatus(status))
}
// GetCommitStatuses returns all statuses for any given commit hash
@@ -222,7 +223,7 @@ func getCommitStatuses(ctx *context.APIContext, sha string) {
apiStatuses := make([]*api.Status, 0, len(statuses))
for _, status := range statuses {
- apiStatuses = append(apiStatuses, status.APIFormat())
+ apiStatuses = append(apiStatuses, convert.ToCommitStatus(status))
}
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
@@ -305,7 +306,7 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) {
retStatus.Statuses = make([]*api.Status, 0, len(statuses))
for _, status := range statuses {
- retStatus.Statuses = append(retStatus.Statuses, status.APIFormat())
+ retStatus.Statuses = append(retStatus.Statuses, convert.ToCommitStatus(status))
if status.State.NoBetterThan(retStatus.State) {
retStatus.State = status.State
}
diff --git a/routers/repo/issue.go b/routers/repo/issue.go
index 8aef322090..bc65e73f11 100644
--- a/routers/repo/issue.go
+++ b/routers/repo/issue.go
@@ -19,6 +19,7 @@ import (
"code.gitea.io/gitea/modules/auth"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/git"
issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
"code.gitea.io/gitea/modules/log"
@@ -2453,7 +2454,7 @@ func GetIssueAttachments(ctx *context.Context) {
issue := GetActionIssue(ctx)
var attachments = make([]*api.Attachment, len(issue.Attachments))
for i := 0; i < len(issue.Attachments); i++ {
- attachments[i] = issue.Attachments[i].APIFormat()
+ attachments[i] = convert.ToReleaseAttachment(issue.Attachments[i])
}
ctx.JSON(200, attachments)
}
@@ -2472,7 +2473,7 @@ func GetCommentAttachments(ctx *context.Context) {
return
}
for i := 0; i < len(comment.Attachments); i++ {
- attachments = append(attachments, comment.Attachments[i].APIFormat())
+ attachments = append(attachments, convert.ToReleaseAttachment(comment.Attachments[i]))
}
}
ctx.JSON(200, attachments)
diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go
index bec401021c..13b95c8076 100644
--- a/routers/repo/webhook.go
+++ b/routers/repo/webhook.go
@@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/auth"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
@@ -1052,7 +1053,7 @@ func TestWebhook(ctx *context.Context) {
}
}
- apiUser := ctx.User.APIFormat()
+ apiUser := convert.ToUser(ctx.User, true, true)
p := &api.PushPayload{
Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
Before: commit.ID.String(),