mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
[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:
@@ -339,3 +339,24 @@ func ToOAuth2Application(app *models.OAuth2Application) *api.OAuth2Application {
|
||||
Created: app.CreatedUnix.AsTime(),
|
||||
}
|
||||
}
|
||||
|
||||
// ToCommitStatus converts models.CommitStatus to api.Status
|
||||
func ToCommitStatus(status *models.CommitStatus) *api.Status {
|
||||
apiStatus := &api.Status{
|
||||
Created: status.CreatedUnix.AsTime(),
|
||||
Updated: status.CreatedUnix.AsTime(),
|
||||
State: api.StatusState(status.State),
|
||||
TargetURL: status.TargetURL,
|
||||
Description: status.Description,
|
||||
ID: status.Index,
|
||||
URL: status.APIURL(),
|
||||
Context: status.Context,
|
||||
}
|
||||
|
||||
if status.CreatorID != 0 {
|
||||
creator, _ := models.GetUserByID(status.CreatorID)
|
||||
apiStatus.Creator = ToUser(creator, false, false)
|
||||
}
|
||||
|
||||
return apiStatus
|
||||
}
|
||||
|
@@ -86,13 +86,13 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string]
|
||||
}
|
||||
|
||||
if ok {
|
||||
apiAuthor = cacheAuthor.APIFormat()
|
||||
apiAuthor = ToUser(cacheAuthor, false, false)
|
||||
} else {
|
||||
author, err := models.GetUserByEmail(commit.Author.Email)
|
||||
if err != nil && !models.IsErrUserNotExist(err) {
|
||||
return nil, err
|
||||
} else if err == nil {
|
||||
apiAuthor = author.APIFormat()
|
||||
apiAuthor = ToUser(author, false, false)
|
||||
if userCache != nil {
|
||||
userCache[commit.Author.Email] = author
|
||||
}
|
||||
@@ -108,13 +108,13 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string]
|
||||
}
|
||||
|
||||
if ok {
|
||||
apiCommitter = cacheCommitter.APIFormat()
|
||||
apiCommitter = ToUser(cacheCommitter, false, false)
|
||||
} else {
|
||||
committer, err := models.GetUserByEmail(commit.Committer.Email)
|
||||
if err != nil && !models.IsErrUserNotExist(err) {
|
||||
return nil, err
|
||||
} else if err == nil {
|
||||
apiCommitter = committer.APIFormat()
|
||||
apiCommitter = ToUser(committer, false, false)
|
||||
if userCache != nil {
|
||||
userCache[commit.Committer.Email] = committer
|
||||
}
|
||||
|
@@ -31,7 +31,7 @@ func ToAPIIssue(issue *models.Issue) *api.Issue {
|
||||
URL: issue.APIURL(),
|
||||
HTMLURL: issue.HTMLURL(),
|
||||
Index: issue.Index,
|
||||
Poster: issue.Poster.APIFormat(),
|
||||
Poster: ToUser(issue.Poster, false, false),
|
||||
Title: issue.Title,
|
||||
Body: issue.Content,
|
||||
Labels: ToLabelList(issue.Labels),
|
||||
@@ -65,9 +65,9 @@ func ToAPIIssue(issue *models.Issue) *api.Issue {
|
||||
}
|
||||
if len(issue.Assignees) > 0 {
|
||||
for _, assignee := range issue.Assignees {
|
||||
apiIssue.Assignees = append(apiIssue.Assignees, assignee.APIFormat())
|
||||
apiIssue.Assignees = append(apiIssue.Assignees, ToUser(assignee, false, false))
|
||||
}
|
||||
apiIssue.Assignee = issue.Assignees[0].APIFormat() // For compatibility, we're keeping the first assignee as `apiIssue.Assignee`
|
||||
apiIssue.Assignee = ToUser(issue.Assignees[0], false, false) // For compatibility, we're keeping the first assignee as `apiIssue.Assignee`
|
||||
}
|
||||
if issue.IsPull {
|
||||
if err := issue.LoadPullRequest(); err != nil {
|
||||
|
24
modules/convert/issue_comment.go
Normal file
24
modules/convert/issue_comment.go
Normal file
@@ -0,0 +1,24 @@
|
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package convert
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/models"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
)
|
||||
|
||||
// ToComment converts a models.Comment to the api.Comment format
|
||||
func ToComment(c *models.Comment) *api.Comment {
|
||||
return &api.Comment{
|
||||
ID: c.ID,
|
||||
Poster: ToUser(c.Poster, false, false),
|
||||
HTMLURL: c.HTMLURL(),
|
||||
IssueURL: c.IssueURL(),
|
||||
PRURL: c.PRURL(),
|
||||
Body: c.Content,
|
||||
Created: c.CreatedUnix.AsTime(),
|
||||
Updated: c.UpdatedUnix.AsTime(),
|
||||
}
|
||||
}
|
@@ -141,7 +141,7 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
|
||||
if pr.HasMerged {
|
||||
apiPullRequest.Merged = pr.MergedUnix.AsTimePtr()
|
||||
apiPullRequest.MergedCommitID = &pr.MergedCommitID
|
||||
apiPullRequest.MergedBy = pr.Merger.APIFormat()
|
||||
apiPullRequest.MergedBy = ToUser(pr.Merger, false, false)
|
||||
}
|
||||
|
||||
return apiPullRequest
|
||||
|
48
modules/convert/release.go
Normal file
48
modules/convert/release.go
Normal file
@@ -0,0 +1,48 @@
|
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package convert
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/models"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
)
|
||||
|
||||
// ToRelease convert a models.Release to api.Release
|
||||
func ToRelease(r *models.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,
|
||||
Target: r.Target,
|
||||
Title: r.Title,
|
||||
Note: r.Note,
|
||||
URL: r.APIURL(),
|
||||
HTMLURL: r.HTMLURL(),
|
||||
TarURL: r.TarURL(),
|
||||
ZipURL: r.ZipURL(),
|
||||
IsDraft: r.IsDraft,
|
||||
IsPrerelease: r.IsPrerelease,
|
||||
CreatedAt: r.CreatedUnix.AsTime(),
|
||||
PublishedAt: r.CreatedUnix.AsTime(),
|
||||
Publisher: ToUser(r.Publisher, false, false),
|
||||
Attachments: assets,
|
||||
}
|
||||
}
|
||||
|
||||
// ToReleaseAttachment converts models.Attachment to api.Attachment
|
||||
func ToReleaseAttachment(a *models.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(),
|
||||
}
|
||||
}
|
@@ -13,6 +13,9 @@ import (
|
||||
// ToUser convert models.User to api.User
|
||||
// signed shall only be set if requester is logged in. authed shall only be set if user is site admin or user himself
|
||||
func ToUser(user *models.User, signed, authed bool) *api.User {
|
||||
if user == nil {
|
||||
return nil
|
||||
}
|
||||
result := &api.User{
|
||||
ID: user.ID,
|
||||
UserName: user.Name,
|
||||
|
28
modules/convert/user_test.go
Normal file
28
modules/convert/user_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package convert
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestUser_ToUser(t *testing.T) {
|
||||
|
||||
user1 := models.AssertExistsAndLoadBean(t, &models.User{ID: 1, IsAdmin: true}).(*models.User)
|
||||
|
||||
apiUser := ToUser(user1, true, true)
|
||||
assert.True(t, apiUser.IsAdmin)
|
||||
|
||||
user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2, IsAdmin: false}).(*models.User)
|
||||
|
||||
apiUser = ToUser(user2, true, true)
|
||||
assert.False(t, apiUser.IsAdmin)
|
||||
|
||||
apiUser = ToUser(user1, false, false)
|
||||
assert.False(t, apiUser.IsAdmin)
|
||||
}
|
Reference in New Issue
Block a user