Skip to content
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

[refactor] Unify the export of user data via API #15144

Merged
merged 7 commits into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
6 changes: 3 additions & 3 deletions integrations/api_issue_reaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestAPIIssuesReactions(t *testing.T) {
DecodeJSON(t, resp, &apiReactions)
expectResponse := make(map[int]api.Reaction)
expectResponse[0] = api.Reaction{
User: convert.ToUser(user2, true, true),
User: convert.ToUser(user2, user2),
Reaction: "eyes",
Created: time.Unix(1573248003, 0),
}
Expand Down Expand Up @@ -121,12 +121,12 @@ func TestAPICommentReactions(t *testing.T) {
DecodeJSON(t, resp, &apiReactions)
expectResponse := make(map[int]api.Reaction)
expectResponse[0] = api.Reaction{
User: convert.ToUser(user2, true, true),
User: convert.ToUser(user2, user2),
Reaction: "laugh",
Created: time.Unix(1573248004, 0),
}
expectResponse[1] = api.Reaction{
User: convert.ToUser(user1, true, true),
User: convert.ToUser(user1, user1),
Reaction: "laugh",
Created: time.Unix(1573248005, 0),
}
Expand Down
10 changes: 9 additions & 1 deletion integrations/api_team_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,13 @@ func TestAPITeamUser(t *testing.T) {
user2.Created = user2.Created.In(time.Local)
user := models.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User)

assert.Equal(t, convert.ToUser(user, true, false), user2)
expectedUser := convert.ToUser(user, user)

// test time via unix timestamp
assert.EqualValues(t, expectedUser.LastLogin.Unix(), user2.LastLogin.Unix())
assert.EqualValues(t, expectedUser.Created.Unix(), user2.Created.Unix())
expectedUser.LastLogin = user2.LastLogin
expectedUser.Created = user2.Created

assert.Equal(t, expectedUser, user2)
}
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 = 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
}
Expand All @@ -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
}
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: ToUser(issue.Poster, false, false),
Poster: ToUser(issue.Poster, nil),
Title: issue.Title,
Body: issue.Content,
Ref: issue.Ref,
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion modules/convert/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion modules/convert/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 2 additions & 11 deletions modules/convert/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: comment.PosterID,
Created: comment.CreatedUnix.AsTime(),
Updated: comment.UpdatedUnix.AsTime(),
Expand Down
2 changes: 1 addition & 1 deletion modules/convert/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/convert/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,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,
Expand Down
2 changes: 1 addition & 1 deletion modules/convert/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 23 additions & 2 deletions modules/convert/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions modules/convert/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading