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/util/interrupt"
 17	"github.com/MichaelMure/git-bug/util/test"
 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 := test.CreateRepo(false)
128
129	backend, err := cache.NewRepoCache(repo)
130	require.NoError(t, err)
131
132	defer backend.Close()
133	interrupt.RegisterCleaner(backend.Close)
134
135	token := os.Getenv("GITHUB_TOKEN")
136	if token == "" {
137		t.Skip("Env var GITHUB_TOKEN missing")
138	}
139
140	importer := &githubImporter{}
141	err = importer.Init(core.Configuration{
142		"user":    "MichaelMure",
143		"project": "git-bug-test-github-bridge",
144		"token":   token,
145	})
146	require.NoError(t, err)
147
148	start := time.Now()
149
150	err = importer.ImportAll(backend, time.Time{})
151	require.NoError(t, err)
152
153	fmt.Printf("test repository imported in %f seconds\n", time.Since(start).Seconds())
154
155	require.Len(t, backend.AllBugsIds(), 9)
156
157	for _, tt := range tests {
158		t.Run(tt.name, func(t *testing.T) {
159			b, err := backend.ResolveBugCreateMetadata(keyGithubUrl, tt.url)
160			require.NoError(t, err)
161
162			ops := b.Snapshot().Operations
163			assert.Len(t, tt.bug.Operations, len(b.Snapshot().Operations))
164
165			for i, op := range tt.bug.Operations {
166				require.IsType(t, ops[i], op)
167
168				switch op.(type) {
169				case *bug.CreateOperation:
170					assert.Equal(t, op.(*bug.CreateOperation).Title, ops[i].(*bug.CreateOperation).Title)
171					assert.Equal(t, op.(*bug.CreateOperation).Message, ops[i].(*bug.CreateOperation).Message)
172					assert.Equal(t, op.(*bug.CreateOperation).Author.Name(), ops[i].(*bug.CreateOperation).Author.Name())
173				case *bug.SetStatusOperation:
174					assert.Equal(t, op.(*bug.SetStatusOperation).Status, ops[i].(*bug.SetStatusOperation).Status)
175					assert.Equal(t, op.(*bug.SetStatusOperation).Author.Name(), ops[i].(*bug.SetStatusOperation).Author.Name())
176				case *bug.SetTitleOperation:
177					assert.Equal(t, op.(*bug.SetTitleOperation).Was, ops[i].(*bug.SetTitleOperation).Was)
178					assert.Equal(t, op.(*bug.SetTitleOperation).Title, ops[i].(*bug.SetTitleOperation).Title)
179					assert.Equal(t, op.(*bug.SetTitleOperation).Author.Name(), ops[i].(*bug.SetTitleOperation).Author.Name())
180				case *bug.LabelChangeOperation:
181					assert.ElementsMatch(t, op.(*bug.LabelChangeOperation).Added, ops[i].(*bug.LabelChangeOperation).Added)
182					assert.ElementsMatch(t, op.(*bug.LabelChangeOperation).Removed, ops[i].(*bug.LabelChangeOperation).Removed)
183					assert.Equal(t, op.(*bug.LabelChangeOperation).Author.Name(), ops[i].(*bug.LabelChangeOperation).Author.Name())
184				case *bug.AddCommentOperation:
185					assert.Equal(t, op.(*bug.AddCommentOperation).Message, ops[i].(*bug.AddCommentOperation).Message)
186					assert.Equal(t, op.(*bug.AddCommentOperation).Author.Name(), ops[i].(*bug.AddCommentOperation).Author.Name())
187				case *bug.EditCommentOperation:
188					assert.Equal(t, op.(*bug.EditCommentOperation).Message, ops[i].(*bug.EditCommentOperation).Message)
189					assert.Equal(t, op.(*bug.EditCommentOperation).Author.Name(), ops[i].(*bug.EditCommentOperation).Author.Name())
190
191				default:
192					panic("Unknown operation type")
193				}
194			}
195		})
196	}
197}