From b9e8cb94d8ee9aa063297b9dfb5ec6ed23c63751 Mon Sep 17 00:00:00 2001 From: wass3r <1301201+wass3r@users.noreply.github.com> Date: Tue, 18 Jun 2024 14:11:05 -0500 Subject: [PATCH] refactor: use builtin min/max funcs --- api/build/list_org.go | 2 +- api/build/list_repo.go | 2 +- api/deployment/list.go | 2 +- api/hook/list.go | 2 +- api/log/list_build.go | 2 +- api/pipeline/list.go | 2 +- api/repo/list.go | 2 +- api/repo/list_org.go | 2 +- api/repo/update.go | 26 ++++---------------------- api/schedule/list.go | 2 +- api/secret/list.go | 2 +- api/service/list.go | 2 +- api/step/list.go | 2 +- api/user/list.go | 2 +- util/util.go | 24 ------------------------ 15 files changed, 17 insertions(+), 59 deletions(-) diff --git a/api/build/list_org.go b/api/build/list_org.go index ff516c39c..e2b336ef7 100644 --- a/api/build/list_org.go +++ b/api/build/list_org.go @@ -191,7 +191,7 @@ func ListBuildsForOrg(c *gin.Context) { } // ensure per_page isn't above or below allowed values - perPage = util.MaxInt(1, util.MinInt(100, perPage)) + perPage = max(1, min(100, perPage)) // See if the user is an org admin to bypass individual permission checks perm, err := scm.FromContext(c).OrgAccess(ctx, u, o) diff --git a/api/build/list_repo.go b/api/build/list_repo.go index f631e68ef..0a491676a 100644 --- a/api/build/list_repo.go +++ b/api/build/list_repo.go @@ -225,7 +225,7 @@ func ListBuildsForRepo(c *gin.Context) { } // ensure per_page isn't above or below allowed values - perPage = util.MaxInt(1, util.MinInt(100, perPage)) + perPage = max(1, min(100, perPage)) // capture before query parameter if present, default to now before, err := strconv.ParseInt(c.DefaultQuery("before", strconv.FormatInt(time.Now().UTC().Unix(), 10)), 10, 64) diff --git a/api/deployment/list.go b/api/deployment/list.go index b07e3ba0a..a2cc5b63c 100644 --- a/api/deployment/list.go +++ b/api/deployment/list.go @@ -117,7 +117,7 @@ func ListDeployments(c *gin.Context) { } // ensure per_page isn't above or below allowed values - perPage = util.MaxInt(1, util.MinInt(100, perPage)) + perPage = max(1, min(100, perPage)) // send API call to capture the total number of deployments for the repo t, err := database.FromContext(c).CountDeploymentsForRepo(c, r) diff --git a/api/hook/list.go b/api/hook/list.go index 3f1b47851..61714c8f4 100644 --- a/api/hook/list.go +++ b/api/hook/list.go @@ -119,7 +119,7 @@ func ListHooks(c *gin.Context) { } // ensure per_page isn't above or below allowed values - perPage = util.MaxInt(1, util.MinInt(100, perPage)) + perPage = max(1, min(100, perPage)) // send API call to capture the list of steps for the build h, t, err := database.FromContext(c).ListHooksForRepo(ctx, r, page, perPage) diff --git a/api/log/list_build.go b/api/log/list_build.go index a33fd2c32..bebe8e73f 100644 --- a/api/log/list_build.go +++ b/api/log/list_build.go @@ -120,7 +120,7 @@ func ListLogsForBuild(c *gin.Context) { } // ensure per_page isn't above or below allowed values - perPage = util.MaxInt(1, util.MinInt(100, perPage)) + perPage = max(1, min(100, perPage)) // send API call to capture the list of logs for the build l, t, err := database.FromContext(c).ListLogsForBuild(ctx, b, page, perPage) diff --git a/api/pipeline/list.go b/api/pipeline/list.go index c2fd7f0b6..0060422d2 100644 --- a/api/pipeline/list.go +++ b/api/pipeline/list.go @@ -123,7 +123,7 @@ func ListPipelines(c *gin.Context) { // ensure per_page isn't above or below allowed values // //nolint:gomnd // ignore magic number - perPage = util.MaxInt(1, util.MinInt(100, perPage)) + perPage = max(1, min(100, perPage)) p, t, err := database.FromContext(c).ListPipelinesForRepo(ctx, r, page, perPage) if err != nil { diff --git a/api/repo/list.go b/api/repo/list.go index 306214605..94601594d 100644 --- a/api/repo/list.go +++ b/api/repo/list.go @@ -99,7 +99,7 @@ func ListRepos(c *gin.Context) { } // ensure per_page isn't above or below allowed values - perPage = util.MaxInt(1, util.MinInt(100, perPage)) + perPage = max(1, min(100, perPage)) // capture the sort_by query parameter if present sortBy := util.QueryParameter(c, "sort_by", "name") diff --git a/api/repo/list_org.go b/api/repo/list_org.go index 2466efce1..8023fc59d 100644 --- a/api/repo/list_org.go +++ b/api/repo/list_org.go @@ -122,7 +122,7 @@ func ListReposForOrg(c *gin.Context) { } // ensure per_page isn't above or below allowed values - perPage = util.MaxInt(1, util.MinInt(100, perPage)) + perPage = max(1, min(100, perPage)) // capture the sort_by query parameter if present sortBy := util.QueryParameter(c, "sort_by", "name") diff --git a/api/repo/update.go b/api/repo/update.go index 4626dd72b..536f793e6 100644 --- a/api/repo/update.go +++ b/api/repo/update.go @@ -115,32 +115,14 @@ func UpdateRepo(c *gin.Context) { // update build limit if set if input.GetBuildLimit() > 0 { // allow build limit between 1 - value configured by server - r.SetBuildLimit( - int64( - util.MaxInt( - constants.BuildLimitMin, - util.MinInt( - int(input.GetBuildLimit()), - int(maxBuildLimit), - ), // clamp max - ), // clamp min - ), - ) + limit := max(constants.BuildLimitMin, min(input.GetBuildLimit(), maxBuildLimit)) + r.SetBuildLimit(limit) } if input.GetTimeout() > 0 { // update build timeout if set - r.SetTimeout( - int64( - util.MaxInt( - constants.BuildTimeoutMin, - util.MinInt( - int(input.GetTimeout()), - constants.BuildTimeoutMax, - ), // clamp max - ), // clamp min - ), - ) + limit := max(constants.BuildTimeoutMin, min(input.GetTimeout(), constants.BuildTimeoutMax)) + r.SetTimeout(limit) } if input.GetCounter() > 0 { diff --git a/api/schedule/list.go b/api/schedule/list.go index a42569ba0..3909b27f8 100644 --- a/api/schedule/list.go +++ b/api/schedule/list.go @@ -113,7 +113,7 @@ func ListSchedules(c *gin.Context) { } // ensure per_page isn't above or below allowed values - perPage = util.MaxInt(1, util.MinInt(100, perPage)) + perPage = max(1, min(100, perPage)) // send API call to capture the list of schedules for the repo s, t, err := database.FromContext(c).ListSchedulesForRepo(ctx, r, page, perPage) diff --git a/api/secret/list.go b/api/secret/list.go index a6699ef34..4f216fe16 100644 --- a/api/secret/list.go +++ b/api/secret/list.go @@ -177,7 +177,7 @@ func ListSecrets(c *gin.Context) { } // ensure per_page isn't above or below allowed values - perPage = util.MaxInt(1, util.MinInt(100, perPage)) + perPage = max(1, min(100, perPage)) // send API call to capture the list of secrets s, err := secret.FromContext(c, e).List(ctx, t, o, n, page, perPage, teams) diff --git a/api/service/list.go b/api/service/list.go index 2862a28d4..daa4f15b5 100644 --- a/api/service/list.go +++ b/api/service/list.go @@ -128,7 +128,7 @@ func ListServices(c *gin.Context) { } // ensure per_page isn't above or below allowed values - perPage = util.MaxInt(1, util.MinInt(100, perPage)) + perPage = max(1, min(100, perPage)) // send API call to capture the list of services for the build s, t, err := database.FromContext(c).ListServicesForBuild(ctx, b, map[string]interface{}{}, page, perPage) diff --git a/api/step/list.go b/api/step/list.go index 67617b2b6..6ccc238c3 100644 --- a/api/step/list.go +++ b/api/step/list.go @@ -128,7 +128,7 @@ func ListSteps(c *gin.Context) { } // ensure per_page isn't above or below allowed values - perPage = util.MaxInt(1, util.MinInt(100, perPage)) + perPage = max(1, min(100, perPage)) // send API call to capture the list of steps for the build s, t, err := database.FromContext(c).ListStepsForBuild(ctx, b, map[string]interface{}{}, page, perPage) diff --git a/api/user/list.go b/api/user/list.go index d4f5ad0ff..19e64523b 100644 --- a/api/user/list.go +++ b/api/user/list.go @@ -98,7 +98,7 @@ func ListUsers(c *gin.Context) { } // ensure per_page isn't above or below allowed values - perPage = util.MaxInt(1, util.MinInt(100, perPage)) + perPage = max(1, min(100, perPage)) // send API call to capture the list of users users, t, err := database.FromContext(c).ListLiteUsers(ctx, page, perPage) diff --git a/util/util.go b/util/util.go index b4a1a29ae..864fad7b3 100644 --- a/util/util.go +++ b/util/util.go @@ -31,30 +31,6 @@ func HandleError(c context.Context, status int, err error) { } } -// MaxInt is a helper function to clamp the integer which -// prevents it from being higher then the provided value. -// -// Currently, Go only supports float64 via math. ( max | min ). -func MaxInt(a, b int) int { - if a > b { - return a - } - - return b -} - -// MinInt is a helper function to clamp the integer which -// prevents it from being lower then the provided value. -// -// Currently, Go only supports float64 via math. ( max | min ). -func MinInt(a, b int) int { - if a < b { - return a - } - - return b -} - // FormParameter safely captures a form parameter from the context // by removing any new lines and HTML escaping the value. func FormParameter(c *gin.Context, parameter string) string {