From cdb4d1a8db096d60dba04728924dab85def45b19 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 21 Mar 2024 23:07:35 +0800 Subject: [PATCH] Refactor StringsToInt64s (#29967) And close #27176 --- models/issues/pull_list.go | 9 +++------ models/issues/pull_test.go | 2 -- modules/base/tool.go | 13 ++++++++----- modules/base/tool_test.go | 7 ++++--- options/locale/locale_en-US.ini | 1 + routers/api/v1/repo/pull.go | 9 +++++++-- routers/web/repo/issue.go | 3 +-- routers/web/user/home.go | 7 ++----- routers/web/user/notification.go | 3 +-- templates/repo/issue/list.tmpl | 1 + templates/repo/issue/milestone_issues.tmpl | 1 + templates/user/dashboard/issues.tmpl | 1 + 12 files changed, 30 insertions(+), 27 deletions(-) diff --git a/models/issues/pull_list.go b/models/issues/pull_list.go index 2ee69cd32..de3eceed3 100644 --- a/models/issues/pull_list.go +++ b/models/issues/pull_list.go @@ -11,7 +11,6 @@ import ( access_model "code.gitea.io/gitea/models/perm/access" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/util" @@ -23,7 +22,7 @@ type PullRequestsOptions struct { db.ListOptions State string SortType string - Labels []string + Labels []int64 MilestoneID int64 } @@ -36,11 +35,9 @@ func listPullRequestStatement(ctx context.Context, baseRepoID int64, opts *PullR sess.And("issue.is_closed=?", opts.State == "closed") } - if labelIDs, err := base.StringsToInt64s(opts.Labels); err != nil { - return nil, err - } else if len(labelIDs) > 0 { + if len(opts.Labels) > 0 { sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id"). - In("issue_label.label_id", labelIDs) + In("issue_label.label_id", opts.Labels) } if opts.MilestoneID > 0 { diff --git a/models/issues/pull_test.go b/models/issues/pull_test.go index 3a30b2f3d..675c90527 100644 --- a/models/issues/pull_test.go +++ b/models/issues/pull_test.go @@ -66,7 +66,6 @@ func TestPullRequestsNewest(t *testing.T) { }, State: "open", SortType: "newest", - Labels: []string{}, }) assert.NoError(t, err) assert.EqualValues(t, 3, count) @@ -113,7 +112,6 @@ func TestPullRequestsOldest(t *testing.T) { }, State: "open", SortType: "oldest", - Labels: []string{}, }) assert.NoError(t, err) assert.EqualValues(t, 3, count) diff --git a/modules/base/tool.go b/modules/base/tool.go index 168a2220b..40785e74e 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -150,13 +150,16 @@ func TruncateString(str string, limit int) string { // StringsToInt64s converts a slice of string to a slice of int64. func StringsToInt64s(strs []string) ([]int64, error) { - ints := make([]int64, len(strs)) - for i := range strs { - n, err := strconv.ParseInt(strs[i], 10, 64) + if strs == nil { + return nil, nil + } + ints := make([]int64, 0, len(strs)) + for _, s := range strs { + n, err := strconv.ParseInt(s, 10, 64) if err != nil { - return ints, err + return nil, err } - ints[i] = n + ints = append(ints, n) } return ints, nil } diff --git a/modules/base/tool_test.go b/modules/base/tool_test.go index d28deb593..f21b89c74 100644 --- a/modules/base/tool_test.go +++ b/modules/base/tool_test.go @@ -138,12 +138,13 @@ func TestStringsToInt64s(t *testing.T) { assert.NoError(t, err) assert.Equal(t, expected, result) } + testSuccess(nil, nil) testSuccess([]string{}, []int64{}) testSuccess([]string{"-1234"}, []int64{-1234}) - testSuccess([]string{"1", "4", "16", "64", "256"}, - []int64{1, 4, 16, 64, 256}) + testSuccess([]string{"1", "4", "16", "64", "256"}, []int64{1, 4, 16, 64, 256}) - _, err := StringsToInt64s([]string{"-1", "a", "$"}) + ints, err := StringsToInt64s([]string{"-1", "a"}) + assert.Len(t, ints, 0) assert.Error(t, err) } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 6622a25ef..49a564a89 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -114,6 +114,7 @@ loading = Loading… error = Error error404 = The page you are trying to reach either does not exist or you are not authorized to view it. go_back = Go Back +invalid_data = Invalid data: %v never = Never unknown = Unknown diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index fc4765607..e43366ff1 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -21,6 +21,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" @@ -96,13 +97,17 @@ func ListPullRequests(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" + labelIDs, err := base.StringsToInt64s(ctx.FormStrings("labels")) + if err != nil { + ctx.Error(http.StatusInternalServerError, "PullRequests", err) + return + } listOptions := utils.GetListOptions(ctx) - prs, maxResults, err := issues_model.PullRequests(ctx, ctx.Repo.Repository.ID, &issues_model.PullRequestsOptions{ ListOptions: listOptions, State: ctx.FormTrim("state"), SortType: ctx.FormTrim("sort"), - Labels: ctx.FormStrings("labels"), + Labels: labelIDs, MilestoneID: ctx.FormInt64("milestone"), }) if err != nil { diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index fb03ed9d9..930a71d35 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -187,8 +187,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption opt if len(selectLabels) > 0 { labelIDs, err = base.StringsToInt64s(strings.Split(selectLabels, ",")) if err != nil { - ctx.ServerError("StringsToInt64s", err) - return + ctx.Flash.Error(ctx.Tr("invalid_data", selectLabels), true) } } diff --git a/routers/web/user/home.go b/routers/web/user/home.go index 465de500a..ff6c2a6c3 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -529,17 +529,14 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) { // Get IDs for labels (a filter option for issues/pulls). // Required for IssuesOptions. - var labelIDs []int64 selectedLabels := ctx.FormString("labels") if len(selectedLabels) > 0 && selectedLabels != "0" { var err error - labelIDs, err = base.StringsToInt64s(strings.Split(selectedLabels, ",")) + opts.LabelIDs, err = base.StringsToInt64s(strings.Split(selectedLabels, ",")) if err != nil { - ctx.ServerError("StringsToInt64s", err) - return + ctx.Flash.Error(ctx.Tr("invalid_data", selectedLabels), true) } } - opts.LabelIDs = labelIDs // ------------------------------ // Get issues as defined by opts. diff --git a/routers/web/user/notification.go b/routers/web/user/notification.go index 28f9846d6..ae0132e6e 100644 --- a/routers/web/user/notification.go +++ b/routers/web/user/notification.go @@ -268,8 +268,7 @@ func NotificationSubscriptions(ctx *context.Context) { var err error labelIDs, err = base.StringsToInt64s(strings.Split(selectedLabels, ",")) if err != nil { - ctx.ServerError("StringsToInt64s", err) - return + ctx.Flash.Error(ctx.Tr("invalid_data", selectedLabels), true) } } diff --git a/templates/repo/issue/list.tmpl b/templates/repo/issue/list.tmpl index 62c1d00f0..45bddefa4 100644 --- a/templates/repo/issue/list.tmpl +++ b/templates/repo/issue/list.tmpl @@ -2,6 +2,7 @@
{{template "repo/header" .}}
+ {{template "base/alert" .}} {{if .PinnedIssues}}
diff --git a/templates/repo/issue/milestone_issues.tmpl b/templates/repo/issue/milestone_issues.tmpl index 35a8a7768..507c3ce37 100644 --- a/templates/repo/issue/milestone_issues.tmpl +++ b/templates/repo/issue/milestone_issues.tmpl @@ -2,6 +2,7 @@
{{template "repo/header" .}}
+ {{template "base/alert" .}}

{{.Milestone.Name}}

{{if not .Repository.IsArchived}} diff --git a/templates/user/dashboard/issues.tmpl b/templates/user/dashboard/issues.tmpl index 88afcf58e..5080821dd 100644 --- a/templates/user/dashboard/issues.tmpl +++ b/templates/user/dashboard/issues.tmpl @@ -2,6 +2,7 @@
{{template "user/dashboard/navbar" .}}
+ {{template "base/alert" .}}