Skip to content

[Refactor] Move APIFormat functions into convert package #12856

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

Merged
merged 28 commits into from
Oct 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1b1270f
USER APIFormat -> ToUser
6543 Sep 27, 2020
e423b5a
Migrate more and mark APIFormat deprecated
6543 Sep 27, 2020
109fa5c
models.Comment APIFormat() -> convert.ToComment
6543 Sep 27, 2020
ab06cd6
models.Release APIFormat() -> convert.ToRelease
6543 Sep 27, 2020
4296c39
models.Attachments APIFormat() -> convert.ToReleaseAttachments
6543 Sep 27, 2020
cf548d6
models.CommitStatus APIFormat() -> convert.ToCommitStatus
6543 Sep 27, 2020
9f30914
finish migration to convert.ToUser
6543 Sep 27, 2020
9bb5436
Move Test
6543 Sep 27, 2020
c8e246d
Merge branch 'master' into refacotr-user-2-apifromat
6543 Sep 29, 2020
5c68b39
Merge branch 'master' into refacotr-user-2-apifromat
6543 Oct 1, 2020
d71e763
Imprufe Test
6543 Oct 1, 2020
72c2800
fix test
6543 Oct 1, 2020
d6aa456
Merge branch 'master' into refacotr-user-2-apifromat
6543 Oct 2, 2020
70269f8
Merge branch 'master' into refacotr-user-2-apifromat
6543 Oct 3, 2020
bace6ed
Merge branch 'master' into refacotr-user-2-apifromat
6543 Oct 5, 2020
f06b12c
Merge branch 'master' into refacotr-user-2-apifromat
6543 Oct 6, 2020
43297ea
Merge branch 'master' into refacotr-user-2-apifromat
6543 Oct 7, 2020
e1a4472
Merge branch 'master' into refacotr-user-2-apifromat
6543 Oct 10, 2020
2ad59aa
Merge branch 'master' into refacotr-user-2-apifromat
6543 Oct 12, 2020
09a97ae
Merge branch 'master' into refacotr-user-2-apifromat
6543 Oct 13, 2020
cbdf007
Merge branch 'master' into refacotr-user-2-apifromat
6543 Oct 13, 2020
3913c19
Merge branch 'master' into refacotr-user-2-apifromat
6543 Oct 14, 2020
6c6d748
Merge branch 'master' into refacotr-user-2-apifromat
6543 Oct 14, 2020
50b9225
Merge branch 'master' into refacotr-user-2-apifromat
6543 Oct 14, 2020
c9d4078
Merge branch 'master' into refacotr-user-2-apifromat
6543 Oct 14, 2020
c1f8bea
Merge branch 'master' into refacotr-user-2-apifromat
6543 Oct 16, 2020
58a2511
Merge branch 'master' into refacotr-user-2-apifromat
6543 Oct 16, 2020
6c6891e
Merge branch 'master' into refacotr-user-2-apifromat
techknowlogick Oct 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion integrations/api_comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs"

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

assert.NoError(t, comment.LoadPoster())
expect := comment.APIFormat()
expect := convert.ToComment(comment)

assert.Equal(t, expect.ID, apiComment.ID)
assert.Equal(t, expect.Poster.FullName, apiComment.Poster.FullName)
Expand Down
7 changes: 4 additions & 3 deletions integrations/api_issue_reaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -60,7 +61,7 @@ func TestAPIIssuesReactions(t *testing.T) {
DecodeJSON(t, resp, &apiReactions)
expectResponse := make(map[int]api.Reaction)
expectResponse[0] = api.Reaction{
User: user2.APIFormat(),
User: convert.ToUser(user2, true, true),
Reaction: "eyes",
Created: time.Unix(1573248003, 0),
}
Expand Down Expand Up @@ -120,12 +121,12 @@ func TestAPICommentReactions(t *testing.T) {
DecodeJSON(t, resp, &apiReactions)
expectResponse := make(map[int]api.Reaction)
expectResponse[0] = api.Reaction{
User: user2.APIFormat(),
User: convert.ToUser(user2, true, true),
Reaction: "laugh",
Created: time.Unix(1573248004, 0),
}
expectResponse[1] = api.Reaction{
User: user1.APIFormat(),
User: convert.ToUser(user1, true, true),
Reaction: "laugh",
Created: time.Unix(1573248005, 0),
}
Expand Down
14 changes: 0 additions & 14 deletions models/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"

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

// APIFormat converts models.Attachment to api.Attachment
func (a *Attachment) APIFormat() *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(),
}
}

// AttachmentRelativePath returns the relative path
func AttachmentRelativePath(uuid string) string {
return path.Join(uuid[0:1], uuid[1:2], uuid)
Expand Down
21 changes: 0 additions & 21 deletions models/commit_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,6 @@ func (status *CommitStatus) APIURL() string {
setting.AppURL, status.Repo.FullName(), status.SHA)
}

