Skip to content

Commit 47376c8

Browse files
committed
rewrite stringToRoleDescriptor
1 parent 04d89a0 commit 47376c8

File tree

3 files changed

+54
-77
lines changed

3 files changed

+54
-77
lines changed

models/issues/comment.go

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"code.gitea.io/gitea/modules/references"
2424
"code.gitea.io/gitea/modules/structs"
2525
"code.gitea.io/gitea/modules/timeutil"
26+
"code.gitea.io/gitea/modules/translation"
2627
"code.gitea.io/gitea/modules/util"
2728

2829
"xorm.io/builder"
@@ -181,49 +182,40 @@ func (t CommentType) HasAttachmentSupport() bool {
181182
return false
182183
}
183184

185+
type Role string
186+
184187
// RoleDescriptor defines comment tag type
185-
type RoleDescriptor int
188+
type RoleDescriptor struct {
189+
IsPoster util.OptionalBool
190+
Role Role
191+
}
186192

187193
// Enumerate all the role tags.
188194
const (
189-
RoleDescriptorNone RoleDescriptor = iota
190-
RoleDescriptorPoster
191-
RoleDescriptorOwner
192-
RoleDescriptorMember
193-
RoleDescriptorCollaborator
194-
RoleDescriptorFirstTimeContributor
195-
RoleDescriptorContributor
195+
RoleDescriptorOwner Role = "owner"
196+
RoleDescriptorMember Role = "member"
197+
RoleDescriptorCollaborator Role = "collaborator"
198+
RoleDescriptorFirstTimeContributor Role = "first_time_contributor"
199+
RoleDescriptorContributor Role = "contributor"
196200
)
197201

198-
// WithRole enable a specific tag on the RoleDescriptor.
199-
func (rd RoleDescriptor) WithRole(role RoleDescriptor) RoleDescriptor {
200-
return rd | (1 << role)
201-
}
202-
203-
func stringToRoleDescriptor(role string) RoleDescriptor {
204-
switch role {
205-
case "Poster":
206-
return RoleDescriptorPoster
207-
case "Owner":
208-
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
217-
default:
218-
return RoleDescriptorNone
219-
}
220-
}
221-
222-
// HasRole returns if a certain role is enabled on the RoleDescriptor.
223-
func (rd RoleDescriptor) HasRole(role string) bool {
224-
roleDescriptor := stringToRoleDescriptor(role)
225-
bitValue := rd & (1 << roleDescriptor)
226-
return (bitValue > 0)
202+
// HasRole returns if a role is not none
203+
func (r Role) HasRole() bool {
204+
return r.String() != ""
205+
}
206+
207+
func (r Role) String() string {
208+
return string(r)
209+
}
210+
211+
// LocaleString returns the locale string name of the Status
212+
func (r Role) LocaleString(lang translation.Locale) string {
213+
return lang.Tr("repo.issues." + r.String())
214+
}
215+
216+
// LocaleHelper returns the locale string name of the Status
217+
func (r Role) LocaleHelper(lang translation.Locale) string {
218+
return lang.Tr("repo.issues." + r.String() + "_helper")
227219
}
228220

229221
// Comment represents a comment in commit and issue page.

routers/web/repo/issue.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,79 +1230,80 @@ func NewIssuePost(ctx *context.Context) {
12301230

12311231
// roleDescriptor returns the Role Descriptor for a comment in/with the given repo, poster and issue
12321232
func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *user_model.User, issue *issues_model.Issue, hasOriginalAuthor bool) (issues_model.RoleDescriptor, error) {
1233+
roleDescriptor := issues_model.RoleDescriptor{}
1234+
12331235
if hasOriginalAuthor {
1234-
return issues_model.RoleDescriptorNone, nil
1236+
return roleDescriptor, nil
12351237
}
12361238

12371239
perm, err := access_model.GetUserRepoPermission(ctx, repo, poster)
12381240
if err != nil {
1239-
return issues_model.RoleDescriptorNone, err
1241+
return roleDescriptor, err
12401242
}
12411243

1242-
// By default the poster has no roles on the comment.
1243-
roleDescriptor := issues_model.RoleDescriptorNone
1244-
12451244
// If the poster is the actual poster of the issue, enable Poster role.
12461245
if issue.IsPoster(poster.ID) {
1247-
roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorPoster)
1246+
roleDescriptor.IsPoster = util.OptionalBoolTrue
12481247
}
12491248

12501249
// Check if the poster is owner of the repo.
12511250
if perm.IsOwner() {
12521251
// If the poster isn't a admin, enable the owner role.
12531252
if !poster.IsAdmin {
1254-
roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorOwner)
1253+
roleDescriptor.Role = issues_model.RoleDescriptorOwner
12551254
return roleDescriptor, nil
12561255
}
12571256

12581257
// Otherwise check if poster is the real repo admin.
12591258
ok, err := access_model.IsUserRealRepoAdmin(repo, poster)
12601259
if err != nil {
1261-
return issues_model.RoleDescriptorNone, err
1260+
return roleDescriptor, err
12621261
}
12631262
if ok {
1264-
roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorOwner)
1263+
roleDescriptor.Role = issues_model.RoleDescriptorOwner
12651264
return roleDescriptor, nil
12661265
}
12671266
}
12681267

