1package github
2
3import (
4 "fmt"
5 "os"
6 "testing"
7 "time"
8
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11
12 "github.com/MichaelMure/git-bug/bridge/core"
13 "github.com/MichaelMure/git-bug/bug"
14 "github.com/MichaelMure/git-bug/cache"
15 "github.com/MichaelMure/git-bug/identity"
16 "github.com/MichaelMure/git-bug/util/interrupt"
17 "github.com/MichaelMure/git-bug/util/test"
18)
19
20func Test_Importer(t *testing.T) {
21 author := identity.NewIdentity("Michael Muré", "batolettre@gmail.com")
22 tests := []struct {
23 name string
24 url string
25 bug *bug.Snapshot
26 }{
27 {
28 name: "simple issue",
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 url: "https://github.com/MichaelMure/git-but-test-github-bridge/issues/2",
40 bug: &bug.Snapshot{
41 Operations: []bug.Operation{
42 bug.NewCreateOp(author, 0, "empty issue", "", nil),
43 },
44 },
45 },
46 {
47 name: "complex issue",
48 url: "https://github.com/MichaelMure/git-but-test-github-bridge/issues/3",
49 bug: &bug.Snapshot{
50 Operations: []bug.Operation{
51 bug.NewCreateOp(author, 0, "complex issue", "initial comment", nil),
52 bug.NewLabelChangeOperation(author, 0, []bug.Label{"bug"}, []bug.Label{}),
53 bug.NewLabelChangeOperation(author, 0, []bug.Label{"duplicate"}, []bug.Label{}),
54 bug.NewLabelChangeOperation(author, 0, []bug.Label{}, []bug.Label{"duplicate"}),
55 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),
56 bug.NewSetTitleOp(author, 0, "complex issue edited", "complex issue"),
57 bug.NewSetTitleOp(author, 0, "complex issue", "complex issue edited"),
58 bug.NewSetStatusOp(author, 0, bug.ClosedStatus),
59 bug.NewSetStatusOp(author, 0, bug.OpenStatus),
60 },
61 },
62 },
63 {
64 name: "editions",
65 url: "https://github.com/MichaelMure/git-but-test-github-bridge/issues/4",
66 bug: &bug.Snapshot{
67 Operations: []bug.Operation{
68 bug.NewCreateOp(author, 0, "editions", "initial comment edited", nil),
69 bug.NewEditCommentOp(author, 0, "", "erased then edited again", nil),
70 bug.NewAddCommentOp(author, 0, "first comment", nil),
71 bug.NewEditCommentOp(author, 0, "", "first comment edited", nil),
72 },
73 },
74 },
75 {
76 name: "comment deletion",
77 url: "https://github.com/MichaelMure/git-but-test-github-bridge/issues/5",
78 bug: &bug.Snapshot{
79 Operations: []bug.Operation{
80 bug.NewCreateOp(author, 0, "comment deletion", "", nil),
81 },
82 },
83 },
84 {
85 name: "edition deletion",
86 url: "https://github.com/MichaelMure/git-but-test-github-bridge/issues/6",
87 bug: &bug.Snapshot{
88 Operations: []bug.Operation{
89 bug.NewCreateOp(author, 0, "edition deletion", "initial comment", nil),
90 bug.NewEditCommentOp(author, 0, "", "initial comment edited again", nil),
91 bug.NewAddCommentOp(author, 0, "first comment", nil),
92 bug.NewEditCommentOp(author, 0, "", "first comment edited again", nil),
93 },
94 },
95 },
96 {
97 name: "hidden comment",
98 url: "https://github.com/MichaelMure/git-but-test-github-bridge/issues/7",
99 bug: &bug.Snapshot{
100 Operations: []bug.Operation{
101 bug.NewCreateOp(author, 0, "hidden comment", "initial comment", nil),
102 bug.NewAddCommentOp(author, 0, "first comment", nil),
103 },
104 },
105 },
106 {
107 name: "transfered issue",
108 url: "https://github.com/MichaelMure/git-but-test-github-bridge/issues/8",
109 bug: &bug.Snapshot{
110 Operations: []bug.Operation{
111 bug.NewCreateOp(author, 0, "transfered issue", "", nil),
112 },
113 },
114 },
115 }
116
117 repo := test.CreateRepo(false)
118
119 backend, err := cache.NewRepoCache(repo)
120 require.NoError(t, err)
121
122 defer backend.Close()
123 interrupt.RegisterCleaner(backend.Close)
124
125 token := os.Getenv("GITHUB_TOKEN")
126 if token == "" {
127 t.Skip("Env var GITHUB_TOKEN missing")
128 }
129
130 importer := &githubImporter{}
131 err = importer.Init(core.Configuration{
132 "user": "MichaelMure",
133 "project": "git-but-test-github-bridge",
134 "token": token,
135 })
136 require.NoError(t, err)
137
138 start := time.Now()
139
140 err = importer.ImportAll(backend, time.Time{})
141 require.NoError(t, err)
142
143 fmt.Printf("test repository imported in %f seconds\n", time.Since(start).Seconds())
144
145 require.Len(t, backend.AllBugsIds(), 8)
146
147 for _, tt := range tests {
148 t.Run(tt.name, func(t *testing.T) {
149 b, err := backend.ResolveBugCreateMetadata(keyGithubUrl, tt.url)
150 require.NoError(t, err)
151
152 ops := b.Snapshot().Operations
153 assert.Len(t, tt.bug.Operations, len(b.Snapshot().Operations))
154
155 for i, op := range tt.bug.Operations {
156 assert.IsType(t, ops[i], op)
157
158 switch op.(type) {
159 case *bug.CreateOperation:
160 assert.Equal(t, ops[i].(*bug.CreateOperation).Title, op.(*bug.CreateOperation).Title)
161 assert.Equal(t, ops[i].(*bug.CreateOperation).Message, op.(*bug.CreateOperation).Message)
162 case *bug.SetStatusOperation:
163 assert.Equal(t, ops[i].(*bug.SetStatusOperation).Status, op.(*bug.SetStatusOperation).Status)
164 case *bug.SetTitleOperation:
165 assert.Equal(t, ops[i].(*bug.SetTitleOperation).Was, op.(*bug.SetTitleOperation).Was)
166 assert.Equal(t, ops[i].(*bug.SetTitleOperation).Title, op.(*bug.SetTitleOperation).Title)
167 case *bug.LabelChangeOperation:
168 assert.ElementsMatch(t, ops[i].(*bug.LabelChangeOperation).Added, op.(*bug.LabelChangeOperation).Added)
169 assert.ElementsMatch(t, ops[i].(*bug.LabelChangeOperation).Removed, op.(*bug.LabelChangeOperation).Removed)
170 case *bug.AddCommentOperation:
171 assert.Equal(t, ops[i].(*bug.AddCommentOperation).Message, op.(*bug.AddCommentOperation).Message)
172 case *bug.EditCommentOperation:
173 assert.Equal(t, ops[i].(*bug.EditCommentOperation).Message, op.(*bug.EditCommentOperation).Message)
174 default:
175 panic("Unknown operation type")
176 }
177 }
178 })
179 }
180}