1package gitlab
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/bug"
15 "github.com/MichaelMure/git-bug/cache"
16 "github.com/MichaelMure/git-bug/identity"
17 "github.com/MichaelMure/git-bug/repository"
18 "github.com/MichaelMure/git-bug/util/interrupt"
19)
20
21func TestImport(t *testing.T) {
22 author := identity.NewIdentity("Amine Hilaly", "hilalyamine@gmail.com")
23 tests := []struct {
24 name string
25 url string
26 bug *bug.Snapshot
27 }{
28 {
29 name: "simple issue",
30 url: "https://gitlab.com/git-bug/test/issues/1",
31 bug: &bug.Snapshot{
32 Operations: []bug.Operation{
33 bug.NewCreateOp(author, 0, "simple issue", "initial comment", nil),
34 bug.NewAddCommentOp(author, 0, "first comment", nil),
35 bug.NewAddCommentOp(author, 0, "second comment", nil),
36 },
37 },
38 },
39 {
40 name: "empty issue",
41 url: "https://gitlab.com/git-bug/test/issues/2",
42 bug: &bug.Snapshot{
43 Operations: []bug.Operation{
44 bug.NewCreateOp(author, 0, "empty issue", "", nil),
45 },
46 },
47 },
48 {
49 name: "complex issue",
50 url: "https://gitlab.com/git-bug/test/issues/3",
51 bug: &bug.Snapshot{
52 Operations: []bug.Operation{
53 bug.NewCreateOp(author, 0, "complex issue", "initial comment", nil),
54 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", nil),
55 bug.NewSetTitleOp(author, 0, "complex issue edited", "complex issue"),
56 bug.NewSetTitleOp(author, 0, "complex issue", "complex issue edited"),
57 bug.NewSetStatusOp(author, 0, bug.ClosedStatus),
58 bug.NewSetStatusOp(author, 0, bug.OpenStatus),
59 bug.NewLabelChangeOperation(author, 0, []bug.Label{"bug"}, []bug.Label{}),
60 bug.NewLabelChangeOperation(author, 0, []bug.Label{"critical"}, []bug.Label{}),
61 bug.NewLabelChangeOperation(author, 0, []bug.Label{}, []bug.Label{"critical"}),
62 },
63 },
64 },
65 {
66 name: "editions",
67 url: "https://gitlab.com/git-bug/test/issues/4",
68 bug: &bug.Snapshot{
69 Operations: []bug.Operation{
70 bug.NewCreateOp(author, 0, "editions", "initial comment edited", nil),
71 bug.NewAddCommentOp(author, 0, "first comment edited", nil),
72 },
73 },
74 },
75 }
76
77 repo := repository.CreateTestRepo(false)
78 defer repository.CleanupTestRepos(t, repo)
79
80 backend, err := cache.NewRepoCache(repo)
81 require.NoError(t, err)
82
83 defer backend.Close()
84 interrupt.RegisterCleaner(backend.Close)
85
86 token := os.Getenv("GITLAB_API_TOKEN")
87 if token == "" {
88 t.Skip("Env var GITLAB_API_TOKEN missing")
89 }
90
91 projectID := os.Getenv("GITLAB_PROJECT_ID")
92 if projectID == "" {
93 t.Skip("Env var GITLAB_PROJECT_ID missing")
94 }
95
96 importer := &gitlabImporter{}
97 err = importer.Init(core.Configuration{
98 keyProjectID: projectID,
99 keyToken: token,
100 })
101 require.NoError(t, err)
102
103 ctx := context.Background()
104 start := time.Now()
105
106 events, err := importer.ImportAll(ctx, backend, time.Time{})
107 require.NoError(t, err)
108
109 for result := range events {
110 require.NoError(t, result.Err)
111 }
112
113 fmt.Printf("test repository imported in %f seconds\n", time.Since(start).Seconds())
114
115 require.Len(t, backend.AllBugsIds(), len(tests))
116
117 for _, tt := range tests {
118 t.Run(tt.name, func(t *testing.T) {
119 b, err := backend.ResolveBugCreateMetadata(keyGitlabUrl, tt.url)
120 require.NoError(t, err)
121
122 ops := b.Snapshot().Operations
123 require.Len(t, tt.bug.Operations, len(ops))
124
125 for i, op := range tt.bug.Operations {
126
127 require.IsType(t, ops[i], op)
128
129 switch op.(type) {
130 case *bug.CreateOperation:
131 assert.Equal(t, op.(*bug.CreateOperation).Title, ops[i].(*bug.CreateOperation).Title)
132 assert.Equal(t, op.(*bug.CreateOperation).Message, ops[i].(*bug.CreateOperation).Message)
133 assert.Equal(t, op.(*bug.CreateOperation).Author.Name(), ops[i].(*bug.CreateOperation).Author.Name())
134 case *bug.SetStatusOperation:
135 assert.Equal(t, op.(*bug.SetStatusOperation).Status, ops[i].(*bug.SetStatusOperation).Status)
136 assert.Equal(t, op.(*bug.SetStatusOperation).Author.Name(), ops[i].(*bug.SetStatusOperation).Author.Name())
137 case *bug.SetTitleOperation:
138 assert.Equal(t, op.(*bug.SetTitleOperation).Was, ops[i].(*bug.SetTitleOperation).Was)
139 assert.Equal(t, op.(*bug.SetTitleOperation).Title, ops[i].(*bug.SetTitleOperation).Title)
140 assert.Equal(t, op.(*bug.SetTitleOperation).Author.Name(), ops[i].(*bug.SetTitleOperation).Author.Name())
141 case *bug.LabelChangeOperation:
142 assert.ElementsMatch(t, op.(*bug.LabelChangeOperation).Added, ops[i].(*bug.LabelChangeOperation).Added)
143 assert.ElementsMatch(t, op.(*bug.LabelChangeOperation).Removed, ops[i].(*bug.LabelChangeOperation).Removed)
144 assert.Equal(t, op.(*bug.LabelChangeOperation).Author.Name(), ops[i].(*bug.LabelChangeOperation).Author.Name())
145 case *bug.AddCommentOperation:
146 assert.Equal(t, op.(*bug.AddCommentOperation).Message, ops[i].(*bug.AddCommentOperation).Message)
147 assert.Equal(t, op.(*bug.AddCommentOperation).Author.Name(), ops[i].(*bug.AddCommentOperation).Author.Name())
148 case *bug.EditCommentOperation:
149 assert.Equal(t, op.(*bug.EditCommentOperation).Message, ops[i].(*bug.EditCommentOperation).Message)
150 assert.Equal(t, op.(*bug.EditCommentOperation).Author.Name(), ops[i].(*bug.EditCommentOperation).Author.Name())
151
152 default:
153 panic("unknown operation type")
154 }
155 }
156 })
157 }
158}