[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>
This commit is contained in:
parent
131278ff22
commit
d453533beb
28 changed files with 234 additions and 208 deletions
|
@ -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
|
||||
|
|
|
@ -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(),
|
||||
})
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue