2019-06-09 00:14:59 +03:00
|
|
|
package github
|
|
|
|
|
2019-06-21 02:09:05 +03:00
|
|
|
import (
|
2019-06-23 01:52:30 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2019-06-21 02:09:05 +03:00
|
|
|
"fmt"
|
2019-06-23 01:52:30 +03:00
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
2019-06-21 02:09:05 +03:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-06-23 01:52:30 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-06-21 02:09:05 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bridge/core"
|
2019-06-23 16:18:56 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
2019-06-21 02:09:05 +03:00
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
|
|
|
"github.com/MichaelMure/git-bug/util/interrupt"
|
|
|
|
)
|
2019-06-09 00:14:59 +03:00
|
|
|
|
2019-06-23 01:52:30 +03:00
|
|
|
const (
|
|
|
|
testRepoBaseName = "git-bug-test-github-exporter"
|
|
|
|
)
|
|
|
|
|
2019-06-23 14:50:45 +03:00
|
|
|
type testCase struct {
|
|
|
|
name string
|
|
|
|
bug *cache.BugCache
|
|
|
|
numOrOp int // number of original operations
|
|
|
|
}
|
2019-06-23 01:52:30 +03:00
|
|
|
|
2019-07-05 19:32:51 +03:00
|
|
|
func testCases(t *testing.T, repo *cache.RepoCache, identity *cache.IdentityCache) []*testCase {
|
2019-06-23 01:52:30 +03:00
|
|
|
// simple bug
|
2019-06-23 20:20:10 +03:00
|
|
|
simpleBug, _, err := repo.NewBug("simple bug", "new bug")
|
2019-07-05 19:32:51 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 01:52:30 +03:00
|
|
|
|
2019-06-23 14:50:45 +03:00
|
|
|
// bug with comments
|
2019-06-23 20:20:10 +03:00
|
|
|
bugWithComments, _, err := repo.NewBug("bug with comments", "new bug")
|
2019-07-05 19:32:51 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 01:52:30 +03:00
|
|
|
|
2019-06-23 14:50:45 +03:00
|
|
|
_, err = bugWithComments.AddComment("new comment")
|
2019-07-05 19:32:51 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 01:52:30 +03:00
|
|
|
|
2019-06-23 14:50:45 +03:00
|
|
|
// bug with label changes
|
2019-06-23 20:20:10 +03:00
|
|
|
bugLabelChange, _, err := repo.NewBug("bug label change", "new bug")
|
2019-07-05 19:32:51 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 01:52:30 +03:00
|
|
|
|
2019-06-23 14:50:45 +03:00
|
|
|
_, _, err = bugLabelChange.ChangeLabels([]string{"bug"}, nil)
|
2019-07-05 19:32:51 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 14:50:45 +03:00
|
|
|
|
|
|
|
_, _, err = bugLabelChange.ChangeLabels([]string{"core"}, nil)
|
2019-07-05 19:32:51 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 14:50:45 +03:00
|
|
|
|
|
|
|
_, _, err = bugLabelChange.ChangeLabels(nil, []string{"bug"})
|
2019-07-05 19:32:51 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 14:50:45 +03:00
|
|
|
|
|
|
|
// bug with comments editions
|
2019-06-23 20:20:10 +03:00
|
|
|
bugWithCommentEditions, createOp, err := repo.NewBug("bug with comments editions", "new bug")
|
2019-07-05 19:32:51 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 14:50:45 +03:00
|
|
|
|
2019-08-07 20:14:12 +03:00
|
|
|
_, err = bugWithCommentEditions.EditComment(createOp.ID(), "first comment edited")
|
2019-07-05 19:32:51 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 14:50:45 +03:00
|
|
|
|
|
|
|
commentOp, err := bugWithCommentEditions.AddComment("first comment")
|
2019-07-05 19:32:51 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 14:50:45 +03:00
|
|
|
|
2019-08-07 20:14:12 +03:00
|
|
|
_, err = bugWithCommentEditions.EditComment(commentOp.ID(), "first comment edited")
|
2019-07-05 19:32:51 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 14:50:45 +03:00
|
|
|
|
|
|
|
// bug status changed
|
2019-06-23 20:20:10 +03:00
|
|
|
bugStatusChanged, _, err := repo.NewBug("bug status changed", "new bug")
|
2019-07-05 19:32:51 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 14:50:45 +03:00
|
|
|
|
|
|
|
_, err = bugStatusChanged.Close()
|
2019-07-05 19:32:51 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 14:50:45 +03:00
|
|
|
|
|
|
|
_, err = bugStatusChanged.Open()
|
2019-07-05 19:32:51 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 14:50:45 +03:00
|
|
|
|
|
|
|
// bug title changed
|
2019-06-23 20:20:10 +03:00
|
|
|
bugTitleEdited, _, err := repo.NewBug("bug title edited", "new bug")
|
2019-07-05 19:32:51 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 14:50:45 +03:00
|
|
|
|
|
|
|
_, err = bugTitleEdited.SetTitle("bug title edited again")
|
2019-07-05 19:32:51 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 14:50:45 +03:00
|
|
|
|
|
|
|
return []*testCase{
|
|
|
|
&testCase{
|
|
|
|
name: "simple bug",
|
|
|
|
bug: simpleBug,
|
|
|
|
numOrOp: 1,
|
|
|
|
},
|
|
|
|
&testCase{
|
|
|
|
name: "bug with comments",
|
|
|
|
bug: bugWithComments,
|
|
|
|
numOrOp: 2,
|
|
|
|
},
|
|
|
|
&testCase{
|
|
|
|
name: "bug label change",
|
|
|
|
bug: bugLabelChange,
|
|
|
|
numOrOp: 4,
|
|
|
|
},
|
|
|
|
&testCase{
|
|
|
|
name: "bug with comment editions",
|
|
|
|
bug: bugWithCommentEditions,
|
|
|
|
numOrOp: 4,
|
|
|
|
},
|
|
|
|
&testCase{
|
|
|
|
name: "bug changed status",
|
|
|
|
bug: bugStatusChanged,
|
|
|
|
numOrOp: 3,
|
|
|
|
},
|
|
|
|
&testCase{
|
|
|
|
name: "bug title edited",
|
|
|
|
bug: bugTitleEdited,
|
|
|
|
numOrOp: 2,
|
|
|
|
},
|
2019-07-05 19:32:51 +03:00
|
|
|
}
|
2019-06-23 01:52:30 +03:00
|
|
|
}
|
|
|
|
|
2019-06-23 18:54:38 +03:00
|
|
|
func TestPushPull(t *testing.T) {
|
|
|
|
// repo owner
|
2019-06-25 02:33:48 +03:00
|
|
|
user := os.Getenv("GITHUB_TEST_USER")
|
2019-06-23 18:54:38 +03:00
|
|
|
|
|
|
|
// token must have 'repo' and 'delete_repo' scopes
|
2019-06-23 16:18:56 +03:00
|
|
|
token := os.Getenv("GITHUB_TOKEN_ADMIN")
|
2019-06-23 01:52:30 +03:00
|
|
|
if token == "" {
|
2019-06-23 16:18:56 +03:00
|
|
|
t.Skip("Env var GITHUB_TOKEN_ADMIN missing")
|
2019-06-21 02:09:05 +03:00
|
|
|
}
|
|
|
|
|
2019-06-23 18:54:38 +03:00
|
|
|
// create repo backend
|
2019-06-21 02:09:05 +03:00
|
|
|
repo := repository.CreateTestRepo(false)
|
|
|
|
defer repository.CleanupTestRepos(t, repo)
|
|
|
|
|
|
|
|
backend, err := cache.NewRepoCache(repo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-06-23 18:54:38 +03:00
|
|
|
// set author identity
|
2019-06-23 14:50:45 +03:00
|
|
|
author, err := backend.NewIdentity("test identity", "test@test.org")
|
2019-06-25 02:33:48 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 01:52:30 +03:00
|
|
|
|
|
|
|
err = backend.SetUserIdentity(author)
|
2019-06-25 02:33:48 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-23 01:52:30 +03:00
|
|
|
|
2019-06-21 02:09:05 +03:00
|
|
|
defer backend.Close()
|
|
|
|
interrupt.RegisterCleaner(backend.Close)
|
|
|
|
|
2019-07-05 19:32:51 +03:00
|
|
|
tests := testCases(t, backend, author)
|
2019-06-21 02:09:05 +03:00
|
|
|
|
2019-06-23 01:52:30 +03:00
|
|
|
// generate project name
|
|
|
|
projectName := generateRepoName()
|
2019-06-23 14:50:45 +03:00
|
|
|
|
2019-06-23 18:54:38 +03:00
|
|
|
// create target Github repository
|
2019-06-25 02:33:48 +03:00
|
|
|
err = createRepository(projectName, token)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-06-23 14:50:45 +03:00
|
|
|
fmt.Println("created repository", projectName)
|
2019-06-23 01:52:30 +03:00
|
|
|
|
2019-06-23 18:54:38 +03:00
|
|
|
// Make sure to remove the Github repository when the test end
|
2019-06-23 01:52:30 +03:00
|
|
|
defer func(t *testing.T) {
|
|
|
|
if err := deleteRepository(projectName, user, token); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-06-23 14:50:45 +03:00
|
|
|
fmt.Println("deleted repository:", projectName)
|
2019-06-23 01:52:30 +03:00
|
|
|
}(t)
|
|
|
|
|
2019-06-23 20:20:10 +03:00
|
|
|
interrupt.RegisterCleaner(func() error {
|
|
|
|
return deleteRepository(projectName, user, token)
|
|
|
|
})
|
|
|
|
|
2019-06-23 14:50:45 +03:00
|
|
|
// initialize exporter
|
2019-06-21 02:09:05 +03:00
|
|
|
exporter := &githubExporter{}
|
|
|
|
err = exporter.Init(core.Configuration{
|
2019-06-23 01:52:30 +03:00
|
|
|
keyOwner: user,
|
|
|
|
keyProject: projectName,
|
2019-06-21 02:09:05 +03:00
|
|
|
keyToken: token,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
2019-06-23 14:50:45 +03:00
|
|
|
// export all bugs
|
2019-07-05 19:32:51 +03:00
|
|
|
events, err := exporter.ExportAll(backend, time.Time{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for result := range events {
|
2019-06-25 02:33:48 +03:00
|
|
|
require.NoError(t, result.Err)
|
|
|
|
}
|
2019-06-21 02:09:05 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fmt.Printf("test repository exported in %f seconds\n", time.Since(start).Seconds())
|
2019-06-23 14:50:45 +03:00
|
|
|
|
|
|
|
repoTwo := repository.CreateTestRepo(false)
|
|
|
|
defer repository.CleanupTestRepos(t, repoTwo)
|
|
|
|
|
|
|
|
// create a second backend
|
|
|
|
backendTwo, err := cache.NewRepoCache(repoTwo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
importer := &githubImporter{}
|
|
|
|
err = importer.Init(core.Configuration{
|
|
|
|
keyOwner: user,
|
|
|
|
keyProject: projectName,
|
|
|
|
keyToken: token,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// import all exported bugs to the second backend
|
|
|
|
err = importer.ImportAll(backendTwo, time.Time{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Len(t, backendTwo.AllBugsIds(), len(tests))
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
// for each operation a SetMetadataOperation will be added
|
|
|
|
// so number of operations should double
|
|
|
|
require.Len(t, tt.bug.Snapshot().Operations, tt.numOrOp*2)
|
|
|
|
|
2019-07-05 19:32:51 +03:00
|
|
|
// verify operation have correct metadata
|
2019-06-23 16:18:56 +03:00
|
|
|
for _, op := range tt.bug.Snapshot().Operations {
|
2019-06-23 18:54:38 +03:00
|
|
|
// Check if the originals operations (*not* SetMetadata) are tagged properly
|
2019-06-23 16:18:56 +03:00
|
|
|
if _, ok := op.(*bug.SetMetadataOperation); !ok {
|
|
|
|
_, haveIDMetadata := op.GetMetadata(keyGithubId)
|
|
|
|
require.True(t, haveIDMetadata)
|
|
|
|
|
|
|
|
_, haveURLMetada := op.GetMetadata(keyGithubUrl)
|
|
|
|
require.True(t, haveURLMetada)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 18:54:38 +03:00
|
|
|
// get bug github ID
|
2019-06-23 20:20:10 +03:00
|
|
|
bugGithubID, ok := tt.bug.Snapshot().GetCreateMetadata(keyGithubId)
|
2019-06-23 14:50:45 +03:00
|
|
|
require.True(t, ok)
|
|
|
|
|
2019-07-05 19:32:51 +03:00
|
|
|
// retrieve bug from backendTwo
|
2019-06-23 14:50:45 +03:00
|
|
|
importedBug, err := backendTwo.ResolveBugCreateMetadata(keyGithubId, bugGithubID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-06-23 18:54:38 +03:00
|
|
|
// verify bug have same number of original operations
|
2019-06-23 14:50:45 +03:00
|
|
|
require.Len(t, importedBug.Snapshot().Operations, tt.numOrOp)
|
|
|
|
|
2019-06-23 18:54:38 +03:00
|
|
|
// verify bugs are taged with origin=github
|
2019-06-23 20:20:10 +03:00
|
|
|
issueOrigin, ok := importedBug.Snapshot().GetCreateMetadata(keyOrigin)
|
2019-06-23 16:18:56 +03:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, issueOrigin, target)
|
|
|
|
|
2019-06-23 18:54:38 +03:00
|
|
|
//TODO: maybe more tests to ensure bug final state
|
2019-06-23 01:52:30 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateRepoName() string {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
|
|
b := make([]rune, 8)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = letterRunes[rand.Intn(len(letterRunes))]
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s-%s", testRepoBaseName, string(b))
|
|
|
|
}
|
2019-06-21 02:09:05 +03:00
|
|
|
|
2019-06-23 18:54:38 +03:00
|
|
|
// create repository need a token with scope 'repo'
|
2019-06-23 01:52:30 +03:00
|
|
|
func createRepository(project, token string) error {
|
2019-06-23 20:20:10 +03:00
|
|
|
// This function use the V3 Github API because repository creation is not supported yet on the V4 API.
|
2019-06-23 01:52:30 +03:00
|
|
|
url := fmt.Sprintf("%s/user/repos", githubV3Url)
|
2019-06-21 02:09:05 +03:00
|
|
|
|
2019-06-23 01:52:30 +03:00
|
|
|
params := struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Private bool `json:"private"`
|
|
|
|
HasIssues bool `json:"has_issues"`
|
|
|
|
}{
|
|
|
|
Name: project,
|
|
|
|
Description: "git-bug exporter temporary test repository",
|
|
|
|
Private: true,
|
|
|
|
HasIssues: true,
|
2019-06-21 02:09:05 +03:00
|
|
|
}
|
2019-06-23 01:52:30 +03:00
|
|
|
|
|
|
|
data, err := json.Marshal(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// need the token for private repositories
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("token %s", token))
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Body.Close()
|
2019-06-21 02:09:05 +03:00
|
|
|
}
|
|
|
|
|
2019-06-23 18:54:38 +03:00
|
|
|
// delete repository need a token with scope 'delete_repo'
|
2019-06-23 01:52:30 +03:00
|
|
|
func deleteRepository(project, owner, token string) error {
|
2019-06-23 20:20:10 +03:00
|
|
|
// This function use the V3 Github API because repository removal is not supported yet on the V4 API.
|
2019-06-23 01:52:30 +03:00
|
|
|
url := fmt.Sprintf("%s/repos/%s/%s", githubV3Url, owner, project)
|
2019-06-21 02:09:05 +03:00
|
|
|
|
2019-06-23 01:52:30 +03:00
|
|
|
req, err := http.NewRequest("DELETE", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-21 02:09:05 +03:00
|
|
|
|
2019-06-23 01:52:30 +03:00
|
|
|
// need the token for private repositories
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("token %s", token))
|
2019-06-21 02:09:05 +03:00
|
|
|
|
2019-06-23 01:52:30 +03:00
|
|
|
client := &http.Client{
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusNoContent {
|
|
|
|
return fmt.Errorf("error deleting repository")
|
|
|
|
}
|
2019-06-21 02:09:05 +03:00
|
|
|
|
2019-06-23 01:52:30 +03:00
|
|
|
return nil
|
2019-06-09 00:14:59 +03:00
|
|
|
}
|