diff options
author | 6543 <6543@obermui.de> | 2020-10-17 06:23:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-17 00:23:08 -0400 |
commit | d453533beb197d25b25530dc5f0e3c3c0742d8d1 (patch) | |
tree | 6d66e1163333344d91af63e9ab50a82367470b8f /models | |
parent | 131278ff222f1a8580f20e6bbdff405341403042 (diff) | |
download | gitea-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 'models')
-rw-r--r-- | models/attachment.go | 14 | ||||
-rw-r--r-- | models/commit_status.go | 21 | ||||
-rw-r--r-- | models/issue_comment.go | 15 | ||||
-rw-r--r-- | models/release.go | 26 | ||||
-rw-r--r-- | models/repo.go | 13 | ||||
-rw-r--r-- | models/user.go | 19 | ||||
-rw-r--r-- | models/user_test.go | 17 | ||||
-rw-r--r-- | models/userlist.go | 10 |
8 files changed, 11 insertions, 124 deletions
diff --git a/models/attachment.go b/models/attachment.go index 26f466a400..55a6cfc014 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -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" @@ -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) diff --git a/models/commit_status.go b/models/commit_status.go index ed6f8702c5..15fcbff6f9 100644 --- a/models/commit_status.go +++ b/models/commit_status.go @@ -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 diff --git a/models/issue_comment.go b/models/issue_comment.go index 270a10e240..5c053ec02a 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -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" @@ -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) diff --git a/models/release.go b/models/release.go index f55341b86b..b8ffb257a2 100644 --- a/models/release.go +++ b/models/release.go @@ -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" @@ -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 { diff --git a/models/repo.go b/models/repo.go index efdd7049de..5198026329 100644 --- a/models/repo.go +++ b/models/repo.go @@ -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, diff --git a/models/user.go b/models/user.go index f019475298..2e38502c5b 100644 --- a/models/user.go +++ b/models/user.go @@ -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" @@ -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 diff --git a/models/user_test.go b/models/user_test.go index d03ef4fad4..7a6f5aa512 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -78,23 +78,6 @@ func TestGetUserEmailsByNames(t *testing.T) { assert.Equal(t, []string{"user8@example.com"}, 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()) diff --git a/models/userlist.go b/models/userlist.go index 7e6cab50ba..a2a4248482 100644 --- a/models/userlist.go +++ b/models/userlist.go @@ -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. @@ -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 -} |