-
Notifications
You must be signed in to change notification settings - Fork 28
/
util.go
132 lines (107 loc) · 3.33 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package util
import (
"html"
"strings"
"github.com/go-vela/types/library"
"github.com/gin-gonic/gin"
"github.com/go-vela/types"
)
// HandleError appends the error to the handler chain for logging and outputs it.
func HandleError(c *gin.Context, status int, err error) {
msg := err.Error()
//nolint:errcheck // ignore checking error
c.Error(err)
c.AbortWithStatusJSON(status, types.Error{Message: &msg})
}
// 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 {
return EscapeValue(c.Request.FormValue(parameter))
}
// QueryParameter safely captures a query parameter from the context
// by removing any new lines and HTML escaping the value.
func QueryParameter(c *gin.Context, parameter, value string) string {
return EscapeValue(c.DefaultQuery(parameter, value))
}
// PathParameter safely captures a path parameter from the context
// by removing any new lines and HTML escaping the value.
func PathParameter(c *gin.Context, parameter string) string {
return EscapeValue(c.Param(parameter))
}
// SplitFullName safely splits the repo.FullName field into an org and name.
func SplitFullName(value string) (string, string) {
// split repo full name into org and repo
repoSlice := strings.Split(value, "/")
if len(repoSlice) != 2 {
return "", ""
}
org := repoSlice[0]
repo := repoSlice[1]
return org, repo
}
// EscapeValue safely escapes any string by removing any new lines and HTML escaping it.
func EscapeValue(value string) string {
// replace all new lines in the value
escaped := strings.Replace(strings.Replace(value, "\n", "", -1), "\r", "", -1)
// HTML escape the new line escaped value
return html.EscapeString(escaped)
}
// Unique is a helper function that takes a slice and
// validates that there are no duplicate entries.
func Unique(stringSlice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range stringSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
// CheckAllowlist is a helper function to ensure only repos in the
// allowlist are specified.
//
// a single entry of '*' allows any repo to be enabled.
func CheckAllowlist(r *library.Repo, allowlist []string) bool {
// check if all repos are allowed to be enabled
if len(allowlist) == 1 && allowlist[0] == "*" {
return true
}
for _, repo := range allowlist {
// allow all repos in org
if strings.Contains(repo, "/*") {
if strings.HasPrefix(repo, r.GetOrg()) {
return true
}
}
// allow specific repo within org
if repo == r.GetFullName() {
return true
}
}
return false
}