Skip to content

Commit d453533

Browse files
6543techknowlogick
andauthored
[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 <[email protected]>
1 parent 131278f commit d453533

28 files changed

+234
-208
lines changed

integrations/api_comment_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212

1313
"code.gitea.io/gitea/models"
14+
"code.gitea.io/gitea/modules/convert"
1415
api "code.gitea.io/gitea/modules/structs"
1516

1617
"github.com/stretchr/testify/assert"
@@ -125,7 +126,7 @@ func TestAPIGetComment(t *testing.T) {
125126
DecodeJSON(t, resp, &apiComment)
126127

127128
assert.NoError(t, comment.LoadPoster())
128-
expect := comment.APIFormat()
129+
expect := convert.ToComment(comment)
129130

130131
assert.Equal(t, expect.ID, apiComment.ID)
131132
assert.Equal(t, expect.Poster.FullName, apiComment.Poster.FullName)

integrations/api_issue_reaction_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"code.gitea.io/gitea/models"
14+
"code.gitea.io/gitea/modules/convert"
1415
api "code.gitea.io/gitea/modules/structs"
1516

1617
"github.com/stretchr/testify/assert"
@@ -60,7 +61,7 @@ func TestAPIIssuesReactions(t *testing.T) {
6061
DecodeJSON(t, resp, &apiReactions)
6162
expectResponse := make(map[int]api.Reaction)
6263
expectResponse[0] = api.Reaction{
63-
User: user2.APIFormat(),
64+
User: convert.ToUser(user2, true, true),
6465
Reaction: "eyes",
6566
Created: time.Unix(1573248003, 0),
6667
}
@@ -120,12 +121,12 @@ func TestAPICommentReactions(t *testing.T) {
120121
DecodeJSON(t, resp, &apiReactions)
121122
expectResponse := make(map[int]api.Reaction)
122123
expectResponse[0] = api.Reaction{
123-
User: user2.APIFormat(),
124+
User: convert.ToUser(user2, true, true),
124125
Reaction: "laugh",
125126
Created: time.Unix(1573248004, 0),
126127
}
127128
expectResponse[1] = api.Reaction{
128-
User: user1.APIFormat(),
129+
User: convert.ToUser(user1, true, true),
129130
Reaction: "laugh",
130131
Created: time.Unix(1573248005, 0),
131132
}

models/attachment.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"code.gitea.io/gitea/modules/setting"
1414
"code.gitea.io/gitea/modules/storage"
15-
api "code.gitea.io/gitea/modules/structs"
1615
"code.gitea.io/gitea/modules/timeutil"
1716

1817
gouuid "github.com/google/uuid"
@@ -43,19 +42,6 @@ func (a *Attachment) IncreaseDownloadCount() error {
4342
return nil
4443
}
4544

46-
// APIFormat converts models.Attachment to api.Attachment
47-
func (a *Attachment) APIFormat() *api.Attachment {
48-
return &api.Attachment{
49-
ID: a.ID,
50-
Name: a.Name,
51-
Created: a.CreatedUnix.AsTime(),
52-
DownloadCount: a.DownloadCount,
53-
Size: a.Size,
54-
UUID: a.UUID,
55-
DownloadURL: a.DownloadURL(),
56-
}
57-
}
58-
5945
// AttachmentRelativePath returns the relative path
6046
func AttachmentRelativePath(uuid string) string {
6147
return path.Join(uuid[0:1], uuid[1:2], uuid)

models/commit_status.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,6 @@ func (status *CommitStatus) APIURL() string {
6161
setting.AppURL, status.Repo.FullName(), status.SHA)
6262
}
6363

64-
// APIFormat assumes some fields assigned with values:
65-
// Required - Repo, Creator
66-
func (status *CommitStatus) APIFormat() *api.Status {
67-
_ = status.loadRepo(x)
68-
apiStatus := &api.Status{
69-
Created: status.CreatedUnix.AsTime(),
70-
Updated: status.CreatedUnix.AsTime(),
71-
State: api.StatusState(status.State),
72-
TargetURL: status.TargetURL,
73-
Description: status.Description,
74-
ID: status.Index,
75-
URL: status.APIURL(),
76-
Context: status.Context,
77-
}
78-
if status.Creator != nil {
79-
apiStatus.Creator = status.Creator.APIFormat()
80-
}
81-
82-
return apiStatus
83-
}
84-
8564
// CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc
8665
func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
8766
var lastStatus *CommitStatus

models/issue_comment.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"code.gitea.io/gitea/modules/markup/markdown"
2121
"code.gitea.io/gitea/modules/references"
2222
"code.gitea.io/gitea/modules/structs"
23-
api "code.gitea.io/gitea/modules/structs"
2423
"code.gitea.io/gitea/modules/timeutil"
2524

2625
"github.com/unknwon/com"
@@ -354,20 +353,6 @@ func (c *Comment) PRURL() string {
354353
return c.Issue.HTMLURL()
355354
}
356355

357-
// APIFormat converts a Comment to the api.Comment format
358-
func (c *Comment) APIFormat() *api.Comment {
359-
return &api.Comment{
360-
ID: c.ID,
361-
Poster: c.Poster.APIFormat(),
362-
HTMLURL: c.HTMLURL(),
363-
IssueURL: c.IssueURL(),
364-
PRURL: c.PRURL(),
365-
Body: c.Content,
366-
Created: c.CreatedUnix.AsTime(),
367-
Updated: c.UpdatedUnix.AsTime(),
368-
}
369-
}
370-
371356
// CommentHashTag returns unique hash tag for comment id.
372357
func CommentHashTag(id int64) string {
373358
return fmt.Sprintf("issuecomment-%d", id)

models/release.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"code.gitea.io/gitea/modules/setting"
1414
"code.gitea.io/gitea/modules/structs"
15-
api "code.gitea.io/gitea/modules/structs"
1615
"code.gitea.io/gitea/modules/timeutil"
1716

1817
"xorm.io/builder"
@@ -86,31 +85,6 @@ func (r *Release) HTMLURL() string {
8685
return fmt.Sprintf("%s/releases/tag/%s", r.Repo.HTMLURL(), r.TagName)
8786
}
8887

89-
// APIFormat convert a Release to api.Release
90-
func (r *Release) APIFormat() *api.Release {
91-
assets := make([]*api.Attachment, 0)
92-
for _, att := range r.Attachments {
93-
assets = append(assets, att.APIFormat())
94-
}
95-
return &api.Release{
96-
ID: r.ID,
97-
TagName: r.TagName,
98-
Target: r.Target,
99-
Title: r.Title,
100-
Note: r.Note,
101-
URL: r.APIURL(),
102-
HTMLURL: r.HTMLURL(),
103-
TarURL: r.TarURL(),
104-
ZipURL: r.ZipURL(),
105-
IsDraft: r.IsDraft,
106-
IsPrerelease: r.IsPrerelease,
107-
CreatedAt: r.CreatedUnix.AsTime(),
108-
PublishedAt: r.CreatedUnix.AsTime(),
109-
Publisher: r.Publisher.APIFormat(),
110-
Attachments: assets,
111-
}
112-
}
113-
11488
// IsReleaseExist returns true if release with given tag name already exists.
11589
func IsReleaseExist(repoID int64, tagName string) (bool, error) {
11690
if len(tagName) == 0 {

models/repo.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,17 @@ func (repo *Repository) innerAPIFormat(e Engine, mode AccessMode, isParent bool)
410410
numReleases, _ := GetReleaseCountByRepoID(repo.ID, FindReleasesOptions{IncludeDrafts: false, IncludeTags: true})
411411

412412
return &api.Repository{
413-
ID: repo.ID,
414-
Owner: repo.Owner.APIFormat(),
413+
ID: repo.ID,
414+
// TODO use convert.ToUser(repo.Owner)
415+
Owner: &api.User{
416+
ID: repo.Owner.ID,
417+
UserName: repo.Owner.Name,
418+
FullName: repo.Owner.FullName,
419+
Email: repo.Owner.GetEmail(),
420+
AvatarURL: repo.Owner.AvatarLink(),
421+
LastLogin: repo.Owner.LastLoginUnix.AsTime(),
422+
Created: repo.Owner.CreatedUnix.AsTime(),
423+
},
415424
Name: repo.Name,
416425
FullName: repo.FullName(),
417426
Description: repo.Description,

models/user.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"code.gitea.io/gitea/modules/setting"
3030
"code.gitea.io/gitea/modules/storage"
3131
"code.gitea.io/gitea/modules/structs"
32-
api "code.gitea.io/gitea/modules/structs"
3332
"code.gitea.io/gitea/modules/timeutil"
3433
"code.gitea.io/gitea/modules/util"
3534

@@ -241,24 +240,6 @@ func GetAllUsers() ([]*User, error) {
241240
return users, x.OrderBy("id").Find(&users)
242241
}
243242

244-
// APIFormat converts a User to api.User
245-
func (u *User) APIFormat() *api.User {
246-
if u == nil {
247-
return nil
248-
}
249-
return &api.User{
250-
ID: u.ID,
251-
UserName: u.Name,
252-
FullName: u.FullName,
253-
Email: u.GetEmail(),
254-
AvatarURL: u.AvatarLink(),
255-
Language: u.Language,
256-
IsAdmin: u.IsAdmin,
257-
LastLogin: u.LastLoginUnix.AsTime(),
258-
Created: u.CreatedUnix.AsTime(),
259-
}
260-
}
261-
262243
// IsLocal returns true if user login type is LoginPlain.
263244
func (u *User) IsLocal() bool {
264245
return u.LoginType <= LoginPlain

models/user_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,6 @@ func TestGetUserEmailsByNames(t *testing.T) {
7878
assert.Equal(t, []string{"[email protected]"}, GetUserEmailsByNames([]string{"user8", "user7"}))
7979
}
8080

81-
func TestUser_APIFormat(t *testing.T) {
82-
83-
user, err := GetUserByID(1)
84-
assert.NoError(t, err)
85-
assert.True(t, user.IsAdmin)
86-
87-
apiUser := user.APIFormat()
88-
assert.True(t, apiUser.IsAdmin)
89-
90-
user, err = GetUserByID(2)
91-
assert.NoError(t, err)
92-
assert.False(t, user.IsAdmin)
93-
94-
apiUser = user.APIFormat()
95-
assert.False(t, apiUser.IsAdmin)
96-
}
97-
9881
func TestCanCreateOrganization(t *testing.T) {
9982
assert.NoError(t, PrepareTestDatabase())
10083

models/userlist.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99

1010
"code.gitea.io/gitea/modules/log"
11-
api "code.gitea.io/gitea/modules/structs"
1211
)
1312

1413
//UserList is a list of user.
@@ -94,12 +93,3 @@ func (users UserList) loadTwoFactorStatus(e Engine) (map[int64]*TwoFactor, error
9493
}
9594
return tokenMaps, nil
9695
}
97-
98-
//APIFormat return list of users in api format
99-
func (users UserList) APIFormat() []*api.User {
100-
result := make([]*api.User, 0, len(users))
101-
for _, u := range users {
102-
result = append(result, u.APIFormat())
103-
}
104-
return result
105-
}

modules/convert/convert.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,24 @@ func ToOAuth2Application(app *models.OAuth2Application) *api.OAuth2Application {
339339
Created: app.CreatedUnix.AsTime(),
340340
}
341341
}
342+
343+
// ToCommitStatus converts models.CommitStatus to api.Status
344+
func ToCommitStatus(status *models.CommitStatus) *api.Status {
345+
apiStatus := &api.Status{
346+
Created: status.CreatedUnix.AsTime(),
347+
Updated: status.CreatedUnix.AsTime(),
348+
State: api.StatusState(status.State),
349+
TargetURL: status.TargetURL,
350+
Description: status.Description,
351+
ID: status.Index,
352+
URL: status.APIURL(),
353+
Context: status.Context,
354+
}
355+
356+
if status.CreatorID != 0 {
357+
creator, _ := models.GetUserByID(status.CreatorID)
358+
apiStatus.Creator = ToUser(creator, false, false)
359+
}
360+
361+
return apiStatus
362+
}

modules/convert/git_commit.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string]
8686
}
8787

8888
if ok {
89-
apiAuthor = cacheAuthor.APIFormat()
89+
apiAuthor = ToUser(cacheAuthor, false, false)
9090
} else {
9191
author, err := models.GetUserByEmail(commit.Author.Email)
9292
if err != nil && !models.IsErrUserNotExist(err) {
9393
return nil, err
9494
} else if err == nil {
95-
apiAuthor = author.APIFormat()
95+
apiAuthor = ToUser(author, false, false)
9696
if userCache != nil {
9797
userCache[commit.Author.Email] = author
9898
}
@@ -108,13 +108,13 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string]
108108
}
109109

110110
if ok {
111-
apiCommitter = cacheCommitter.APIFormat()
111+
apiCommitter = ToUser(cacheCommitter, false, false)
112112
} else {
113113
committer, err := models.GetUserByEmail(commit.Committer.Email)
114114
if err != nil && !models.IsErrUserNotExist(err) {
115115
return nil, err
116116
} else if err == nil {
117-
apiCommitter = committer.APIFormat()
117+
apiCommitter = ToUser(committer, false, false)
118118
if userCache != nil {
119119
userCache[commit.Committer.Email] = committer
120120
}

modules/convert/issue.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func ToAPIIssue(issue *models.Issue) *api.Issue {
3131
URL: issue.APIURL(),
3232
HTMLURL: issue.HTMLURL(),
3333
Index: issue.Index,
34-
Poster: issue.Poster.APIFormat(),
34+
Poster: ToUser(issue.Poster, false, false),
3535
Title: issue.Title,
3636
Body: issue.Content,
3737
Labels: ToLabelList(issue.Labels),
@@ -65,9 +65,9 @@ func ToAPIIssue(issue *models.Issue) *api.Issue {
6565
}
6666
if len(issue.Assignees) > 0 {
6767
for _, assignee := range issue.Assignees {
68-
apiIssue.Assignees = append(apiIssue.Assignees, assignee.APIFormat())
68+
apiIssue.Assignees = append(apiIssue.Assignees, ToUser(assignee, false, false))
6969
}
70-
apiIssue.Assignee = issue.Assignees[0].APIFormat() // For compatibility, we're keeping the first assignee as `apiIssue.Assignee`
70+
apiIssue.Assignee = ToUser(issue.Assignees[0], false, false) // For compatibility, we're keeping the first assignee as `apiIssue.Assignee`
7171
}
7272
if issue.IsPull {
7373
if err := issue.LoadPullRequest(); err != nil {

modules/convert/issue_comment.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package convert
6+
7+
import (
8+
"code.gitea.io/gitea/models"
9+
api "code.gitea.io/gitea/modules/structs"
10+
)
11+
12+
// ToComment converts a models.Comment to the api.Comment format
13+
func ToComment(c *models.Comment) *api.Comment {
14+
return &api.Comment{
15+
ID: c.ID,
16+
Poster: ToUser(c.Poster, false, false),
17+
HTMLURL: c.HTMLURL(),
18+
IssueURL: c.IssueURL(),
19+
PRURL: c.PRURL(),
20+
Body: c.Content,
21+
Created: c.CreatedUnix.AsTime(),
22+
Updated: c.UpdatedUnix.AsTime(),
23+
}
24+
}

modules/convert/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
141141
if pr.HasMerged {
142142
apiPullRequest.Merged = pr.MergedUnix.AsTimePtr()
143143
apiPullRequest.MergedCommitID = &pr.MergedCommitID
144-
apiPullRequest.MergedBy = pr.Merger.APIFormat()
144+
apiPullRequest.MergedBy = ToUser(pr.Merger, false, false)
145145
}
146146

147147
return apiPullRequest

0 commit comments

Comments
 (0)