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