Add drone test

Fix lint problems
Fix import order

Signed-off-by: Jonas Franz <info@jonasfranz.software>
remotes/1678313473031378209/master
Jonas Franz 6 years ago
parent c68048a672
commit 87de9fc875
Signed by: JonasFranzDEV
GPG Key ID: 506AEEBE80BEDECD
  1. 15
      .drone.yml
  2. 37
      cmd/flags.go
  3. 10
      cmd/migrate-all.go
  4. 54
      cmd/migrate.go
  5. 2
      main.go
  6. 7
      migrations/issue.go
  7. 1
      migrations/migratory.go
  8. 1
      migrations/repo.go

@ -17,19 +17,28 @@ pipeline:
commands: commands:
- go get -u github.com/golang/dep/cmd/dep - go get -u github.com/golang/dep/cmd/dep
- dep ensure - 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: static:
image: karalabe/xgo-latest:latest image: karalabe/xgo-latest:latest
pull: true pull: true
environment: environment:
GOPATH: /go GOPATH: /go
commands: commands:
- mkdir releases
- go get -u github.com/karalabe/xgo - 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: when:
event: [ tag ] event: [ tag ]
gitea: gitea:
image: plugins/gitea-release:1 image: plugins/gitea-release:latest
pull: true pull: true
base_url: "https://git.jonasfranz.software" base_url: "https://git.jonasfranz.software"
secrets: [ gitea_token ] secrets: [ gitea_token ]

@ -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",
},
}

@ -1,21 +1,23 @@
package cmd package cmd
import ( import (
"code.gitea.io/sdk/gitea"
"context" "context"
"fmt" "fmt"
"sync"
"code.gitea.io/sdk/gitea"
"git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/migrations" "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/migrations"
"github.com/google/go-github/github" "github.com/google/go-github/github"
"github.com/urfave/cli" "github.com/urfave/cli"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"sync"
) )
// CmdMigrateAll is a command to migrate all repositories of an user
var CmdMigrateAll = cli.Command{ var CmdMigrateAll = cli.Command{
Name: "migrate-all", Name: "migrate-all",
Usage: "migrates all repositories of an user from github to a gitea repository", Usage: "migrates all repositories of an user from github to a gitea repository",
Action: runMigrateAll, Action: runMigrateAll,
Flags: append(migrateFlags, Flags: append(defaultMigrateFlags,
cli.StringFlag{ cli.StringFlag{
Name: "gh-user", Name: "gh-user",
EnvVar: "GH_USER", EnvVar: "GH_USER",
@ -66,7 +68,7 @@ func runMigrateAll(ctx *cli.Context) error {
for _, repo := range allRepos { for _, repo := range allRepos {
go func(r *github.Repository) { go func(r *github.Repository) {
defer wg.Done() 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) }(repo)
} }

@ -1,55 +1,23 @@
package cmd package cmd
import ( import (
"code.gitea.io/sdk/gitea"
"context" "context"
"fmt" "fmt"
"strings"
"code.gitea.io/sdk/gitea"
"git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/migrations" "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/migrations"
"github.com/google/go-github/github" "github.com/google/go-github/github"
"github.com/urfave/cli" "github.com/urfave/cli"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"strings"
) )
var migrateFlags = []cli.Flag{ // CmdMigrate migrates a given repository to gitea
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",
},
}
var CmdMigrate = cli.Command{ var CmdMigrate = cli.Command{
Name: "migrate", Name: "migrate",
Usage: "migrates a github to a gitea repository", Usage: "migrates a github to a gitea repository",
Action: runMigrate, Action: runMigrate,
Flags: append(migrateFlags, Flags: append(defaultMigrateFlags,
cli.StringFlag{ cli.StringFlag{
Name: "gh-repo", Name: "gh-repo",
Usage: "GitHub Repository", Usage: "GitHub Repository",
@ -80,21 +48,23 @@ func runMigrate(ctx *cli.Context) error {
username := strings.Split(ctx.String("gh-repo"), "/")[0] username := strings.Split(ctx.String("gh-repo"), "/")[0]
repo := strings.Split(ctx.String("gh-repo"), "/")[1] 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) fmt.Printf("Fetching repository %s/%s...\n", username, repo)
gr, _, err := gc.Repositories.Get(c, username, repo) gr, _, err := gc.Repositories.Get(c, username, repo)
if err != nil { if err != nil {
return err return err
} }
fmt.Printf("Migrating repository %s/%s...\n", username, repo) 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 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 { if onlyRepo {
return nil return nil
} }

@ -1,11 +1,11 @@
package main package main
import ( import (
"fmt"
"os" "os"
"github.com/urfave/cli" "github.com/urfave/cli"
"fmt"
"git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/cmd" "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/cmd"
) )

@ -1,11 +1,13 @@
package migrations package migrations
import ( import (
"code.gitea.io/sdk/gitea"
"fmt" "fmt"
"code.gitea.io/sdk/gitea"
"github.com/google/go-github/github" "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) { func (m *Migratory) Issue(gi *github.Issue) (*gitea.Issue, error) {
if m.migratedMilestones == nil { if m.migratedMilestones == nil {
m.migratedMilestones = make(map[int64]int64) m.migratedMilestones = make(map[int64]int64)
@ -58,6 +60,7 @@ func (m *Migratory) labels(gls []github.Label) (results []int64, err error) {
return return
} }
// Label migrates a GitHub Label to a Gitea Label without caching its id
func (m *Migratory) Label(gl *github.Label) (*gitea.Label, error) { func (m *Migratory) Label(gl *github.Label) (*gitea.Label, error) {
return m.Client.CreateLabel(m.repository.Owner.UserName, m.repository.Name, return m.Client.CreateLabel(m.repository.Owner.UserName, m.repository.Name,
gitea.CreateLabelOption{ 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) { func (m *Migratory) Milestone(gm *github.Milestone) (*gitea.Milestone, error) {
ms, err := m.Client.CreateMilestone(m.repository.Owner.UserName, m.repository.Name, ms, err := m.Client.CreateMilestone(m.repository.Owner.UserName, m.repository.Name,
gitea.CreateMilestoneOption{ gitea.CreateMilestoneOption{
@ -101,6 +105,7 @@ func githubStateToGiteaState(ghstate *string) *string {
return nil return nil
} }
// IssueComment migrates a GitHub IssueComment to a Gitea Comment
func (m *Migratory) IssueComment(issue *gitea.Issue, gic *github.IssueComment) (*gitea.Comment, error) { func (m *Migratory) IssueComment(issue *gitea.Issue, gic *github.IssueComment) (*gitea.Comment, error) {
return m.Client.CreateIssueComment(m.repository.Owner.UserName, return m.Client.CreateIssueComment(m.repository.Owner.UserName,
m.repository.Name, m.repository.Name,

@ -2,6 +2,7 @@ package migrations
import "code.gitea.io/sdk/gitea" import "code.gitea.io/sdk/gitea"
// Migratory is the context for migrating things from GitHub to Gitea
type Migratory struct { type Migratory struct {
Client *gitea.Client Client *gitea.Client
AuthUsername string AuthUsername string

@ -5,6 +5,7 @@ import (
"github.com/google/go-github/github" "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) { func (m *Migratory) Repository(gr *github.Repository) (*gitea.Repository, error) {
var err error var err error
m.repository, err = m.Client.MigrateRepo(gitea.MigrateRepoOption{ m.repository, err = m.Client.MigrateRepo(gitea.MigrateRepoOption{

Loading…
Cancel
Save