From 87de9fc8752785eede1037376f5908667b03c82d Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Mon, 19 Mar 2018 10:50:44 +0100 Subject: [PATCH] Add drone test Fix lint problems Fix import order Signed-off-by: Jonas Franz --- .drone.yml | 15 +++++++++--- cmd/flags.go | 37 ++++++++++++++++++++++++++++ cmd/migrate-all.go | 10 +++++--- cmd/migrate.go | 54 +++++++++-------------------------------- main.go | 2 +- migrations/issue.go | 7 +++++- migrations/migratory.go | 1 + migrations/repo.go | 1 + 8 files changed, 76 insertions(+), 51 deletions(-) create mode 100644 cmd/flags.go diff --git a/.drone.yml b/.drone.yml index 6a86dfa..688aae2 100644 --- a/.drone.yml +++ b/.drone.yml @@ -17,19 +17,28 @@ pipeline: commands: - go get -u github.com/golang/dep/cmd/dep - dep ensure + test: + image: golang:1.10 + pull: true + enviroment: + GOPATH: /go + commands: + - go get -u golang.org/x/lint/golint + - golint -set_exit_status $(go list ./...) + - go vet ./... + - go test -cover ./... static: image: karalabe/xgo-latest:latest pull: true environment: GOPATH: /go commands: - - mkdir releases - go get -u github.com/karalabe/xgo - - xgo -dest releases -out gitea-github-migrator -ldflags "-X main.version=${DRONE_TAG##v} -X main.build=${DRONE_BUILD_NUMBER}" --targets=windows/*,darwin/*,linux/* . + - xgo -out gitea-github-migrator -ldflags "-X main.version=${DRONE_TAG##v} -X main.build=${DRONE_BUILD_NUMBER}" --targets=windows/*,darwin/*,linux/* . when: event: [ tag ] gitea: - image: plugins/gitea-release:1 + image: plugins/gitea-release:latest pull: true base_url: "https://git.jonasfranz.software" secrets: [ gitea_token ] diff --git a/cmd/flags.go b/cmd/flags.go new file mode 100644 index 0000000..2b18827 --- /dev/null +++ b/cmd/flags.go @@ -0,0 +1,37 @@ +package cmd + +import "github.com/urfave/cli" + +var defaultMigrateFlags = []cli.Flag{ + cli.IntFlag{ + Name: "owner", + Usage: "Owner ID", + EnvVar: "OWNER_ID", + Value: 0, + }, + cli.StringFlag{ + Name: "token", + Usage: "Gitea Token", + EnvVar: "GITEA_TOKEN", + }, + cli.StringFlag{ + Name: "gh-token", + Usage: "GitHub Token (optional)", + EnvVar: "GITHUB_TOKEN", + }, + cli.StringFlag{ + Name: "url", + Usage: "Gitea URL", + EnvVar: "GITEA_URL", + }, + cli.BoolFlag{ + Name: "private", + Usage: "should new repository be private", + EnvVar: "GITEA_PRIVATE", + }, + cli.BoolFlag{ + Name: "only-repo", + Usage: "skip issues etc. and only migrate repo", + EnvVar: "ONLY_REPO", + }, +} diff --git a/cmd/migrate-all.go b/cmd/migrate-all.go index 3fb34a9..de3d840 100644 --- a/cmd/migrate-all.go +++ b/cmd/migrate-all.go @@ -1,21 +1,23 @@ package cmd import ( - "code.gitea.io/sdk/gitea" "context" "fmt" + "sync" + + "code.gitea.io/sdk/gitea" "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/migrations" "github.com/google/go-github/github" "github.com/urfave/cli" "golang.org/x/oauth2" - "sync" ) +// CmdMigrateAll is a command to migrate all repositories of an user var CmdMigrateAll = cli.Command{ Name: "migrate-all", Usage: "migrates all repositories of an user from github to a gitea repository", Action: runMigrateAll, - Flags: append(migrateFlags, + Flags: append(defaultMigrateFlags, cli.StringFlag{ Name: "gh-user", EnvVar: "GH_USER", @@ -66,7 +68,7 @@ func runMigrateAll(ctx *cli.Context) error { for _, repo := range allRepos { go func(r *github.Repository) { defer wg.Done() - errs <- migrate(gc, c, m, r.Owner.GetLogin(), r.GetName(), ctx.Bool("only-repo")) + errs <- migrate(c, gc, m, r.Owner.GetLogin(), r.GetName(), ctx.Bool("only-repo")) }(repo) } diff --git a/cmd/migrate.go b/cmd/migrate.go index 23a91aa..4b8d605 100644 --- a/cmd/migrate.go +++ b/cmd/migrate.go @@ -1,55 +1,23 @@ package cmd import ( - "code.gitea.io/sdk/gitea" "context" "fmt" + "strings" + + "code.gitea.io/sdk/gitea" "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/migrations" "github.com/google/go-github/github" "github.com/urfave/cli" "golang.org/x/oauth2" - "strings" ) -var migrateFlags = []cli.Flag{ - cli.IntFlag{ - Name: "owner", - Usage: "Owner ID", - EnvVar: "OWNER_ID", - Value: 0, - }, - cli.StringFlag{ - Name: "token", - Usage: "Gitea Token", - EnvVar: "GITEA_TOKEN", - }, - cli.StringFlag{ - Name: "gh-token", - Usage: "GitHub Token (optional)", - EnvVar: "GITHUB_TOKEN", - }, - cli.StringFlag{ - Name: "url", - Usage: "Gitea URL", - EnvVar: "GITEA_URL", - }, - cli.BoolFlag{ - Name: "private", - Usage: "should new repository be private", - EnvVar: "GITEA_PRIVATE", - }, - cli.BoolFlag{ - Name: "only-repo", - Usage: "skip issues etc. and only migrate repo", - EnvVar: "ONLY_REPO", - }, -} - +// CmdMigrate migrates a given repository to gitea var CmdMigrate = cli.Command{ Name: "migrate", Usage: "migrates a github to a gitea repository", Action: runMigrate, - Flags: append(migrateFlags, + Flags: append(defaultMigrateFlags, cli.StringFlag{ Name: "gh-repo", Usage: "GitHub Repository", @@ -80,21 +48,23 @@ func runMigrate(ctx *cli.Context) error { username := strings.Split(ctx.String("gh-repo"), "/")[0] repo := strings.Split(ctx.String("gh-repo"), "/")[1] - return migrate(gc, c, m, username, repo, ctx.Bool("only-repo")) + return migrate(c, gc, m, username, repo, ctx.Bool("only-repo")) } -func migrate(gc *github.Client, c context.Context, m *migrations.Migratory, username, repo string, onlyRepo bool) error { +func migrate(c context.Context, gc *github.Client, m *migrations.Migratory, username, repo string, onlyRepo bool) error { fmt.Printf("Fetching repository %s/%s...\n", username, repo) gr, _, err := gc.Repositories.Get(c, username, repo) if err != nil { return err } + fmt.Printf("Migrating repository %s/%s...\n", username, repo) - if mr, err := m.Repository(gr); err != nil { + var mr *gitea.Repository + if mr, err = m.Repository(gr); err != nil { return err - } else { - fmt.Printf("Repository migrated to %s/%s\n", mr.Owner.UserName, mr.Name) } + fmt.Printf("Repository migrated to %s/%s\n", mr.Owner.UserName, mr.Name) + if onlyRepo { return nil } diff --git a/main.go b/main.go index aee1c44..ec14e33 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,11 @@ package main import ( + "fmt" "os" "github.com/urfave/cli" - "fmt" "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/cmd" ) diff --git a/migrations/issue.go b/migrations/issue.go index 996729c..5a216b4 100644 --- a/migrations/issue.go +++ b/migrations/issue.go @@ -1,11 +1,13 @@ package migrations import ( - "code.gitea.io/sdk/gitea" "fmt" + + "code.gitea.io/sdk/gitea" "github.com/google/go-github/github" ) +// Issue migrates a GitHub Issue to a Gitea Issue func (m *Migratory) Issue(gi *github.Issue) (*gitea.Issue, error) { if m.migratedMilestones == nil { m.migratedMilestones = make(map[int64]int64) @@ -58,6 +60,7 @@ func (m *Migratory) labels(gls []github.Label) (results []int64, err error) { return } +// Label migrates a GitHub Label to a Gitea Label without caching its id func (m *Migratory) Label(gl *github.Label) (*gitea.Label, error) { return m.Client.CreateLabel(m.repository.Owner.UserName, m.repository.Name, gitea.CreateLabelOption{ @@ -66,6 +69,7 @@ func (m *Migratory) Label(gl *github.Label) (*gitea.Label, error) { }) } +// Milestone migrates a GitHub Milesteon to a Gitea Milestone and caches its id func (m *Migratory) Milestone(gm *github.Milestone) (*gitea.Milestone, error) { ms, err := m.Client.CreateMilestone(m.repository.Owner.UserName, m.repository.Name, gitea.CreateMilestoneOption{ @@ -101,6 +105,7 @@ func githubStateToGiteaState(ghstate *string) *string { return nil } +// IssueComment migrates a GitHub IssueComment to a Gitea Comment func (m *Migratory) IssueComment(issue *gitea.Issue, gic *github.IssueComment) (*gitea.Comment, error) { return m.Client.CreateIssueComment(m.repository.Owner.UserName, m.repository.Name, diff --git a/migrations/migratory.go b/migrations/migratory.go index 9f6fc91..77d9cfd 100644 --- a/migrations/migratory.go +++ b/migrations/migratory.go @@ -2,6 +2,7 @@ package migrations import "code.gitea.io/sdk/gitea" +// Migratory is the context for migrating things from GitHub to Gitea type Migratory struct { Client *gitea.Client AuthUsername string diff --git a/migrations/repo.go b/migrations/repo.go index eb6d110..9495aef 100644 --- a/migrations/repo.go +++ b/migrations/repo.go @@ -5,6 +5,7 @@ import ( "github.com/google/go-github/github" ) +// Repository migrates a GitHub Repository to a Gitea Repository without migrating issues etc. func (m *Migratory) Repository(gr *github.Repository) (*gitea.Repository, error) { var err error m.repository, err = m.Client.MigrateRepo(gitea.MigrateRepoOption{