Skip to content

Commit

Permalink
refactor: use builtin min/max funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
wass3r committed Jun 18, 2024
1 parent 62c4225 commit b9e8cb9
Show file tree
Hide file tree
Showing 15 changed files with 17 additions and 59 deletions.
2 changes: 1 addition & 1 deletion api/build/list_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion api/deployment/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion api/hook/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion api/pipeline/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion api/repo/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
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 @@ -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")
Expand Down
26 changes: 4 additions & 22 deletions api/repo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion api/schedule/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion api/secret/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion api/service/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion api/step/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion api/user/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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

0 comments on commit b9e8cb9

Please sign in to comment.