Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor StringsToInt64s #29967

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions models/issues/pull_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -23,7 +22,7 @@ type PullRequestsOptions struct {
db.ListOptions
State string
SortType string
Labels []string
Labels []int64
MilestoneID int64
}

Expand All @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions models/issues/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func TestPullRequestsNewest(t *testing.T) {
},
State: "open",
SortType: "newest",
Labels: []string{},
})
assert.NoError(t, err)
assert.EqualValues(t, 3, count)
Expand Down Expand Up @@ -113,7 +112,6 @@ func TestPullRequestsOldest(t *testing.T) {
},
State: "open",
SortType: "oldest",
Labels: []string{},
})
assert.NoError(t, err)
assert.EqualValues(t, 3, count)
Expand Down
13 changes: 8 additions & 5 deletions modules/base/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions modules/base/tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ loading = Loading…
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.
go_back = Go Back
invalid_data = Invalid data: %v

never = Never
unknown = Unknown
Expand Down
9 changes: 7 additions & 2 deletions routers/api/v1/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
7 changes: 2 additions & 5 deletions routers/web/user/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions routers/web/user/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
1 change: 1 addition & 0 deletions templates/repo/issue/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<div role="main" aria-label="{{.Title}}" class="page-content repository issue-list">
{{template "repo/header" .}}
<div class="ui container">
{{template "base/alert" .}}

{{if .PinnedIssues}}
<div id="issue-pins" {{if .IsRepoAdmin}}data-is-repo-admin{{end}}>
Expand Down
1 change: 1 addition & 0 deletions templates/repo/issue/milestone_issues.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<div role="main" aria-label="{{.Title}}" class="page-content repository milestone-issue-list">
{{template "repo/header" .}}
<div class="ui container">
{{template "base/alert" .}}
<div class="gt-df">
<h1 class="gt-mb-3">{{.Milestone.Name}}</h1>
{{if not .Repository.IsArchived}}
Expand Down
1 change: 1 addition & 0 deletions templates/user/dashboard/issues.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<div role="main" aria-label="{{.Title}}" class="page-content dashboard issues">
{{template "user/dashboard/navbar" .}}
<div class="ui container">
{{template "base/alert" .}}
<div class="flex-container">
<div class="flex-container-nav">
<div class="ui secondary vertical filter menu tw-bg-transparent">
Expand Down