Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

Goit/src/repo/create.go (104 lines, 3.0 KiB) -rw-r--r-- file download

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
20732d3 Jakob Wakeling 2023-07-26 22:38:15
3
package repo
20732d3 Jakob Wakeling 2023-07-26 22:38:15
4
20732d3 Jakob Wakeling 2023-07-26 22:38:15
5
import (
b804701 Jakob Wakeling 2023-11-27 23:52:28
6
	"html/template"
20732d3 Jakob Wakeling 2023-07-26 22:38:15
7
	"log"
20732d3 Jakob Wakeling 2023-07-26 22:38:15
8
	"net/http"
23b01c4 Jakob Wakeling 2023-08-26 14:44:52
9
	"slices"
efb68c3 Jakob Wakeling 2023-12-06 20:37:06
10
	"strings"
20732d3 Jakob Wakeling 2023-07-26 22:38:15
11
1547a71 Jakob Wakeling 2023-12-16 15:24:55
12
	"github.com/Jamozed/Goit/src/cron"
945fd71 Jakob Wakeling 2023-11-05 15:39:31
13
	"github.com/Jamozed/Goit/src/goit"
1547a71 Jakob Wakeling 2023-12-16 15:24:55
14
	"github.com/Jamozed/Goit/src/util"
b804701 Jakob Wakeling 2023-11-27 23:52:28
15
	"github.com/gorilla/csrf"
20732d3 Jakob Wakeling 2023-07-26 22:38:15
16
)
20732d3 Jakob Wakeling 2023-07-26 22:38:15
17
20732d3 Jakob Wakeling 2023-07-26 22:38:15
18
func HandleCreate(w http.ResponseWriter, r *http.Request) {
473d02c Jakob Wakeling 2023-11-28 00:01:06
19
	auth, user, err := goit.Auth(w, r, true)
473d02c Jakob Wakeling 2023-11-28 00:01:06
20
	if err != nil {
473d02c Jakob Wakeling 2023-11-28 00:01:06
21
		log.Println("[admin]", err.Error())
473d02c Jakob Wakeling 2023-11-28 00:01:06
22
		goit.HttpError(w, http.StatusInternalServerError)
473d02c Jakob Wakeling 2023-11-28 00:01:06
23
	}
473d02c Jakob Wakeling 2023-11-28 00:01:06
24
473d02c Jakob Wakeling 2023-11-28 00:01:06
25
	if !auth {
20732d3 Jakob Wakeling 2023-07-26 22:38:15
26
		goit.HttpError(w, http.StatusUnauthorized)
20732d3 Jakob Wakeling 2023-07-26 22:38:15
27
		return
20732d3 Jakob Wakeling 2023-07-26 22:38:15
28
	}
20732d3 Jakob Wakeling 2023-07-26 22:38:15
29
20732d3 Jakob Wakeling 2023-07-26 22:38:15
30
	data := struct {
778d467 Jakob Wakeling 2024-01-17 18:29:55
31
		Title, Message                 string
778d467 Jakob Wakeling 2024-01-17 18:29:55
32
		Name, Description              string
778d467 Jakob Wakeling 2024-01-17 18:29:55
33
		DefaultBranch, Url, Visibility string
778d467 Jakob Wakeling 2024-01-17 18:29:55
34
		IsMirror                       bool
b804701 Jakob Wakeling 2023-11-27 23:52:28
35
b804701 Jakob Wakeling 2023-11-27 23:52:28
36
		CsrfField template.HTML
b804701 Jakob Wakeling 2023-11-27 23:52:28
37
	}{
b804701 Jakob Wakeling 2023-11-27 23:52:28
38
		Title: "Repository - Create",
b804701 Jakob Wakeling 2023-11-27 23:52:28
39
b804701 Jakob Wakeling 2023-11-27 23:52:28
40
		CsrfField: csrf.TemplateField(r),
b804701 Jakob Wakeling 2023-11-27 23:52:28
41
	}
20732d3 Jakob Wakeling 2023-07-26 22:38:15
42
20732d3 Jakob Wakeling 2023-07-26 22:38:15
43
	if r.Method == http.MethodPost {
20732d3 Jakob Wakeling 2023-07-26 22:38:15
44
		data.Name = r.FormValue("reponame")
20732d3 Jakob Wakeling 2023-07-26 22:38:15
45
		data.Description = r.FormValue("description")
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
46
		data.DefaultBranch = util.If(r.FormValue("branch") == "", "master", r.FormValue("branch"))
1547a71 Jakob Wakeling 2023-12-16 15:24:55
47
		data.Url = r.FormValue("url")
778d467 Jakob Wakeling 2024-01-17 18:29:55
48
		data.Visibility = r.FormValue("visibility")
1547a71 Jakob Wakeling 2023-12-16 15:24:55
49
		data.IsMirror = r.FormValue("mirror") == "mirror"
20732d3 Jakob Wakeling 2023-07-26 22:38:15
50
20732d3 Jakob Wakeling 2023-07-26 22:38:15
51
		if data.Name == "" {
20732d3 Jakob Wakeling 2023-07-26 22:38:15
52
			data.Message = "Name cannot be empty"
efb68c3 Jakob Wakeling 2023-12-06 20:37:06
53
		} else if slices.Contains(goit.Reserved, strings.SplitN(data.Name, "/", 2)[0]) || !goit.IsLegal(data.Name) {
efb68c3 Jakob Wakeling 2023-12-06 20:37:06
54
			data.Message = "Name \"" + data.Name + "\" is illegal"
20732d3 Jakob Wakeling 2023-07-26 22:38:15
55
		} else if exists, err := goit.RepoExists(data.Name); err != nil {
20732d3 Jakob Wakeling 2023-07-26 22:38:15
56
			log.Println("[/repo/create]", err.Error())
20732d3 Jakob Wakeling 2023-07-26 22:38:15
57
			goit.HttpError(w, http.StatusInternalServerError)
20732d3 Jakob Wakeling 2023-07-26 22:38:15
58
			return
20732d3 Jakob Wakeling 2023-07-26 22:38:15
59
		} else if exists {
20732d3 Jakob Wakeling 2023-07-26 22:38:15
60
			data.Message = "Name \"" + data.Name + "\" is taken"
778d467 Jakob Wakeling 2024-01-17 18:29:55
61
		} else if len(data.Description) > 256 {
778d467 Jakob Wakeling 2024-01-17 18:29:55
62
			data.Message = "Description cannot exceed 256 characters"
778d467 Jakob Wakeling 2024-01-17 18:29:55
63
		} else if visibility := goit.VisibilityFromString(data.Visibility); visibility == -1 {
778d467 Jakob Wakeling 2024-01-17 18:29:55
64
			data.Message = "Visibility \"" + data.Visibility + "\" is invalid"
1547a71 Jakob Wakeling 2023-12-16 15:24:55
65
		} else if rid, err := goit.CreateRepo(goit.Repo{
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
66
			OwnerId: user.Id, Name: data.Name, Description: data.Description, DefaultBranch: data.DefaultBranch,
778d467 Jakob Wakeling 2024-01-17 18:29:55
67
			Upstream: data.Url, Visibility: visibility, IsMirror: data.IsMirror,
20732d3 Jakob Wakeling 2023-07-26 22:38:15
68
		}); err != nil {
20732d3 Jakob Wakeling 2023-07-26 22:38:15
69
			log.Println("[/repo/create]", err.Error())
20732d3 Jakob Wakeling 2023-07-26 22:38:15
70
			goit.HttpError(w, http.StatusInternalServerError)
20732d3 Jakob Wakeling 2023-07-26 22:38:15
71
			return
20732d3 Jakob Wakeling 2023-07-26 22:38:15
72
		} else {
1547a71 Jakob Wakeling 2023-12-16 15:24:55
73
			if data.Url != "" {
1547a71 Jakob Wakeling 2023-12-16 15:24:55
74
				goit.Cron.Add(rid, cron.Immediate, func() {
1547a71 Jakob Wakeling 2023-12-16 15:24:55
75
					if err := goit.Pull(rid); err != nil {
1547a71 Jakob Wakeling 2023-12-16 15:24:55
76
						log.Println("[cron:import]", err.Error())
1547a71 Jakob Wakeling 2023-12-16 15:24:55
77
					}
1547a71 Jakob Wakeling 2023-12-16 15:24:55
78
					log.Println("[cron:import] imported", data.Name)
1547a71 Jakob Wakeling 2023-12-16 15:24:55
79
				})
1547a71 Jakob Wakeling 2023-12-16 15:24:55
80
1547a71 Jakob Wakeling 2023-12-16 15:24:55
81
				if data.IsMirror {
1547a71 Jakob Wakeling 2023-12-16 15:24:55
82
					util.Debugln("Adding mirror cron job for", data.Name)
1547a71 Jakob Wakeling 2023-12-16 15:24:55
83
					goit.Cron.Add(rid, cron.Daily, func() {
1547a71 Jakob Wakeling 2023-12-16 15:24:55
84
						if err := goit.Pull(rid); err != nil {
1547a71 Jakob Wakeling 2023-12-16 15:24:55
85
							log.Println("[cron:mirror]", err.Error())
1547a71 Jakob Wakeling 2023-12-16 15:24:55
86
						}
1547a71 Jakob Wakeling 2023-12-16 15:24:55
87
						log.Println("[cron:mirror] updated", data.Name)
1547a71 Jakob Wakeling 2023-12-16 15:24:55
88
					})
1547a71 Jakob Wakeling 2023-12-16 15:24:55
89
				}
1547a71 Jakob Wakeling 2023-12-16 15:24:55
90
1547a71 Jakob Wakeling 2023-12-16 15:24:55
91
				goit.Cron.Update()
1547a71 Jakob Wakeling 2023-12-16 15:24:55
92
			}
1547a71 Jakob Wakeling 2023-12-16 15:24:55
93
20732d3 Jakob Wakeling 2023-07-26 22:38:15
94
			http.Redirect(w, r, "/"+data.Name, http.StatusFound)
20732d3 Jakob Wakeling 2023-07-26 22:38:15
95
			return
20732d3 Jakob Wakeling 2023-07-26 22:38:15
96
		}
20732d3 Jakob Wakeling 2023-07-26 22:38:15
97
	}
20732d3 Jakob Wakeling 2023-07-26 22:38:15
98
20732d3 Jakob Wakeling 2023-07-26 22:38:15
99
	if err := goit.Tmpl.ExecuteTemplate(w, "repo/create", data); err != nil {
20732d3 Jakob Wakeling 2023-07-26 22:38:15
100
		log.Println("[/repo/create]", err.Error())
20732d3 Jakob Wakeling 2023-07-26 22:38:15
101
	}
20732d3 Jakob Wakeling 2023-07-26 22:38:15
102
}
103