knotserver/internal: calculate language breakdown on push #315

merged
opened by oppi.li targeting master from push-snktzuwttuvu
Changed files
+34 -9
knotserver
+30 -7
knotserver/git/post_receive.go
··· 2 3 import ( 4 "bufio" 5 "fmt" 6 "io" 7 "strings" 8 9 "tangled.sh/tangled.sh/core/api/tangled" 10 ··· 46 } 47 48 type RefUpdateMeta struct { 49 - CommitCount CommitCount 50 - IsDefaultRef bool 51 } 52 53 type CommitCount struct { ··· 57 func (g *GitRepo) RefUpdateMeta(line PostReceiveLine) RefUpdateMeta { 58 commitCount, err := g.newCommitCount(line) 59 if err != nil { 60 - // TODO: non-fatal, log this 61 } 62 63 isDefaultRef, err := g.isDefaultBranch(line) 64 if err != nil { 65 - // TODO: non-fatal, log this 66 } 67 68 return RefUpdateMeta{ 69 - CommitCount: commitCount, 70 - IsDefaultRef: isDefaultRef, 71 } 72 } 73 ··· 135 }) 136 } 137 138 return tangled.GitRefUpdate_Meta{ 139 CommitCount: &tangled.GitRefUpdate_Meta_CommitCount{ 140 ByEmail: byEmail, 141 }, 142 - IsDefaultRef: m.IsDefaultRef, 143 } 144 }
··· 2 3 import ( 4 "bufio" 5 + "context" 6 "fmt" 7 "io" 8 "strings" 9 + "time" 10 11 "tangled.sh/tangled.sh/core/api/tangled" 12 ··· 48 } 49 50 type RefUpdateMeta struct { 51 + CommitCount CommitCount 52 + IsDefaultRef bool 53 + LangBreakdown LangBreakdown 54 } 55 56 type CommitCount struct { ··· 60 func (g *GitRepo) RefUpdateMeta(line PostReceiveLine) RefUpdateMeta { 61 commitCount, err := g.newCommitCount(line) 62 if err != nil { 63 + // TODO: log this 64 } 65 66 isDefaultRef, err := g.isDefaultBranch(line) 67 if err != nil { 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 76 } 77 78 return RefUpdateMeta{ 79 + CommitCount: commitCount, 80 + IsDefaultRef: isDefaultRef, 81 + LangBreakdown: breakdown, 82 } 83 } 84 ··· 146 }) 147 } 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 + 160 return tangled.GitRefUpdate_Meta{ 161 CommitCount: &tangled.GitRefUpdate_Meta_CommitCount{ 162 ByEmail: byEmail, 163 }, 164 + IsDefaultRef: m.IsDefaultRef, 165 + LangBreakdown: langBreakdown, 166 } 167 }
+4 -2
knotserver/internal.go
··· 3 import ( 4 "context" 5 "encoding/json" 6 "log/slog" 7 "net/http" 8 "path/filepath" ··· 115 return err 116 } 117 118 - gr, err := git.PlainOpen(repoPath) 119 if err != nil { 120 - return err 121 } 122 123 meta := gr.RefUpdateMeta(line) 124 metaRecord := meta.AsRecord() 125 126 refUpdate := tangled.GitRefUpdate{
··· 3 import ( 4 "context" 5 "encoding/json" 6 + "fmt" 7 "log/slog" 8 "net/http" 9 "path/filepath" ··· 116 return err 117 } 118 119 + gr, err := git.Open(repoPath, line.Ref) 120 if err != nil { 121 + return fmt.Errorf("failed to open git repo at ref %s: %w", line.Ref, err) 122 } 123 124 meta := gr.RefUpdateMeta(line) 125 + 126 metaRecord := meta.AsRecord() 127 128 refUpdate := tangled.GitRefUpdate{