forked from forgejo/forgejo
[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
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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())
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue