-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.go
145 lines (122 loc) · 3.6 KB
/
utility.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
133
134
135
136
137
138
139
140
141
142
143
144
145
package project
import (
"errors"
"fmt"
"net/http"
"regexp"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/swarajkumarsingh/turbo-deploy/constants"
"github.com/swarajkumarsingh/turbo-deploy/constants/messages"
"github.com/swarajkumarsingh/turbo-deploy/functions/general"
"github.com/swarajkumarsingh/turbo-deploy/functions/logger"
validators "github.com/swarajkumarsingh/turbo-deploy/functions/validator"
model "github.com/swarajkumarsingh/turbo-deploy/models/project"
)
func getUserIdFromReq(ctx *gin.Context) (string, bool) {
uid, valid := ctx.Get(constants.UserIdMiddlewareConstant)
if !valid || uid == nil || fmt.Sprintf("%v", uid) == "" {
return "", false
}
return fmt.Sprintf("%v", uid), true
}
func getCurrentPageValue(ctx *gin.Context) int {
val, err := strconv.Atoi(ctx.DefaultQuery("page", "1"))
if err != nil {
logger.WithRequest(ctx).Errorln("error while extracting current page value: ", err)
return 1
}
return val
}
func getOffsetValue(page int, itemsPerPage int) int {
return (page - 1) * itemsPerPage
}
func getItemPerPageValue(ctx *gin.Context) int {
val, err := strconv.Atoi(ctx.DefaultQuery("per_page", strconv.Itoa(constants.DefaultPerPageSize)))
if err != nil {
logger.WithRequest(ctx).Errorln("error while extracting item per-page value: ", err)
return constants.DefaultPerPageSize
}
return val
}
func calculateTotalPages(page, itemsPerPage int) int {
if page <= 0 {
return 1
}
return (page + itemsPerPage - 1) / itemsPerPage
}
func IsValidGitHubURL(url string) bool {
regex := `^https:\/\/github\.com\/[a-zA-Z0-9-]+\/[a-zA-Z0-9._-]+$`
matched, err := regexp.MatchString(regex, url)
return err == nil && matched
}
func ValidateGitHubURL(url string, resultChan chan<- bool) {
defer close(resultChan)
client := &http.Client{Timeout: 4 * time.Second}
resp, err := client.Get(url)
if err != nil {
resultChan <- false
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
resultChan <- true
} else {
resultChan <- false
}
}
func IsAccessibleGitHubRepo(url string) (bool, error) {
client := &http.Client{Timeout: 4 * time.Second}
resp, err := client.Get(url)
if err != nil {
return false, err
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
return true, nil
case http.StatusNotFound:
return false, errors.New("repository not found or is private")
default:
return false, fmt.Errorf("unexpected status code from source: %d", resp.StatusCode)
}
}
func getProjectIdFromParam(ctx *gin.Context) (int, bool) {
userId := ctx.Param("pid")
valid := general.SQLInjectionValidation(userId)
if !valid {
return 0, false
}
uid, err := general.IsInt(userId)
if err != nil {
return 0, false
}
return uid, true
}
func getCreateProjectBody(ctx *gin.Context) (model.ProjectBody, error) {
var body model.ProjectBody
if err := ctx.ShouldBindJSON(&body); err != nil {
return body, errors.New(messages.InvalidBodyMessage)
}
if err := validators.ValidateStruct(body); err != nil {
return body, err
}
if !general.IsAlphanumeric(body.Name) || !general.IsAlphanumeric(body.Subdomain) {
return body, errors.New(messages.InvalidBodyMessage)
}
return body, nil
}
func getUpdateProjectBody(ctx *gin.Context) (model.UpdateProjectBody, error) {
var body model.UpdateProjectBody
if err := ctx.ShouldBindJSON(&body); err != nil {
return body, errors.New(messages.InvalidBodyMessage)
}
if err := validators.ValidateStruct(body); err != nil {
return body, err
}
if !general.IsAlphanumeric(body.Name) || !general.IsAlphanumeric(body.Subdomain) {
return body, errors.New(messages.InvalidBodyMessage)
}
return body, nil
}