1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
package github
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/MichaelMure/git-bug/bridge/core"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/repository"
"github.com/MichaelMure/git-bug/util/interrupt"
)
const (
testRepoBaseName = "git-bug-test-github-exporter"
)
type testCase struct {
name string
bug *cache.BugCache
numOrOp int // number of original operations
}
func testCases(t *testing.T, repo *cache.RepoCache, identity *cache.IdentityCache) []*testCase {
// simple bug
simpleBug, _, err := repo.NewBug("simple bug", "new bug")
require.NoError(t, err)
// bug with comments
bugWithComments, _, err := repo.NewBug("bug with comments", "new bug")
require.NoError(t, err)
_, err = bugWithComments.AddComment("new comment")
require.NoError(t, err)
// bug with label changes
bugLabelChange, _, err := repo.NewBug("bug label change", "new bug")
require.NoError(t, err)
_, _, err = bugLabelChange.ChangeLabels([]string{"bug"}, nil)
require.NoError(t, err)
_, _, err = bugLabelChange.ChangeLabels([]string{"core"}, nil)
require.NoError(t, err)
_, _, err = bugLabelChange.ChangeLabels(nil, []string{"bug"})
require.NoError(t, err)
// bug with comments editions
bugWithCommentEditions, createOp, err := repo.NewBug("bug with comments editions", "new bug")
require.NoError(t, err)
_, err = bugWithCommentEditions.EditComment(createOp.ID(), "first comment edited")
require.NoError(t, err)
commentOp, err := bugWithCommentEditions.AddComment("first comment")
require.NoError(t, err)
_, err = bugWithCommentEditions.EditComment(commentOp.ID(), "first comment edited")
require.NoError(t, err)
// bug status changed
bugStatusChanged, _, err := repo.NewBug("bug status changed", "new bug")
require.NoError(t, err)
_, err = bugStatusChanged.Close()
require.NoError(t, err)
_, err = bugStatusChanged.Open()
require.NoError(t, err)
// bug title changed
bugTitleEdited, _, err := repo.NewBug("bug title edited", "new bug")
require.NoError(t, err)
_, err = bugTitleEdited.SetTitle("bug title edited again")
require.NoError(t, err)
return []*testCase{
&testCase{
name: "simple bug",
bug: simpleBug,
numOrOp: 1,
},
&testCase{
name: "bug with comments",
bug: bugWithComments,
numOrOp: 2,
},
&testCase{
name: "bug label change",
bug: bugLabelChange,
numOrOp: 4,
},
&testCase{
name: "bug with comment editions",
bug: bugWithCommentEditions,
numOrOp: 4,
},
&testCase{
name: "bug changed status",
bug: bugStatusChanged,
numOrOp: 3,
},
&testCase{
name: "bug title edited",
bug: bugTitleEdited,
numOrOp: 2,
},
}
}
func TestPushPull(t *testing.T) {
// repo owner
user := os.Getenv("GITHUB_TEST_USER")
// token must have 'repo' and 'delete_repo' scopes
token := os.Getenv("GITHUB_TOKEN_ADMIN")
if token == "" {
t.Skip("Env var GITHUB_TOKEN_ADMIN missing")
}
// create repo backend
repo := repository.CreateTestRepo(false)
defer repository.CleanupTestRepos(t, repo)
backend, err := cache.NewRepoCache(repo)
require.NoError(t, err)
// set author identity
author, err := backend.NewIdentity("test identity", "test@test.org")
require.NoError(t, err)
err = backend.SetUserIdentity(author)
require.NoError(t, err)
defer backend.Close()
interrupt.RegisterCleaner(backend.Close)
tests := testCases(t, backend, author)
// generate project name
projectName := generateRepoName()
// create target Github repository
err = createRepository(projectName, token)
require.NoError(t, err)
fmt.Println("created repository", projectName)
// Make sure to remove the Github repository when the test end
defer func(t *testing.T) {
if err := deleteRepository(projectName, user, token); err != nil {
t.Fatal(err)
}
fmt.Println("deleted repository:", projectName)
}(t)
interrupt.RegisterCleaner(func() error {
return deleteRepository(projectName, user, token)
})
// initialize exporter
exporter := &githubExporter{}
err = exporter.Init(core.Configuration{
keyOwner: user,
keyProject: projectName,
keyToken: token,
})
require.NoError(t, err)
start := time.Now()
// export all bugs
events, err := exporter.ExportAll(backend, time.Time{})
require.NoError(t, err)
for result := range events {
require.NoError(t, result.Err)
}
require.NoError(t, err)
fmt.Printf("test repository exported in %f seconds\n", time.Since(start).Seconds())
repoTwo := repository.CreateTestRepo(false)
defer repository.CleanupTestRepos(t, repoTwo)
// create a second backend
backendTwo, err := cache.NewRepoCache(repoTwo)
require.NoError(t, err)
importer := &githubImporter{}
err = importer.Init(core.Configuration{
keyOwner: user,
keyProject: projectName,
keyToken: token,
})
require.NoError(t, err)
// import all exported bugs to the second backend
err = importer.ImportAll(backendTwo, time.Time{})
require.NoError(t, err)
require.Len(t, backendTwo.AllBugsIds(), len(tests))
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// for each operation a SetMetadataOperation will be added
// so number of operations should double
require.Len(t, tt.bug.Snapshot().Operations, tt.numOrOp*2)
// verify operation have correct metadata
for _, op := range tt.bug.Snapshot().Operations {
// Check if the originals operations (*not* SetMetadata) are tagged properly
if _, ok := op.(*bug.SetMetadataOperation); !ok {
_, haveIDMetadata := op.GetMetadata(keyGithubId)
require.True(t, haveIDMetadata)
_, haveURLMetada := op.GetMetadata(keyGithubUrl)
require.True(t, haveURLMetada)
}
}
// get bug github ID
bugGithubID, ok := tt.bug.Snapshot().GetCreateMetadata(keyGithubId)
require.True(t, ok)
// retrieve bug from backendTwo
importedBug, err := backendTwo.ResolveBugCreateMetadata(keyGithubId, bugGithubID)
require.NoError(t, err)
// verify bug have same number of original operations
require.Len(t, importedBug.Snapshot().Operations, tt.numOrOp)
// verify bugs are taged with origin=github
issueOrigin, ok := importedBug.Snapshot().GetCreateMetadata(keyOrigin)
require.True(t, ok)
require.Equal(t, issueOrigin, target)
//TODO: maybe more tests to ensure bug final state
})
}
}
func generateRepoName() string {
rand.Seed(time.Now().UnixNano())
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, 8)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return fmt.Sprintf("%s-%s", testRepoBaseName, string(b))
}
// create repository need a token with scope 'repo'
func createRepository(project, token string) error {
// This function use the V3 Github API because repository creation is not supported yet on the V4 API.
url := fmt.Sprintf("%s/user/repos", githubV3Url)
params := struct {
Name string `json:"name"`
Description string `json:"description"`
Private bool `json:"private"`
HasIssues bool `json:"has_issues"`
}{
Name: project,
Description: "git-bug exporter temporary test repository",
Private: true,
HasIssues: true,
}
data, err := json.Marshal(params)
if err != nil {
return err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil {
return err
}
// need the token for private repositories
req.Header.Set("Authorization", fmt.Sprintf("token %s", token))
client := &http.Client{
Timeout: defaultTimeout,
}
resp, err := client.Do(req)
if err != nil {
return err
}
return resp.Body.Close()
}
// delete repository need a token with scope 'delete_repo'
func deleteRepository(project, owner, token string) error {
// This function use the V3 Github API because repository removal is not supported yet on the V4 API.
url := fmt.Sprintf("%s/repos/%s/%s", githubV3Url, owner, project)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
// need the token for private repositories
req.Header.Set("Authorization", fmt.Sprintf("token %s", token))
client := &http.Client{
Timeout: defaultTimeout,
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("error deleting repository")
}
return nil
}
|