e2e/commentmigrate_test.go
74 lines · 2789 bytes
1package e2e
2
3import (
4 "path/filepath"
5 "strings"
6 "testing"
7
8 "gitbay.org/gitbay/internal/store"
9)
10
11func TestMigrateCommitRefComments(t *testing.T) {
12 inst := startInstance(t)
13 aliceKey := inst.newKey(t, "alice")
14 inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
15 if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
16 t.Fatal("repo create failed")
17 }
18 // Two issues; the migration should touch commit-ref comments only.
19 inst.ssh(t, aliceKey, "", "issue", "create", "alice/app", "--title", "'bug'")
20 inst.ssh(t, aliceKey, "", "issue", "create", "alice/app", "--title", "'other'")
21 // A plain human comment that must be left alone.
22 inst.ssh(t, aliceKey, "", "issue", "comment", "alice/app", "2", "--message", "'just a note'")
23
24 // Simulate a legacy commit-reference comment: author-attributed
25 // (kind='comment'), bare short sha, the pre-system-message format.
26 dbPath := filepath.Join(inst.root, "gitbay.db")
27 legacy := "referenced in commit 2e6467a72f: Audit logging, auth throttling, pack limits, user disable"
28 st, err := store.Open(dbPath)
29 if err != nil {
30 t.Fatal(err)
31 }
32 _, err = st.DB.Exec(
33 "INSERT INTO issue_comments (issue_id, author_id, body, kind) "+
34 "VALUES ((SELECT id FROM issues WHERE number=1), "+
35 "(SELECT id FROM users WHERE username='alice'), ?, 'comment')", legacy)
36 st.Close()
37 if err != nil {
38 t.Fatal(err)
39 }
40
41 // Before: it renders as a comment card from alice, no link.
42 _, body := inst.get(t, "/alice/app/issues/1")
43 if !strings.Contains(body, "referenced in commit 2e6467a72f") ||
44 strings.Contains(body, "/alice/app/commit/2e6467a72f") {
45 t.Fatal("precondition: legacy comment not seeded as expected")
46 }
47
48 out := inst.admin(t, "admin", "migrate-commit-refs")
49 if !strings.Contains(out, "converted 1 commit-reference comment") {
50 t.Fatalf("migrate output: %s", out)
51 }
52
53 // After: a system message with a linked sha.
54 _, body = inst.get(t, "/alice/app/issues/1")
55 if !strings.Contains(body, `class="syscomment"`) ||
56 !strings.Contains(body, `href="/alice/app/commit/2e6467a72f"`) {
57 t.Fatalf("comment not converted:\n%s", body)
58 }
59 show, _, _ := inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "1", "--json")
60 if !strings.Contains(show, `"author":"system"`) {
61 t.Fatalf("author not system: %s", show)
62 }
63
64 // The human comment on issue 2 is untouched.
65 out, _, _ = inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "2", "--json")
66 if !strings.Contains(out, "just a note") || strings.Contains(out, `"author":"system"`) {
67 t.Fatalf("human comment altered: %s", out)
68 }
69
70 // Re-running converts nothing (idempotent).
71 if out := inst.admin(t, "admin", "migrate-commit-refs"); !strings.Contains(out, "converted 0") {
72 t.Fatalf("re-run not idempotent: %s", out)
73 }
74}