repos / git-pr

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

git-pr / contrib / dev
Eric Bower  ·  2024-10-23

main.go

 1package main
 2
 3import (
 4	"flag"
 5	"fmt"
 6	"log/slog"
 7	"os"
 8	"os/signal"
 9	"syscall"
10	"time"
11
12	"github.com/picosh/git-pr"
13	"github.com/picosh/git-pr/fixtures"
14	"github.com/picosh/git-pr/util"
15)
16
17func main() {
18	cleanupFlag := flag.Bool("cleanup", true, "Clean up tmp dir after quitting (default: true)")
19	flag.Parse()
20
21	opts := &slog.HandlerOptions{
22		AddSource: true,
23	}
24	logger := slog.New(
25		slog.NewTextHandler(os.Stdout, opts),
26	)
27
28	dataDir := util.CreateTmpDir()
29	defer func() {
30		if *cleanupFlag {
31			os.RemoveAll(dataDir)
32		}
33	}()
34
35	adminKey, userKey := util.GenerateKeys()
36	cfgPath := util.CreateCfgFile(dataDir, cfgTmpl, adminKey)
37	git.LoadConfigFile(cfgPath, logger)
38	cfg := git.NewGitCfg(logger)
39
40	go git.GitSshServer(cfg, nil)
41	time.Sleep(time.Millisecond * 100)
42	go git.StartWebServer(cfg)
43
44	// Hack to wait for startup
45	time.Sleep(time.Millisecond * 100)
46
47	patch, err := fixtures.Fixtures.ReadFile("single.patch")
48	if err != nil {
49		panic(err)
50	}
51	otherPatch, err := fixtures.Fixtures.ReadFile("with-cover.patch")
52	if err != nil {
53		panic(err)
54	}
55
56	// Accepted patch
57	userKey.MustCmd(patch, "pr create test")
58	userKey.MustCmd(nil, "pr edit 1 Accepted patch")
59	adminKey.MustCmd(nil, "pr accept 1")
60
61	// Closed patch (admin)
62	userKey.MustCmd(patch, "pr create test")
63	userKey.MustCmd(nil, "pr edit 2 Closed patch (admin)")
64	adminKey.MustCmd(nil, "pr close 2")
65
66	// Closed patch (contributor)
67	userKey.MustCmd(patch, "pr create test")
68	userKey.MustCmd(nil, "pr edit 3 Closed patch (contributor)")
69	userKey.MustCmd(nil, "pr close 3")
70
71	// Reviewed patch
72	userKey.MustCmd(patch, "pr create test")
73	userKey.MustCmd(nil, "pr edit 4 Reviewed patch")
74	adminKey.MustCmd(otherPatch, "pr add --review 4")
75
76	// Accepted patch with review
77	userKey.MustCmd(patch, "pr create test")
78	userKey.MustCmd(nil, "pr edit 5 Accepted patch with review")
79	adminKey.MustCmd(otherPatch, "pr add --accept 5")
80
81	// Closed patch with review
82	userKey.MustCmd(patch, "pr create test")
83	userKey.MustCmd(nil, "pr edit 6 Closed patch with review")
84	adminKey.MustCmd(otherPatch, "pr add --close 6")
85
86	fmt.Println("time to do some testing...")
87	ch := make(chan os.Signal, 1)
88	signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
89	<-ch
90}
91
92// args: tmpdir, adminKey
93var cfgTmpl = `
94url = "localhost"
95data_dir = %q
96admins = [%q]
97time_format = "01/02/2006 15:04:05 07:00"
98create_repo = "user"`