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/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 TestImport(t *testing.T) {
 23	author := identity.NewIdentity("Amine Hilaly", "hilalyamine@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://gitlab.com/git-bug/test/-/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://gitlab.com/git-bug/test/-/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://gitlab.com/git-bug/test/-/issues/3",
 53			bug: &bug.Snapshot{
 54				Operations: []bug.Operation{
 55					bug.NewCreateOp(author, 0, "complex issue", "initial comment", nil),
 56					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),
 57					bug.NewSetTitleOp(author, 0, "complex issue edited", "complex issue"),
 58					bug.NewSetTitleOp(author, 0, "complex issue", "complex issue edited"),
 59					bug.NewSetStatusOp(author, 0, bug.ClosedStatus),
 60					bug.NewSetStatusOp(author, 0, bug.OpenStatus),
 61					bug.NewLabelChangeOperation(author, 0, []bug.Label{"bug"}, []bug.Label{}),
 62					bug.NewLabelChangeOperation(author, 0, []bug.Label{"critical"}, []bug.Label{}),
 63					bug.NewLabelChangeOperation(author, 0, []bug.Label{}, []bug.Label{"critical"}),
 64				},
 65			},
 66		},
 67		{
 68			name: "editions",
 69			url:  "https://gitlab.com/git-bug/test/-/issues/4",
 70			bug: &bug.Snapshot{
 71				Operations: []bug.Operation{
 72					bug.NewCreateOp(author, 0, "editions", "initial comment edited", nil),
 73					bug.NewAddCommentOp(author, 0, "first comment edited", nil),
 74				},
 75			},
 76		},
 77	}
 78
 79	repo := repository.CreateTestRepo(false)
 80	defer repository.CleanupTestRepos(t, repo)
 81
 82	backend, err := cache.NewRepoCache(repo)
 83	require.NoError(t, err)
 84
 85	defer backend.Close()
 86	interrupt.RegisterCleaner(backend.Close)
 87
 88	envToken := os.Getenv("GITLAB_API_TOKEN")
 89	if envToken == "" {
 90		t.Skip("Env var GITLAB_API_TOKEN missing")
 91	}
 92
 93	projectID := os.Getenv("GITLAB_PROJECT_ID")
 94	if projectID == "" {
 95		t.Skip("Env var GITLAB_PROJECT_ID missing")
 96	}
 97
 98	login := "test-identity"
 99	author.SetMetadata(metaKeyGitlabLogin, login)
100
101	token := auth.NewToken(target, envToken)
102	token.SetMetadata(auth.MetaKeyLogin, login)
103	token.SetMetadata(auth.MetaKeyBaseURL, defaultBaseURL)
104	err = auth.Store(repo, token)
105	require.NoError(t, err)
106
107	ctx := context.Background()
108
109	importer := &gitlabImporter{}
110	err = importer.Init(ctx, backend, core.Configuration{
111		confKeyProjectID:     projectID,
112		confKeyGitlabBaseUrl: defaultBaseURL,
113		confKeyDefaultLogin:  login,
114	})
115	require.NoError(t, err)
116
117	start := time.Now()
118
119	events, err := importer.ImportAll(ctx, backend, time.Time{})
120	require.NoError(t, err)
121
122	for result := range events {
123		require.NoError(t, result.Err)
124	}
125
126	fmt.Printf("test repository imported in %f seconds\n", time.Since(start).Seconds())
127
128	require.Len(t, backend.AllBugsIds(), len(tests))
129
130	for _, tt := range tests {
131		t.Run(tt.name, func(t *testing.T) {
132			b, err := backend.ResolveBugCreateMetadata(metaKeyGitlabUrl, tt.url)
133			require.NoError(t, err)
134
135			ops := b.Snapshot().Operations
136			require.Len(t, tt.bug.Operations, len(ops))
137
138			for i, op := range tt.bug.Operations {
139
140				require.IsType(t, ops[i], op)
141
142				switch op.(type) {
143				case *bug.CreateOperation:
144					assert.Equal(t, op.(*bug.CreateOperation).Title, ops[i].(*bug.CreateOperation).Title)
145					assert.Equal(t, op.(*bug.CreateOperation).Message, ops[i].(*bug.CreateOperation).Message)
146					assert.Equal(t, op.(*bug.CreateOperation).Author.Name(), ops[i].(*bug.CreateOperation).Author.Name())
147				case *bug.SetStatusOperation:
148					assert.Equal(t, op.(*bug.SetStatusOperation).Status, ops[i].(*bug.SetStatusOperation).Status)
149					assert.Equal(t, op.(*bug.SetStatusOperation).Author.Name(), ops[i].(*bug.SetStatusOperation).Author.Name())
150				case *bug.SetTitleOperation:
151					assert.Equal(t, op.(*bug.SetTitleOperation).Was, ops[i].(*bug.SetTitleOperation).Was)
152					assert.Equal(t, op.(*bug.SetTitleOperation).Title, ops[i].(*bug.SetTitleOperation).Title)
153					assert.Equal(t, op.(*bug.SetTitleOperation).Author.Name(), ops[i].(*bug.SetTitleOperation).Author.Name())
154				case *bug.LabelChangeOperation:
155					assert.ElementsMatch(t, op.(*bug.LabelChangeOperation).Added, ops[i].(*bug.LabelChangeOperation).Added)
156					assert.ElementsMatch(t, op.(*bug.LabelChangeOperation).Removed, ops[i].(*bug.LabelChangeOperation).Removed)
157					assert.Equal(t, op.(*bug.LabelChangeOperation).Author.Name(), ops[i].(*bug.LabelChangeOperation).Author.Name())
158				case *bug.AddCommentOperation:
159					assert.Equal(t, op.(*bug.AddCommentOperation).Message, ops[i].(*bug.AddCommentOperation).Message)
160					assert.Equal(t, op.(*bug.AddCommentOperation).Author.Name(), ops[i].(*bug.AddCommentOperation).Author.Name())
161				case *bug.EditCommentOperation:
162					assert.Equal(t, op.(*bug.EditCommentOperation).Message, ops[i].(*bug.EditCommentOperation).Message)
163					assert.Equal(t, op.(*bug.EditCommentOperation).Author.Name(), ops[i].(*bug.EditCommentOperation).Author.Name())
164
165				default:
166					panic("unknown operation type")
167				}
168			}
169		})
170	}
171}