1package github
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "testing"
8 "time"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12
13 "github.com/MichaelMure/git-bug/bridge/core"
14 "github.com/MichaelMure/git-bug/bridge/core/auth"
15 "github.com/MichaelMure/git-bug/bug"
16 "github.com/MichaelMure/git-bug/cache"
17 "github.com/MichaelMure/git-bug/identity"
18 "github.com/MichaelMure/git-bug/repository"
19 "github.com/MichaelMure/git-bug/util/interrupt"
20)
21
22func Test_Importer(t *testing.T) {
23 author := identity.NewIdentity("Michael Muré", "batolettre@gmail.com")
24 tests := []struct {
25 name string
26 url string
27 bug *bug.Snapshot
28 }{
29 {
30 name: "simple issue",
31 url: "https://github.com/MichaelMure/git-bug-test-github-bridge/issues/1",
32 bug: &bug.Snapshot{
33 Operations: []bug.Operation{
34 bug.NewCreateOp(author, 0, "simple issue", "initial comment", nil),
35 bug.NewAddCommentOp(author, 0, "first comment", nil),
36 bug.NewAddCommentOp(author, 0, "second comment", nil),
37 },
38 },
39 },
40 {
41 name: "empty issue",
42 url: "https://github.com/MichaelMure/git-bug-test-github-bridge/issues/2",
43 bug: &bug.Snapshot{
44 Operations: []bug.Operation{
45 bug.NewCreateOp(author, 0, "empty issue", "", nil),
46 },
47 },
48 },
49 {
50 name: "complex issue",
51 url: "https://github.com/MichaelMure/git-bug-test-github-bridge/issues/3",
52 bug: &bug.Snapshot{
53 Operations: []bug.Operation{
54 bug.NewCreateOp(author, 0, "complex issue", "initial comment", nil),
55 bug.NewLabelChangeOperation(author, 0, []bug.Label{"bug"}, []bug.Label{}),
56 bug.NewLabelChangeOperation(author, 0, []bug.Label{"duplicate"}, []bug.Label{}),
57 bug.NewLabelChangeOperation(author, 0, []bug.Label{}, []bug.Label{"duplicate"}),
58 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),
59 bug.NewSetTitleOp(author, 0, "complex issue edited", "complex issue"),
60 bug.NewSetTitleOp(author, 0, "complex issue", "complex issue edited"),
61 bug.NewSetStatusOp(author, 0, bug.ClosedStatus),
62 bug.NewSetStatusOp(author, 0, bug.OpenStatus),
63 },
64 },
65 },
66 {
67 name: "editions",
68 url: "https://github.com/MichaelMure/git-bug-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 url: "https://github.com/MichaelMure/git-bug-test-github-bridge/issues/5",
81 bug: &bug.Snapshot{
82 Operations: []bug.Operation{
83 bug.NewCreateOp(author, 0, "comment deletion", "", nil),
84 },
85 },
86 },
87 {
88 name: "edition deletion",
89 url: "https://github.com/MichaelMure/git-bug-test-github-bridge/issues/6",
90 bug: &bug.Snapshot{
91 Operations: []bug.Operation{
92 bug.NewCreateOp(author, 0, "edition deletion", "initial comment", nil),
93 bug.NewEditCommentOp(author, 0, "", "initial comment edited again", nil),
94 bug.NewAddCommentOp(author, 0, "first comment", nil),
95 bug.NewEditCommentOp(author, 0, "", "first comment edited again", nil),
96 },
97 },
98 },
99 {
100 name: "hidden comment",
101 url: "https://github.com/MichaelMure/git-bug-test-github-bridge/issues/7",
102 bug: &bug.Snapshot{
103 Operations: []bug.Operation{
104 bug.NewCreateOp(author, 0, "hidden comment", "initial comment", nil),
105 bug.NewAddCommentOp(author, 0, "first comment", nil),
106 },
107 },
108 },
109 {
110 name: "transfered issue",
111 url: "https://github.com/MichaelMure/git-bug-test-github-bridge/issues/8",
112 bug: &bug.Snapshot{
113 Operations: []bug.Operation{
114 bug.NewCreateOp(author, 0, "transfered issue", "", nil),
115 },
116 },
117 },
118 {
119 name: "unicode control characters",
120 url: "https://github.com/MichaelMure/git-bug-test-github-bridge/issues/10",
121 bug: &bug.Snapshot{
122 Operations: []bug.Operation{
123 bug.NewCreateOp(author, 0, "unicode control characters", "u0000: \nu0001: \nu0002: \nu0003: \nu0004: \nu0005: \nu0006: \nu0007: \nu0008: \nu0009: \t\nu0010: \nu0011: \nu0012: \nu0013: \nu0014: \nu0015: \nu0016: \nu0017: \nu0018: \nu0019:", nil),
124 },
125 },
126 },
127 }
128
129 repo := repository.CreateTestRepo(false)
130 defer repository.CleanupTestRepos(t, repo)
131
132 backend, err := cache.NewRepoCache(repo)
133 require.NoError(t, err)
134
135 defer backend.Close()
136 interrupt.RegisterCleaner(backend.Close)
137
138 envToken := os.Getenv("GITHUB_TOKEN_PRIVATE")
139 if envToken == "" {
140 t.Skip("Env var GITHUB_TOKEN_PRIVATE missing")
141 }
142
143 err = author.Commit(repo)
144 require.NoError(t, err)
145
146 err = identity.SetUserIdentity(repo, author)
147 require.NoError(t, err)
148
149 token := auth.NewToken(author.Id(), envToken, target)
150 err = auth.Store(repo, token)
151 require.NoError(t, err)
152
153 importer := &githubImporter{}
154 err = importer.Init(backend, core.Configuration{
155 keyOwner: "MichaelMure",
156 keyProject: "git-bug-test-github-bridge",
157 })
158 require.NoError(t, err)
159
160 ctx := context.Background()
161 start := time.Now()
162
163 events, err := importer.ImportAll(ctx, backend, time.Time{})
164 require.NoError(t, err)
165
166 for result := range events {
167 require.NoError(t, result.Err)
168 }
169
170 fmt.Printf("test repository imported in %f seconds\n", time.Since(start).Seconds())
171
172 require.Len(t, backend.AllBugsIds(), len(tests))
173
174 for _, tt := range tests {
175 t.Run(tt.name, func(t *testing.T) {
176 b, err := backend.ResolveBugCreateMetadata(metaKeyGithubUrl, tt.url)
177 require.NoError(t, err)
178
179 ops := b.Snapshot().Operations
180 assert.Len(t, tt.bug.Operations, len(b.Snapshot().Operations))
181
182 for i, op := range tt.bug.Operations {
183 require.IsType(t, ops[i], op)
184
185 switch op.(type) {
186 case *bug.CreateOperation:
187 assert.Equal(t, op.(*bug.CreateOperation).Title, ops[i].(*bug.CreateOperation).Title)
188 assert.Equal(t, op.(*bug.CreateOperation).Message, ops[i].(*bug.CreateOperation).Message)
189 assert.Equal(t, op.(*bug.CreateOperation).Author.Name(), ops[i].(*bug.CreateOperation).Author.Name())
190 case *bug.SetStatusOperation:
191 assert.Equal(t, op.(*bug.SetStatusOperation).Status, ops[i].(*bug.SetStatusOperation).Status)
192 assert.Equal(t, op.(*bug.SetStatusOperation).Author.Name(), ops[i].(*bug.SetStatusOperation).Author.Name())
193 case *bug.SetTitleOperation:
194 assert.Equal(t, op.(*bug.SetTitleOperation).Was, ops[i].(*bug.SetTitleOperation).Was)
195 assert.Equal(t, op.(*bug.SetTitleOperation).Title, ops[i].(*bug.SetTitleOperation).Title)
196 assert.Equal(t, op.(*bug.SetTitleOperation).Author.Name(), ops[i].(*bug.SetTitleOperation).Author.Name())
197 case *bug.LabelChangeOperation:
198 assert.ElementsMatch(t, op.(*bug.LabelChangeOperation).Added, ops[i].(*bug.LabelChangeOperation).Added)
199 assert.ElementsMatch(t, op.(*bug.LabelChangeOperation).Removed, ops[i].(*bug.LabelChangeOperation).Removed)
200 assert.Equal(t, op.(*bug.LabelChangeOperation).Author.Name(), ops[i].(*bug.LabelChangeOperation).Author.Name())
201 case *bug.AddCommentOperation:
202 assert.Equal(t, op.(*bug.AddCommentOperation).Message, ops[i].(*bug.AddCommentOperation).Message)
203 assert.Equal(t, op.(*bug.AddCommentOperation).Author.Name(), ops[i].(*bug.AddCommentOperation).Author.Name())
204 case *bug.EditCommentOperation:
205 assert.Equal(t, op.(*bug.EditCommentOperation).Message, ops[i].(*bug.EditCommentOperation).Message)
206 assert.Equal(t, op.(*bug.EditCommentOperation).Author.Name(), ops[i].(*bug.EditCommentOperation).Author.Name())
207
208 default:
209 panic("unknown operation type")
210 }
211 }
212 })
213 }
214}