summaryrefslogtreecommitdiffstats
path: root/modules/convert
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2020-10-17 06:23:08 +0200
committerGitHub <noreply@github.com>2020-10-17 00:23:08 -0400
commitd453533beb197d25b25530dc5f0e3c3c0742d8d1 (patch)
tree6d66e1163333344d91af63e9ab50a82367470b8f /modules/convert
parent131278ff222f1a8580f20e6bbdff405341403042 (diff)
downloadgitea-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 'modules/convert')
-rw-r--r--modules/convert/convert.go21
-rw-r--r--modules/convert/git_commit.go8
-rw-r--r--modules/convert/issue.go6
-rw-r--r--modules/convert/issue_comment.go24
-rw-r--r--modules/convert/pull.go2
-rw-r--r--modules/convert/release.go48
-rw-r--r--modules/convert/user.go3
-rw-r--r--modules/convert/user_test.go28
8 files changed, 132 insertions, 8 deletions
diff --git a/modules/convert/convert.go b/modules/convert/convert.go
index ec676002b9..e81df0c0c3 100644
--- a/modules/convert/convert.go
+++ b/modules/convert/convert.go
@@ -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
+}
diff --git a/modules/convert/git_commit.go b/modules/convert/git_commit.go
index 86d6dae961..87dfb51e70 100644
--- a/modules/convert/git_commit.go
+++ b/modules/convert/git_commit.go
@@ -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
}
diff --git a/modules/convert/issue.go b/modules/convert/issue.go
index 724ec8ffcf..f34656b47c 100644
--- a/modules/convert/issue.go
+++ b/modules/convert/issue.go
@@ -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 {
diff --git a/modules/convert/issue_comment.go b/modules/convert/issue_comment.go
new file mode 100644
index 0000000000..cf65c876d3
--- /dev/null
+++ b/modules/convert/issue_comment.go
@@ -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(),
+ }
+}
diff --git a/modules/convert/pull.go b/modules/convert/pull.go
index 2fa22efcb3..e522bee787 100644
--- a/modules/convert/pull.go
+++ b/modules/convert/pull.go
@@ -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
diff --git a/modules/convert/release.go b/modules/convert/release.go
new file mode 100644
index 0000000000..d9def89637
--- /dev/null
+++ b/modules/convert/release.go
@@ -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(),
+ }
+}
diff --git a/modules/convert/user.go b/modules/convert/user.go
index c75a8aac52..010c92b969 100644
--- a/modules/convert/user.go
+++ b/modules/convert/user.go
@@ -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,
diff --git a/modules/convert/user_test.go b/modules/convert/user_test.go
new file mode 100644
index 0000000000..eff60d5183
--- /dev/null
+++ b/modules/convert/user_test.go
@@ -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)
+}