12691268
// If repo is organization, check Member role
12701269
if err := repo.LoadOwner(ctx); err != nil {
1271-
return issues_model.RoleDescriptorNone, err
1270+
return roleDescriptor, err
12721271
}
12731272
if repo.Owner.IsOrganization() {
12741273
if isMember, err := organization.IsOrganizationMember(ctx, repo.Owner.ID, poster.ID); err != nil {
1275-
return issues_model.RoleDescriptorNone, err
1274+
return roleDescriptor, err
12761275
} else if isMember {
1277-
roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorMember)
1276+
roleDescriptor.Role = issues_model.RoleDescriptorMember
12781277
return roleDescriptor, nil
12791278
}
12801279
}
12811280

12821281
// If the poster is the collaborator of the repo
12831282
if isCollaborator, err := repo_model.IsCollaborator(ctx, repo.ID, poster.ID); err != nil {
1284-
return issues_model.RoleDescriptorNone, err
1283+
return roleDescriptor, err
12851284
} else if isCollaborator {
1286-
roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorCollaborator)
1285+
roleDescriptor.Role = issues_model.RoleDescriptorCollaborator
12871286
return roleDescriptor, nil
12881287
}
12891288

12901289
// If the poster is the contributor of the repo
12911290
searchOpt := &issue_indexer.SearchOptions{
12921291
Paginator: &db.ListOptions{
12931292
Page: 1,
1294-
PageSize: 2,
1293+
PageSize: 1,
12951294
},
12961295
RepoIDs: []int64{repo.ID},
1296+
IsClosed: util.OptionalBoolTrue,
12971297
IsPull: util.OptionalBoolTrue,
12981298
PosterID: &poster.ID,
12991299
}
13001300
if _, total, err := issue_indexer.SearchIssues(ctx, searchOpt); err != nil {
1301-
return issues_model.RoleDescriptorNone, err
1302-
} else if total == 1 {
1303-
roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorFirstTimeContributor)
1304-
} else if total > 1 {
1305-
roleDescriptor = roleDescriptor.WithRole(issues_model.RoleDescriptorContributor)
1301+
return roleDescriptor, err
1302+
} else if total > 0 {
1303+
roleDescriptor.Role = issues_model.RoleDescriptorContributor
1304+
} else if total == 0 && issue.IsPull && !issue.IsClosed {
1305+
// only display first time contributor in the first opening pull request
1306+
roleDescriptor.Role = issues_model.RoleDescriptorFirstTimeContributor
13061307
}
13071308

13081309
return roleDescriptor, nil
Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
1-
{{if and (.ShowRole.HasRole "Poster") (not .IgnorePoster)}}
1+
{{if and .ShowRole.IsPoster.IsTrue (not .IgnorePoster)}}
22
<div class="ui basic label role-label" data-tooltip-content="{{ctx.Locale.Tr "repo.issues.author_helper"}}">
33
{{ctx.Locale.Tr "repo.issues.author"}}
44
</div>
55
{{end}}
66

7-
{{if (.ShowRole.HasRole "Owner")}}
8-
<div class="ui basic label role-label" data-tooltip-content="{{ctx.Locale.Tr "repo.issues.owner_helper"}}">
9-
{{ctx.Locale.Tr "repo.issues.owner"}}
10-
</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"}}
7+
{{if (.ShowRole.Role.HasRole)}}
8+
<div class="ui basic label role-label" data-tooltip-content="{{.ShowRole.Role.LocaleHelper ctx.Locale}}">
9+
{{.ShowRole.Role.LocaleString ctx.Locale}}
2610
</div>
2711
{{end}}

0 commit comments

Comments
 (0)