From bc65c9a77d210cc918e820bcf04f5c470eea0958 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 22 Aug 2023 08:03:47 +0000 Subject: [PATCH 01/17] improve --- models/issues/comment.go | 15 ++++-- modules/git/commit.go | 5 +- modules/git/repo_commit.go | 17 ++++-- options/locale/locale_en-US.ini | 13 ++++- routers/web/repo/issue.go | 52 ++++++++++++++----- .../repo/issue/view_content/show_role.tmpl | 28 +++++++--- 6 files changed, 101 insertions(+), 29 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index e78193126183..141a28d8b11c 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -188,8 +188,11 @@ type RoleDescriptor int const ( RoleDescriptorNone RoleDescriptor = iota RoleDescriptorPoster - RoleDescriptorWriter RoleDescriptorOwner + RoleDescriptorMember + RoleDescriptorCollaborator + RoleDescriptorFirstTimeContributor + RoleDescriptorContributor ) // WithRole enable a specific tag on the RoleDescriptor. @@ -201,10 +204,16 @@ func stringToRoleDescriptor(role string) RoleDescriptor { switch role { case "Poster": return RoleDescriptorPoster - case "Writer": - return RoleDescriptorWriter case "Owner": return RoleDescriptorOwner + case "Member": + return RoleDescriptorMember + case "Collaborator": + return RoleDescriptorCollaborator + case "First-time contributor": + return RoleDescriptorFirstTimeContributor + case "Contributor": + return RoleDescriptorContributor default: return RoleDescriptorNone } diff --git a/modules/git/commit.go b/modules/git/commit.go index c44882d88617..3d5b4f3ad4c1 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -258,10 +258,12 @@ func (c *Commit) CommitsBeforeUntil(commitID string) ([]*Commit, error) { // SearchCommitsOptions specify the parameters for SearchCommits type SearchCommitsOptions struct { + CommitID SHA1 Keywords []string Authors, Committers []string After, Before string All bool + Limit int } // NewSearchCommitsOptions construct a SearchCommitsOption from a space-delimited search string @@ -297,7 +299,8 @@ func NewSearchCommitsOptions(searchString string, forAllRefs bool) SearchCommits // SearchCommits returns the commits match the keyword before current revision func (c *Commit) SearchCommits(opts SearchCommitsOptions) ([]*Commit, error) { - return c.repo.searchCommits(c.ID, opts) + opts.CommitID = c.ID + return c.repo.SearchCommits(opts) } // GetFilesChangedSinceCommit get all changed file names between pastCommit to current revision diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 6fc306362911..6fab4c88c520 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -109,7 +109,7 @@ func (repo *Repository) commitsByRange(id SHA1, page, pageSize int, not string) return repo.parsePrettyFormatLogToList(stdout) } -func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) ([]*Commit, error) { +func (repo *Repository) SearchCommits(opts SearchCommitsOptions) ([]*Commit, error) { // add common arguments to git command addCommonSearchArgs := func(c *Command) { // ignore case @@ -138,8 +138,19 @@ func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) ([]*Co } } - // create new git log command with limit of 100 commits - cmd := NewCommand(repo.Ctx, "log", "-100", prettyLogFormat).AddDynamicArguments(id.String()) + // create new git log command + cmd := NewCommand(repo.Ctx, "log") + + // By default, limit 100 commits + limit := 100 + if opts.Limit > 0 { + limit = opts.Limit + } + cmd = cmd.AddOptionFormat("-%d", limit).AddArguments(prettyLogFormat) + + if !opts.CommitID.IsZero() { + cmd = cmd.AddDynamicArguments(opts.CommitID.String()) + } // pretend that all refs along with HEAD were listed on command line as // https://git-scm.com/docs/git-log#Documentation/git-log.txt---all diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 587a2b14bcf2..72265104432e 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1480,9 +1480,18 @@ issues.ref_reopening_from = `referenced a pull request %[4]s tha issues.ref_closed_from = `closed this issue %[4]s %[2]s` issues.ref_reopened_from = `reopened this issue %[4]s %[2]s` issues.ref_from = `from %[1]s` -issues.poster = Poster -issues.collaborator = Collaborator +issues.author = Author +issues.author_helper = This user is the author. issues.owner = Owner +issues.owner_helper = This user is the owner of this repository. +issues.member = Member +issues.member_helper = This user is a member of the organization. +issues.collaborator = Collaborator +issues.collaborator_helper = This user has been invited to collaborate on the repository. +issues.first_time_contributor = First-time contributor +issues.first_time_contributor_helper = This user is a first-time contributor to the repository. +issues.contributor = Contributor +issues.contributor_helper = This user has previously committed to the repository. issues.re_request_review=Re-request review issues.is_stale = There have been changes to this PR since this review issues.remove_request_review=Remove review request diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index b04802e4520d..5435217937eb 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1229,7 +1229,7 @@ func NewIssuePost(ctx *context.Context) { } // roleDescriptor returns the Role Descriptor for a comment in/with the given repo, poster and issue -func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *user_model.User, issue *issues_model.Issue, hasOriginalAuthor bool) (issues_model.RoleDescriptor, error) { +func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *user_model.User, issue *issues_model.Issue, gitRepo *git.Repository, hasOriginalAuthor bool) (issues_model.RoleDescriptor, error) { if hasOriginalAuthor { return issues_model.RoleDescriptorNone, nil } @@ -1242,13 +1242,18 @@ func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *use // By default the poster has no roles on the comment. roleDescriptor := issues_model.RoleDescriptorNone + // If the poster is the actual poster of the issue, enable Poster role. + if issue.IsPoster(poster.ID) { + roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorPoster) + } + // Check if the poster is owner of the repo. if perm.IsOwner() { // If the poster isn't a admin, enable the owner role. if !poster.IsAdmin { roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorOwner) + return roleDescriptor, nil } else { - // Otherwise check if poster is the real repo admin. ok, err := access_model.IsUserRealRepoAdmin(repo, poster) if err != nil { @@ -1256,19 +1261,42 @@ func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *use } if ok { roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorOwner) + return roleDescriptor, nil } } } - // Is the poster can write issues or pulls to the repo, enable the Writer role. - // Only enable this if the poster doesn't have the owner role already. - if !roleDescriptor.HasRole("Owner") && perm.CanWriteIssuesOrPulls(issue.IsPull) { - roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorWriter) + // If repo is organization, check Member role + if err := repo.LoadOwner(ctx); err != nil { + return issues_model.RoleDescriptorNone, err + } + if repo.Owner.IsOrganization() { + if isMember, err := organization.IsOrganizationMember(ctx, repo.Owner.ID, poster.ID); err != nil { + return issues_model.RoleDescriptorNone, err + } else if isMember { + roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorMember) + return roleDescriptor, nil + } } - // If the poster is the actual poster of the issue, enable Poster role. - if issue.IsPoster(poster.ID) { - roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorPoster) + // If the poster is the collaborator of the repo + if isCollaborator, err := repo_model.IsCollaborator(ctx, repo.ID, poster.ID); err != err { + return issues_model.RoleDescriptorNone, err + } else if isCollaborator { + roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorCollaborator) + return roleDescriptor, nil + } + + if commits, err := gitRepo.SearchCommits(git.SearchCommitsOptions{ + Authors: []string{poster.Name}, + All: true, + Limit: 2, + }); err != nil { + return issues_model.RoleDescriptorNone, err + } else if len(commits) == 1 { + roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorContributor) + } else if len(commits) > 1 { + roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorFirstTimeContributor) } return roleDescriptor, nil @@ -1526,7 +1554,7 @@ func ViewIssue(ctx *context.Context) { // check if dependencies can be created across repositories ctx.Data["AllowCrossRepositoryDependencies"] = setting.Service.AllowCrossRepositoryDependencies - if issue.ShowRole, err = roleDescriptor(ctx, repo, issue.Poster, issue, issue.HasOriginalAuthor()); err != nil { + if issue.ShowRole, err = roleDescriptor(ctx, repo, issue.Poster, issue, ctx.Repo.GitRepo, issue.HasOriginalAuthor()); err != nil { ctx.ServerError("roleDescriptor", err) return } @@ -1565,7 +1593,7 @@ func ViewIssue(ctx *context.Context) { continue } - comment.ShowRole, err = roleDescriptor(ctx, repo, comment.Poster, issue, comment.HasOriginalAuthor()) + comment.ShowRole, err = roleDescriptor(ctx, repo, comment.Poster, issue, ctx.Repo.GitRepo, comment.HasOriginalAuthor()) if err != nil { ctx.ServerError("roleDescriptor", err) return @@ -1664,7 +1692,7 @@ func ViewIssue(ctx *context.Context) { continue } - c.ShowRole, err = roleDescriptor(ctx, repo, c.Poster, issue, c.HasOriginalAuthor()) + c.ShowRole, err = roleDescriptor(ctx, repo, c.Poster, issue, ctx.Repo.GitRepo, c.HasOriginalAuthor()) if err != nil { ctx.ServerError("roleDescriptor", err) return diff --git a/templates/repo/issue/view_content/show_role.tmpl b/templates/repo/issue/view_content/show_role.tmpl index f85f43bd66ab..b15e16e84015 100644 --- a/templates/repo/issue/view_content/show_role.tmpl +++ b/templates/repo/issue/view_content/show_role.tmpl @@ -1,15 +1,27 @@ {{if and (.ShowRole.HasRole "Poster") (not .IgnorePoster)}} -
- {{ctx.Locale.Tr "repo.issues.poster"}} -
-{{end}} -{{if (.ShowRole.HasRole "Writer")}} -
- {{ctx.Locale.Tr "repo.issues.collaborator"}} +
+ {{ctx.Locale.Tr "repo.issues.author"}}
{{end}} + {{if (.ShowRole.HasRole "Owner")}} -
+
{{ctx.Locale.Tr "repo.issues.owner"}}
+{{else if (.ShowRole.HasRole "Member")}} +
+ {{ctx.Locale.Tr "repo.issues.member"}} +
+{{else if (.ShowRole.HasRole "Collaborator")}} +
+ {{ctx.Locale.Tr "repo.issues.collaborator"}} +
+{{else if and (.ShowRole.HasRole "First-time contributor") (.ShowRole.HasRole "Poster")}} +
+ {{ctx.Locale.Tr "repo.issues.first_time_contributor"}} +
+{{else if (.ShowRole.HasRole "Contributor")}} +
+ {{ctx.Locale.Tr "repo.issues.contributor"}} +
{{end}} From 5807b8ccd965b9f39f29337344804381ba02bc49 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 22 Aug 2023 08:21:32 +0000 Subject: [PATCH 02/17] fix lint --- routers/web/repo/issue.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 5435217937eb..0b1f22ab80ba 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1253,16 +1253,16 @@ func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *use if !poster.IsAdmin { roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorOwner) return roleDescriptor, nil - } else { - // Otherwise check if poster is the real repo admin. - ok, err := access_model.IsUserRealRepoAdmin(repo, poster) - if err != nil { - return issues_model.RoleDescriptorNone, err - } - if ok { - roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorOwner) - return roleDescriptor, nil - } + } + + // Otherwise check if poster is the real repo admin. + ok, err := access_model.IsUserRealRepoAdmin(repo, poster) + if err != nil { + return issues_model.RoleDescriptorNone, err + } + if ok { + roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorOwner) + return roleDescriptor, nil } } @@ -1280,7 +1280,7 @@ func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *use } // If the poster is the collaborator of the repo - if isCollaborator, err := repo_model.IsCollaborator(ctx, repo.ID, poster.ID); err != err { + if isCollaborator, err := repo_model.IsCollaborator(ctx, repo.ID, poster.ID); err != nil { return issues_model.RoleDescriptorNone, err } else if isCollaborator { roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorCollaborator) From 02147a4564a9defb070ced20da5ce69a358ac893 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 23 Aug 2023 01:25:49 +0000 Subject: [PATCH 03/17] fix --- routers/web/repo/issue.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 0b1f22ab80ba..d56f1c9a7d90 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1294,9 +1294,9 @@ func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *use }); err != nil { return issues_model.RoleDescriptorNone, err } else if len(commits) == 1 { - roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorContributor) - } else if len(commits) > 1 { roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorFirstTimeContributor) + } else if len(commits) > 1 { + roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorContributor) } return roleDescriptor, nil From d9d118991c34ae3a23cb54692512ba9dd6c4ad15 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 23 Aug 2023 10:29:38 +0900 Subject: [PATCH 04/17] Update options/locale/locale_en-US.ini Co-authored-by: delvh --- options/locale/locale_en-US.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 72265104432e..4fc5bc258b92 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1485,7 +1485,7 @@ issues.author_helper = This user is the author. issues.owner = Owner issues.owner_helper = This user is the owner of this repository. issues.member = Member -issues.member_helper = This user is a member of the organization. +issues.member_helper = This user is a member of the organization owning this repository. issues.collaborator = Collaborator issues.collaborator_helper = This user has been invited to collaborate on the repository. issues.first_time_contributor = First-time contributor From 40335f1bcc743e423bbb7390f0e4e238131ba75f Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 23 Aug 2023 10:29:49 +0900 Subject: [PATCH 05/17] Update options/locale/locale_en-US.ini Co-authored-by: delvh --- options/locale/locale_en-US.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 4fc5bc258b92..6e9ef58af7ed 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1489,7 +1489,7 @@ issues.member_helper = This user is a member of the organization owning this rep issues.collaborator = Collaborator issues.collaborator_helper = This user has been invited to collaborate on the repository. issues.first_time_contributor = First-time contributor -issues.first_time_contributor_helper = This user is a first-time contributor to the repository. +issues.first_time_contributor_helper = This is the first contribution of this user to the repository. issues.contributor = Contributor issues.contributor_helper = This user has previously committed to the repository. issues.re_request_review=Re-request review From 04d89a0ae9df0c88a566e97876f11725e51cbc5f Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 23 Aug 2023 02:11:35 +0000 Subject: [PATCH 06/17] change the logic of detecting first time contributor --- modules/git/commit.go | 5 +---- modules/git/repo_commit.go | 17 +++-------------- routers/web/repo/issue.go | 28 +++++++++++++++++----------- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index 3d5b4f3ad4c1..c44882d88617 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -258,12 +258,10 @@ func (c *Commit) CommitsBeforeUntil(commitID string) ([]*Commit, error) { // SearchCommitsOptions specify the parameters for SearchCommits type SearchCommitsOptions struct { - CommitID SHA1 Keywords []string Authors, Committers []string After, Before string All bool - Limit int } // NewSearchCommitsOptions construct a SearchCommitsOption from a space-delimited search string @@ -299,8 +297,7 @@ func NewSearchCommitsOptions(searchString string, forAllRefs bool) SearchCommits // SearchCommits returns the commits match the keyword before current revision func (c *Commit) SearchCommits(opts SearchCommitsOptions) ([]*Commit, error) { - opts.CommitID = c.ID - return c.repo.SearchCommits(opts) + return c.repo.searchCommits(c.ID, opts) } // GetFilesChangedSinceCommit get all changed file names between pastCommit to current revision diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 6fab4c88c520..6fc306362911 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -109,7 +109,7 @@ func (repo *Repository) commitsByRange(id SHA1, page, pageSize int, not string) return repo.parsePrettyFormatLogToList(stdout) } -func (repo *Repository) SearchCommits(opts SearchCommitsOptions) ([]*Commit, error) { +func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) ([]*Commit, error) { // add common arguments to git command addCommonSearchArgs := func(c *Command) { // ignore case @@ -138,19 +138,8 @@ func (repo *Repository) SearchCommits(opts SearchCommitsOptions) ([]*Commit, err } } - // create new git log command - cmd := NewCommand(repo.Ctx, "log") - - // By default, limit 100 commits - limit := 100 - if opts.Limit > 0 { - limit = opts.Limit - } - cmd = cmd.AddOptionFormat("-%d", limit).AddArguments(prettyLogFormat) - - if !opts.CommitID.IsZero() { - cmd = cmd.AddDynamicArguments(opts.CommitID.String()) - } + // create new git log command with limit of 100 commits + cmd := NewCommand(repo.Ctx, "log", "-100", prettyLogFormat).AddDynamicArguments(id.String()) // pretend that all refs along with HEAD were listed on command line as // https://git-scm.com/docs/git-log#Documentation/git-log.txt---all diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index d56f1c9a7d90..0a9f681a44a7 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1229,7 +1229,7 @@ func NewIssuePost(ctx *context.Context) { } // roleDescriptor returns the Role Descriptor for a comment in/with the given repo, poster and issue -func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *user_model.User, issue *issues_model.Issue, gitRepo *git.Repository, hasOriginalAuthor bool) (issues_model.RoleDescriptor, error) { +func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *user_model.User, issue *issues_model.Issue, hasOriginalAuthor bool) (issues_model.RoleDescriptor, error) { if hasOriginalAuthor { return issues_model.RoleDescriptorNone, nil } @@ -1287,15 +1287,21 @@ func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *use return roleDescriptor, nil } - if commits, err := gitRepo.SearchCommits(git.SearchCommitsOptions{ - Authors: []string{poster.Name}, - All: true, - Limit: 2, - }); err != nil { + // If the poster is the contributor of the repo + searchOpt := &issue_indexer.SearchOptions{ + Paginator: &db.ListOptions{ + Page: 1, + PageSize: 2, + }, + RepoIDs: []int64{repo.ID}, + IsPull: util.OptionalBoolTrue, + PosterID: &poster.ID, + } + if _, total, err := issue_indexer.SearchIssues(ctx, searchOpt); err != nil { return issues_model.RoleDescriptorNone, err - } else if len(commits) == 1 { + } else if total == 1 { roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorFirstTimeContributor) - } else if len(commits) > 1 { + } else if total > 1 { roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorContributor) } @@ -1554,7 +1560,7 @@ func ViewIssue(ctx *context.Context) { // check if dependencies can be created across repositories ctx.Data["AllowCrossRepositoryDependencies"] = setting.Service.AllowCrossRepositoryDependencies - if issue.ShowRole, err = roleDescriptor(ctx, repo, issue.Poster, issue, ctx.Repo.GitRepo, issue.HasOriginalAuthor()); err != nil { + if issue.ShowRole, err = roleDescriptor(ctx, repo, issue.Poster, issue, issue.HasOriginalAuthor()); err != nil { ctx.ServerError("roleDescriptor", err) return } @@ -1593,7 +1599,7 @@ func ViewIssue(ctx *context.Context) { continue } - comment.ShowRole, err = roleDescriptor(ctx, repo, comment.Poster, issue, ctx.Repo.GitRepo, comment.HasOriginalAuthor()) + comment.ShowRole, err = roleDescriptor(ctx, repo, comment.Poster, issue, comment.HasOriginalAuthor()) if err != nil { ctx.ServerError("roleDescriptor", err) return @@ -1692,7 +1698,7 @@ func ViewIssue(ctx *context.Context) { continue } - c.ShowRole, err = roleDescriptor(ctx, repo, c.Poster, issue, ctx.Repo.GitRepo, c.HasOriginalAuthor()) + c.ShowRole, err = roleDescriptor(ctx, repo, c.Poster, issue, c.HasOriginalAuthor()) if err != nil { ctx.ServerError("roleDescriptor", err) return From 47376c876595ea05050cfcd324c740cd94c7689e Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 23 Aug 2023 04:03:27 +0000 Subject: [PATCH 07/17] rewrite stringToRoleDescriptor --- models/issues/comment.go | 66 ++++++++----------- routers/web/repo/issue.go | 41 ++++++------ .../repo/issue/view_content/show_role.tmpl | 24 ++----- 3 files changed, 54 insertions(+), 77 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index 141a28d8b11c..acee1c5a55b6 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -23,6 +23,7 @@ import ( "code.gitea.io/gitea/modules/references" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/translation" "code.gitea.io/gitea/modules/util" "xorm.io/builder" @@ -181,49 +182,40 @@ func (t CommentType) HasAttachmentSupport() bool { return false } +type Role string + // RoleDescriptor defines comment tag type -type RoleDescriptor int +type RoleDescriptor struct { + IsPoster util.OptionalBool + Role Role +} // Enumerate all the role tags. const ( - RoleDescriptorNone RoleDescriptor = iota - RoleDescriptorPoster - RoleDescriptorOwner - RoleDescriptorMember - RoleDescriptorCollaborator - RoleDescriptorFirstTimeContributor - RoleDescriptorContributor + RoleDescriptorOwner Role = "owner" + RoleDescriptorMember Role = "member" + RoleDescriptorCollaborator Role = "collaborator" + RoleDescriptorFirstTimeContributor Role = "first_time_contributor" + RoleDescriptorContributor Role = "contributor" ) -// WithRole enable a specific tag on the RoleDescriptor. -func (rd RoleDescriptor) WithRole(role RoleDescriptor) RoleDescriptor { - return rd | (1 << role) -} - -func stringToRoleDescriptor(role string) RoleDescriptor { - switch role { - case "Poster": - return RoleDescriptorPoster - case "Owner": - return RoleDescriptorOwner - case "Member": - return RoleDescriptorMember - case "Collaborator": - return RoleDescriptorCollaborator - case "First-time contributor": - return RoleDescriptorFirstTimeContributor - case "Contributor": - return RoleDescriptorContributor - default: - return RoleDescriptorNone - } -} - -// HasRole returns if a certain role is enabled on the RoleDescriptor. -func (rd RoleDescriptor) HasRole(role string) bool { - roleDescriptor := stringToRoleDescriptor(role) - bitValue := rd & (1 << roleDescriptor) - return (bitValue > 0) +// HasRole returns if a role is not none +func (r Role) HasRole() bool { + return r.String() != "" +} + +func (r Role) String() string { + return string(r) +} + +// LocaleString returns the locale string name of the Status +func (r Role) LocaleString(lang translation.Locale) string { + return lang.Tr("repo.issues." + r.String()) +} + +// LocaleHelper returns the locale string name of the Status +func (r Role) LocaleHelper(lang translation.Locale) string { + return lang.Tr("repo.issues." + r.String() + "_helper") } // Comment represents a comment in commit and issue page. diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 0a9f681a44a7..5ba90f400b01 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1230,60 +1230,59 @@ func NewIssuePost(ctx *context.Context) { // roleDescriptor returns the Role Descriptor for a comment in/with the given repo, poster and issue func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *user_model.User, issue *issues_model.Issue, hasOriginalAuthor bool) (issues_model.RoleDescriptor, error) { + roleDescriptor := issues_model.RoleDescriptor{} + if hasOriginalAuthor { - return issues_model.RoleDescriptorNone, nil + return roleDescriptor, nil } perm, err := access_model.GetUserRepoPermission(ctx, repo, poster) if err != nil { - return issues_model.RoleDescriptorNone, err + return roleDescriptor, err } - // By default the poster has no roles on the comment. - roleDescriptor := issues_model.RoleDescriptorNone - // If the poster is the actual poster of the issue, enable Poster role. if issue.IsPoster(poster.ID) { - roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorPoster) + roleDescriptor.IsPoster = util.OptionalBoolTrue } // Check if the poster is owner of the repo. if perm.IsOwner() { // If the poster isn't a admin, enable the owner role. if !poster.IsAdmin { - roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorOwner) + roleDescriptor.Role = issues_model.RoleDescriptorOwner return roleDescriptor, nil } // Otherwise check if poster is the real repo admin. ok, err := access_model.IsUserRealRepoAdmin(repo, poster) if err != nil { - return issues_model.RoleDescriptorNone, err + return roleDescriptor, err } if ok { - roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorOwner) + roleDescriptor.Role = issues_model.RoleDescriptorOwner return roleDescriptor, nil } } // If repo is organization, check Member role if err := repo.LoadOwner(ctx); err != nil { - return issues_model.RoleDescriptorNone, err + return roleDescriptor, err } if repo.Owner.IsOrganization() { if isMember, err := organization.IsOrganizationMember(ctx, repo.Owner.ID, poster.ID); err != nil { - return issues_model.RoleDescriptorNone, err + return roleDescriptor, err } else if isMember { - roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorMember) + roleDescriptor.Role = issues_model.RoleDescriptorMember return roleDescriptor, nil } } // If the poster is the collaborator of the repo if isCollaborator, err := repo_model.IsCollaborator(ctx, repo.ID, poster.ID); err != nil { - return issues_model.RoleDescriptorNone, err + return roleDescriptor, err } else if isCollaborator { - roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorCollaborator) + roleDescriptor.Role = issues_model.RoleDescriptorCollaborator return roleDescriptor, nil } @@ -1291,18 +1290,20 @@ func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *use searchOpt := &issue_indexer.SearchOptions{ Paginator: &db.ListOptions{ Page: 1, - PageSize: 2, + PageSize: 1, }, RepoIDs: []int64{repo.ID}, + IsClosed: util.OptionalBoolTrue, IsPull: util.OptionalBoolTrue, PosterID: &poster.ID, } if _, total, err := issue_indexer.SearchIssues(ctx, searchOpt); err != nil { - return issues_model.RoleDescriptorNone, err - } else if total == 1 { - roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorFirstTimeContributor) - } else if total > 1 { - roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorContributor) + return roleDescriptor, err + } else if total > 0 { + roleDescriptor.Role = issues_model.RoleDescriptorContributor + } else if total == 0 && issue.IsPull && !issue.IsClosed { + // only display first time contributor in the first opening pull request + roleDescriptor.Role = issues_model.RoleDescriptorFirstTimeContributor } return roleDescriptor, nil diff --git a/templates/repo/issue/view_content/show_role.tmpl b/templates/repo/issue/view_content/show_role.tmpl index b15e16e84015..b99c25a78b7e 100644 --- a/templates/repo/issue/view_content/show_role.tmpl +++ b/templates/repo/issue/view_content/show_role.tmpl @@ -1,27 +1,11 @@ -{{if and (.ShowRole.HasRole "Poster") (not .IgnorePoster)}} +{{if and .ShowRole.IsPoster.IsTrue (not .IgnorePoster)}}
{{ctx.Locale.Tr "repo.issues.author"}}
{{end}} -{{if (.ShowRole.HasRole "Owner")}} -
- {{ctx.Locale.Tr "repo.issues.owner"}} -
-{{else if (.ShowRole.HasRole "Member")}} -
- {{ctx.Locale.Tr "repo.issues.member"}} -
-{{else if (.ShowRole.HasRole "Collaborator")}} -
- {{ctx.Locale.Tr "repo.issues.collaborator"}} -
-{{else if and (.ShowRole.HasRole "First-time contributor") (.ShowRole.HasRole "Poster")}} -
- {{ctx.Locale.Tr "repo.issues.first_time_contributor"}} -
-{{else if (.ShowRole.HasRole "Contributor")}} -
- {{ctx.Locale.Tr "repo.issues.contributor"}} +{{if (.ShowRole.Role.HasRole)}} +
+ {{.ShowRole.Role.LocaleString ctx.Locale}}
{{end}} From 3b90d2b4ce97065a9e5edb0a2a85da950b25f385 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 23 Aug 2023 04:45:03 +0000 Subject: [PATCH 08/17] imrpove --- models/issues/comment.go | 6 +++--- options/locale/locale_en-US.ini | 20 +++++++++---------- routers/web/repo/issue.go | 2 +- .../repo/issue/view_content/show_role.tmpl | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index acee1c5a55b6..0afa3a401f40 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -186,7 +186,7 @@ type Role string // RoleDescriptor defines comment tag type type RoleDescriptor struct { - IsPoster util.OptionalBool + IsPoster bool Role Role } @@ -210,12 +210,12 @@ func (r Role) String() string { // LocaleString returns the locale string name of the Status func (r Role) LocaleString(lang translation.Locale) string { - return lang.Tr("repo.issues." + r.String()) + return lang.Tr("repo.issues.role." + r.String()) } // LocaleHelper returns the locale string name of the Status func (r Role) LocaleHelper(lang translation.Locale) string { - return lang.Tr("repo.issues." + r.String() + "_helper") + return lang.Tr("repo.issues.role." + r.String() + "_helper") } // Comment represents a comment in commit and issue page. diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 6e9ef58af7ed..be8b691eacf6 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1482,16 +1482,16 @@ issues.ref_reopened_from = `reopened this issue %[4]s {{ctx.Locale.Tr "repo.issues.author"}}
From cbf0c31588a66dcb24c32dbe02bd44bc5e484181 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 23 Aug 2023 13:04:03 +0800 Subject: [PATCH 09/17] Apply suggestions from code review --- models/issues/comment.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index 0afa3a401f40..f706e50df940 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -201,21 +201,17 @@ const ( // HasRole returns if a role is not none func (r Role) HasRole() bool { - return r.String() != "" -} - -func (r Role) String() string { - return string(r) + return r.Role != "" } // LocaleString returns the locale string name of the Status func (r Role) LocaleString(lang translation.Locale) string { - return lang.Tr("repo.issues.role." + r.String()) + return lang.Tr("repo.issues.role." + r.Role) } // LocaleHelper returns the locale string name of the Status func (r Role) LocaleHelper(lang translation.Locale) string { - return lang.Tr("repo.issues.role." + r.String() + "_helper") + return lang.Tr("repo.issues.role." + r.Role + "_helper") } // Comment represents a comment in commit and issue page. From 994efc561ec935a1acec3ebc7e3c1ef3f869c651 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 23 Aug 2023 13:05:56 +0800 Subject: [PATCH 10/17] Update routers/web/repo/issue.go --- routers/web/repo/issue.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 941132b853bf..48cb5df95662 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1242,9 +1242,7 @@ func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *use } // If the poster is the actual poster of the issue, enable Poster role. - if issue.IsPoster(poster.ID) { - roleDescriptor.IsPoster = true - } + roleDescriptor.IsPoster = issue.IsPoster(poster.ID) // Check if the poster is owner of the repo. if perm.IsOwner() { From e624603eae0eed02e898b06016d8065b2b6e1d46 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 23 Aug 2023 13:36:35 +0800 Subject: [PATCH 11/17] Update models/issues/comment.go --- models/issues/comment.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index f706e50df940..9b22f82ac9fd 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -200,8 +200,8 @@ const ( ) // HasRole returns if a role is not none -func (r Role) HasRole() bool { - return r.Role != "" +func (rd RoleDescriptor) HasRole() bool { + return rd.Role != "" } // LocaleString returns the locale string name of the Status From 67cbdad2f08a9b08e511458f76b151541318396d Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 23 Aug 2023 13:42:06 +0800 Subject: [PATCH 12/17] fix --- models/issues/comment.go | 9 ++------- templates/repo/issue/view_content/show_role.tmpl | 3 +-- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index 9b22f82ac9fd..c12487e8636f 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -199,19 +199,14 @@ const ( RoleDescriptorContributor Role = "contributor" ) -// HasRole returns if a role is not none -func (rd RoleDescriptor) HasRole() bool { - return rd.Role != "" -} - // LocaleString returns the locale string name of the Status func (r Role) LocaleString(lang translation.Locale) string { - return lang.Tr("repo.issues.role." + r.Role) + return lang.Tr("repo.issues.role." + string(r)) } // LocaleHelper returns the locale string name of the Status func (r Role) LocaleHelper(lang translation.Locale) string { - return lang.Tr("repo.issues.role." + r.Role + "_helper") + return lang.Tr("repo.issues.role." + string(r) + "_helper") } // Comment represents a comment in commit and issue page. diff --git a/templates/repo/issue/view_content/show_role.tmpl b/templates/repo/issue/view_content/show_role.tmpl index 10eacee5b68a..19f030181a74 100644 --- a/templates/repo/issue/view_content/show_role.tmpl +++ b/templates/repo/issue/view_content/show_role.tmpl @@ -3,8 +3,7 @@ {{ctx.Locale.Tr "repo.issues.author"}}
{{end}} - -{{if (.ShowRole.Role.HasRole)}} +{{if .ShowRole.Role}}
{{.ShowRole.Role.LocaleString ctx.Locale}}
From 7542db01f13f9eb592c3aa1b45aa1fe8ccea1a84 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 23 Aug 2023 13:52:02 +0800 Subject: [PATCH 13/17] rename var --- models/issues/comment.go | 23 ++++++++++--------- routers/web/repo/issue.go | 16 ++++++------- .../repo/issue/view_content/show_role.tmpl | 6 ++--- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index c12487e8636f..b50763195a85 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -182,30 +182,31 @@ func (t CommentType) HasAttachmentSupport() bool { return false } -type Role string +// RoleInRepo presents the user's participation in the repo +type RoleInRepo string -// RoleDescriptor defines comment tag type +// RoleDescriptor defines comment "role" tags type RoleDescriptor struct { - IsPoster bool - Role Role + IsPoster bool + RoleInRepo RoleInRepo } // Enumerate all the role tags. const ( - RoleDescriptorOwner Role = "owner" - RoleDescriptorMember Role = "member" - RoleDescriptorCollaborator Role = "collaborator" - RoleDescriptorFirstTimeContributor Role = "first_time_contributor" - RoleDescriptorContributor Role = "contributor" + RoleRepoOwner RoleInRepo = "owner" + RoleRepoMember RoleInRepo = "member" + RoleRepoCollaborator RoleInRepo = "collaborator" + RoleRepoFirstTimeContributor RoleInRepo = "first_time_contributor" + RoleRepoContributor RoleInRepo = "contributor" ) // LocaleString returns the locale string name of the Status -func (r Role) LocaleString(lang translation.Locale) string { +func (r RoleInRepo) LocaleString(lang translation.Locale) string { return lang.Tr("repo.issues.role." + string(r)) } // LocaleHelper returns the locale string name of the Status -func (r Role) LocaleHelper(lang translation.Locale) string { +func (r RoleInRepo) LocaleHelper(lang translation.Locale) string { return lang.Tr("repo.issues.role." + string(r) + "_helper") } diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 48cb5df95662..325f6dec7c34 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1228,7 +1228,7 @@ func NewIssuePost(ctx *context.Context) { } } -// roleDescriptor returns the Role Descriptor for a comment in/with the given repo, poster and issue +// roleDescriptor returns the role descriptor for a comment in/with the given repo, poster and issue func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *user_model.User, issue *issues_model.Issue, hasOriginalAuthor bool) (issues_model.RoleDescriptor, error) { roleDescriptor := issues_model.RoleDescriptor{} @@ -1246,9 +1246,9 @@ func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *use // Check if the poster is owner of the repo. if perm.IsOwner() { - // If the poster isn't a admin, enable the owner role. + // If the poster isn't an admin, enable the owner role. if !poster.IsAdmin { - roleDescriptor.Role = issues_model.RoleDescriptorOwner + roleDescriptor.RoleInRepo = issues_model.RoleRepoOwner return roleDescriptor, nil } @@ -1258,7 +1258,7 @@ func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *use return roleDescriptor, err } if ok { - roleDescriptor.Role = issues_model.RoleDescriptorOwner + roleDescriptor.RoleInRepo = issues_model.RoleRepoOwner return roleDescriptor, nil } } @@ -1271,7 +1271,7 @@ func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *use if isMember, err := organization.IsOrganizationMember(ctx, repo.Owner.ID, poster.ID); err != nil { return roleDescriptor, err } else if isMember { - roleDescriptor.Role = issues_model.RoleDescriptorMember + roleDescriptor.RoleInRepo = issues_model.RoleRepoMember return roleDescriptor, nil } } @@ -1280,7 +1280,7 @@ func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *use if isCollaborator, err := repo_model.IsCollaborator(ctx, repo.ID, poster.ID); err != nil { return roleDescriptor, err } else if isCollaborator { - roleDescriptor.Role = issues_model.RoleDescriptorCollaborator + roleDescriptor.RoleInRepo = issues_model.RoleRepoCollaborator return roleDescriptor, nil } @@ -1298,10 +1298,10 @@ func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *use if _, total, err := issue_indexer.SearchIssues(ctx, searchOpt); err != nil { return roleDescriptor, err } else if total > 0 { - roleDescriptor.Role = issues_model.RoleDescriptorContributor + roleDescriptor.RoleInRepo = issues_model.RoleRepoContributor } else if total == 0 && issue.IsPull && !issue.IsClosed { // only display first time contributor in the first opening pull request - roleDescriptor.Role = issues_model.RoleDescriptorFirstTimeContributor + roleDescriptor.RoleInRepo = issues_model.RoleRepoFirstTimeContributor } return roleDescriptor, nil diff --git a/templates/repo/issue/view_content/show_role.tmpl b/templates/repo/issue/view_content/show_role.tmpl index 19f030181a74..40c8b67fa974 100644 --- a/templates/repo/issue/view_content/show_role.tmpl +++ b/templates/repo/issue/view_content/show_role.tmpl @@ -3,8 +3,8 @@ {{ctx.Locale.Tr "repo.issues.author"}}
{{end}} -{{if .ShowRole.Role}} -
- {{.ShowRole.Role.LocaleString ctx.Locale}} +{{if .ShowRole.RoleInRepo}} +
+ {{.ShowRole.RoleInRepo.LocaleString ctx.Locale}}
{{end}} From 46b8b6bccb10bb16d12babbe40c03c0abf217082 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 23 Aug 2023 13:54:58 +0800 Subject: [PATCH 14/17] fix comment --- models/issues/comment.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index b50763195a85..d378a9cdd30b 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -200,12 +200,12 @@ const ( RoleRepoContributor RoleInRepo = "contributor" ) -// LocaleString returns the locale string name of the Status +// LocaleString returns the locale string name of the role func (r RoleInRepo) LocaleString(lang translation.Locale) string { return lang.Tr("repo.issues.role." + string(r)) } -// LocaleHelper returns the locale string name of the Status +// LocaleHelper returns the locale string name of the role func (r RoleInRepo) LocaleHelper(lang translation.Locale) string { return lang.Tr("repo.issues.role." + string(r) + "_helper") } From 05c45630f6a94da6c10be26073a44868fba53310 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 23 Aug 2023 13:56:58 +0800 Subject: [PATCH 15/17] fix comment --- models/issues/comment.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index d378a9cdd30b..17e579b455d0 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -205,7 +205,7 @@ func (r RoleInRepo) LocaleString(lang translation.Locale) string { return lang.Tr("repo.issues.role." + string(r)) } -// LocaleHelper returns the locale string name of the role +// LocaleHelper returns the locale tooltip of the role func (r RoleInRepo) LocaleHelper(lang translation.Locale) string { return lang.Tr("repo.issues.role." + string(r) + "_helper") } From 503701c91ccaf136a08d3c074be28533ffa13b85 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 23 Aug 2023 19:04:57 +0800 Subject: [PATCH 16/17] search database to detect whether the user is first time contributors --- models/issues/pull_list.go | 11 +++++++++++ routers/web/repo/issue.go | 16 +++------------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/models/issues/pull_list.go b/models/issues/pull_list.go index 3b2416900b5d..5c5cd6eb29b3 100644 --- a/models/issues/pull_list.go +++ b/models/issues/pull_list.go @@ -199,3 +199,14 @@ func (prs PullRequestList) GetIssueIDs() []int64 { } return issueIDs } + +// CountMergedPullRequestInRepo return the count the user merged into the repository via pull request +func CountMergedPullRequestInRepo(ctx context.Context, repoID, posterID int64) (int64, error) { + return db.GetEngine(ctx). + Join("INNER", "pull", "pull.issue_id = issue.id"). + Where("repo_id=?", repoID). + And("poster_id=?", posterID). + And("is_pull=?", true). + And("pull.has_merged=?", true). + Count(new(Issue)) +} diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 325f6dec7c34..e72d0952cbe3 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1284,22 +1284,12 @@ func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *use return roleDescriptor, nil } - // If the poster is the contributor of the repo - searchOpt := &issue_indexer.SearchOptions{ - Paginator: &db.ListOptions{ - Page: 1, - PageSize: 1, - }, - RepoIDs: []int64{repo.ID}, - IsClosed: util.OptionalBoolTrue, - IsPull: util.OptionalBoolTrue, - PosterID: &poster.ID, - } - if _, total, err := issue_indexer.SearchIssues(ctx, searchOpt); err != nil { + total, err := issues_model.CountMergedPullRequestInRepo(ctx, repo.ID, poster.ID) + if err != nil { return roleDescriptor, err } else if total > 0 { roleDescriptor.RoleInRepo = issues_model.RoleRepoContributor - } else if total == 0 && issue.IsPull && !issue.IsClosed { + } else if total == 0 { // only display first time contributor in the first opening pull request roleDescriptor.RoleInRepo = issues_model.RoleRepoFirstTimeContributor } From 6a31b74cc20ff055f7cdc6182bc693549dba9b07 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 23 Aug 2023 19:21:46 +0800 Subject: [PATCH 17/17] use HasMergedPullRequestInRepo --- models/issues/pull_list.go | 12 +++++++----- routers/web/repo/issue.go | 6 +++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/models/issues/pull_list.go b/models/issues/pull_list.go index 5c5cd6eb29b3..c4506ef15085 100644 --- a/models/issues/pull_list.go +++ b/models/issues/pull_list.go @@ -200,13 +200,15 @@ func (prs PullRequestList) GetIssueIDs() []int64 { return issueIDs } -// CountMergedPullRequestInRepo return the count the user merged into the repository via pull request -func CountMergedPullRequestInRepo(ctx context.Context, repoID, posterID int64) (int64, error) { +// HasMergedPullRequestInRepo returns whether the user(poster) has merged pull-request in the repo +func HasMergedPullRequestInRepo(ctx context.Context, repoID, posterID int64) (bool, error) { return db.GetEngine(ctx). - Join("INNER", "pull", "pull.issue_id = issue.id"). + Join("INNER", "pull_request", "pull_request.issue_id = issue.id"). Where("repo_id=?", repoID). And("poster_id=?", posterID). And("is_pull=?", true). - And("pull.has_merged=?", true). - Count(new(Issue)) + And("pull_request.has_merged=?", true). + Select("issue.id"). + Limit(1). + Get(new(Issue)) } diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index e72d0952cbe3..9a2add14524c 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1284,12 +1284,12 @@ func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *use return roleDescriptor, nil } - total, err := issues_model.CountMergedPullRequestInRepo(ctx, repo.ID, poster.ID) + hasMergedPR, err := issues_model.HasMergedPullRequestInRepo(ctx, repo.ID, poster.ID) if err != nil { return roleDescriptor, err - } else if total > 0 { + } else if hasMergedPR { roleDescriptor.RoleInRepo = issues_model.RoleRepoContributor - } else if total == 0 { + } else { // only display first time contributor in the first opening pull request roleDescriptor.RoleInRepo = issues_model.RoleRepoFirstTimeContributor }