main git-pr / contrib / dev / main.go
Eric Bower  ·  2026-02-24
  1package main
  2
  3import (
  4	"context"
  5	"flag"
  6	"fmt"
  7	"log/slog"
  8	"net/http"
  9	"os"
 10	"os/signal"
 11	"syscall"
 12	"time"
 13
 14	"github.com/picosh/git-pr"
 15	"github.com/picosh/git-pr/fixtures"
 16	"github.com/picosh/git-pr/util"
 17)
 18
 19func main() {
 20	cleanupFlag := flag.Bool("cleanup", true, "Clean up tmp dir after quitting (default: true)")
 21	flag.Parse()
 22
 23	opts := &slog.HandlerOptions{
 24		AddSource: true,
 25	}
 26	logger := slog.New(
 27		slog.NewTextHandler(os.Stdout, opts),
 28	)
 29
 30	dataDir := util.CreateTmpDir()
 31	defer func() {
 32		if *cleanupFlag {
 33			_ = os.RemoveAll(dataDir)
 34		}
 35	}()
 36
 37	adminKey, userKey := util.GenerateKeys()
 38	cfgPath := util.CreateCfgFile(dataDir, cfgTmpl, adminKey)
 39	git.LoadConfigFile(cfgPath, logger)
 40	cfg := git.NewGitCfg(logger)
 41
 42	ctx, cancel := context.WithCancel(context.Background())
 43	defer cancel()
 44	s := git.GitSshServer(ctx, cfg)
 45	go func() {
 46		_ = s.ListenAndServe()
 47	}()
 48	time.Sleep(time.Millisecond * 100)
 49	w := git.GitWebServer(cfg)
 50	addr := fmt.Sprintf("%s:%s", cfg.Host, cfg.WebPort)
 51	go func() {
 52		_ = http.ListenAndServe(addr, w)
 53	}()
 54
 55	// Hack to wait for startup
 56	time.Sleep(time.Millisecond * 100)
 57
 58	patch, err := fixtures.Fixtures.ReadFile("single.patch")
 59	if err != nil {
 60		panic(err)
 61	}
 62	otherPatch, err := fixtures.Fixtures.ReadFile("with-cover.patch")
 63	if err != nil {
 64		panic(err)
 65	}
 66	rd1, err := fixtures.Fixtures.ReadFile("a_b_reorder.patch")
 67	if err != nil {
 68		panic(err)
 69	}
 70	rd2, err := fixtures.Fixtures.ReadFile("a_c_changed_commit.patch")
 71	if err != nil {
 72		panic(err)
 73	}
 74
 75	// Accepted patch
 76	userKey.MustCmd(patch, "pr create test")
 77	userKey.MustCmd(nil, "pr edit 1 Accepted patch")
 78	adminKey.MustCmd(nil, `pr accept --comment "lgtm!" 1`)
 79
 80	// Closed patch (admin)
 81	userKey.MustCmd(patch, "pr create test")
 82	userKey.MustCmd(nil, "pr edit 2 Closed patch (admin)")
 83	adminKey.MustCmd(nil, `pr close --comment "Thanks for the effort! I think we might use PR #1 though." 2`)
 84
 85	// Closed patch (contributor)
 86	userKey.MustCmd(patch, "pr create test")
 87	userKey.MustCmd(nil, "pr edit 3 Closed patch (contributor)")
 88	userKey.MustCmd(nil, `pr close --comment "Woops, didn't mean to submit yet" 3`)
 89
 90	// Reviewed patch
 91	userKey.MustCmd(patch, "pr create test")
 92	userKey.MustCmd(nil, "pr edit 4 Reviewed patch")
 93	adminKey.MustCmd(otherPatch, "pr add --review 4")
 94
 95	// Accepted patch with review
 96	userKey.MustCmd(patch, "pr create test")
 97	userKey.MustCmd(nil, "pr edit 5 Accepted patch with review")
 98	adminKey.MustCmd(otherPatch, `pr add --accept --comment "L G T M" 5`)
 99
100	// Closed patch with review
101	userKey.MustCmd(patch, "pr create test")
102	userKey.MustCmd(nil, "pr edit 6 Closed patch with review")
103	adminKey.MustCmd(otherPatch, `pr add --close --comment "So close! I think we might try something else instead." 6`)
104
105	// Range Diff
106	userKey.MustCmd(rd1, "pr create test")
107	userKey.MustCmd(nil, "pr edit 7 Range Diff")
108	userKey.MustCmd(rd2, "pr add 7")
109
110	fmt.Println("time to do some testing...")
111	ch := make(chan os.Signal, 1)
112	signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
113	<-ch
114}
115
116// args: tmpdir, adminKey
117var cfgTmpl = `
118url = "localhost"
119data_dir = %q
120admins = [%q]
121time_format = "01/02/2006 15:04:05 07:00"
122create_repo = "user"`