-
Notifications
You must be signed in to change notification settings - Fork 562
/
Copy pathweb.go
280 lines (242 loc) · 7.9 KB
/
web.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package controllers
import (
"html/template"
"log"
"net/http"
"strconv"
"strings"
"github.com/diggerhq/digger/backend/config"
"github.com/diggerhq/digger/backend/middleware"
"github.com/diggerhq/digger/backend/models"
"github.com/diggerhq/digger/backend/services"
"github.com/gin-gonic/gin"
"github.com/robert-nix/ansihtml"
"golang.org/x/exp/maps"
)
type WebController struct {
Config *config.Config
}
func (web *WebController) validateRequestProjectId(c *gin.Context) (*models.Project, bool) {
projectId64, err := strconv.ParseUint(c.Param("projectid"), 10, 32)
if err != nil {
c.String(http.StatusInternalServerError, "Failed to parse project id")
return nil, false
}
projectId := uint(projectId64)
projects, done := models.DB.GetProjectsFromContext(c, middleware.ORGANISATION_ID_KEY)
if !done {
return nil, false
}
for _, p := range projects {
if projectId == p.ID {
return &p, true
}
}
c.String(http.StatusForbidden, "Not allowed to access this resource")
return nil, false
}
func (web *WebController) ProjectsPage(c *gin.Context) {
projects, done := models.DB.GetProjectsFromContext(c, middleware.ORGANISATION_ID_KEY)
if !done {
return
}
pageContext := services.GetMessages(c)
maps.Copy(pageContext, gin.H{
"Projects": projects,
})
c.HTML(http.StatusOK, "projects.tmpl", pageContext)
}
func (web *WebController) ReposPage(c *gin.Context) {
repos, done := models.DB.GetReposFromContext(c, middleware.ORGANISATION_ID_KEY)
if !done {
return
}
pageContext := services.GetMessages(c)
maps.Copy(pageContext, gin.H{
"Repos": repos,
//"GithubApp": githubApp,
})
c.HTML(http.StatusOK, "repos.tmpl", pageContext)
}
func (web *WebController) RunsPage(c *gin.Context) {
runs, done := models.DB.GetProjectRunsFromContext(c, middleware.ORGANISATION_ID_KEY)
if !done {
return
}
context := gin.H{
"Runs": runs,
}
c.HTML(http.StatusOK, "runs.tmpl", context)
}
func (web *WebController) PoliciesPage(c *gin.Context) {
policies, done := models.DB.GetPoliciesFromContext(c, middleware.ORGANISATION_ID_KEY)
if !done {
return
}
pageContext := services.GetMessages(c)
maps.Copy(pageContext, gin.H{
"Policies": policies,
})
c.HTML(http.StatusOK, "policies.tmpl", pageContext)
}
func (web *WebController) AddPolicyPage(c *gin.Context) {
if c.Request.Method == "GET" {
message := ""
projects, done := models.DB.GetProjectsFromContext(c, middleware.ORGANISATION_ID_KEY)
if !done {
return
}
policyTypes := make([]string, 0)
policyTypes = append(policyTypes, "drift")
policyTypes = append(policyTypes, "plan")
policyTypes = append(policyTypes, "access")
log.Printf("projects: %v\n", projects)
c.HTML(http.StatusOK, "policy_add.tmpl", gin.H{
"Message": message, "Projects": projects, "PolicyTypes": policyTypes,
})
} else if c.Request.Method == "POST" {
policyText := c.PostForm("policytext")
if policyText == "" {
message := "Policy can't be empty"
services.AddWarning(c, message)
pageContext := services.GetMessages(c)
c.HTML(http.StatusOK, "policy_add.tmpl", pageContext)
}
policyType := c.PostForm("policytype")
projectIdStr := c.PostForm("projectid")
projectId64, err := strconv.ParseUint(projectIdStr, 10, 32)
if err != nil {
c.String(http.StatusInternalServerError, "Failed to parse policy id")
return
}
projectId := uint(projectId64)
project, ok := models.DB.GetProjectByProjectId(c, projectId, middleware.ORGANISATION_ID_KEY)
if !ok {
log.Printf("Failed to fetch specified project by id: %v, %v\n", projectIdStr, err)
message := "Failed to create a policy"
services.AddError(c, message)
pageContext := services.GetMessages(c)
c.HTML(http.StatusOK, "policy_add.tmpl", pageContext)
}
log.Printf("repo: %v\n", project.Repo)
policy := models.Policy{Project: project, Policy: policyText, Type: policyType, Organisation: project.Organisation, Repo: project.Repo}
err = models.DB.GormDB.Create(&policy).Error
if err != nil {
log.Printf("Failed to create a new policy, %v\n", err)
message := "Failed to create a policy"
services.AddError(c, message)
pageContext := services.GetMessages(c)
c.HTML(http.StatusOK, "policy_add.tmpl", pageContext)
}
c.Redirect(http.StatusFound, "/policies")
}
}
func (web *WebController) PolicyDetailsPage(c *gin.Context) {
policyId64, err := strconv.ParseUint(c.Param("policyid"), 10, 32)
if err != nil {
c.String(http.StatusInternalServerError, "Failed to parse policy id")
return
}
policyId := uint(policyId64)
policy, ok := models.DB.GetPolicyByPolicyId(c, policyId, middleware.ORGANISATION_ID_KEY)
if !ok {
return
}
pageContext := services.GetMessages(c)
maps.Copy(pageContext, gin.H{"Policy": policy})
c.HTML(http.StatusOK, "policy_details.tmpl", pageContext)
}
func (web *WebController) ProjectDetailsPage(c *gin.Context) {
project, ok := web.validateRequestProjectId(c)
if !ok {
return
}
pageContext := services.GetMessages(c)
maps.Copy(pageContext, gin.H{"Project": project})
c.HTML(http.StatusOK, "project_details.tmpl", pageContext)
}
func (web *WebController) RunDetailsPage(c *gin.Context) {
runId64, err := strconv.ParseUint(c.Param("runid"), 10, 32)
if err != nil {
c.String(http.StatusInternalServerError, "Failed to parse project run id")
return
}
runId := uint(runId64)
run, ok := models.DB.GetProjectByRunId(c, runId, middleware.ORGANISATION_ID_KEY)
if !ok {
return
}
stateSyncOutput := ""
terraformPlanOutput := ""
runOutput := string(ansihtml.ConvertToHTMLWithClasses([]byte(run.Output), "terraform-output-", true))
runOutput = strings.Replace(runOutput, " ", " ", -1)
runOutput = strings.Replace(runOutput, "\n", "<br>\n", -1)
planIndex := strings.Index(runOutput, "Terraform used the selected providers to generate the following execution")
if planIndex != -1 {
stateSyncOutput = runOutput[:planIndex]
terraformPlanOutput = runOutput[planIndex:]
pageContext := services.GetMessages(c)
maps.Copy(pageContext, gin.H{
"Run": run,
"TerraformStateSyncOutput": template.HTML(stateSyncOutput),
"TerraformPlanOutput": template.HTML(terraformPlanOutput),
})
c.HTML(http.StatusOK, "run_details.tmpl", pageContext)
} else {
pageContext := services.GetMessages(c)
maps.Copy(pageContext, gin.H{
"Run": run,
"RunOutput": template.HTML(runOutput),
})
c.HTML(http.StatusOK, "run_details.tmpl", pageContext)
}
}
func (web *WebController) ProjectDetailsUpdatePage(c *gin.Context) {
project, ok := web.validateRequestProjectId(c)
if !ok {
return
}
projectName := c.PostForm("project_name")
if projectName != project.Name {
project.Name = projectName
models.DB.GormDB.Save(project)
log.Printf("project name has been updated to %s\n", projectName)
services.AddMessage(c, "Project has been updated successfully")
}
pageContext := services.GetMessages(c)
maps.Copy(pageContext, gin.H{
"Project": project,
})
c.HTML(http.StatusOK, "project_details.tmpl", pageContext)
}
func (web *WebController) PolicyDetailsUpdatePage(c *gin.Context) {
policyId64, err := strconv.ParseUint(c.Param("policyid"), 10, 32)
if err != nil {
c.String(http.StatusInternalServerError, "Failed to parse policy id")
return
}
policyId := uint(policyId64)
policy, ok := models.DB.GetPolicyByPolicyId(c, policyId, middleware.ORGANISATION_ID_KEY)
if !ok {
return
}
policyText := c.PostForm("policy")
log.Printf("policyText: %v\n", policyText)
if policyText == "" {
services.AddWarning(c, "Policy can't be empty.")
} else if policyText != policy.Policy {
policy.Policy = policyText
models.DB.GormDB.Save(policy)
log.Printf("Policy has been updated. policy id: %v\n", policy.ID)
services.AddMessage(c, "Policy has been updated successfully")
c.Redirect(http.StatusFound, "/policies")
return
} else {
services.AddMessage(c, "No changes to policy")
}
pageContext := services.GetMessages(c)
maps.Copy(pageContext, gin.H{
"Policy": policy,
})
c.HTML(http.StatusOK, "policy_details.tmpl", pageContext)
}