summaryrefslogtreecommitdiffstats
path: root/modules/convert
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2021-03-27 17:45:26 +0100
committerGitHub <noreply@github.com>2021-03-27 17:45:26 +0100
commit290cf75f9343a43d9770b1d6f8f3332a23357e27 (patch)
tree520f9f8616fbff3b43a1808ef2d73af3f2fe534b /modules/convert
parentf4d27498bdc615605e14f783501e1d1256cd8d22 (diff)
downloadgitea-290cf75f9343a43d9770b1d6f8f3332a23357e27.tar.gz
gitea-290cf75f9343a43d9770b1d6f8f3332a23357e27.zip
[refactor] Unify the export of user data via API (#15144)
* [refactor] unify how user data is exported via API * test time via unix timestamp
Diffstat (limited to 'modules/convert')
-rw-r--r--modules/convert/git_commit.go8
-rw-r--r--modules/convert/issue.go6
-rw-r--r--modules/convert/issue_comment.go2
-rw-r--r--modules/convert/pull.go2
-rw-r--r--modules/convert/pull_review.go13
-rw-r--r--modules/convert/release.go2
-rw-r--r--modules/convert/repository.go2
-rw-r--r--modules/convert/status.go2
-rw-r--r--modules/convert/user.go25
-rw-r--r--modules/convert/user_test.go6
10 files changed, 40 insertions, 28 deletions
diff --git a/modules/convert/git_commit.go b/modules/convert/git_commit.go
index 4e30ec2c0b..c647dd4e18 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 = ToUser(cacheAuthor, false, false)
+ apiAuthor = ToUser(cacheAuthor, nil)
} else {
author, err := models.GetUserByEmail(commit.Author.Email)
if err != nil && !models.IsErrUserNotExist(err) {
return nil, err
} else if err == nil {
- apiAuthor = ToUser(author, false, false)
+ apiAuthor = ToUser(author, nil)
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 = ToUser(cacheCommitter, false, false)
+ apiCommitter = ToUser(cacheCommitter, nil)
} else {
committer, err := models.GetUserByEmail(commit.Committer.Email)
if err != nil && !models.IsErrUserNotExist(err) {
return nil, err
} else if err == nil {
- apiCommitter = ToUser(committer, false, false)
+ apiCommitter = ToUser(committer, nil)
if userCache != nil {
userCache[commit.Committer.Email] = committer
}
diff --git a/modules/convert/issue.go b/modules/convert/issue.go
index b773e78a6b..da09aeaca4 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: ToUser(issue.Poster, false, false),
+ Poster: ToUser(issue.Poster, nil),
Title: issue.Title,
Body: issue.Content,
Ref: issue.Ref,
@@ -66,9 +66,9 @@ func ToAPIIssue(issue *models.Issue) *api.Issue {
}
if len(issue.Assignees) > 0 {
for _, assignee := range issue.Assignees {
- apiIssue.Assignees = append(apiIssue.Assignees, ToUser(assignee, false, false))
+ apiIssue.Assignees = append(apiIssue.Assignees, ToUser(assignee, nil))
}
- apiIssue.Assignee = ToUser(issue.Assignees[0], false, false) // For compatibility, we're keeping the first assignee as `apiIssue.Assignee`
+ apiIssue.Assignee = ToUser(issue.Assignees[0], nil) // 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
index cf65c876d3..1610b9f0d8 100644
--- a/modules/convert/issue_comment.go
+++ b/modules/convert/issue_comment.go
@@ -13,7 +13,7 @@ import (
func ToComment(c *models.Comment) *api.Comment {
return &api.Comment{
ID: c.ID,
- Poster: ToUser(c.Poster, false, false),
+ Poster: ToUser(c.Poster, nil),
HTMLURL: c.HTMLURL(),
IssueURL: c.IssueURL(),
PRURL: c.PRURL(),
diff --git a/modules/convert/pull.go b/modules/convert/pull.go
index 3c24f4532f..8bdf17a049 100644
--- a/modules/convert/pull.go
+++ b/modules/convert/pull.go
@@ -159,7 +159,7 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
if pr.HasMerged {
apiPullRequest.Merged = pr.MergedUnix.AsTimePtr()
apiPullRequest.MergedCommitID = &pr.MergedCommitID
- apiPullRequest.MergedBy = ToUser(pr.Merger, false, false)
+ apiPullRequest.MergedBy = ToUser(pr.Merger, nil)
}
return apiPullRequest
diff --git a/modules/convert/pull_review.go b/modules/convert/pull_review.go
index 418cb711dc..18c6c8e58c 100644
--- a/modules/convert/pull_review.go
+++ b/modules/convert/pull_review.go
@@ -20,14 +20,9 @@ func ToPullReview(r *models.Review, doer *models.User) (*api.PullReview, error)
r.Reviewer = models.NewGhostUser()
}
- auth := false
- if doer != nil {
- auth = doer.IsAdmin || doer.ID == r.ReviewerID
- }
-
result := &api.PullReview{
ID: r.ID,
- Reviewer: ToUser(r.Reviewer, doer != nil, auth),
+ Reviewer: ToUser(r.Reviewer, doer),
ReviewerTeam: ToTeam(r.ReviewerTeam),
State: api.ReviewStateUnknown,
Body: r.Content,
@@ -88,14 +83,10 @@ func ToPullReviewCommentList(review *models.Review, doer *models.User) ([]*api.P
for _, lines := range review.CodeComments {
for _, comments := range lines {
for _, comment := range comments {
- auth := false
- if doer != nil {
- auth = doer.IsAdmin || doer.ID == comment.Poster.ID
- }
apiComment := &api.PullReviewComment{
ID: comment.ID,
Body: comment.Content,
- Reviewer: ToUser(comment.Poster, doer != nil, auth),
+ Reviewer: ToUser(comment.Poster, doer),
ReviewID: review.ID,
Created: comment.CreatedUnix.AsTime(),
Updated: comment.UpdatedUnix.AsTime(),
diff --git a/modules/convert/release.go b/modules/convert/release.go
index d9def89637..70f0d6e764 100644
--- a/modules/convert/release.go
+++ b/modules/convert/release.go
@@ -29,7 +29,7 @@ func ToRelease(r *models.Release) *api.Release {
IsPrerelease: r.IsPrerelease,
CreatedAt: r.CreatedUnix.AsTime(),
PublishedAt: r.CreatedUnix.AsTime(),
- Publisher: ToUser(r.Publisher, false, false),
+ Publisher: ToUser(r.Publisher, nil),
Attachments: assets,
}
}
diff --git a/modules/convert/repository.go b/modules/convert/repository.go
index 63a22bb04e..9a4fbb97ca 100644
--- a/modules/convert/repository.go
+++ b/modules/convert/repository.go
@@ -102,7 +102,7 @@ func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool)
return &api.Repository{
ID: repo.ID,
- Owner: ToUser(repo.Owner, mode != models.AccessModeNone, mode >= models.AccessModeAdmin),
+ Owner: ToUserWithAccessMode(repo.Owner, mode),
Name: repo.Name,
FullName: repo.FullName(),
Description: repo.Description,
diff --git a/modules/convert/status.go b/modules/convert/status.go
index f972fc333c..bb2f964fe4 100644
--- a/modules/convert/status.go
+++ b/modules/convert/status.go
@@ -24,7 +24,7 @@ func ToCommitStatus(status *models.CommitStatus) *api.CommitStatus {
if status.CreatorID != 0 {
creator, _ := models.GetUserByID(status.CreatorID)
- apiStatus.Creator = ToUser(creator, false, false)
+ apiStatus.Creator = ToUser(creator, nil)
}
return apiStatus
diff --git a/modules/convert/user.go b/modules/convert/user.go
index f5d853fd4d..f3a33aee41 100644
--- a/modules/convert/user.go
+++ b/modules/convert/user.go
@@ -11,11 +11,32 @@ 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 doer is set, private information is added if the doer has the permission to see it
+func ToUser(user, doer *models.User) *api.User {
+ if user == nil {
+ return nil
+ }
+ authed := false
+ signed := false
+ if doer != nil {
+ signed = true
+ authed = doer.ID == user.ID || doer.IsAdmin
+ }
+ return toUser(user, signed, authed)
+}
+
+// ToUserWithAccessMode convert models.User to api.User
+// AccessMode is not none show add some more information
+func ToUserWithAccessMode(user *models.User, accessMode models.AccessMode) *api.User {
if user == nil {
return nil
}
+ return toUser(user, accessMode != models.AccessModeNone, false)
+}
+
+// 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 {
result := &api.User{
ID: user.ID,
UserName: user.Name,
diff --git a/modules/convert/user_test.go b/modules/convert/user_test.go
index eff60d5183..3939653441 100644
--- a/modules/convert/user_test.go
+++ b/modules/convert/user_test.go
@@ -15,14 +15,14 @@ func TestUser_ToUser(t *testing.T) {
user1 := models.AssertExistsAndLoadBean(t, &models.User{ID: 1, IsAdmin: true}).(*models.User)
- apiUser := ToUser(user1, true, true)
+ 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)
+ apiUser = toUser(user2, true, true)
assert.False(t, apiUser.IsAdmin)
- apiUser = ToUser(user1, false, false)
+ apiUser = toUser(user1, false, false)
assert.False(t, apiUser.IsAdmin)
}