1package github
2
3import (
4 "os"
5 "testing"
6 "time"
7
8 "github.com/stretchr/testify/assert"
9
10 "github.com/MichaelMure/git-bug/bridge/core"
11 "github.com/MichaelMure/git-bug/bug"
12 "github.com/MichaelMure/git-bug/cache"
13 "github.com/MichaelMure/git-bug/identity"
14 "github.com/MichaelMure/git-bug/repository"
15 "github.com/MichaelMure/git-bug/util/interrupt"
16)
17
18func Test_Importer(t *testing.T) {
19 author := identity.NewIdentity("Michael Muré", "batolettre@gmail.com")
20 tests := []struct {
21 name string
22 exist bool
23 url string
24 bug *bug.Snapshot
25 }{
26 {
27 name: "simple issue",
28 exist: true,
29 url: "https://github.com/MichaelMure/git-but-test-github-bridge/issues/1",
30 bug: &bug.Snapshot{
31 Operations: []bug.Operation{
32 bug.NewCreateOp(author, 0, "simple issue", "initial comment", nil),
33 bug.NewAddCommentOp(author, 0, "first comment", nil),
34 bug.NewAddCommentOp(author, 0, "second comment", nil)},
35 },
36 },
37 {
38 name: "empty issue",
39 exist: true,
40 url: "https://github.com/MichaelMure/git-but-test-github-bridge/issues/2",
41 bug: &bug.Snapshot{
42 Operations: []bug.Operation{
43 bug.NewCreateOp(author, 0, "empty issue", "", nil),
44 },
45 },
46 },
47 {
48 name: "complex issue",
49 exist: true,
50 url: "https://github.com/MichaelMure/git-but-test-github-bridge/issues/3",
51 bug: &bug.Snapshot{
52 Operations: []bug.Operation{
53 bug.NewCreateOp(author, 0, "complex issue", "initial comment", nil),
54 bug.NewLabelChangeOperation(author, 0, []bug.Label{"bug"}, []bug.Label{}),
55 bug.NewLabelChangeOperation(author, 0, []bug.Label{"duplicate"}, []bug.Label{}),
56 bug.NewLabelChangeOperation(author, 0, []bug.Label{}, []bug.Label{"duplicate"}),
57 bug.NewAddCommentOp(author, 0, "### header\n\n**bold**\n\n_italic_\n\n> with quote\n\n`inline code`\n\n```\nmultiline code\n```\n\n- bulleted\n- list\n\n1. numbered\n1. list\n\n- [ ] task\n- [x] list\n\n@MichaelMure mention\n\n#2 reference issue\n#3 auto-reference issue\n\n", nil),
58 bug.NewSetTitleOp(author, 0, "complex issue edited", "complex issue"),
59 bug.NewSetTitleOp(author, 0, "complex issue", "complex issue edited"),
60 bug.NewSetStatusOp(author, 0, bug.ClosedStatus),
61 bug.NewSetStatusOp(author, 0, bug.OpenStatus),
62 },
63 },
64 },
65 {
66 name: "editions",
67 exist: true,
68 url: "https://github.com/MichaelMure/git-but-test-github-bridge/issues/4",
69 bug: &bug.Snapshot{
70 Operations: []bug.Operation{
71 bug.NewCreateOp(author, 0, "editions", "initial comment edited", nil),
72 bug.NewEditCommentOp(author, 0, "", "erased then edited again", nil),
73 bug.NewAddCommentOp(author, 0, "first comment", nil),
74 bug.NewEditCommentOp(author, 0, "", "first comment edited", nil),
75 },
76 },
77 },
78 {
79 name: "comment deletion",
80 exist: true,
81 url: "https://github.com/MichaelMure/git-but-test-github-bridge/issues/5",
82 bug: &bug.Snapshot{
83 Operations: []bug.Operation{
84 bug.NewCreateOp(author, 0, "comment deletion", "", nil),
85 },
86 },
87 },
88 {
89 name: "edition deletion",
90 exist: true,
91 url: "https://github.com/MichaelMure/git-but-test-github-bridge/issues/6",
92 bug: &bug.Snapshot{
93 Operations: []bug.Operation{
94 bug.NewCreateOp(author, 0, "edition deletion", "initial comment", nil),
95 bug.NewEditCommentOp(author, 0, "", "initial comment edited again", nil),
96 bug.NewAddCommentOp(author, 0, "first comment", nil),
97 bug.NewEditCommentOp(author, 0, "", "first comment edited again", nil),
98 },
99 },
100 },
101 {
102 name: "hidden comment",
103 exist: true,
104 url: "https://github.com/MichaelMure/git-but-test-github-bridge/issues/7",
105 bug: &bug.Snapshot{
106 Operations: []bug.Operation{
107 bug.NewCreateOp(author, 0, "hidden comment", "initial comment", nil),
108 bug.NewAddCommentOp(author, 0, "first comment", nil),
109 },
110 },
111 },
112 {
113 name: "transfered issue",
114 exist: true,
115 url: "https://github.com/MichaelMure/git-but-test-github-bridge/issues/8",
116 bug: &bug.Snapshot{
117 Operations: []bug.Operation{
118 bug.NewCreateOp(author, 0, "transfered issue", "", nil),
119 },
120 },
121 },
122 }
123
124 cwd, err := os.Getwd()
125 if err != nil {
126 t.Fatal(err)
127 }
128
129 repo, err := repository.NewGitRepo(cwd, bug.Witnesser)
130 if err != nil {
131 t.Fatal(err)
132 }
133
134 backend, err := cache.NewRepoCache(repo)
135 if err != nil {
136 t.Fatal(err)
137 }
138
139 defer backend.Close()
140 interrupt.RegisterCleaner(backend.Close)
141
142 importer := &githubImporter{}
143 err = importer.Init(core.Configuration{
144 "user": "MichaelMure",
145 "project": "git-but-test-github-bridge",
146 "token": os.Getenv("GITHUB_TOKEN"),
147 })
148 if err != nil {
149 t.Fatal(err)
150 }
151
152 err = importer.ImportAll(backend, time.Time{})
153 if err != nil {
154 t.Fatal(err)
155 }
156
157 ids := backend.AllBugsIds()
158 assert.Equal(t, len(ids), 8)
159
160 for _, tt := range tests {
161 t.Run(tt.name, func(t *testing.T) {
162 b, err := backend.ResolveBugCreateMetadata(keyGithubUrl, tt.url)
163 if err != nil {
164 t.Fatal(err)
165 }
166
167 ops := b.Snapshot().Operations
168 if tt.exist {
169 assert.Equal(t, len(tt.bug.Operations), len(b.Snapshot().Operations))
170
171 for i, op := range tt.bug.Operations {
172 switch op.(type) {
173 case *bug.CreateOperation:
174 if op2, ok := ops[i].(*bug.CreateOperation); ok {
175 assert.Equal(t, op2.Title, op.(*bug.CreateOperation).Title)
176 assert.Equal(t, op2.Message, op.(*bug.CreateOperation).Message)
177 continue
178 }
179 t.Errorf("bad operation type index = %d expected = CreationOperation", i)
180 case *bug.SetStatusOperation:
181 if op2, ok := ops[i].(*bug.SetStatusOperation); ok {
182 assert.Equal(t, op2.Status, op.(*bug.SetStatusOperation).Status)
183 continue
184 }
185 t.Errorf("bad operation type index = %d expected = SetStatusOperation", i)
186 case *bug.SetTitleOperation:
187 if op2, ok := ops[i].(*bug.SetTitleOperation); ok {
188 assert.Equal(t, op.(*bug.SetTitleOperation).Was, op2.Was)
189 assert.Equal(t, op.(*bug.SetTitleOperation).Title, op2.Title)
190 continue
191 }
192 t.Errorf("bad operation type index = %d expected = SetTitleOperation", i)
193 case *bug.LabelChangeOperation:
194 if op2, ok := ops[i].(*bug.LabelChangeOperation); ok {
195 assert.ElementsMatch(t, op.(*bug.LabelChangeOperation).Added, op2.Added)
196 assert.ElementsMatch(t, op.(*bug.LabelChangeOperation).Removed, op2.Removed)
197 continue
198 }
199 t.Errorf("bad operation type index = %d expected = ChangeLabelOperation", i)
200 case *bug.AddCommentOperation:
201 if op2, ok := ops[i].(*bug.AddCommentOperation); ok {
202 assert.Equal(t, op.(*bug.AddCommentOperation).Message, op2.Message)
203 continue
204 }
205 t.Errorf("bad operation type index = %d expected = AddCommentOperation", i)
206 case *bug.EditCommentOperation:
207 if op2, ok := ops[i].(*bug.EditCommentOperation); ok {
208 assert.Equal(t, op.(*bug.EditCommentOperation).Message, op2.Message)
209 continue
210 }
211 t.Errorf("bad operation type index = %d expected = EditCommentOperation", i)
212 default:
213
214 }
215 }
216
217 } else {
218 assert.Equal(t, b, nil)
219 }
220 })
221 }
222
223}