cmd/gitbayd/adminusers.go

d7d409e545d4b0999652559ac53e94ce556b56f6
gitbay/cmd/gitbayd/adminusers.go history · blame · raw

87 lines · 2422 bytes

 1package main
 2
 3import (
 4	"fmt"
 5
 6	"github.com/spf13/cobra"
 7
 8	"gitbay.org/gitbay/internal/config"
 9	"gitbay.org/gitbay/internal/control"
10	"gitbay.org/gitbay/internal/gitutil"
11)
12
13// adminMigrateCommitRefsCmd is a one-shot backfill: legacy commit-reference
14// comments (author-attributed, bare sha) become system messages with a
15// linked sha. Idempotent.
16func adminMigrateCommitRefsCmd() *cobra.Command {
17	return &cobra.Command{
18		Use:   "migrate-commit-refs",
19		Short: "convert legacy commit-reference comments into linked system messages",
20		RunE: func(cmd *cobra.Command, args []string) error {
21			cfg, err := config.Load(configPath)
22			if err != nil {
23				return err
24			}
25			st, err := openStore(cfg)
26			if err != nil {
27				return err
28			}
29			defer st.Close()
30			n, err := st.MigrateCommitRefComments()
31			if err != nil {
32				return err
33			}
34			fmt.Printf("converted %d commit-reference comment(s) to system messages\n", n)
35			return nil
36		},
37	}
38}
39
40// adminBackfillActivityCmd walks every repo's default branch and records
41// commit activity for commits authored by verified addresses. Idempotent:
42// (repo, sha) dedup makes re-runs free. Imported history keeps its real
43// author dates, so migrated repos light up their actual timeline.
44func adminBackfillActivityCmd() *cobra.Command {
45	var perRepo int
46	cmd := &cobra.Command{
47		Use:   "backfill-activity",
48		Short: "record commit activity for existing default-branch history",
49		RunE: func(cmd *cobra.Command, args []string) error {
50			cfg, err := config.Load(configPath)
51			if err != nil {
52				return err
53			}
54			st, err := openStore(cfg)
55			if err != nil {
56				return err
57			}
58			defer st.Close()
59			repos, err := st.ListAllRepos()
60			if err != nil {
61				return err
62			}
63			total := 0
64			for _, r := range repos {
65				dir := control.RepoDir(cfg.Server.Root, r.OwnerName, r.Name)
66				authors, err := gitutil.RevListAuthors(dir, "", r.DefaultBranch, perRepo)
67				if err != nil {
68					continue // empty repo or missing branch
69				}
70				n := 0
71				for _, a := range authors {
72					if uid, ok := st.UserIDByVerifiedEmail(a.Email); ok {
73						if st.RecordCommitActivity(r.ID, a.SHA, uid, a.Day) {
74							n++
75						}
76					}
77				}
78				total += n
79				fmt.Printf("%s\t%d attributed\n", r.Path(), n)
80			}
81			fmt.Printf("total\t%d commits recorded\n", total)
82			return nil
83		},
84	}
85	cmd.Flags().IntVar(&perRepo, "per-repo", 20000, "max commits walked per repository")
86	return cmd
87}