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: use builtin min/max funcs #1148

Merged
merged 8 commits into from
Aug 20, 2024
2 changes: 1 addition & 1 deletion api/build/list_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,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)
Expand Down
2 changes: 1 addition & 1 deletion api/build/list_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,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)
Expand Down
2 changes: 1 addition & 1 deletion api/deployment/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,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)
Expand Down
2 changes: 1 addition & 1 deletion api/hook/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,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)
Expand Down
2 changes: 1 addition & 1 deletion api/log/list_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,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
bl, t, err := database.FromContext(c).ListLogsForBuild(ctx, b, page, perPage)
Expand Down
2 changes: 1 addition & 1 deletion api/pipeline/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,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 {
Expand Down
2 changes: 1 addition & 1 deletion api/repo/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,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")
Expand Down
2 changes: 1 addition & 1 deletion api/repo/list_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,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")
Expand Down
26 changes: 4 additions & 22 deletions api/repo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,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 {
Expand Down
2 changes: 1 addition & 1 deletion api/schedule/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,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)
Expand Down
2 changes: 1 addition & 1 deletion api/secret/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,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)
Expand Down
2 changes: 1 addition & 1 deletion api/service/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,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)
Expand Down
2 changes: 1 addition & 1 deletion api/step/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,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)
Expand Down
2 changes: 1 addition & 1 deletion api/user/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,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)
Expand Down
24 changes: 0 additions & 24 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading