e2e/commitrefs_test.go
112 lines · 5080 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 aliceEnv := append(append([]string{}, env...),
38 "GIT_AUTHOR_NAME=Alice", "GIT_AUTHOR_EMAIL=alice@example.test")
39 mustGit(t, dir, env, "add", ".")
40 mustGit(t, dir, aliceEnv, "commit", "-q", "-m", "repair the widget\n\nFixes #1. Related to #2 but not #99.")
41 mustGit(t, dir, env, "push", "-q", "origin", "main")
42
43 out, _, _ := inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "1", "--json")
44 if !strings.Contains(out, `"state":"closed"`) || !strings.Contains(out, "closed by commit") {
45 t.Fatalf("issue 1 not closed by commit: %s", out)
46 }
47 // The entry is a system message with a linked sha, not a user comment.
48 if !strings.Contains(out, `"author":"system"`) || !strings.Contains(out, "](/alice/app/commit/") {
49 t.Fatalf("close entry not a linked system message: %s", out)
50 }
51 if status, body := inst.get(t, "/alice/app/issues/1"); status != 200 ||
52 !strings.Contains(body, `class="syscomment"`) || !strings.Contains(body, `/alice/app/commit/`) {
53 t.Fatalf("web system message: %d", status)
54 }
55 out, _, _ = inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "2", "--json")
56 if !strings.Contains(out, `"state":"open"`) || !strings.Contains(out, "referenced in commit") ||
57 !strings.Contains(out, "repair the widget") {
58 t.Fatalf("issue 2 not referenced: %s", out)
59 }
60 // The reference names who wrote the commit, and links them when the
61 // author email is verified on an account here.
62 if !strings.Contains(out, "by [alice](/alice)") {
63 t.Fatalf("reference does not attribute the author: %s", out)
64 }
65 if status, body := inst.get(t, "/alice/app/issues/2"); status != 200 ||
66 !strings.Contains(body, `href="/alice"`) {
67 t.Fatalf("web reference does not link the author: %d\n%s", status, body)
68 }
69
70 // Commits on a feature branch do nothing until they land on the
71 // default branch via a merge — then the merge path acts exactly once.
72 mustGit(t, dir, env, "checkout", "-q", "-b", "feat")
73 os.WriteFile(filepath.Join(dir, "c.txt"), []byte("c\n"), 0o644)
74 mustGit(t, dir, env, "add", ".")
75 mustGit(t, dir, env, "commit", "-q", "-m", "finish the gadget\n\nCloses #3")
76 mustGit(t, dir, env, "push", "-q", "origin", "feat")
77 out, _, _ = inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "3", "--json")
78 if !strings.Contains(out, `"state":"open"`) {
79 t.Fatalf("branch push acted early: %s", out)
80 }
81 if _, _, code := inst.ssh(t, aliceKey, "", "mr", "create", "alice/app",
82 "--source", "feat", "--target", "main", "--title", "'gadget'"); code != 0 {
83 t.Fatal("mr create failed")
84 }
85 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "merge", "alice/app", "1"); code != 0 {
86 t.Fatalf("merge: %s", errOut)
87 }
88 out, _, _ = inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "3", "--json")
89 if !strings.Contains(out, `"state":"closed"`) || !strings.Contains(out, "closed by commit") {
90 t.Fatalf("merge did not close issue 3: %s", out)
91 }
92 // This one was authored by an address nobody has verified, so it names
93 // git's author without inventing a profile link for them.
94 if !strings.Contains(out, "by t:") || strings.Contains(out, "by [t]") {
95 t.Fatalf("unresolved author should stay plain text: %s", out)
96 }
97 if strings.Count(out, "closed by commit") != 1 {
98 t.Fatalf("duplicate close comments: %s", out)
99 }
100
101 // Pushing more commits does not re-act on already-processed shas.
102 os.WriteFile(filepath.Join(dir, "d.txt"), []byte("d\n"), 0o644)
103 mustGit(t, dir, env, "checkout", "-q", "main")
104 mustGit(t, dir, env, "pull", "-q", "origin", "main")
105 mustGit(t, dir, env, "add", ".")
106 mustGit(t, dir, env, "commit", "-q", "-m", "unrelated")
107 mustGit(t, dir, env, "push", "-q", "origin", "main")
108 out, _, _ = inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "2", "--json")
109 if strings.Count(out, "referenced in commit") != 1 {
110 t.Fatalf("reference duplicated: %s", out)
111 }
112}