tenseleyflow/shithub / 52ab4e1

Browse files

H2/H3: sync default_branch_oid post-merge; delete dead KindPRMerge

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
52ab4e1f23e76e4abfdbce7b80edf0bcdad9b2b7
Parents
89bc712
Tree
2661f2d

4 changed files

StatusFile+-
M cmd/shithubd/worker.go 0 1
M internal/pulls/merge.go 32 11
M internal/worker/jobs/pr_jobs.go 0 35
M internal/worker/types.go 7 4
cmd/shithubd/worker.gomodified
@@ -95,7 +95,6 @@ var workerCmd = &cobra.Command{
9595
 		prDeps := jobs.PRJobsDeps{Pool: pool, RepoFS: rfs, Logger: logger}
9696
 		p.Register(worker.KindPRSynchronize, jobs.PRSynchronize(prDeps))
9797
 		p.Register(worker.KindPRMergeability, jobs.PRMergeability(prDeps))
98
-		p.Register(worker.KindPRMerge, jobs.PRMerge(prDeps))
9998
 
10099
 		return p.Run(ctx)
101100
 	},
internal/pulls/merge.gomodified
@@ -4,7 +4,6 @@ package pulls
44
 
55
 import (
66
 	"context"
7
-	"errors"
87
 	"fmt"
98
 	"strconv"
109
 	"strings"
@@ -12,12 +11,13 @@ import (
1211
 
1312
 	"github.com/jackc/pgx/v5/pgtype"
1413
 
14
+	"github.com/tenseleyFlow/shithub/internal/auth/audit"
1515
 	"github.com/tenseleyFlow/shithub/internal/checks"
16
-	"github.com/tenseleyFlow/shithub/internal/issues"
1716
 	issuesdb "github.com/tenseleyFlow/shithub/internal/issues/sqlc"
1817
 	"github.com/tenseleyFlow/shithub/internal/pulls/review"
1918
 	pullsdb "github.com/tenseleyFlow/shithub/internal/pulls/sqlc"
2019
 	repogit "github.com/tenseleyFlow/shithub/internal/repos/git"
20
+	reposdb "github.com/tenseleyFlow/shithub/internal/repos/sqlc"
2121
 	usersdb "github.com/tenseleyFlow/shithub/internal/users/sqlc"
2222
 )
2323
 
@@ -173,6 +173,25 @@ func Merge(ctx context.Context, deps Deps, p MergeParams) error {
173173
 		return err
174174
 	}
175175
 
176
+	// PerformMerge moved refs/heads/<base> on disk via update-ref,
177
+	// bypassing the post-receive hook that would normally maintain
178
+	// repos.default_branch_oid. When the merged base IS the repo's
179
+	// default branch, sync the column ourselves so the repo home view
180
+	// stays accurate. The audit caught this gap (S00-S25, H2).
181
+	rq := reposdb.New()
182
+	repo, err := rq.GetRepoByID(ctx, tx, issue.RepoID)
183
+	if err != nil {
184
+		return fmt.Errorf("merge: load repo: %w", err)
185
+	}
186
+	if repo.DefaultBranch == pr.BaseRef {
187
+		if err := rq.UpdateRepoDefaultBranchOID(ctx, tx, reposdb.UpdateRepoDefaultBranchOIDParams{
188
+			ID:               repo.ID,
189
+			DefaultBranchOid: pgtype.Text{String: mergeRes.MergedOID, Valid: true},
190
+		}); err != nil {
191
+			return fmt.Errorf("merge: update default_branch_oid: %w", err)
192
+		}
193
+	}
194
+
176195
 	// Close the issue side with state_reason=completed.
177196
 	if err := iq.SetIssueState(ctx, tx, issuesdb.SetIssueStateParams{
178197
 		ID:             p.PRID,
@@ -233,6 +252,11 @@ func Merge(ctx context.Context, deps Deps, p MergeParams) error {
233252
 		return err
234253
 	}
235254
 	committed = true
255
+	if deps.Audit != nil {
256
+		_ = deps.Audit.Record(ctx, deps.Pool, p.ActorUserID,
257
+			audit.ActionPullMerged, audit.TargetPull, p.PRID,
258
+			map[string]any{"method": p.Method, "commit": mergeRes.MergedOID})
259
+	}
236260
 	return nil
237261
 }
238262
 
@@ -273,6 +297,3 @@ func fetchCommitsForLinkScan(ctx context.Context, db pullsdb.DBTX, prID int64) [
273297
 	return out
274298
 }
275299
 
276
-// Compile-time check that issues' typed errors are still in scope so
277
-// EditPR's wrapping continues to work after refactors.
278
-var _ = errors.Is(issues.ErrEmptyTitle, issues.ErrEmptyTitle)
internal/worker/jobs/pr_jobs.gomodified
@@ -86,41 +86,6 @@ func PRMergeability(deps PRJobsDeps) worker.Handler {
8686
 	}
8787
 }
8888
 
89
-// PRMergePayload — enqueued from the POST .../merge handler. Contains
90
-// the actor + chosen method + optional subject/body overrides.
91
-type PRMergePayload struct {
92
-	PRID        int64  `json:"pr_id"`
93
-	ActorUserID int64  `json:"actor_user_id"`
94
-	Method      string `json:"method"`
95
-	Subject     string `json:"subject,omitempty"`
96
-	Body        string `json:"body,omitempty"`
97
-}
98
-
99
-// PRMerge performs the requested merge strategy and updates DB state.
100
-func PRMerge(deps PRJobsDeps) worker.Handler {
101
-	return func(ctx context.Context, raw json.RawMessage) error {
102
-		var p PRMergePayload
103
-		if err := json.Unmarshal(raw, &p); err != nil {
104
-			return worker.PoisonError(fmt.Errorf("bad payload: %w", err))
105
-		}
106
-		if p.PRID == 0 || p.Method == "" {
107
-			return worker.PoisonError(errors.New("missing pr_id or method"))
108
-		}
109
-		gitDir, err := resolveGitDirForPR(ctx, deps.Pool, deps.RepoFS, p.PRID)
110
-		if err != nil {
111
-			return err
112
-		}
113
-		return pulls.Merge(ctx, pulls.Deps{Pool: deps.Pool, Logger: deps.Logger}, pulls.MergeParams{
114
-			PRID:        p.PRID,
115
-			ActorUserID: p.ActorUserID,
116
-			GitDir:      gitDir,
117
-			Method:      p.Method,
118
-			Subject:     p.Subject,
119
-			Body:        p.Body,
120
-		})
121
-	}
122
-}
123
-
12489
 // resolveGitDirForPR turns a PR id into the bare-repo path on disk
12590
 // using the repo + owner-user lookups. Cached per-call only; the
12691
 // caller's job-level context bounds the work.
internal/worker/types.gomodified
@@ -37,12 +37,15 @@ const (
3737
 )
3838
 
3939
 // S22 pull-request kinds. Synchronize refreshes commits + files after
40
-// a head-side push; mergeability runs the merge-tree probe; merge
41
-// performs the requested strategy in a temp worktree.
40
+// a head-side push; mergeability runs the merge-tree probe.
41
+//
42
+// (No async-merge kind: the POST .../merge handler executes the merge
43
+// synchronously so the redirect lands on the merged state. If we add
44
+// async merging later — for very large repos — re-introduce a
45
+// KindPRMerge here and a matching jobs.PRMerge handler.)
4246
 const (
4347
 	KindPRSynchronize  Kind = "pr:synchronize"
4448
 	KindPRMergeability Kind = "pr:mergeability"
45
-	KindPRMerge        Kind = "pr:merge"
4649
 )
4750
 
4851
 // NotifyChannel is the Postgres LISTEN/NOTIFY channel the pool subscribes