aboutsummaryrefslogtreecommitdiff
path: root/http_handle_group_index.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-03-06 22:01:08 +0800
committerRunxi Yu <me@runxiyu.org>2025-03-06 22:01:31 +0800
commitfa62d8eae273d89937d65d6a294f028e1ce22d88 (patch)
tree226d5fab65927ef977b664a35e6a2b2f30169440 /http_handle_group_index.go
parentgroup: Add description field to the create repo form (diff)
downloadforge-fa62d8eae273d89937d65d6a294f028e1ce22d88.tar.gz
forge-fa62d8eae273d89937d65d6a294f028e1ce22d88.tar.zst
forge-fa62d8eae273d89937d65d6a294f028e1ce22d88.zip
group/index: Allow repo creation via web
Diffstat (limited to 'http_handle_group_index.go')
-rw-r--r--http_handle_group_index.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/http_handle_group_index.go b/http_handle_group_index.go
index 97ffee5..edb1368 100644
--- a/http_handle_group_index.go
+++ b/http_handle_group_index.go
@@ -5,6 +5,8 @@ package main
import (
"net/http"
+ "path/filepath"
+ "strconv"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
@@ -74,6 +76,60 @@ func handle_group_index(w http.ResponseWriter, r *http.Request, params map[strin
}
direct_access := (count > 0)
+ if r.Method == "POST" {
+ if !direct_access {
+ http.Error(w, "You do not have direct access to this group", http.StatusForbidden)
+ return
+ }
+
+ repo_name := r.FormValue("repo_name")
+ repo_description := r.FormValue("repo_description")
+ contrib_requirements := r.FormValue("repo_contrib")
+ if repo_name == "" {
+ http.Error(w, "Repo name is required", http.StatusBadRequest)
+ return
+ }
+
+ var new_repo_id int
+ err := database.QueryRow(
+ r.Context(),
+ `INSERT INTO repos (name, description, group_id, contrib_requirements)
+ VALUES ($1, $2, $3, $4)
+ RETURNING id`,
+ repo_name,
+ repo_description,
+ group_id,
+ contrib_requirements,
+ ).Scan(&new_repo_id)
+ if err != nil {
+ http.Error(w, "Error creating repo: "+err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ file_path := filepath.Join(config.Git.RepoDir, strconv.Itoa(new_repo_id)+".git")
+
+ _, err = database.Exec(
+ r.Context(),
+ `UPDATE repos
+ SET filesystem_path = $1
+ WHERE id = $2`,
+ file_path,
+ new_repo_id,
+ )
+ if err != nil {
+ http.Error(w, "Error updating repo path: "+err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ if err = git_bare_init_with_default_hooks(file_path); err != nil {
+ http.Error(w, "Error initializing repo: "+err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ redirect_unconditionally(w, r)
+ return
+ }
+
// Repos
var rows pgx.Rows
rows, err = database.Query(r.Context(), `