diff --git a/.drone.yml b/.drone.yml index ea0ae69..111d9da 100644 --- a/.drone.yml +++ b/.drone.yml @@ -81,4 +81,9 @@ pipeline: strip_prefix: releases/ endpoint: https://storage.h.jonasfranz.software when: - event: [ tag ] \ No newline at end of file + event: [ tag ] + +services: + gitea: + image: jonasfranz/gitea-service + pull: true \ No newline at end of file diff --git a/Gopkg.lock b/Gopkg.lock index 0ccb00a..bcb88be 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -7,6 +7,12 @@ packages = ["gitea"] revision = "39c609e903992e25deca0e7aa2c5304fd680530f" +[[projects]] + name = "github.com/davecgh/go-spew" + packages = ["spew"] + revision = "346938d642f2ec3594ed81d874461961cd0faa76" + version = "v1.1.0" + [[projects]] name = "github.com/golang/protobuf" packages = ["proto"] @@ -25,6 +31,29 @@ packages = ["query"] revision = "53e6ce116135b80d037921a7fdd5138cf32d7a8a" +[[projects]] + name = "github.com/pmezard/go-difflib" + packages = ["difflib"] + revision = "792786c7400a136282c1664665ae0a8db921c6c2" + version = "v1.0.0" + +[[projects]] + name = "github.com/stretchr/objx" + packages = ["."] + revision = "477a77ecc69700c7cdeb1fa9e129548e1c1c393c" + version = "v0.1.1" + +[[projects]] + name = "github.com/stretchr/testify" + packages = [ + ".", + "assert", + "http", + "mock" + ] + revision = "f35b8ab0b5a2cef36673838d662e249dd9c94686" + version = "v1.2.2" + [[projects]] name = "github.com/urfave/cli" packages = ["."] @@ -66,6 +95,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "8d6cfc8199a1dde1c7b34a3771993afc73f7952a4b261f24939a63d0dce83b2e" + inputs-digest = "884bf618611f0aaad19defa64a14fa70fd2b522afe8cf99dedbfada2a9d8b88d" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 0b5da9a..7dd781b 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -12,4 +12,7 @@ [[constraint]] name = "github.com/google/go-github" - version = "15.0.0" \ No newline at end of file + version = "15.0.0" +[[constraint]] + name = "github.com/stretchr/testify" + version = "1.2.2" diff --git a/cmd/migrate.go b/cmd/migrate.go index 4b8d605..271f492 100644 --- a/cmd/migrate.go +++ b/cmd/migrate.go @@ -55,13 +55,13 @@ func migrate(c context.Context, gc *github.Client, m *migrations.Migratory, user fmt.Printf("Fetching repository %s/%s...\n", username, repo) gr, _, err := gc.Repositories.Get(c, username, repo) if err != nil { - return err + return fmt.Errorf("error while fetching repo[%s/%s]: %v", username, repo, err) } fmt.Printf("Migrating repository %s/%s...\n", username, repo) var mr *gitea.Repository if mr, err = m.Repository(gr); err != nil { - return err + return fmt.Errorf("error while migrating repo[%s/%s]: %v", username, repo, err) } fmt.Printf("Repository migrated to %s/%s\n", mr.Owner.UserName, mr.Name) @@ -82,7 +82,7 @@ func migrate(c context.Context, gc *github.Client, m *migrations.Migratory, user for { issues, resp, err := gc.Issues.ListByRepo(c, username, repo, opt) if err != nil { - return err + return fmt.Errorf("error while listing repos: %v", err) } allIssues = append(allIssues, issues...) if resp.NextPage == 0 { @@ -91,7 +91,6 @@ func migrate(c context.Context, gc *github.Client, m *migrations.Migratory, user opt.Page = resp.NextPage } fmt.Println("Migrating issues...") - //bar := p.AddBar(int64(len(issues))) for _, gi := range allIssues { fmt.Printf("Migrating #%d...\n", *gi.Number) issue, err := m.Issue(gi) @@ -110,7 +109,6 @@ func migrate(c context.Context, gc *github.Client, m *migrations.Migratory, user fmt.Print("Done!\n") } fmt.Printf("Migrated #%d...\n", *gi.Number) - //bar.Increment() } return nil diff --git a/cmd/migrate_test.go b/cmd/migrate_test.go new file mode 100644 index 0000000..0f458e6 --- /dev/null +++ b/cmd/migrate_test.go @@ -0,0 +1,23 @@ +package cmd + +import ( + "context" + "testing" + + "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/migrations" + "github.com/google/go-github/github" + "github.com/stretchr/testify/assert" +) + +// Test_migrate is an integration tests for migrate command +// using repo JonasFranzDEV/test +func Test_migrate(t *testing.T) { + assert.NoError(t, migrate( + context.Background(), + github.NewClient(nil), + migrations.DemoMigratory, + "JonasFranzDEV", + "test", + false, + )) +} diff --git a/migrations/issue.go b/migrations/issue.go index 5a216b4..ca2c9d2 100644 --- a/migrations/issue.go +++ b/migrations/issue.go @@ -96,6 +96,7 @@ func githubStateToGiteaState(ghstate *string) *string { } switch *ghstate { case "open": + fallthrough case "closed": return ghstate case "all": diff --git a/migrations/issue_test.go b/migrations/issue_test.go new file mode 100644 index 0000000..85cc69c --- /dev/null +++ b/migrations/issue_test.go @@ -0,0 +1,49 @@ +package migrations + +import ( + "testing" + + "code.gitea.io/sdk/gitea" + "github.com/google/go-github/github" + "github.com/stretchr/testify/assert" +) + +func Test_githubStateToGiteaState(t *testing.T) { + open := "open" + all := "all" + closed := "closed" + tests := map[*string]string{&all: "open", &open: "open", &closed: "closed"} + for input, exceptedResult := range tests { + actualResult := githubStateToGiteaState(input) + assert.NotNil(t, actualResult) + assert.NotEmpty(t, *actualResult) + assert.Equal(t, exceptedResult, *actualResult) + } + nilInput := "teoafweogwoe" + assert.Nil(t, githubStateToGiteaState(&nilInput)) +} + +func TestMigratory_Label(t *testing.T) { + res, err := DemoMigratory.Label(&github.Label{ + Name: github.String("testlabel"), + Color: github.String("123456"), + }) + assert.NoError(t, err) + assert.Equal(t, "123456", res.Color) + assert.Equal(t, "testlabel", res.Name) +} + +func TestMigratory_Milestone(t *testing.T) { + res, err := DemoMigratory.Milestone(&github.Milestone{ + ID: github.Int64(1), + State: github.String("open"), + Description: github.String("test milestone"), + Title: github.String("TEST"), + DueOn: &demoTime, + }) + assert.NoError(t, err) + assert.Equal(t, "TEST", res.Title) + assert.Equal(t, "test milestone", res.Description) + assert.Equal(t, demoTime.Unix(), res.Deadline.Unix()) + assert.Equal(t, gitea.StateOpen, res.State) +} diff --git a/migrations/utils.go b/migrations/utils.go new file mode 100644 index 0000000..e0f6552 --- /dev/null +++ b/migrations/utils.go @@ -0,0 +1,24 @@ +package migrations + +import ( + "time" + + "code.gitea.io/sdk/gitea" +) + +var DemoMigratory = &Migratory{ + AuthUsername: "demo", + AuthPassword: "demo", + Client: gitea.NewClient("http://gitea:3000", "8bffa364d5a4b2f18421426da0baf6ccddd16d6b"), + repository: &gitea.Repository{ + Name: "demo", + Owner: &gitea.User{ + UserName: "demo", + }, + }, + NewOwnerID: 1, + migratedMilestones: make(map[int64]int64), + migratedLabels: make(map[int64]int64), +} + +var demoTime = time.Date(2018, 01, 01, 01, 01, 01, 01, time.UTC)