Skip to content

Commit cdb4d1a

Browse files
authored
Refactor StringsToInt64s (#29967)
And close #27176
1 parent 8297958 commit cdb4d1a

File tree

12 files changed

+30
-27
lines changed

12 files changed

+30
-27
lines changed

models/issues/pull_list.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
access_model "code.gitea.io/gitea/models/perm/access"
1212
"code.gitea.io/gitea/models/unit"
1313
user_model "code.gitea.io/gitea/models/user"
14-
"code.gitea.io/gitea/modules/base"
1514
"code.gitea.io/gitea/modules/log"
1615
"code.gitea.io/gitea/modules/util"
1716

@@ -23,7 +22,7 @@ type PullRequestsOptions struct {
2322
db.ListOptions
2423
State string
2524
SortType string
26-
Labels []string
25+
Labels []int64
2726
MilestoneID int64
2827
}
2928

@@ -36,11 +35,9 @@ func listPullRequestStatement(ctx context.Context, baseRepoID int64, opts *PullR
3635
sess.And("issue.is_closed=?", opts.State == "closed")
3736
}
3837

39-
if labelIDs, err := base.StringsToInt64s(opts.Labels); err != nil {
40-
return nil, err
41-
} else if len(labelIDs) > 0 {
38+
if len(opts.Labels) > 0 {
4239
sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
43-
In("issue_label.label_id", labelIDs)
40+
In("issue_label.label_id", opts.Labels)
4441
}
4542

4643
if opts.MilestoneID > 0 {

models/issues/pull_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ func TestPullRequestsNewest(t *testing.T) {
6666
},
6767
State: "open",
6868
SortType: "newest",
69-
Labels: []string{},
7069
})
7170
assert.NoError(t, err)
7271
assert.EqualValues(t, 3, count)
@@ -113,7 +112,6 @@ func TestPullRequestsOldest(t *testing.T) {
113112
},
114113
State: "open",
115114
SortType: "oldest",
116-
Labels: []string{},
117115
})
118116
assert.NoError(t, err)
119117
assert.EqualValues(t, 3, count)

