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