Skip to content

Commit

Permalink
Merge branch 'master' into fix-14682-update-size-cron-gc
Browse files Browse the repository at this point in the history
  • Loading branch information
zeripath committed Mar 27, 2021
2 parents b7f0def + 290cf75 commit 467e294
Show file tree
Hide file tree
Showing 36 changed files with 187 additions and 99 deletions.
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)
}
2 changes: 1 addition & 1 deletion models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ func CreateRepository(ctx DBContext, doer, u *User, repo *Repository, overwriteO
units = append(units, RepoUnit{
RepoID: repo.ID,
Type: tp,
Config: &PullRequestsConfig{AllowMerge: true, AllowRebase: true, AllowRebaseMerge: true, AllowSquash: true},
Config: &PullRequestsConfig{AllowMerge: true, AllowRebase: true, AllowRebaseMerge: true, AllowSquash: true, DefaultMergeStyle: MergeStyleMerge},
})
} else {
units = append(units, RepoUnit{
Expand Down
10 changes: 10 additions & 0 deletions models/repo_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type PullRequestsConfig struct {
AllowSquash bool
AllowManualMerge bool
AutodetectManualMerge bool
DefaultMergeStyle MergeStyle
}

// FromDB fills up a PullRequestsConfig from serialized format.
Expand All @@ -125,6 +126,15 @@ func (cfg *PullRequestsConfig) IsMergeStyleAllowed(mergeStyle MergeStyle) bool {
mergeStyle == MergeStyleManuallyMerged && cfg.AllowManualMerge
}

// GetDefaultMergeStyle returns the default merge style for this pull request
func (cfg *PullRequestsConfig) GetDefaultMergeStyle() MergeStyle {
if len(cfg.DefaultMergeStyle) != 0 {
return cfg.DefaultMergeStyle
}

return MergeStyleMerge
}

// AllowedMergeStyleCount returns the total count of allowed merge styles for the PullRequestsConfig
func (cfg *PullRequestsConfig) AllowedMergeStyleCount() int {
count := 0
Expand Down
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: review.ID,
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
5 changes: 4 additions & 1 deletion modules/convert/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool)
allowRebase := false
allowRebaseMerge := false
allowSquash := false
defaultMergeStyle := models.MergeStyleMerge
if unit, err := repo.GetUnit(models.UnitTypePullRequests); err == nil {
config := unit.PullRequestsConfig()
hasPullRequests = true
Expand All @@ -79,6 +80,7 @@ func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool)
allowRebase = config.AllowRebase
allowRebaseMerge = config.AllowRebaseMerge
allowSquash = config.AllowSquash
defaultMergeStyle = config.GetDefaultMergeStyle()
}
hasProjects := false
if _, err := repo.GetUnit(models.UnitTypeProjects); err == nil {
Expand All @@ -100,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,
Expand Down Expand Up @@ -139,6 +141,7 @@ func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool)
AllowRebase: allowRebase,
AllowRebaseMerge: allowRebaseMerge,
AllowSquash: allowSquash,
DefaultMergeStyle: string(defaultMergeStyle),
AvatarURL: repo.AvatarLink(),
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
MirrorInterval: mirrorInterval,
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)
}
1 change: 1 addition & 0 deletions modules/forms/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ type RepoSettingForm struct {
PullsAllowRebaseMerge bool
PullsAllowSquash bool
PullsAllowManualMerge bool
PullsDefaultMergeStyle string
EnableAutodetectManualMerge bool
EnableTimetracker bool
AllowOnlyContributorsToTrackTime bool
Expand Down

0 comments on commit 467e294

Please sign in to comment.