1package github
  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 Test_Importer(t *testing.T) {
 22	author := identity.NewIdentity("Michael Muré", "batolettre@gmail.com")
 23	tests := []struct {
 24		name string
 25		url  string
 26		bug  *bug.Snapshot
 27	}{
 28		{
 29			name: "simple issue",
 30			url:  "https://github.com/MichaelMure/git-bug-test-github-bridge/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://github.com/MichaelMure/git-bug-test-github-bridge/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://github.com/MichaelMure/git-bug-test-github-bridge/issues/3",
 51			bug: &bug.Snapshot{
 52				Operations: []bug.Operation{
 53					bug.NewCreateOp(author, 0, "complex issue", "initial comment", nil),
 54					bug.NewLabelChangeOperation(author, 0, []bug.Label{"bug"}, []bug.Label{}),
 55					bug.NewLabelChangeOperation(author, 0, []bug.Label{"duplicate"}, []bug.Label{}),
 56					bug.NewLabelChangeOperation(author, 0, []bug.Label{}, []bug.Label{"duplicate"}),
 57					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),
 58					bug.NewSetTitleOp(author, 0, "complex issue edited", "complex issue"),
 59					bug.NewSetTitleOp(author, 0, "complex issue", "complex issue edited"),
 60					bug.NewSetStatusOp(author, 0, bug.ClosedStatus),
 61					bug.NewSetStatusOp(author, 0, bug.OpenStatus),
 62				},
 63			},
 64		},
 65		{
 66			name: "editions",
 67			url:  "https://github.com/MichaelMure/git-bug-test-github-bridge/issues/4",
 68			bug: &bug.Snapshot{
 69				Operations: []bug.Operation{
 70					bug.NewCreateOp(author, 0, "editions", "initial comment edited", nil),
 71					bug.NewEditCommentOp(author, 0, "", "erased then edited again", nil),
 72					bug.NewAddCommentOp(author, 0, "first comment", nil),
 73					bug.NewEditCommentOp(author, 0, "", "first comment edited", nil),
 74				},
 75			},
 76		},
 77		{
 78			name: "comment deletion",
 79			url:  "https://github.com/MichaelMure/git-bug-test-github-bridge/issues/5",
 80			bug: &bug.Snapshot{
 81				Operations: []bug.Operation{
 82					bug.NewCreateOp(author, 0, "comment deletion", "", nil),
 83				},
 84			},
 85		},
 86		{
 87			name: "edition deletion",
 88			url:  "https://github.com/MichaelMure/git-bug-test-github-bridge/issues/6",
 89			bug: &bug.Snapshot{
 90				Operations: []bug.Operation{
 91					bug.NewCreateOp(author, 0, "edition deletion", "initial comment", nil),
 92					bug.NewEditCommentOp(author, 0, "", "initial comment edited again", nil),
 93					bug.NewAddCommentOp(author, 0, "first comment", nil),
 94					bug.NewEditCommentOp(author, 0, "", "first comment edited again", nil),
 95				},
 96			},
 97		},
 98		{
 99			name: "hidden comment",
100			url:  "https://github.com/MichaelMure/git-bug-test-github-bridge/issues/7",
101			bug: &bug.Snapshot{
102				Operations: []bug.Operation{
103					bug.NewCreateOp(author, 0, "hidden comment", "initial comment", nil),
104					bug.NewAddCommentOp(author, 0, "first comment", nil),
105				},
106			},
107		},
108		{
109			name: "transfered issue",
110			url:  "https://github.com/MichaelMure/git-bug-test-github-bridge/issues/8",
111			bug: &bug.Snapshot{
112				Operations: []bug.Operation{
113					bug.NewCreateOp(author, 0, "transfered issue", "", nil),
114				},
115			},
116		},
117		{
118			name: "unicode control characters",
119			url:  "https://github.com/MichaelMure/git-bug-test-github-bridge/issues/10",
120			bug: &bug.Snapshot{
121				Operations: []bug.Operation{
122					bug.NewCreateOp(author, 0, "unicode control characters", "u0000: \nu0001: \nu0002: \nu0003: \nu0004: \nu0005: \nu0006: \nu0007: \nu0008: \nu0009: \t\nu0010: \nu0011: \nu0012: \nu0013: \nu0014: \nu0015: \nu0016: \nu0017: \nu0018: \nu0019:", nil),
123				},
124			},
125		},
126	}
127
128	repo := repository.CreateTestRepo(false)
129	defer repository.CleanupTestRepos(t, repo)
130
131	backend, err := cache.NewRepoCache(repo)
132	require.NoError(t, err)
133
134	defer backend.Close()
135	interrupt.RegisterCleaner(backend.Close)
136
137	token := os.Getenv("GITHUB_TOKEN_PRIVATE")
138	if token == "" {
139		t.Skip("Env var GITHUB_TOKEN_PRIVATE missing")
140	}
141
142	importer := &githubImporter{}
143	err = importer.Init(core.Configuration{
144		keyOwner:   "MichaelMure",
145		keyProject: "git-bug-test-github-bridge",
146		keyToken:   token,
147	})
148	require.NoError(t, err)
149
150	ctx := context.Background()
151	start := time.Now()
152
153	events, err := importer.ImportAll(ctx, backend, time.Time{})
154	require.NoError(t, err)
155
156	for result := range events {
157		require.NoError(t, result.Err)
158	}
159
160	fmt.Printf("test repository imported in %f seconds\n", time.Since(start).Seconds())
161
162	require.Len(t, backend.AllBugsIds(), len(tests))
163
164	for _, tt := range tests {
165		t.Run(tt.name, func(t *testing.T) {
166			b, err := backend.ResolveBugCreateMetadata(metaKeyGithubUrl, tt.url)
167			require.NoError(t, err)
168
169			ops := b.Snapshot().Operations
170			assert.Len(t, tt.bug.Operations, len(b.Snapshot().Operations))
171
172			for i, op := range tt.bug.Operations {
173				require.IsType(t, ops[i], op)
174
175				switch op.(type) {
176				case *bug.CreateOperation:
177					assert.Equal(t, op.(*bug.CreateOperation).Title, ops[i].(*bug.CreateOperation).Title)
178					assert.Equal(t, op.(*bug.CreateOperation).Message, ops[i].(*bug.CreateOperation).Message)
179					assert.Equal(t, op.(*bug.CreateOperation).Author.Name(), ops[i].(*bug.CreateOperation).Author.Name())
180				case *bug.SetStatusOperation:
181					assert.Equal(t, op.(*bug.SetStatusOperation).Status, ops[i].(*bug.SetStatusOperation).Status)
182					assert.Equal(t, op.(*bug.SetStatusOperation).Author.Name(), ops[i].(*bug.SetStatusOperation).Author.Name())
183				case *bug.SetTitleOperation:
184					assert.Equal(t, op.(*bug.SetTitleOperation).Was, ops[i].(*bug.SetTitleOperation).Was)
185					assert.Equal(t, op.(*bug.SetTitleOperation).Title, ops[i].(*bug.SetTitleOperation).Title)
186					assert.Equal(t, op.(*bug.SetTitleOperation).Author.Name(), ops[i].(*bug.SetTitleOperation).Author.Name())
187				case *bug.LabelChangeOperation:
188					assert.ElementsMatch(t, op.(*bug.LabelChangeOperation).Added, ops[i].(*bug.LabelChangeOperation).Added)
189					assert.ElementsMatch(t, op.(*bug.LabelChangeOperation).Removed, ops[i].(*bug.LabelChangeOperation).Removed)
190					assert.Equal(t, op.(*bug.LabelChangeOperation).Author.Name(), ops[i].(*bug.LabelChangeOperation).Author.Name())
191				case *bug.AddCommentOperation:
192					assert.Equal(t, op.(*bug.AddCommentOperation).Message, ops[i].(*bug.AddCommentOperation).Message)
193					assert.Equal(t, op.(*bug.AddCommentOperation).Author.Name(), ops[i].(*bug.AddCommentOperation).Author.Name())
194				case *bug.EditCommentOperation:
195					assert.Equal(t, op.(*bug.EditCommentOperation).Message, ops[i].(*bug.EditCommentOperation).Message)
196					assert.Equal(t, op.(*bug.EditCommentOperation).Author.Name(), ops[i].(*bug.EditCommentOperation).Author.Name())
197
198				default:
199					panic("unknown operation type")
200				}
201			}
202		})
203	}
204}