// APIFormat assumes some fields assigned with values:
// Required - Repo, Creator
func (status *CommitStatus) APIFormat() *api.Status {
_ = status.loadRepo(x)
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.Creator != nil {
apiStatus.Creator = status.Creator.APIFormat()
}

return apiStatus
}

// CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc
func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
var lastStatus *CommitStatus
Expand Down
15 changes: 0 additions & 15 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/references"
"code.gitea.io/gitea/modules/structs"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"

"github.com/unknwon/com"
Expand Down Expand Up @@ -354,20 +353,6 @@ func (c *Comment) PRURL() string {
return c.Issue.HTMLURL()
}

// APIFormat converts a Comment to the api.Comment format
func (c *Comment) APIFormat() *api.Comment {
return &api.Comment{
ID: c.ID,
Poster: c.Poster.APIFormat(),
HTMLURL: c.HTMLURL(),
IssueURL: c.IssueURL(),
PRURL: c.PRURL(),
Body: c.Content,
Created: c.CreatedUnix.AsTime(),
Updated: c.UpdatedUnix.AsTime(),
}
}

// CommentHashTag returns unique hash tag for comment id.
func CommentHashTag(id int64) string {
return fmt.Sprintf("issuecomment-%d", id)
Expand Down
26 changes: 0 additions & 26 deletions models/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"

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

// APIFormat convert a Release to api.Release
func (r *Release) APIFormat() *api.Release {
assets := make([]*api.Attachment, 0)
for _, att := range r.Attachments {
assets = append(assets, att.APIFormat())
}
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: r.Publisher.APIFormat(),
Attachments: assets,
}
}

// IsReleaseExist returns true if release with given tag name already exists.
func IsReleaseExist(repoID int64, tagName string) (bool, error) {
if len(tagName) == 0 {
Expand Down
13 changes: 11 additions & 2 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,17 @@ func (repo *Repository) innerAPIFormat(e Engine, mode AccessMode, isParent bool)
numReleases, _ := GetReleaseCountByRepoID(repo.ID, FindReleasesOptions{IncludeDrafts: false, IncludeTags: true})

return &api.Repository{
ID: repo.ID,
Owner: repo.Owner.APIFormat(),
ID: repo.ID,
// TODO use convert.ToUser(repo.Owner)
Owner: &api.User{
ID: repo.Owner.ID,
UserName: repo.Owner.Name,
FullName: repo.Owner.FullName,
Email: repo.Owner.GetEmail(),
AvatarURL: repo.Owner.AvatarLink(),
LastLogin: repo.Owner.LastLoginUnix.AsTime(),
Created: repo.Owner.CreatedUnix.AsTime(),
},
Name: repo.Name,
FullName: repo.FullName(),
Description: repo.Description,
Expand Down
19 changes: 0 additions & 19 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/structs"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

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

// APIFormat converts a User to api.User
func (u *User) APIFormat() *api.User {
if u == nil {
return nil
}
return &api.User{
ID: u.ID,
UserName: u.Name,
FullName: u.FullName,
Email: u.GetEmail(),
AvatarURL: u.AvatarLink(),
Language: u.Language,
IsAdmin: u.IsAdmin,
LastLogin: u.LastLoginUnix.AsTime(),
Created: u.CreatedUnix.AsTime(),
}
}

// IsLocal returns true if user login type is LoginPlain.
func (u *User) IsLocal() bool {
return u.LoginType <= LoginPlain
Expand Down
17 changes: 0 additions & 17 deletions models/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,6 @@ func TestGetUserEmailsByNames(t *testing.T) {
assert.Equal(t, []string{"[email protected]"}, GetUserEmailsByNames([]string{"user8", "user7"}))
}

func TestUser_APIFormat(t *testing.T) {

user, err := GetUserByID(1)
assert.NoError(t, err)
assert.True(t, user.IsAdmin)

apiUser := user.APIFormat()
assert.True(t, apiUser.IsAdmin)

user, err = GetUserByID(2)
assert.NoError(t, err)
assert.False(t, user.IsAdmin)

apiUser = user.APIFormat()
assert.False(t, apiUser.IsAdmin)
}

func TestCanCreateOrganization(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

Expand Down
10 changes: 0 additions & 10 deletions models/userlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"

"code.gitea.io/gitea/modules/log"
api "code.gitea.io/gitea/modules/structs"
)

//UserList is a list of user.
Expand Down Expand Up @@ -94,12 +93,3 @@ func (users UserList) loadTwoFactorStatus(e Engine) (map[int64]*TwoFactor, error
}
return tokenMaps, nil
}

//APIFormat return list of users in api format
func (users UserList) APIFormat() []*api.User {
result := make([]*api.User, 0, len(users))
for _, u := range users {
result = append(result, u.APIFormat())
}
return result
}
21 changes: 21 additions & 0 deletions modules/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions modules/convert/git_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions modules/convert/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions modules/convert/issue_comment.go
Original file line number Diff line number Diff line change
@@ -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(),
}
}
2 changes: 1 addition & 1 deletion modules/convert/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading