Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-25 11:16:11 +01:00

Refactor StringsToInt64s (#29967)

And close #27176

(cherry picked from commit cdb4d1a8db096d60dba04728924dab85def45b19)
This commit is contained in:
wxiaoguang 2024-03-21 23:07:35 +08:00 committed by Earl Warren
parent f8060bb5d9
commit e5920b4a62
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
12 changed files with 30 additions and 27 deletions

View file

@ -11,7 +11,6 @@ import (
access_model "code.gitea.io/gitea/models/perm/access" access_model "code.gitea.io/gitea/models/perm/access"
"code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user" 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/log"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
@ -23,7 +22,7 @@ type PullRequestsOptions struct {
db.ListOptions db.ListOptions
State string State string
SortType string SortType string
Labels []string Labels []int64
MilestoneID int64 MilestoneID int64
} }
@ -36,11 +35,9 @@ func listPullRequestStatement(ctx context.Context, baseRepoID int64, opts *PullR
sess.And("issue.is_closed=?", opts.State == "closed") sess.And("issue.is_closed=?", opts.State == "closed")
} }
if labelIDs, err := base.StringsToInt64s(opts.Labels); err != nil { if len(opts.Labels) > 0 {
return nil, err
} else if len(labelIDs) > 0 {
sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id"). 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 { if opts.MilestoneID > 0 {

View file

@ -67,7 +67,6 @@ func TestPullRequestsNewest(t *testing.T) {
}, },
State: "open", State: "open",
SortType: "newest", SortType: "newest",
Labels: []string{},
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, 3, count) assert.EqualValues(t, 3, count)
@ -114,7 +113,6 @@ func TestPullRequestsOldest(t *testing.T) {
}, },
State: "open", State: "open",
SortType: "oldest", SortType: "oldest",
Labels: []string{},
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, 3, count) assert.EqualValues(t, 3, count)

View file

@ -150,13 +150,16 @@ func TruncateString(str string, limit int) string {
// StringsToInt64s converts a slice of string to a slice of int64. // StringsToInt64s converts a slice of string to a slice of int64.
func StringsToInt64s(strs []string) ([]int64, error) { func StringsToInt64s(strs []string) ([]int64, error) {
ints := make([]int64, len(strs)) if strs == nil {
for i := range strs { return nil, nil
n, err := strconv.ParseInt(strs[i], 10, 64)
if err != nil {
return ints, err
} }
ints[i] = n ints := make([]int64, 0, len(strs))
for _, s := range strs {
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return nil, err
}
ints = append(ints, n)
} }
return ints, nil return ints, nil
} }

View file

@ -138,12 +138,13 @@ func TestStringsToInt64s(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expected, result) assert.Equal(t, expected, result)
} }
testSuccess(nil, nil)
testSuccess([]string{}, []int64{}) testSuccess([]string{}, []int64{})
testSuccess([]string{"-1234"}, []int64{-1234}) testSuccess([]string{"-1234"}, []int64{-1234})
testSuccess([]string{"1", "4", "16", "64", "256"}, testSuccess([]string{"1", "4", "16", "64", "256"}, []int64{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) assert.Error(t, err)
} }

View file

@ -114,6 +114,7 @@ loading = Loading…
error = Error error = Error
error404 = The page you are trying to reach either <strong>does not exist</strong> or <strong>you are not authorized</strong> to view it. error404 = The page you are trying to reach either <strong>does not exist</strong> or <strong>you are not authorized</strong> to view it.
go_back = Go Back go_back = Go Back
invalid_data = Invalid data: %v
never = Never never = Never
unknown = Unknown unknown = Unknown

View file

@ -21,6 +21,7 @@ import (
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user" 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/git"
"code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -96,13 +97,17 @@ func ListPullRequests(ctx *context.APIContext) {
// "404": // "404":
// "$ref": "#/responses/notFound" // "$ref": "#/responses/notFound"
labelIDs, err := base.StringsToInt64s(ctx.FormStrings("labels"))
if err != nil {
ctx.Error(http.StatusInternalServerError, "PullRequests", err)
return
}
listOptions := utils.GetListOptions(ctx) listOptions := utils.GetListOptions(ctx)
prs, maxResults, err := issues_model.PullRequests(ctx, ctx.Repo.Repository.ID, &issues_model.PullRequestsOptions{ prs, maxResults, err := issues_model.PullRequests(ctx, ctx.Repo.Repository.ID, &issues_model.PullRequestsOptions{
ListOptions: listOptions, ListOptions: listOptions,
State: ctx.FormTrim("state"), State: ctx.FormTrim("state"),
SortType: ctx.FormTrim("sort"), SortType: ctx.FormTrim("sort"),
Labels: ctx.FormStrings("labels"), Labels: labelIDs,
MilestoneID: ctx.FormInt64("milestone"), MilestoneID: ctx.FormInt64("milestone"),
}) })
if err != nil { if err != nil {

View file

@ -192,8 +192,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption opt
if len(selectLabels) > 0 { if len(selectLabels) > 0 {
labelIDs, err = base.StringsToInt64s(strings.Split(selectLabels, ",")) labelIDs, err = base.StringsToInt64s(strings.Split(selectLabels, ","))
if err != nil { if err != nil {
ctx.ServerError("StringsToInt64s", err) ctx.Flash.Error(ctx.Tr("invalid_data", selectLabels), true)
return
} }
} }

View file

@ -529,17 +529,14 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
// Get IDs for labels (a filter option for issues/pulls). // Get IDs for labels (a filter option for issues/pulls).
// Required for IssuesOptions. // Required for IssuesOptions.
var labelIDs []int64
selectedLabels := ctx.FormString("labels") selectedLabels := ctx.FormString("labels")
if len(selectedLabels) > 0 && selectedLabels != "0" { if len(selectedLabels) > 0 && selectedLabels != "0" {
var err error var err error
labelIDs, err = base.StringsToInt64s(strings.Split(selectedLabels, ",")) opts.LabelIDs, err = base.StringsToInt64s(strings.Split(selectedLabels, ","))
if err != nil { if err != nil {
ctx.ServerError("StringsToInt64s", err) ctx.Flash.Error(ctx.Tr("invalid_data", selectedLabels), true)
return
} }
} }
opts.LabelIDs = labelIDs
// ------------------------------ // ------------------------------
// Get issues as defined by opts. // Get issues as defined by opts.

View file

@ -268,8 +268,7 @@ func NotificationSubscriptions(ctx *context.Context) {
var err error var err error
labelIDs, err = base.StringsToInt64s(strings.Split(selectedLabels, ",")) labelIDs, err = base.StringsToInt64s(strings.Split(selectedLabels, ","))
if err != nil { if err != nil {
ctx.ServerError("StringsToInt64s", err) ctx.Flash.Error(ctx.Tr("invalid_data", selectedLabels), true)
return
} }
} }

View file

@ -2,6 +2,7 @@
<div role="main" aria-label="{{.Title}}" class="page-content repository issue-list"> <div role="main" aria-label="{{.Title}}" class="page-content repository issue-list">
{{template "repo/header" .}} {{template "repo/header" .}}
<div class="ui container"> <div class="ui container">
{{template "base/alert" .}}
{{if .PinnedIssues}} {{if .PinnedIssues}}
<div id="issue-pins" {{if .IsRepoAdmin}}data-is-repo-admin{{end}}> <div id="issue-pins" {{if .IsRepoAdmin}}data-is-repo-admin{{end}}>

View file

@ -2,6 +2,7 @@
<div role="main" aria-label="{{.Title}}" class="page-content repository milestone-issue-list"> <div role="main" aria-label="{{.Title}}" class="page-content repository milestone-issue-list">
{{template "repo/header" .}} {{template "repo/header" .}}
<div class="ui container"> <div class="ui container">
{{template "base/alert" .}}
<div class="gt-df"> <div class="gt-df">
<h1 class="gt-mb-3">{{.Milestone.Name}}</h1> <h1 class="gt-mb-3">{{.Milestone.Name}}</h1>
{{if not .Repository.IsArchived}} {{if not .Repository.IsArchived}}

View file

@ -2,6 +2,7 @@
<div role="main" aria-label="{{.Title}}" class="page-content dashboard issues"> <div role="main" aria-label="{{.Title}}" class="page-content dashboard issues">
{{template "user/dashboard/navbar" .}} {{template "user/dashboard/navbar" .}}
<div class="ui container"> <div class="ui container">
{{template "base/alert" .}}
<div class="flex-container"> <div class="flex-container">
<div class="flex-container-nav"> <div class="flex-container-nav">
<div class="ui secondary vertical filter menu tw-bg-transparent"> <div class="ui secondary vertical filter menu tw-bg-transparent">