repos / git-pr

a self-hosted git collaboration server
git clone https://github.com/picosh/git-pr.git

commit
02d2f96
parent
570852b
author
Eric Bower
date
2024-06-24 15:19:00 -0400 EDT
chore: cleanup
7 files changed,  +92, -74
M cfg.go
M pr.go
M ssh.go
M web.go
M cfg.go
+43, -40
 1@@ -1,55 +1,58 @@
 2 package git
 3 
 4-import "github.com/charmbracelet/ssh"
 5+import (
 6+	"os"
 7+
 8+	"github.com/charmbracelet/ssh"
 9+)
10 
11 type Repo struct {
12-	ID        string
13-	Desc      string
14-	CloneAddr string
15+	ID            string
16+	Desc          string
17+	CloneAddr     string
18+	DefaultBranch string
19+}
20+
21+func NewRepo(id, cloneAddr string) *Repo {
22+	return &Repo{
23+		ID:            id,
24+		CloneAddr:     cloneAddr,
25+		DefaultBranch: "main",
26+	}
27 }
28 
29 type GitCfg struct {
30 	DataPath string
31 	Admins   []ssh.PublicKey
32-	Repos    []Repo
33+	Repos    []*Repo
34 	Url      string
35+	Host     string
36+	SshPort  string
37+	WebPort  string
38 }
39 
40-func NewGitCfg() *GitCfg {
41+func NewGitCfg(dataPath, url string, repos []*Repo) *GitCfg {
42+	host := os.Getenv("GIT_HOST")
43+	if host == "" {
44+		host = "0.0.0.0"
45+	}
46+
47+	sshPort := os.Getenv("GIT_SSH_PORT")
48+	if sshPort == "" {
49+		sshPort = "2222"
50+	}
51+
52+	webPort := os.Getenv("GIT_WEB_PORT")
53+	if webPort == "" {
54+		webPort = "3000"
55+	}
56+
57 	return &GitCfg{
58-		DataPath: "./ssh_data",
59-		Url:      "pr.pico.sh",
60-		Repos: []Repo{
61-			{
62-				ID:        "test",
63-				Desc:      "A test repo to play around with Patch Requests",
64-				CloneAddr: "git@github.com:picosh/test",
65-			},
66-			{
67-				ID:        "pico",
68-				Desc:      "hacker labs - open and managed web services leveraging ssh",
69-				CloneAddr: "git@github.com:picosh/pico",
70-			},
71-			{
72-				ID:        "ptun",
73-				Desc:      "passwordless authentication for the web",
74-				CloneAddr: "git@github.com:picosh/ptun",
75-			},
76-			{
77-				ID:        "pobj",
78-				Desc:      "rsync, scp, sftp for your object store",
79-				CloneAddr: "git@github.com:picosh/ptun",
80-			},
81-			{
82-				ID:        "send",
83-				Desc:      "ssh wish middleware for sending and receiving files from familiar tools (rsync, scp, sftp)",
84-				CloneAddr: "git@github.com:picosh/send",
85-			},
86-			{
87-				ID:        "docs",
88-				Desc:      "pico.sh doc site",
89-				CloneAddr: "git@github.com:picosh/docs",
90-			},
91-		},
92+		DataPath: dataPath,
93+		Url:      url,
94+		Repos:    repos,
95+		Host:     host,
96+		SshPort:  sshPort,
97+		WebPort:  webPort,
98 	}
99 }
A cmd/cfg.go
+29, -0
 1@@ -0,0 +1,29 @@
 2+package cmd
 3+
 4+import "github.com/picosh/git-pr"
 5+
 6+func NewPicoCfg() *git.GitCfg {
 7+	test := git.NewRepo("test", "git@github.com:picosh/test")
 8+	test.Desc = "A test repo to play around with Patch Requests"
 9+
10+	pico := git.NewRepo("pico", "git@github.com:picosh/pico")
11+	pico.Desc = "hacker labs - open and managed web services leveraging ssh"
12+
13+	ptun := git.NewRepo("ptun", "git@github.com:picosh/ptun")
14+	ptun.Desc = "passwordless authentication for the web"
15+
16+	pobj := git.NewRepo("pobj", "git@github.com:picosh/pobj")
17+	pobj.Desc = "rsync, scp, sftp for your object store"
18+
19+	send := git.NewRepo("send", "git@github.com:picosh/send")
20+	send.Desc = "ssh wish middleware for sending and receiving files from familiar tools (rsync, scp, sftp)"
21+
22+	docs := git.NewRepo("docs", "git@github.com:picosh/docs")
23+	docs.Desc = "pico.sh doc site"
24+
25+	return git.NewGitCfg(
26+		"ssh_data",
27+		"pr.pico.sh",
28+		[]*git.Repo{test, pico, ptun, pobj, send, docs},
29+	)
30+}
M cmd/ssh/main.go
+5, -2
 1@@ -1,7 +1,10 @@
 2 package main
 3 
 4-import git "github.com/picosh/git-pr"
 5+import (
 6+	git "github.com/picosh/git-pr"
 7+	"github.com/picosh/git-pr/cmd"
 8+)
 9 
10 func main() {
11-	git.GitSshServer()
12+	git.GitSshServer(cmd.NewPicoCfg())
13 }
M cmd/web/main.go
+5, -2
 1@@ -1,7 +1,10 @@
 2 package main
 3 
 4-import git "github.com/picosh/git-pr"
 5+import (
 6+	git "github.com/picosh/git-pr"
 7+	"github.com/picosh/git-pr/cmd"
 8+)
 9 
10 func main() {
11-	git.StartWebServer()
12+	git.StartWebServer(cmd.NewPicoCfg())
13 }
M pr.go
+5, -5
 1@@ -24,7 +24,7 @@ const (
 2 )
 3 
 4 type GitPatchRequest interface {
 5-	GetRepos() ([]Repo, error)
 6+	GetRepos() ([]*Repo, error)
 7 	GetReposWithLatestPr() ([]RepoWithLatestPr, error)
 8 	GetRepoByID(repoID string) (*Repo, error)
 9 	SubmitPatchRequest(repoID string, pubkey string, patchset io.Reader) (*PatchRequest, error)
10@@ -59,7 +59,7 @@ type RepoWithLatestPr struct {
11 	PatchRequest *PatchRequest
12 }
13 
14-func (pr PrCmd) GetRepos() ([]Repo, error) {
15+func (pr PrCmd) GetRepos() ([]*Repo, error) {
16 	return pr.Backend.Cfg.Repos, nil
17 }
18 
19@@ -76,7 +76,7 @@ func (pr PrCmd) GetReposWithLatestPr() ([]RepoWithLatestPr, error) {
20 		for _, repo := range pr.Backend.Cfg.Repos {
21 			if prq.RepoID == repo.ID {
22 				repos = append(repos, RepoWithLatestPr{
23-					Repo:         &repo,
24+					Repo:         repo,
25 					PatchRequest: &prq,
26 				})
27 			}
28@@ -92,7 +92,7 @@ func (pr PrCmd) GetReposWithLatestPr() ([]RepoWithLatestPr, error) {
29 		}
30 		if !found {
31 			repos = append(repos, RepoWithLatestPr{
32-				Repo: &repo,
33+				Repo: repo,
34 			})
35 		}
36 	}
37@@ -108,7 +108,7 @@ func (pr PrCmd) GetRepoByID(repoID string) (*Repo, error) {
38 
39 	for _, repo := range repos {
40 		if repo.ID == repoID {
41-			return &repo, nil
42+			return repo, nil
43 		}
44 	}
45 
M ssh.go
+3, -13
 1@@ -18,17 +18,7 @@ func authHandler(ctx ssh.Context, key ssh.PublicKey) bool {
 2 	return true
 3 }
 4 
 5-func GitSshServer() {
 6-	host := os.Getenv("GIT_HOST")
 7-	if host == "" {
 8-		host = "0.0.0.0"
 9-	}
10-	port := os.Getenv("GIT_SSH_PORT")
11-	if port == "" {
12-		port = "2222"
13-	}
14-
15-	cfg := NewGitCfg()
16+func GitSshServer(cfg *GitCfg) {
17 	opts := &slog.HandlerOptions{
18 		AddSource: true,
19 	}
20@@ -59,7 +49,7 @@ func GitSshServer() {
21 
22 	s, err := wish.NewServer(
23 		wish.WithAddress(
24-			fmt.Sprintf("%s:%s", host, port),
25+			fmt.Sprintf("%s:%s", cfg.Host, cfg.SshPort),
26 		),
27 		wish.WithHostKeyPath(
28 			filepath.Join(cfg.DataPath, "term_info_ed25519"),
29@@ -76,7 +66,7 @@ func GitSshServer() {
30 
31 	done := make(chan os.Signal, 1)
32 	signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
33-	logger.Info("starting SSH server", "host", host, "port", port)
34+	logger.Info("starting SSH server", "host", cfg.Host, "port", cfg.SshPort)
35 	go func() {
36 		if err = s.ListenAndServe(); err != nil {
37 			logger.Error("serve error", "err", err)
M web.go
+2, -12
 1@@ -8,7 +8,6 @@ import (
 2 	"html/template"
 3 	"log/slog"
 4 	"net/http"
 5-	"os"
 6 	"path/filepath"
 7 	"strconv"
 8 	"time"
 9@@ -422,19 +421,10 @@ func chromaStyleHandler(w http.ResponseWriter, r *http.Request) {
10 	}
11 }
12 
13-func StartWebServer() {
14-	host := os.Getenv("GIT_HOST")
15-	if host == "" {
16-		host = "0.0.0.0"
17-	}
18-	port := os.Getenv("GIT_WEB_PORT")
19-	if port == "" {
20-		port = "3000"
21-	}
22-	addr := fmt.Sprintf("%s:%s", host, port)
23+func StartWebServer(cfg *GitCfg) {
24+	addr := fmt.Sprintf("%s:%s", cfg.Host, cfg.WebPort)
25 	logger := slog.Default()
26 
27-	cfg := NewGitCfg()
28 	dbh, err := Open(filepath.Join(cfg.DataPath, "pr.db"), logger)
29 	if err != nil {
30 		logger.Error("could not open db", "err", err)