c2c5250 |
Jakob Wakeling |
2023-12-12 09:13:37 |
0
|
// Copyright (C) 2023, Jakob Wakeling |
c2c5250 |
Jakob Wakeling |
2023-12-12 09:13:37 |
1
|
// All rights reserved. |
c2c5250 |
Jakob Wakeling |
2023-12-12 09:13:37 |
2
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
3
|
package admin |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
4
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
5
|
import ( |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
6
|
"fmt" |
b804701 |
Jakob Wakeling |
2023-11-27 23:52:28 |
7
|
"html/template" |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
8
|
"log" |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
9
|
"net/http" |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
10
|
"slices" |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
11
|
"strconv" |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
12
|
"strings" |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
13
|
|
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
14
|
"github.com/Jamozed/Goit/src/cron" |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
15
|
"github.com/Jamozed/Goit/src/goit" |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
16
|
"github.com/Jamozed/Goit/src/util" |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
17
|
"github.com/dustin/go-humanize" |
b804701 |
Jakob Wakeling |
2023-11-27 23:52:28 |
18
|
"github.com/gorilla/csrf" |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
19
|
) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
20
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
21
|
func HandleRepos(w http.ResponseWriter, r *http.Request) { |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
22
|
auth, user, err := goit.Auth(w, r, true) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
23
|
if err != nil { |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
24
|
log.Println("[admin/repos]", err.Error()) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
25
|
goit.HttpError(w, http.StatusInternalServerError) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
26
|
return |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
27
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
28
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
29
|
if !auth || !user.IsAdmin { |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
30
|
goit.HttpError(w, http.StatusNotFound) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
31
|
return |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
32
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
33
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
34
|
type row struct{ Id, Owner, Name, Visibility, Size string } |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
35
|
data := struct { |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
36
|
Title string |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
37
|
Repos []row |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
38
|
}{Title: "Admin - Repositories"} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
39
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
40
|
repos, err := goit.GetRepos() |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
41
|
if err != nil { |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
42
|
log.Println("[/admin/repos]", err.Error()) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
43
|
goit.HttpError(w, http.StatusInternalServerError) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
44
|
return |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
45
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
46
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
47
|
for _, r := range repos { |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
48
|
u, err := goit.GetUser(r.OwnerId) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
49
|
if err != nil { |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
50
|
log.Println("[/admin/repos]", err.Error()) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
51
|
u = &goit.User{} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
52
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
53
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
54
|
size, err := util.DirSize(goit.RepoPath(r.Name, true)) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
55
|
if err != nil { |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
56
|
log.Println("[/admin/repos]", err.Error()) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
57
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
58
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
59
|
data.Repos = append(data.Repos, row{ |
778d467 |
Jakob Wakeling |
2024-01-17 18:29:55 |
60
|
fmt.Sprint(r.Id), u.Name, r.Name, r.Visibility.String(), humanize.IBytes(size), |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
61
|
}) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
62
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
63
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
64
|
if err := goit.Tmpl.ExecuteTemplate(w, "admin/repos", data); err != nil { |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
65
|
log.Println("[/admin/repos]", err.Error()) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
66
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
67
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
68
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
69
|
func HandleRepoEdit(w http.ResponseWriter, r *http.Request) { |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
70
|
auth, user, err := goit.Auth(w, r, true) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
71
|
if err != nil { |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
72
|
log.Println("[/admin/repo/edit]", err.Error()) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
73
|
goit.HttpError(w, http.StatusInternalServerError) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
74
|
return |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
75
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
76
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
77
|
if !auth || !user.IsAdmin { |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
78
|
goit.HttpError(w, http.StatusNotFound) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
79
|
return |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
80
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
81
|
|
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
82
|
id, err := strconv.ParseInt(r.FormValue("repo"), 10, 64) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
83
|
if err != nil { |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
84
|
goit.HttpError(w, http.StatusNotFound) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
85
|
return |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
86
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
87
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
88
|
repo, err := goit.GetRepo(id) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
89
|
if err != nil { |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
90
|
log.Println("[/admin/repo/edit]", err.Error()) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
91
|
goit.HttpError(w, http.StatusInternalServerError) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
92
|
return |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
93
|
} else if repo == nil { |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
94
|
goit.HttpError(w, http.StatusNotFound) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
95
|
return |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
96
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
97
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
98
|
owner, err := goit.GetUser(repo.OwnerId) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
99
|
if err != nil { |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
100
|
log.Println("[/admin/repo/edit]", err.Error()) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
101
|
goit.HttpError(w, http.StatusInternalServerError) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
102
|
return |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
103
|
} else if owner == nil { |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
104
|
log.Println("[/admin/repo/edit]", repo.Id, "is owned by a nonexistent user") |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
105
|
owner = &goit.User{} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
106
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
107
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
108
|
data := struct { |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
109
|
Title, Name string |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
110
|
|
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
111
|
Edit struct { |
778d467 |
Jakob Wakeling |
2024-01-17 18:29:55 |
112
|
Id, Owner, Name, Description string |
778d467 |
Jakob Wakeling |
2024-01-17 18:29:55 |
113
|
DefaultBranch, Upstream, Visibility string |
778d467 |
Jakob Wakeling |
2024-01-17 18:29:55 |
114
|
IsMirror bool |
778d467 |
Jakob Wakeling |
2024-01-17 18:29:55 |
115
|
Message string |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
116
|
} |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
117
|
|
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
118
|
Transfer struct{ Owner, Message string } |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
119
|
Delete struct{ Message string } |
b804701 |
Jakob Wakeling |
2023-11-27 23:52:28 |
120
|
|
b804701 |
Jakob Wakeling |
2023-11-27 23:52:28 |
121
|
CsrfField template.HTML |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
122
|
}{ |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
123
|
Title: "Admin - Edit Repository", |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
124
|
Name: repo.Name, |
b804701 |
Jakob Wakeling |
2023-11-27 23:52:28 |
125
|
|
b804701 |
Jakob Wakeling |
2023-11-27 23:52:28 |
126
|
CsrfField: csrf.TemplateField(r), |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
127
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
128
|
|
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
129
|
data.Edit.Id = fmt.Sprint(repo.Id) |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
130
|
data.Edit.Owner = owner.FullName + " (" + owner.Name + ")[" + fmt.Sprint(owner.Id) + "]" |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
131
|
data.Edit.Name = repo.Name |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
132
|
data.Edit.Description = repo.Description |
28a8f13 |
Jakob Wakeling |
2023-12-25 19:52:40 |
133
|
data.Edit.DefaultBranch = repo.DefaultBranch |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
134
|
data.Edit.Upstream = repo.Upstream |
778d467 |
Jakob Wakeling |
2024-01-17 18:29:55 |
135
|
data.Edit.Visibility = repo.Visibility.String() |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
136
|
data.Edit.IsMirror = repo.IsMirror |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
137
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
138
|
if r.Method == http.MethodPost { |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
139
|
switch r.FormValue("action") { |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
140
|
case "edit": |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
141
|
data.Edit.Name = r.FormValue("reponame") |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
142
|
data.Edit.Description = r.FormValue("description") |
28a8f13 |
Jakob Wakeling |
2023-12-25 19:52:40 |
143
|
data.Edit.DefaultBranch = util.If(r.FormValue("branch") == "", "master", r.FormValue("branch")) |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
144
|
data.Edit.Upstream = r.FormValue("upstream") |
778d467 |
Jakob Wakeling |
2024-01-17 18:29:55 |
145
|
data.Edit.Visibility = r.FormValue("visibility") |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
146
|
data.Edit.IsMirror = r.FormValue("mirror") == "mirror" |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
147
|
|
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
148
|
if data.Edit.Name == "" { |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
149
|
data.Edit.Message = "Name cannot be empty" |
efb68c3 |
Jakob Wakeling |
2023-12-06 20:37:06 |
150
|
} else if slices.Contains(goit.Reserved, data.Edit.Name) || !goit.IsLegal(data.Name) { |
efb68c3 |
Jakob Wakeling |
2023-12-06 20:37:06 |
151
|
data.Edit.Message = "Name \"" + data.Edit.Name + "\" is illegal" |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
152
|
} else if exists, err := goit.RepoExists(data.Edit.Name); err != nil { |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
153
|
log.Println("[/admin/repo/edit]", err.Error()) |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
154
|
goit.HttpError(w, http.StatusInternalServerError) |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
155
|
return |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
156
|
} else if exists && !strings.EqualFold(data.Edit.Name, repo.Name) { |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
157
|
data.Edit.Message = "Name \"" + data.Edit.Name + "\" is taken" |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
158
|
} else if len(data.Edit.Description) > 256 { |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
159
|
data.Edit.Message = "Description cannot exceed 256 characters" |
778d467 |
Jakob Wakeling |
2024-01-17 18:29:55 |
160
|
} else if visibility := goit.VisibilityFromString(data.Edit.Visibility); visibility == -1 { |
778d467 |
Jakob Wakeling |
2024-01-17 18:29:55 |
161
|
data.Edit.Message = "Visibility \"" + data.Edit.Visibility + "\" is invalid" |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
162
|
} else if err := goit.UpdateRepo(repo.Id, goit.Repo{ |
28a8f13 |
Jakob Wakeling |
2023-12-25 19:52:40 |
163
|
Name: data.Edit.Name, Description: data.Edit.Description, DefaultBranch: data.Edit.DefaultBranch, |
778d467 |
Jakob Wakeling |
2024-01-17 18:29:55 |
164
|
Upstream: data.Edit.Upstream, Visibility: visibility, IsMirror: data.Edit.IsMirror, |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
165
|
}); err != nil { |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
166
|
log.Println("[/admin/repo/edit]", err.Error()) |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
167
|
goit.HttpError(w, http.StatusInternalServerError) |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
168
|
return |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
169
|
} else { |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
170
|
if (data.Edit.Upstream == "" && repo.Upstream != "") || !data.Edit.IsMirror { |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
171
|
goit.Cron.RemoveFor(repo.Id) |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
172
|
goit.Cron.Update() |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
173
|
} else if data.Edit.Upstream != "" && data.Edit.IsMirror && |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
174
|
(data.Edit.Upstream != repo.Upstream || !repo.IsMirror) { |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
175
|
goit.Cron.RemoveFor(repo.Id) |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
176
|
|
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
177
|
goit.Cron.Add(repo.Id, cron.Immediate, func() { |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
178
|
if err := goit.Pull(repo.Id); err != nil { |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
179
|
log.Println("[cron:import]", err.Error()) |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
180
|
} |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
181
|
log.Println("[cron:import] imported", data.Name) |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
182
|
}) |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
183
|
|
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
184
|
goit.Cron.Add(repo.Id, cron.Daily, func() { |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
185
|
if err := goit.Pull(repo.Id); err != nil { |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
186
|
log.Println("[cron:mirror]", err.Error()) |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
187
|
} |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
188
|
log.Println("[cron:mirror] updated", data.Edit.Name) |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
189
|
}) |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
190
|
|
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
191
|
goit.Cron.Update() |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
192
|
} |
f9225e6 |
Jakob Wakeling |
2023-12-18 23:58:22 |
193
|
|
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
194
|
data.Edit.Message = "Repository \"" + repo.Name + "\" updated successfully" |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
195
|
} |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
196
|
|
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
197
|
case "transfer": |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
198
|
data.Transfer.Owner = r.FormValue("owner") |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
199
|
|
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
200
|
if data.Transfer.Owner == "" { |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
201
|
data.Transfer.Message = "New owner cannot be empty" |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
202
|
} else if u, err := goit.GetUserByName(data.Transfer.Owner); err != nil { |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
203
|
log.Println("[/admin/repo/edit]", err.Error()) |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
204
|
goit.HttpError(w, http.StatusInternalServerError) |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
205
|
return |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
206
|
} else if u == nil { |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
207
|
data.Transfer.Message = "User \"" + data.Transfer.Owner + "\" does not exist" |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
208
|
} else if err := goit.ChownRepo(repo.Id, u.Id); err != nil { |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
209
|
log.Println("[/admin/repo/edit]", err.Error()) |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
210
|
goit.HttpError(w, http.StatusInternalServerError) |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
211
|
return |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
212
|
} else { |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
213
|
log.Println("User", user.Id, "transferred repo", repo.Id, "ownership to", u.Id) |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
214
|
http.Redirect(w, r, "/admin/repo/edit?repo="+data.Edit.Id, http.StatusFound) |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
215
|
return |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
216
|
} |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
217
|
|
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
218
|
case "delete": |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
219
|
var reponame = r.FormValue("reponame") |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
220
|
|
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
221
|
if reponame != repo.Name { |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
222
|
data.Delete.Message = "Input does not match the repository name" |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
223
|
} else if err := goit.DelRepo(repo.Id); err != nil { |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
224
|
log.Println("[/admin/repo/edit]", err.Error()) |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
225
|
goit.HttpError(w, http.StatusInternalServerError) |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
226
|
return |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
227
|
} else { |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
228
|
http.Redirect(w, r, "/admin/repos", http.StatusFound) |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
229
|
return |
3c55d1d |
Jakob Wakeling |
2023-11-27 21:07:45 |
230
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
231
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
232
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
233
|
|
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
234
|
if err := goit.Tmpl.ExecuteTemplate(w, "admin/repo/edit", data); err != nil { |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
235
|
log.Println("[/admin/repo/edit]", err.Error()) |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
236
|
} |
463cf58 |
Jakob Wakeling |
2023-11-22 23:25:37 |
237
|
} |
|
|
|
238
|
|