Monorepo for Tangled tangled.org

knotserver/internal: calculate language breakdown on push

Signed-off-by: oppiliappan <me@oppi.li>

oppi.li 016633ef 318b4ee5

verified
Changed files
+34 -9
knotserver
+30 -7
knotserver/git/post_receive.go
··· 2 2 3 3 import ( 4 4 "bufio" 5 + "context" 5 6 "fmt" 6 7 "io" 7 8 "strings" 9 + "time" 8 10 9 11 "tangled.sh/tangled.sh/core/api/tangled" 10 12 ··· 46 48 } 47 49 48 50 type RefUpdateMeta struct { 49 - CommitCount CommitCount 50 - IsDefaultRef bool 51 + CommitCount CommitCount 52 + IsDefaultRef bool 53 + LangBreakdown LangBreakdown 51 54 } 52 55 53 56 type CommitCount struct { ··· 57 60 func (g *GitRepo) RefUpdateMeta(line PostReceiveLine) RefUpdateMeta { 58 61 commitCount, err := g.newCommitCount(line) 59 62 if err != nil { 60 - // TODO: non-fatal, log this 63 + // TODO: log this 61 64 } 62 65 63 66 isDefaultRef, err := g.isDefaultBranch(line) 64 67 if err != nil { 65 - // TODO: non-fatal, log this 68 + // TODO: log this 69 + } 70 + 71 + ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) 72 + defer cancel() 73 + breakdown, err := g.AnalyzeLanguages(ctx) 74 + if err != nil { 75 + // TODO: log this 66 76 } 67 77 68 78 return RefUpdateMeta{ 69 - CommitCount: commitCount, 70 - IsDefaultRef: isDefaultRef, 79 + CommitCount: commitCount, 80 + IsDefaultRef: isDefaultRef, 81 + LangBreakdown: breakdown, 71 82 } 72 83 } 73 84 ··· 135 146 }) 136 147 } 137 148 149 + var langs []*tangled.GitRefUpdate_Pair 150 + for lang, size := range m.LangBreakdown { 151 + langs = append(langs, &tangled.GitRefUpdate_Pair{ 152 + Lang: lang, 153 + Size: size, 154 + }) 155 + } 156 + langBreakdown := &tangled.GitRefUpdate_Meta_LangBreakdown{ 157 + Inputs: langs, 158 + } 159 + 138 160 return tangled.GitRefUpdate_Meta{ 139 161 CommitCount: &tangled.GitRefUpdate_Meta_CommitCount{ 140 162 ByEmail: byEmail, 141 163 }, 142 - IsDefaultRef: m.IsDefaultRef, 164 + IsDefaultRef: m.IsDefaultRef, 165 + LangBreakdown: langBreakdown, 143 166 } 144 167 }
+4 -2
knotserver/internal.go
··· 3 3 import ( 4 4 "context" 5 5 "encoding/json" 6 + "fmt" 6 7 "log/slog" 7 8 "net/http" 8 9 "path/filepath" ··· 115 116 return err 116 117 } 117 118 118 - gr, err := git.PlainOpen(repoPath) 119 + gr, err := git.Open(repoPath, line.Ref) 119 120 if err != nil { 120 - return err 121 + return fmt.Errorf("failed to open git repo at ref %s: %w", line.Ref, err) 121 122 } 122 123 123 124 meta := gr.RefUpdateMeta(line) 125 + 124 126 metaRecord := meta.AsRecord() 125 127 126 128 refUpdate := tangled.GitRefUpdate{