e2e/commitrefs_test.go
96 lines · 4256 bytes
1package e2e
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10func TestCommitMessageIssueActions(t *testing.T) {
11 inst := startInstance(t)
12 aliceKey := inst.newKey(t, "alice")
13 inst.admin(t, "admin", "user", "create", "alice",
14 "--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
15
16 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
17 t.Fatalf("repo create: %s", errOut)
18 }
19 work := t.TempDir()
20 env := inst.gitEnv(aliceKey)
21 mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
22 dir := filepath.Join(work, "w")
23 os.WriteFile(filepath.Join(dir, "a.txt"), []byte("a\n"), 0o644)
24 mustGit(t, dir, env, "checkout", "-q", "-b", "main")
25 mustGit(t, dir, env, "add", ".")
26 mustGit(t, dir, env, "commit", "-q", "-m", "base")
27 mustGit(t, dir, env, "push", "-q", "origin", "main")
28 for _, title := range []string{"'one'", "'two'", "'three'"} {
29 if _, _, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/app", "--title", title); code != 0 {
30 t.Fatal("issue create failed")
31 }
32 }
33
34 // A closing keyword on the default branch closes the issue with a
35 // comment; a bare reference (and a nonexistent #99) only comments.
36 os.WriteFile(filepath.Join(dir, "b.txt"), []byte("b\n"), 0o644)
37 mustGit(t, dir, env, "add", ".")
38 mustGit(t, dir, env, "commit", "-q", "-m", "repair the widget\n\nFixes #1. Related to #2 but not #99.")
39 mustGit(t, dir, env, "push", "-q", "origin", "main")
40
41 out, _, _ := inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "1", "--json")
42 if !strings.Contains(out, `"state":"closed"`) || !strings.Contains(out, "closed by commit") {
43 t.Fatalf("issue 1 not closed by commit: %s", out)
44 }
45 // The entry is a system message with a linked sha, not a user comment.
46 if !strings.Contains(out, `"author":"system"`) || !strings.Contains(out, "](/alice/app/commit/") {
47 t.Fatalf("close entry not a linked system message: %s", out)
48 }
49 if status, body := inst.get(t, "/alice/app/issues/1"); status != 200 ||
50 !strings.Contains(body, `class="syscomment"`) || !strings.Contains(body, `/alice/app/commit/`) {
51 t.Fatalf("web system message: %d", status)
52 }
53 out, _, _ = inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "2", "--json")
54 if !strings.Contains(out, `"state":"open"`) || !strings.Contains(out, "referenced in commit") ||
55 !strings.Contains(out, "repair the widget") {
56 t.Fatalf("issue 2 not referenced: %s", out)
57 }
58
59 // Commits on a feature branch do nothing until they land on the
60 // default branch via a merge — then the merge path acts exactly once.
61 mustGit(t, dir, env, "checkout", "-q", "-b", "feat")
62 os.WriteFile(filepath.Join(dir, "c.txt"), []byte("c\n"), 0o644)
63 mustGit(t, dir, env, "add", ".")
64 mustGit(t, dir, env, "commit", "-q", "-m", "finish the gadget\n\nCloses #3")
65 mustGit(t, dir, env, "push", "-q", "origin", "feat")
66 out, _, _ = inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "3", "--json")
67 if !strings.Contains(out, `"state":"open"`) {
68 t.Fatalf("branch push acted early: %s", out)
69 }
70 if _, _, code := inst.ssh(t, aliceKey, "", "mr", "create", "alice/app",
71 "--source", "feat", "--target", "main", "--title", "'gadget'"); code != 0 {
72 t.Fatal("mr create failed")
73 }
74 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "merge", "alice/app", "1"); code != 0 {
75 t.Fatalf("merge: %s", errOut)
76 }
77 out, _, _ = inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "3", "--json")
78 if !strings.Contains(out, `"state":"closed"`) || !strings.Contains(out, "closed by commit") {
79 t.Fatalf("merge did not close issue 3: %s", out)
80 }
81 if strings.Count(out, "closed by commit") != 1 {
82 t.Fatalf("duplicate close comments: %s", out)
83 }
84
85 // Pushing more commits does not re-act on already-processed shas.
86 os.WriteFile(filepath.Join(dir, "d.txt"), []byte("d\n"), 0o644)
87 mustGit(t, dir, env, "checkout", "-q", "main")
88 mustGit(t, dir, env, "pull", "-q", "origin", "main")
89 mustGit(t, dir, env, "add", ".")
90 mustGit(t, dir, env, "commit", "-q", "-m", "unrelated")
91 mustGit(t, dir, env, "push", "-q", "origin", "main")
92 out, _, _ = inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "2", "--json")
93 if strings.Count(out, "referenced in commit") != 1 {
94 t.Fatalf("reference duplicated: %s", out)
95 }
96}