e2e/mr_test.go

76668f2f381dd69422df050a9921de7189b6eb70
gitbay/e2e/mr_test.go history · blame · raw

290 lines · 11674 bytes

  1package e2e
  2
  3import (
  4	"encoding/json"
  5	"os"
  6	"path/filepath"
  7	"strings"
  8	"testing"
  9
 10	"golang.org/x/crypto/ssh"
 11
 12	"gitbay.org/gitbay/internal/sig"
 13)
 14
 15type mrShow struct {
 16	Number    int64  `json:"number"`
 17	State     string `json:"state"`
 18	Source    string `json:"source"`
 19	TargetRef string `json:"target_ref"`
 20	HeadSHA   string `json:"head_sha"`
 21	MergedAt  string `json:"merged_at"`
 22	MergedBy  string `json:"merged_by"`
 23	ClosedAt  string `json:"closed_at"`
 24	ClosedBy  string `json:"closed_by"`
 25	Reviews   []struct {
 26		Reviewer  string `json:"reviewer"`
 27		Verdict   string `json:"verdict"`
 28		Stale     bool   `json:"stale"`
 29		CreatedAt string `json:"created_at"`
 30	} `json:"reviews"`
 31}
 32
 33func (i *instance) mrShow(t *testing.T, key, repo, n string) mrShow {
 34	t.Helper()
 35	out, errOut, code := i.ssh(t, key, "", "mr", "show", repo, n, "--json")
 36	if code != 0 {
 37		t.Fatalf("mr show: exit %d, %s", code, errOut)
 38	}
 39	var env struct {
 40		Data mrShow `json:"data"`
 41	}
 42	if err := json.Unmarshal([]byte(out), &env); err != nil {
 43		t.Fatalf("mr show JSON: %v\n%s", err, out)
 44	}
 45	return env.Data
 46}
 47
 48func TestMergeRequests(t *testing.T) {
 49	inst := startInstance(t)
 50
 51	aliceKey := inst.newKey(t, "alice")
 52	bobKey := inst.newKey(t, "bob")
 53	inst.admin(t, "admin", "user", "create", "alice",
 54		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
 55	inst.admin(t, "admin", "user", "create", "bob",
 56		"--key", bobKey+".pub", "--email", "bob@example.test", "--verified")
 57
 58	// Alice's upstream repo with an initial commit.
 59	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/lib"); code != 0 {
 60		t.Fatalf("repo create: %s", errOut)
 61	}
 62	aliceEnv := inst.gitEnv(aliceKey)
 63	aliceWork := t.TempDir()
 64	mustGit(t, aliceWork, aliceEnv, "clone", inst.sshURL("alice/lib"), "w")
 65	aliceDir := filepath.Join(aliceWork, "w")
 66	os.WriteFile(filepath.Join(aliceDir, "lib.txt"), []byte("v1\n"), 0o644)
 67	mustGit(t, aliceDir, aliceEnv, "checkout", "-q", "-b", "main")
 68	mustGit(t, aliceDir, aliceEnv, "add", ".")
 69	mustGit(t, aliceDir, aliceEnv, "commit", "-q", "-m", "base")
 70	mustGit(t, aliceDir, aliceEnv, "push", "-q", "origin", "main")
 71
 72	// Bob forks and pushes a feature branch to his fork.
 73	if _, errOut, code := inst.ssh(t, bobKey, "", "repo", "fork", "alice/lib"); code != 0 {
 74		t.Fatalf("fork: %s", errOut)
 75	}
 76	bobEnv := inst.gitEnv(bobKey)
 77	bobWork := t.TempDir()
 78	mustGit(t, bobWork, bobEnv, "clone", inst.sshURL("bob/lib"), "w")
 79	bobDir := filepath.Join(bobWork, "w")
 80	mustGit(t, bobDir, bobEnv, "checkout", "-q", "-b", "feature", "origin/main")
 81	os.WriteFile(filepath.Join(bobDir, "feature.txt"), []byte("bob's work\n"), 0o644)
 82	mustGit(t, bobDir, bobEnv, "add", ".")
 83	mustGit(t, bobDir, bobEnv, "commit", "-q", "-m", "add feature")
 84	mustGit(t, bobDir, bobEnv, "push", "-q", "origin", "feature")
 85
 86	// MR from the fork into alice/lib.
 87	out, errOut, code := inst.ssh(t, bobKey, "", "mr", "create", "alice/lib",
 88		"--source", "bob/lib:feature", "--target", "main", "--title", "'add feature'", "--json")
 89	if code != 0 {
 90		t.Fatalf("mr create: %s", errOut)
 91	}
 92	if !strings.Contains(out, `"number":1`) {
 93		t.Fatalf("mr create output: %s", out)
 94	}
 95
 96	// The MR head ref is fetchable from the TARGET repo by a reader.
 97	fetchDir := t.TempDir()
 98	mustGit(t, fetchDir, aliceEnv, "clone", "-q", inst.sshURL("alice/lib"), "c")
 99	mustGit(t, filepath.Join(fetchDir, "c"), aliceEnv, "fetch", "-q", "origin", "refs/merge-requests/1/head")
100
101	// Alice approves.
102	if _, errOut, code = inst.ssh(t, aliceKey, "", "mr", "review", "alice/lib", "1", "--approve"); code != 0 {
103		t.Fatalf("review: %s", errOut)
104	}
105	show := inst.mrShow(t, aliceKey, "alice/lib", "1")
106	if len(show.Reviews) != 1 || show.Reviews[0].Stale {
107		t.Fatalf("fresh review wrong: %+v", show.Reviews)
108	}
109	firstHead := show.HeadSHA
110
111	// Bob force-pushes the source branch: the MR head updates and the
112	// review goes stale.
113	mustGit(t, bobDir, bobEnv, "commit", "-q", "--amend", "-m", "add feature (amended)")
114	mustGit(t, bobDir, bobEnv, "push", "-q", "--force", "origin", "feature")
115	show = inst.mrShow(t, aliceKey, "alice/lib", "1")
116	if show.HeadSHA == firstHead {
117		t.Fatal("MR head not updated after force-push")
118	}
119	if len(show.Reviews) != 1 || !show.Reviews[0].Stale {
120		t.Fatalf("review not marked stale: %+v", show.Reviews)
121	}
122
123	// Target advances, so fast-forward is impossible: default merge makes a
124	// merge commit authored by the merging user.
125	mustGit(t, aliceDir, aliceEnv, "commit", "-q", "--allow-empty", "-m", "mainline moves on")
126	mustGit(t, aliceDir, aliceEnv, "push", "-q", "origin", "main")
127	out, errOut, code = inst.ssh(t, aliceKey, "", "mr", "merge", "alice/lib", "1", "--json")
128	if code != 0 {
129		t.Fatalf("merge: exit %d, %s", code, errOut)
130	}
131	if !strings.Contains(out, `"strategy":"merge"`) {
132		t.Fatalf("expected merge-commit strategy: %s", out)
133	}
134	if inst.mrShow(t, aliceKey, "alice/lib", "1").State != "merged" {
135		t.Fatal("MR not marked merged")
136	}
137	mustGit(t, aliceDir, aliceEnv, "pull", "-q", "origin", "main")
138	if _, err := os.Stat(filepath.Join(aliceDir, "feature.txt")); err != nil {
139		t.Fatal("merged content missing from main")
140	}
141	// The merge commit carries the merging user's identity and is unsigned.
142	tip := strings.TrimSpace(mustGit(t, aliceDir, aliceEnv, "log", "-1", "--format=%an <%ae>"))
143	if tip != "alice <alice@example.test>" {
144		t.Fatalf("merge commit identity: %q", tip)
145	}
146	logOut, _, _ := inst.ssh(t, aliceKey, "", "repo", "log", "alice/lib", "--limit", "1")
147	if !strings.Contains(logOut, "unsigned") {
148		t.Fatalf("merge commit should display unsigned:\n%s", logOut)
149	}
150
151	// --- require_signed_commits: push-time and merge-time policy ---
152
153	if _, errOut, code = inst.ssh(t, aliceKey, "", "repo", "create", "alice/sec"); code != 0 {
154		t.Fatalf("create sec: %s", errOut)
155	}
156	if _, errOut, code = inst.ssh(t, aliceKey, "", "repo", "settings", "require-signed", "alice/sec", "on"); code != 0 {
157		t.Fatalf("require-signed: %s", errOut)
158	}
159	secWork := t.TempDir()
160	mustGit(t, secWork, aliceEnv, "clone", inst.sshURL("alice/sec"), "w")
161	secDir := filepath.Join(secWork, "w")
162
163	// Unsigned push is rejected at pre-receive.
164	os.WriteFile(filepath.Join(secDir, "a.txt"), []byte("a\n"), 0o644)
165	mustGit(t, secDir, aliceEnv, "checkout", "-q", "-b", "main")
166	mustGit(t, secDir, aliceEnv, "add", ".")
167	mustGit(t, secDir, aliceEnv, "commit", "-q", "-m", "unsigned attempt")
168	pushOut, pushCode := gitRun(t, secDir, aliceEnv, "push", "origin", "main")
169	if pushCode == 0 {
170		t.Fatal("unsigned push accepted into require-signed repo")
171	}
172	if !strings.Contains(pushOut, "requires signed commits") {
173		t.Fatalf("unsigned push message:\n%s", pushOut)
174	}
175
176	// SSHSIG-signed commits go through.
177	raw, _ := os.ReadFile(aliceKey)
178	signer, err := ssh.ParsePrivateKey(raw)
179	if err != nil {
180		t.Fatal(err)
181	}
182	signAlice := func(p []byte) string {
183		s, err := sig.MarshalSSHSig(signer, p)
184		if err != nil {
185			t.Fatal(err)
186		}
187		return string(s)
188	}
189	buildCommits(t, secDir, aliceEnv, []commitSpec{
190		{authorEmail: "alice@example.test", subject: "signed base", sign: signAlice},
191	})
192	mustGit(t, secDir, aliceEnv, "push", "-q", "origin", "main")
193
194	// A signed feature branch and a same-repo MR.
195	base := strings.TrimSpace(mustGit(t, secDir, aliceEnv, "rev-parse", "main"))
196	tree := strings.TrimSpace(mustGit(t, secDir, aliceEnv, "rev-parse", "main^{tree}"))
197	buildChain(t, secDir, aliceEnv, tree, base, []commitSpec{
198		{authorEmail: "alice@example.test", subject: "signed feature", sign: signAlice},
199	})
200	// buildChain moved refs/heads/main; restore and use a feature branch.
201	feat := strings.TrimSpace(mustGit(t, secDir, aliceEnv, "rev-parse", "main"))
202	mustGit(t, secDir, aliceEnv, "update-ref", "refs/heads/main", base)
203	mustGit(t, secDir, aliceEnv, "update-ref", "refs/heads/feat", feat)
204	mustGit(t, secDir, aliceEnv, "push", "-q", "origin", "feat")
205
206	if _, errOut, code = inst.ssh(t, aliceKey, "", "mr", "create", "alice/sec",
207		"--source", "feat", "--target", "main", "--title", "'signed work'"); code != 0 {
208		t.Fatalf("sec mr create: %s", errOut)
209	}
210
211	// An explicit merge-commit strategy is refused with exit 4 and rebase
212	// instructions.
213	_, errOut, code = inst.ssh(t, aliceKey, "", "mr", "merge", "alice/sec", "1", "--strategy", "merge")
214	if code != 4 {
215		t.Fatalf("merge-commit on require-signed: exit %d (want 4), %s", code, errOut)
216	}
217	if !strings.Contains(errOut, "only fast-forward") || !strings.Contains(errOut, "rebase") {
218		t.Fatalf("refusal message: %s", errOut)
219	}
220
221	// Fast-forward merge of verified commits succeeds.
222	out, errOut, code = inst.ssh(t, aliceKey, "", "mr", "merge", "alice/sec", "1", "--json")
223	if code != 0 {
224		t.Fatalf("ff merge: %s", errOut)
225	}
226	if !strings.Contains(out, `"strategy":"ff"`) {
227		t.Fatalf("expected ff: %s", out)
228	}
229
230	// --- fork deletion leaves the MR diff intact ---
231
232	mustGit(t, bobDir, bobEnv, "checkout", "-q", "-b", "second", "origin/main")
233	os.WriteFile(filepath.Join(bobDir, "second.txt"), []byte("more\n"), 0o644)
234	mustGit(t, bobDir, bobEnv, "add", ".")
235	mustGit(t, bobDir, bobEnv, "commit", "-q", "-m", "second feature")
236	mustGit(t, bobDir, bobEnv, "push", "-q", "origin", "second")
237	if _, errOut, code = inst.ssh(t, bobKey, "", "mr", "create", "alice/lib",
238		"--source", "bob/lib:second", "--target", "main", "--title", "'second'"); code != 0 {
239		t.Fatalf("mr 2 create: %s", errOut)
240	}
241	if _, errOut, code = inst.ssh(t, bobKey, "", "repo", "delete", "bob/lib", "--yes"); code != 0 {
242		t.Fatalf("fork delete: %s", errOut)
243	}
244	show = inst.mrShow(t, aliceKey, "alice/lib", "2")
245	if show.State != "source_gone" {
246		t.Fatalf("MR 2 state after fork deletion: %s", show.State)
247	}
248	diffOut, errOut, code := inst.ssh(t, aliceKey, "", "mr", "diff", "alice/lib", "2")
249	if code != 0 || !strings.Contains(diffOut, "second.txt") {
250		t.Fatalf("diff after fork deletion: exit %d\n%s%s", code, diffOut, errOut)
251	}
252	// And it can still be merged: the target owns the objects.
253	if _, errOut, code = inst.ssh(t, aliceKey, "", "mr", "merge", "alice/lib", "2"); code != 0 {
254		t.Fatalf("merge after fork deletion: %s", errOut)
255	}
256
257	// Merged MRs keep their historical diff: after the fast-forward the
258	// live merge-base equals the head, so the recorded base must be used.
259	diffOut2, _, code := inst.ssh(t, aliceKey, "", "mr", "diff", "alice/lib", "1")
260	if code != 0 || !strings.Contains(diffOut2, "feature.txt") {
261		t.Fatalf("post-merge diff empty: %d\n%s", code, diffOut2)
262	}
263
264	// Web read views.
265	status, body := inst.get(t, "/alice/lib/mrs?state=all")
266	if status != 200 || !strings.Contains(body, "add feature") || !strings.Contains(body, "second") {
267		t.Fatalf("mrs page: %d\n%s", status, body)
268	}
269	status, body = inst.get(t, "/alice/lib/mrs/1")
270	if status != 200 || !strings.Contains(body, "stale") || !strings.Contains(body, "merged") {
271		t.Fatalf("mr detail: %d\n%s", status, body)
272	}
273	if _, diff := inst.get(t, "/alice/lib/mrs/1?view=diff"); !strings.Contains(diff, "feature.txt") {
274		t.Fatalf("merged MR web diff empty:\n%s", diff)
275	}
276	// The MR lists the commits it carries, linked to commit pages.
277	_, commits := inst.get(t, "/alice/lib/mrs/1?view=commits")
278	if !strings.Contains(commits, "/alice/lib/commit/") {
279		t.Fatalf("mr commits view missing:\n%s", commits)
280	}
281	// So does mr show, human and JSON.
282	showOut, _, code := inst.ssh(t, aliceKey, "", "mr", "show", "alice/lib", "1")
283	if code != 0 || !strings.Contains(showOut, "commit: ") {
284		t.Fatalf("mr show missing commits: %d\n%s", code, showOut)
285	}
286	showJSON, _, _ := inst.ssh(t, aliceKey, "", "mr", "show", "alice/lib", "1", "--json")
287	if !strings.Contains(showJSON, `"commits":[`) || !strings.Contains(showJSON, `"subject"`) {
288		t.Fatalf("mr show json missing commits: %s", showJSON)
289	}
290}