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