import_test.go

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