This repository has been archived by the owner on May 22, 2022. It is now read-only.
forked from fallenhitokiri/leeroyci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_admin.go
162 lines (122 loc) · 3.06 KB
/
command_admin.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
package web
import (
"fmt"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/gorilla/schema"
"github.com/francoishill/leeroyci/database"
)
type commandAdminForm struct {
ID int64 `schema:"ID"`
Name string `schema:"name"`
Kind string `schema:"kind"`
Branch string `schema:"branch"`
Execute string `schema:"execute"`
}
func (c commandAdminForm) create(request *http.Request, repo *database.Repository) error {
err := request.ParseForm()
if err != nil {
return err
}
decoder := schema.NewDecoder()
form := new(commandAdminForm)
err = decoder.Decode(form, request.PostForm)
if err != nil {
return err
}
_, err = database.CreateCommand(
repo,
form.Name,
form.Execute,
form.Branch,
form.Kind,
)
return err
}
func (c commandAdminForm) update(request *http.Request, com *database.Command) error {
err := request.ParseForm()
if err != nil {
return err
}
decoder := schema.NewDecoder()
form := new(commandAdminForm)
err = decoder.Decode(form, request.PostForm)
if err != nil {
return err
}
err = com.Update(form.Name, form.Kind, form.Branch, form.Execute)
return err
}
func viewAdminCreateCommand(w http.ResponseWriter, r *http.Request) {
template := "command/admin/create.html"
ctx := make(responseContext)
vars := mux.Vars(r)
rid, _ := strconv.Atoi(vars["rid"])
repo, err := database.GetRepositoryByID(int64(rid))
if err != nil {
render(w, r, "403.html", make(responseContext)) // TODO: create 500
return
}
ctx["repository"] = repo
ctx["kinds"] = []string{
database.CommandKindBuild,
database.CommandKindDeploy,
database.CommandKindTest,
}
if r.Method == "POST" {
err := commandAdminForm{}.create(r, repo)
if err != nil {
ctx["error"] = err.Error()
} else {
uri := fmt.Sprintf("/admin/repository/%d", rid)
http.Redirect(w, r, uri, 302)
return
}
}
render(w, r, template, ctx)
}
func viewAdminUpdateCommand(w http.ResponseWriter, r *http.Request) {
template := "command/admin/update.html"
ctx := make(responseContext)
vars := mux.Vars(r)
rid, _ := strconv.Atoi(vars["rid"])
cid, _ := strconv.Atoi(vars["cid"])
repo, err := database.GetRepositoryByID(int64(rid))
if err != nil {
render(w, r, "403.html", make(responseContext)) // TODO: create 500
return
}
com, err := database.GetCommand(int64(cid))
if err != nil {
render(w, r, "403.html", make(responseContext)) // TODO: create 500
return
}
ctx["repository"] = repo
ctx["command"] = com
ctx["kinds"] = []string{
database.CommandKindBuild,
database.CommandKindDeploy,
database.CommandKindTest,
}
if r.Method == "POST" {
err := commandAdminForm{}.update(r, com)
if err == nil {
ctx["message"] = "Update successful."
} else {
ctx["error"] = err.Error()
}
}
render(w, r, template, ctx)
}
func viewAdminDeleteCommand(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
rid := vars["rid"]
cid, _ := strconv.Atoi(vars["cid"])
com, err := database.GetCommand(int64(cid))
if err == nil {
com.Delete()
}
uri := fmt.Sprintf("/admin/repository/%s", rid)
http.Redirect(w, r, uri, 302)
}