Skip to content

Commit bc65c9a

Browse files
committed
improve
1 parent bd8a253 commit bc65c9a

File tree

6 files changed

+101
-29
lines changed

6 files changed

+101
-29
lines changed

models/issues/comment.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,11 @@ type RoleDescriptor int
188188
const (
189189
RoleDescriptorNone RoleDescriptor = iota
190190
RoleDescriptorPoster
191-
RoleDescriptorWriter
192191
RoleDescriptorOwner
192+
RoleDescriptorMember
193+
RoleDescriptorCollaborator
194+
RoleDescriptorFirstTimeContributor
195+
RoleDescriptorContributor
193196
)
194197

195198
// WithRole enable a specific tag on the RoleDescriptor.
@@ -201,10 +204,16 @@ func stringToRoleDescriptor(role string) RoleDescriptor {
201204
switch role {
202205
case "Poster":
203206
return RoleDescriptorPoster
204-
case "Writer":
205-
return RoleDescriptorWriter
206207
case "Owner":
207208
return RoleDescriptorOwner
209+
case "Member":
210+
return RoleDescriptorMember
211+
case "Collaborator":
212+
return RoleDescriptorCollaborator
213+
case "First-time contributor":
214+
return RoleDescriptorFirstTimeContributor
215+
case "Contributor":
216+
return RoleDescriptorContributor
208217
default:
209218
return RoleDescriptorNone
210219
}

modules/git/commit.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,12 @@ func (c *Commit) CommitsBeforeUntil(commitID string) ([]*Commit, error) {
258258

259259
// SearchCommitsOptions specify the parameters for SearchCommits
260260
type SearchCommitsOptions struct {
261+
CommitID SHA1
261262
Keywords []string
262263
Authors, Committers []string
263264
After, Before string
264265
All bool
266+
Limit int
265267
}
266268

267269
// NewSearchCommitsOptions construct a SearchCommitsOption from a space-delimited search string
@@ -297,7 +299,8 @@ func NewSearchCommitsOptions(searchString string, forAllRefs bool) SearchCommits
297299

298300
// SearchCommits returns the commits match the keyword before current revision
299301
func (c *Commit) SearchCommits(opts SearchCommitsOptions) ([]*Commit, error) {
300-
return c.repo.searchCommits(c.ID, opts)
302+
opts.CommitID = c.ID
303+
return c.repo.SearchCommits(opts)
301304
}
302305

303306
// GetFilesChangedSinceCommit get all changed file names between pastCommit to current revision

modules/git/repo_commit.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (repo *Repository) commitsByRange(id SHA1, page, pageSize int, not string)
109109
return repo.parsePrettyFormatLogToList(stdout)
110110
}
111111

112-
func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) ([]*Commit, error) {
112+
func (repo *Repository) SearchCommits(opts SearchCommitsOptions) ([]*Commit, error) {
113113
// add common arguments to git command
114114
addCommonSearchArgs := func(c *Command) {
115115
// ignore case
@@ -138,8 +138,19 @@ func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) ([]*Co
138138
}
139139
}
140140

141-
// create new git log command with limit of 100 commits
142-
cmd := NewCommand(repo.Ctx, "log", "-100", prettyLogFormat).AddDynamicArguments(id.String())
141+
// create new git log command
142+
cmd := NewCommand(repo.Ctx, "log")
143+
144+
// By default, limit 100 commits
145+
limit := 100
146+
if opts.Limit > 0 {
147+
limit = opts.Limit
148+
}
149+
cmd = cmd.AddOptionFormat("-%d", limit).AddArguments(prettyLogFormat)
150+
151+
if !opts.CommitID.IsZero() {
152+
cmd = cmd.AddDynamicArguments(opts.CommitID.String())
153+
}
143154

144155
// pretend that all refs along with HEAD were listed on command line as <commis>
145156
// https://git-scm.com/docs/git-log#Documentation/git-log.txt---all

options/locale/locale_en-US.ini

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,9 +1480,18 @@ issues.ref_reopening_from = `<a href="%[3]s">referenced a pull request %[4]s tha
14801480
issues.ref_closed_from = `<a href="%[3]s">closed this issue %[4]s</a> <a id="%[1]s" href="#%[1]s">%[2]s</a>`
14811481
issues.ref_reopened_from = `<a href="%[3]s">reopened this issue %[4]s</a> <a id="%[1]s" href="#%[1]s">%[2]s</a>`
14821482
issues.ref_from = `from %[1]s`
1483-
issues.poster = Poster
1484-
issues.collaborator = Collaborator
1483+
issues.author = Author
1484+
issues.author_helper = This user is the author.
14851485
issues.owner = Owner
1486+
issues.owner_helper = This user is the owner of this repository.
1487+
issues.member = Member
1488+
issues.member_helper = This user is a member of the organization.
1489+
issues.collaborator = Collaborator
1490+
issues.collaborator_helper = This user has been invited to collaborate on the repository.
1491+
issues.first_time_contributor = First-time contributor
1492+
issues.first_time_contributor_helper = This user is a first-time contributor to the repository.
1493+
issues.contributor = Contributor
1494+
issues.contributor_helper = This user has previously committed to the repository.
14861495
issues.re_request_review=Re-request review
14871496
issues.is_stale = There have been changes to this PR since this review
14881497
issues.remove_request_review=Remove review request

routers/web/repo/issue.go

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ func NewIssuePost(ctx *context.Context) {
12291229
}
12301230

12311231
// roleDescriptor returns the Role Descriptor for a comment in/with the given repo, poster and issue
1232-
func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *user_model.User, issue *issues_model.Issue, hasOriginalAuthor bool) (issues_model.RoleDescriptor, error) {
1232+
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) {
12331233
if hasOriginalAuthor {
12341234
return issues_model.RoleDescriptorNone, nil
12351235
}
@@ -1242,33 +1242,61 @@ func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *use
12421242
// By default the poster has no roles on the comment.
12431243
roleDescriptor := issues_model.RoleDescriptorNone
12441244

1245+
// If the poster is the actual poster of the issue, enable Poster role.
1246+
if issue.IsPoster(poster.ID) {
1247+
roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorPoster)
1248+
}
1249+
12451250
// Check if the poster is owner of the repo.
12461251
if perm.IsOwner() {
12471252
// If the poster isn't a admin, enable the owner role.
12481253
if !poster.IsAdmin {
12491254
roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorOwner)
1255+
return roleDescriptor, nil
12501256
} else {
1251-
12521257
// Otherwise check if poster is the real repo admin.
12531258
ok, err := access_model.IsUserRealRepoAdmin(repo, poster)
12541259
if err != nil {
12551260
return issues_model.RoleDescriptorNone, err
12561261
}
12571262
if ok {
12581263
roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorOwner)
1264+
return roleDescriptor, nil
12591265
}
12601266
}
12611267
}
12621268

1263-
// Is the poster can write issues or pulls to the repo, enable the Writer role.
1264-
// Only enable this if the poster doesn't have the owner role already.
1265-
if !roleDescriptor.HasRole("Owner") && perm.CanWriteIssuesOrPulls(issue.IsPull) {
1266-
roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorWriter)
1269+
// If repo is organization, check Member role
1270+
if err := repo.LoadOwner(ctx); err != nil {
1271+
return issues_model.RoleDescriptorNone, err
1272+
}
1273+
if repo.Owner.IsOrganization() {
1274+
if isMember, err := organization.IsOrganizationMember(ctx, repo.Owner.ID, poster.ID); err != nil {
1275+
return issues_model.RoleDescriptorNone, err
1276+
} else if isMember {
1277+
roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorMember)
1278+
return roleDescriptor, nil
1279+
}
12671280
}
12681281

1269-
// If the poster is the actual poster of the issue, enable Poster role.
1270-
if issue.IsPoster(poster.ID) {
1271-
roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorPoster)
1282+
// If the poster is the collaborator of the repo
1283+
if isCollaborator, err := repo_model.IsCollaborator(ctx, repo.ID, poster.ID); err != err {
1284+
return issues_model.RoleDescriptorNone, err
1285+
} else if isCollaborator {
1286+
roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorCollaborator)
1287+
return roleDescriptor, nil
1288+
}
1289+
1290+
if commits, err := gitRepo.SearchCommits(git.SearchCommitsOptions{
1291+
Authors: []string{poster.Name},
1292+
All: true,
1293+
Limit: 2,
1294+
}); err != nil {
1295+
return issues_model.RoleDescriptorNone, err
1296+
} else if len(commits) == 1 {
1297+
roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorContributor)
1298+
} else if len(commits) > 1 {
1299+
roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorFirstTimeContributor)
12721300
}
12731301

12741302
return roleDescriptor, nil
@@ -1526,7 +1554,7 @@ func ViewIssue(ctx *context.Context) {
15261554
// check if dependencies can be created across repositories
15271555
ctx.Data["AllowCrossRepositoryDependencies"] = setting.Service.AllowCrossRepositoryDependencies
15281556

1529-
if issue.ShowRole, err = roleDescriptor(ctx, repo, issue.Poster, issue, issue.HasOriginalAuthor()); err != nil {
1557+
if issue.ShowRole, err = roleDescriptor(ctx, repo, issue.Poster, issue, ctx.Repo.GitRepo, issue.HasOriginalAuthor()); err != nil {
15301558
ctx.ServerError("roleDescriptor", err)
15311559
return
15321560
}
@@ -1565,7 +1593,7 @@ func ViewIssue(ctx *context.Context) {
15651593
continue
15661594
}
15671595

1568-
comment.ShowRole, err = roleDescriptor(ctx, repo, comment.Poster, issue, comment.HasOriginalAuthor())
1596+
comment.ShowRole, err = roleDescriptor(ctx, repo, comment.Poster, issue, ctx.Repo.GitRepo, comment.HasOriginalAuthor())
15691597
if err != nil {
15701598
ctx.ServerError("roleDescriptor", err)
15711599
return
@@ -1664,7 +1692,7 @@ func ViewIssue(ctx *context.Context) {
16641692
continue
16651693
}
16661694

1667-
c.ShowRole, err = roleDescriptor(ctx, repo, c.Poster, issue, c.HasOriginalAuthor())
1695+
c.ShowRole, err = roleDescriptor(ctx, repo, c.Poster, issue, ctx.Repo.GitRepo, c.HasOriginalAuthor())
16681696
if err != nil {
16691697
ctx.ServerError("roleDescriptor", err)
16701698
return
Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
{{if and (.ShowRole.HasRole "Poster") (not .IgnorePoster)}}
2-
<div class="ui basic label role-label">
3-
{{ctx.Locale.Tr "repo.issues.poster"}}
4-
</div>
5-
{{end}}
6-
{{if (.ShowRole.HasRole "Writer")}}
7-
<div class="ui basic label role-label">
8-
{{ctx.Locale.Tr "repo.issues.collaborator"}}
2+
<div class="ui basic label role-label" data-tooltip-content="{{ctx.Locale.Tr "repo.issues.author_helper"}}">
3+
{{ctx.Locale.Tr "repo.issues.author"}}
94
</div>
105
{{end}}
6+
117
{{if (.ShowRole.HasRole "Owner")}}
12-
<div class="ui basic label role-label">
8+
<div class="ui basic label role-label" data-tooltip-content="{{ctx.Locale.Tr "repo.issues.owner_helper"}}">
139
{{ctx.Locale.Tr "repo.issues.owner"}}
1410
</div>
11+
{{else if (.ShowRole.HasRole "Member")}}
12+
<div class="ui basic label role-label" data-tooltip-content="{{ctx.Locale.Tr "repo.issues.member_helper"}}">
13+
{{ctx.Locale.Tr "repo.issues.member"}}
14+
</div>
15+
{{else if (.ShowRole.HasRole "Collaborator")}}
16+
<div class="ui basic label role-label" data-tooltip-content="{{ctx.Locale.Tr "repo.issues.collaborator_helper"}}">
17+
{{ctx.Locale.Tr "repo.issues.collaborator"}}
18+
</div>
19+
{{else if and (.ShowRole.HasRole "First-time contributor") (.ShowRole.HasRole "Poster")}}
20+
<div class="ui basic label role-label" data-tooltip-content="{{ctx.Locale.Tr "repo.issues.first_time_contributor_helper"}}">
21+
{{ctx.Locale.Tr "repo.issues.first_time_contributor"}}
22+
</div>
23+
{{else if (.ShowRole.HasRole "Contributor")}}
24+
<div class="ui basic label role-label" data-tooltip-content="{{ctx.Locale.Tr "repo.issues.contributor_helper"}}">
25+
{{ctx.Locale.Tr "repo.issues.contributor"}}
26+
</div>
1527
{{end}}

0 commit comments

Comments
 (0)