modules/base/tool.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,16 @@ func TruncateString(str string, limit int) string {
150150

151151
// StringsToInt64s converts a slice of string to a slice of int64.
152152
func StringsToInt64s(strs []string) ([]int64, error) {
153-
ints := make([]int64, len(strs))
154-
for i := range strs {
155-
n, err := strconv.ParseInt(strs[i], 10, 64)
153+
if strs == nil {
154+
return nil, nil
155+
}
156+
ints := make([]int64, 0, len(strs))
157+
for _, s := range strs {
158+
n, err := strconv.ParseInt(s, 10, 64)
156159
if err != nil {
157-
return ints, err
160+
return nil, err
158161
}
159-
ints[i] = n
162+
ints = append(ints, n)
160163
}
161164
return ints, nil
162165
}

modules/base/tool_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,13 @@ func TestStringsToInt64s(t *testing.T) {
138138
assert.NoError(t, err)
139139
assert.Equal(t, expected, result)
140140
}
141+
testSuccess(nil, nil)
141142
testSuccess([]string{}, []int64{})
142143
testSuccess([]string{"-1234"}, []int64{-1234})
143-
testSuccess([]string{"1", "4", "16", "64", "256"},
144-
[]int64{1, 4, 16, 64, 256})
144+
testSuccess([]string{"1", "4", "16", "64", "256"}, []int64{1, 4, 16, 64, 256})
145145

146-
_, err := StringsToInt64s([]string{"-1", "a", "$"})
146+
ints, err := StringsToInt64s([]string{"-1", "a"})
147+
assert.Len(t, ints, 0)
147148
assert.Error(t, err)
148149
}
149150

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ loading = Loading…
114114
error = Error
115115
error404 = The page you are trying to reach either <strong>does not exist</strong> or <strong>you are not authorized</strong> to view it.
116116
go_back = Go Back
117+
invalid_data = Invalid data: %v
117118

118119
never = Never
119120
unknown = Unknown

routers/api/v1/repo/pull.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
repo_model "code.gitea.io/gitea/models/repo"
2222
"code.gitea.io/gitea/models/unit"
2323
user_model "code.gitea.io/gitea/models/user"
24+
"code.gitea.io/gitea/modules/base"
2425
"code.gitea.io/gitea/modules/git"
2526
"code.gitea.io/gitea/modules/gitrepo"
2627
"code.gitea.io/gitea/modules/log"
@@ -96,13 +97,17 @@ func ListPullRequests(ctx *context.APIContext) {
9697
// "404":
9798
// "$ref": "#/responses/notFound"
9899

100+
labelIDs, err := base.StringsToInt64s(ctx.FormStrings("labels"))
101+
if err != nil {
102+
ctx.Error(http.StatusInternalServerError, "PullRequests", err)
103+
return
104+
}
99105
listOptions := utils.GetListOptions(ctx)
100-
101106
prs, maxResults, err := issues_model.PullRequests(ctx, ctx.Repo.Repository.ID, &issues_model.PullRequestsOptions{
102107
ListOptions: listOptions,
103108
State: ctx.FormTrim("state"),
104109
SortType: ctx.FormTrim("sort"),
105-
Labels: ctx.FormStrings("labels"),
110+
Labels: labelIDs,
106111
MilestoneID: ctx.FormInt64("milestone"),
107112
})
108113
if err != nil {

routers/web/repo/issue.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption opt
187187
if len(selectLabels) > 0 {
188188
labelIDs, err = base.StringsToInt64s(strings.Split(selectLabels, ","))
189189
if err != nil {
190-
ctx.ServerError("StringsToInt64s", err)
191-
return
190+
ctx.Flash.Error(ctx.Tr("invalid_data", selectLabels), true)
192191
}
193192
}
194193

routers/web/user/home.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -529,17 +529,14 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
529529

530530
// Get IDs for labels (a filter option for issues/pulls).
531531
// Required for IssuesOptions.
532-
var labelIDs []int64
533532
selectedLabels := ctx.FormString("labels")
534533
if len(selectedLabels) > 0 && selectedLabels != "0" {
535534
var err error
536-
labelIDs, err = base.StringsToInt64s(strings.Split(selectedLabels, ","))
535+
opts.LabelIDs, err = base.StringsToInt64s(strings.Split(selectedLabels, ","))
537536
if err != nil {
538-
ctx.ServerError("StringsToInt64s", err)
539-
return
537+
ctx.Flash.Error(ctx.Tr("invalid_data", selectedLabels), true)
540538
}
541539
}
542-
opts.LabelIDs = labelIDs
543540

544541
// ------------------------------
545542
// Get issues as defined by opts.

routers/web/user/notification.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ func NotificationSubscriptions(ctx *context.Context) {
268268
var err error
269269
labelIDs, err = base.StringsToInt64s(strings.Split(selectedLabels, ","))
270270
if err != nil {
271-
ctx.ServerError("StringsToInt64s", err)
272-
return
271+
ctx.Flash.Error(ctx.Tr("invalid_data", selectedLabels), true)
273272
}
274273
}
275274

templates/repo/issue/list.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<div role="main" aria-label="{{.Title}}" class="page-content repository issue-list">
33
{{template "repo/header" .}}
44
<div class="ui container">
5+
{{template "base/alert" .}}
56

67
{{if .PinnedIssues}}
78
<div id="issue-pins" {{if .IsRepoAdmin}}data-is-repo-admin{{end}}>

templates/repo/issue/milestone_issues.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<div role="main" aria-label="{{.Title}}" class="page-content repository milestone-issue-list">
33
{{template "repo/header" .}}
44
<div class="ui container">
5+
{{template "base/alert" .}}
56
<div class="gt-df">
67
<h1 class="gt-mb-3">{{.Milestone.Name}}</h1>
78
{{if not .Repository.IsArchived}}

templates/user/dashboard/issues.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<div role="main" aria-label="{{.Title}}" class="page-content dashboard issues">
33
{{template "user/dashboard/navbar" .}}
44
<div class="ui container">
5+
{{template "base/alert" .}}
56
<div class="flex-container">
67
<div class="flex-container-nav">
78
<div class="ui secondary vertical filter menu tw-bg-transparent">

0 commit comments

Comments
 (0)