Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

knotserver: refactor replyCompare, and minor bugfix

knotservers do not respond with the compare link when pushing a tag.

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

authored by

oppiliappan and committed by
Tangled
5f091e74 57907d0f

+84 -48
+84 -48
knotserver/internal.go
··· 27 27 ) 28 28 29 29 type InternalHandle struct { 30 - db *db.DB 31 - c *config.Config 32 - e *rbac.Enforcer 33 - l *slog.Logger 34 - n *notifier.Notifier 30 + db *db.DB 31 + c *config.Config 32 + e *rbac.Enforcer 33 + l *slog.Logger 34 + n *notifier.Notifier 35 + res *idresolver.Resolver 35 36 } 36 37 37 38 func (h *InternalHandle) PushAllowed(w http.ResponseWriter, r *http.Request) { ··· 122 121 // non-fatal 123 122 } 124 123 125 - if (line.NewSha.String() != line.OldSha.String()) && line.OldSha.IsZero() { 126 - msg, err := h.replyCompare(line, repoDid, gitRelativeDir, repoName, r.Context()) 127 - if err != nil { 128 - l.Error("failed to reply with compare link", "err", err, "line", line, "did", gitUserDid, "repo", gitRelativeDir) 129 - // non-fatal 130 - } else { 131 - for msgLine := range msg { 132 - resp.Messages = append(resp.Messages, msg[msgLine]) 133 - } 134 - } 124 + err = h.emitCompareLink(&resp.Messages, line, repoDid, repoName) 125 + if err != nil { 126 + l.Error("failed to reply with compare link", "err", err, "line", line, "did", gitUserDid, "repo", gitRelativeDir) 127 + // non-fatal 135 128 } 136 129 137 130 err = h.triggerPipeline(&resp.Messages, line, gitUserDid, repoDid, repoName, pushOptions) ··· 136 141 } 137 142 138 143 writeJSON(w, resp) 139 - } 140 - 141 - func (h *InternalHandle) replyCompare(line git.PostReceiveLine, repoOwner string, gitRelativeDir string, repoName string, ctx context.Context) ([]string, error) { 142 - l := h.l.With("handler", "replyCompare") 143 - userIdent, err := idresolver.DefaultResolver().ResolveIdent(ctx, repoOwner) 144 - user := repoOwner 145 - if err != nil { 146 - l.Error("Failed to fetch user identity", "err", err) 147 - // non-fatal 148 - } else { 149 - user = userIdent.Handle.String() 150 - } 151 - gr, err := git.PlainOpen(gitRelativeDir) 152 - if err != nil { 153 - l.Error("Failed to open git repository", "err", err) 154 - return []string{}, err 155 - } 156 - defaultBranch, err := gr.FindMainBranch() 157 - if err != nil { 158 - l.Error("Failed to fetch default branch", "err", err) 159 - return []string{}, err 160 - } 161 - if line.Ref == plumbing.NewBranchReferenceName(defaultBranch).String() { 162 - return []string{}, nil 163 - } 164 - ZWS := "\u200B" 165 - var msg []string 166 - msg = append(msg, ZWS) 167 - msg = append(msg, fmt.Sprintf("Create a PR pointing to %s", defaultBranch)) 168 - msg = append(msg, fmt.Sprintf("\t%s/%s/%s/compare/%s...%s", h.c.AppViewEndpoint, user, repoName, defaultBranch, strings.TrimPrefix(line.Ref, "refs/heads/"))) 169 - msg = append(msg, ZWS) 170 - return msg, nil 171 144 } 172 145 173 146 func (h *InternalHandle) insertRefUpdate(line git.PostReceiveLine, gitUserDid, repoDid, repoName string) error { ··· 183 220 return errors.Join(errs, h.db.InsertEvent(event, h.n)) 184 221 } 185 222 186 - func (h *InternalHandle) triggerPipeline(clientMsgs *[]string, line git.PostReceiveLine, gitUserDid, repoDid, repoName string, pushOptions PushOptions) error { 223 + func (h *InternalHandle) triggerPipeline( 224 + clientMsgs *[]string, 225 + line git.PostReceiveLine, 226 + gitUserDid string, 227 + repoDid string, 228 + repoName string, 229 + pushOptions PushOptions, 230 + ) error { 187 231 if pushOptions.skipCi { 188 232 return nil 189 233 } ··· 285 315 return h.db.InsertEvent(event, h.n) 286 316 } 287 317 318 + func (h *InternalHandle) emitCompareLink( 319 + clientMsgs *[]string, 320 + line git.PostReceiveLine, 321 + repoDid string, 322 + repoName string, 323 + ) error { 324 + // this is a second push to a branch, don't reply with the link again 325 + if !line.OldSha.IsZero() { 326 + return nil 327 + } 328 + 329 + // the ref was not updated to a new hash, don't reply with the link 330 + // 331 + // NOTE: do we need this? 332 + if line.NewSha.String() == line.OldSha.String() { 333 + return nil 334 + } 335 + 336 + pushedRef := plumbing.ReferenceName(line.Ref) 337 + 338 + userIdent, err := h.res.ResolveIdent(context.Background(), repoDid) 339 + user := repoDid 340 + if err == nil { 341 + user = userIdent.Handle.String() 342 + } 343 + 344 + didSlashRepo, err := securejoin.SecureJoin(repoDid, repoName) 345 + if err != nil { 346 + return err 347 + } 348 + 349 + repoPath, err := securejoin.SecureJoin(h.c.Repo.ScanPath, didSlashRepo) 350 + if err != nil { 351 + return err 352 + } 353 + 354 + gr, err := git.PlainOpen(repoPath) 355 + if err != nil { 356 + return err 357 + } 358 + 359 + defaultBranch, err := gr.FindMainBranch() 360 + if err != nil { 361 + return err 362 + } 363 + 364 + // pushing to default branch 365 + if pushedRef == plumbing.NewBranchReferenceName(defaultBranch) { 366 + return nil 367 + } 368 + 369 + // pushing a tag, don't prompt the user the open a PR 370 + if pushedRef.IsTag() { 371 + return nil 372 + } 373 + 374 + ZWS := "\u200B" 375 + *clientMsgs = append(*clientMsgs, ZWS) 376 + *clientMsgs = append(*clientMsgs, fmt.Sprintf("Create a PR pointing to %s", defaultBranch)) 377 + *clientMsgs = append(*clientMsgs, fmt.Sprintf("\t%s/%s/%s/compare/%s...%s", h.c.AppViewEndpoint, user, repoName, defaultBranch, strings.TrimPrefix(line.Ref, "refs/heads/"))) 378 + *clientMsgs = append(*clientMsgs, ZWS) 379 + return nil 380 + } 381 + 288 382 func Internal(ctx context.Context, c *config.Config, db *db.DB, e *rbac.Enforcer, n *notifier.Notifier) http.Handler { 289 383 r := chi.NewRouter() 290 384 l := log.FromContext(ctx) 291 385 l = log.SubLogger(l, "internal") 386 + res := idresolver.DefaultResolver() 292 387 293 388 h := InternalHandle{ 294 389 db, ··· 361 326 e, 362 327 l, 363 328 n, 329 + res, 364 330 } 365 331 366 332 r.Get("/push-allowed", h.PushAllowed)