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