aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAmine Hilaly <hilalyamine@gmail.com>2019-06-23 00:52:30 +0200
committerAmine Hilaly <hilalyamine@gmail.com>2019-06-24 21:33:15 +0200
commitfc09f2a492348f3b6013308697cbc86e15e8ab47 (patch)
treeb20f4b76d5fa612fa2f44a3ac791d5c1b50aa90b
parent12cc74e22b35d08d2d342a1b445d967e3e09c1f6 (diff)
downloadgit-bug-fc09f2a492348f3b6013308697cbc86e15e8ab47.tar.gz
[bridge/github] exporter: Add test cases
correct delete repo improve tests set user identity
-rw-r--r--bridge/github/export.go5
-rw-r--r--bridge/github/export_test.go186
2 files changed, 168 insertions, 23 deletions
diff --git a/bridge/github/export.go b/bridge/github/export.go
index 393146d6..947d5aca 100644
--- a/bridge/github/export.go
+++ b/bridge/github/export.go
@@ -35,7 +35,7 @@ type githubExporter struct {
// cache identities clients
identityClient map[string]*githubv4.Client
- // map identity with their tokens
+ // map identities with their tokens
identityToken map[string]string
// github repository ID
@@ -201,6 +201,9 @@ func (ge *githubExporter) exportBug(b *cache.BugCache, since time.Time) error {
return errors.Wrap(err, "exporting github issue")
}
+ // incr exported bugs
+ ge.exportedBugs++
+
hash, err := createOp.Hash()
if err != nil {
return errors.Wrap(err, "comment hash")
diff --git a/bridge/github/export_test.go b/bridge/github/export_test.go
index c3a835b2..b60b86d8 100644
--- a/bridge/github/export_test.go
+++ b/bridge/github/export_test.go
@@ -1,26 +1,76 @@
package github
import (
+ "bytes"
+ "encoding/json"
"fmt"
+ "math/rand"
+ "net/http"
"os"
"testing"
"time"
+ "github.com/stretchr/testify/require"
+
"github.com/MichaelMure/git-bug/bridge/core"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/repository"
"github.com/MichaelMure/git-bug/util/interrupt"
- "github.com/stretchr/testify/require"
)
+const (
+ testRepoBaseName = "git-bug-test-github-exporter"
+)
+
+// testCases creates bugs in repo cache
+func testCases(repo *cache.RepoCache, identity *cache.IdentityCache) (map[string]*cache.BugCache, error) {
+ bugs := make(map[string]*cache.BugCache)
+
+ // simple bug
+ simpleBug, err := repo.NewBugRaw(identity, time.Now().Unix(), "simple bug", "new bug", nil, nil)
+ if err != nil {
+ return nil, err
+ }
+ bugs["simple bug"] = simpleBug
+
+ /*
+ // bug with comments
+ bugWithComments, err := repo.NewBugRaw(author, time.Now().Unix(), "bug with comments", "new bug", nil, nil)
+ if err != nil {
+ return nil, err
+ }
+
+ _, err = bugWithComments.AddCommentRaw(author, time.Now().Unix(), "new comment", nil, nil)
+ if err != nil {
+ return nil, err
+ }
+ bugs["bug with comments"] = bugWithComments
+
+ // bug with label changes
+ bugLabelChange, err := repo.NewBugRaw(author, time.Now().Unix(), "bug label change", "new bug", nil, nil)
+ if err != nil {
+ return nil, err
+ }
+
+ _, _, err = bugLabelChange.ChangeLabelsRaw(author, time.Now().Unix(), []string{"bug", "core"}, nil, nil)
+ if err != nil {
+ return nil, err
+ }
+
+ _, _, err = bugLabelChange.ChangeLabelsRaw(author, time.Now().Unix(), nil, []string{"bug"}, nil)
+ if err != nil {
+ return nil, err
+ }
+ bugs["bug change label"] = bugWithComments
+ */
+ return nil, err
+}
+
func TestExporter(t *testing.T) {
- //TODO test strategy
- tests := []struct {
- name string
- }{
- {
- name: "bug creation",
- },
+ user := os.Getenv("TEST_USER")
+ token := os.Getenv("BOT_TOKEN")
+ if token == "" {
+ t.Skip("Env var GITHUB_TOKEN_PRIVATE missing")
}
repo := repository.CreateTestRepo(false)
@@ -29,18 +79,44 @@ func TestExporter(t *testing.T) {
backend, err := cache.NewRepoCache(repo)
require.NoError(t, err)
+ author, err := backend.NewIdentity("test identity", "hello@testidentity.org")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = backend.SetUserIdentity(author)
+ if err != nil {
+ t.Fatal(err)
+ }
+
defer backend.Close()
interrupt.RegisterCleaner(backend.Close)
- token := os.Getenv("GITHUB_TOKEN_PRIVATE")
- if token == "" {
- t.Skip("Env var GITHUB_TOKEN_PRIVATE missing")
+ tests, err := testCases(backend, author)
+ if err != nil {
+ t.Fatal(err)
}
+ // generate project name
+ projectName := generateRepoName()
+ fmt.Println("creating repo", projectName)
+ if err := createRepository(projectName, token); err != nil {
+ t.Fatal(err)
+ }
+
+ //
+ //
+ defer func(t *testing.T) {
+ fmt.Println("deleting repo", projectName)
+ if err := deleteRepository(projectName, user, token); err != nil {
+ t.Fatal(err)
+ }
+ }(t)
+
exporter := &githubExporter{}
err = exporter.Init(core.Configuration{
- keyOwner: "MichaelMure",
- keyProject: "git-bug-exporter-tests",
+ keyOwner: user,
+ keyProject: projectName,
keyToken: token,
})
require.NoError(t, err)
@@ -51,22 +127,88 @@ func TestExporter(t *testing.T) {
require.NoError(t, err)
fmt.Printf("test repository exported in %f seconds\n", time.Since(start).Seconds())
+ for name, tt := range tests {
+ t.Run(name, func(t *testing.T) {
+ fmt.Println(tt.Snapshot())
+ })
+ }
+}
+
+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))
+}
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
+func createRepository(project, token string) error {
+ url := fmt.Sprintf("%s/user/repos", githubV3Url)
- })
+ 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,
}
+
+ 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()
}
-func genRepoName() {}
+func deleteRepository(project, owner, token string) error {
+ url := fmt.Sprintf("%s/repos/%s/%s", githubV3Url, owner, project)
-func createRepository() {}
+ req, err := http.NewRequest("DELETE", url, nil)
+ if err != nil {
+ return err
+ }
-func deleteRepository() {}
+ // need the token for private repositories
+ req.Header.Set("Authorization", fmt.Sprintf("token %s", token))
-// verifyIssue is full
-// comments
-func verifyIssue() {
+ 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")
+ }
+ return nil
}