Browse Source

Add drone test

Fix lint problems
Fix import order

Signed-off-by: Jonas Franz <info@jonasfranz.software>
tags/v0.0.8
Jonas Franz 2 years ago
parent
commit
87de9fc875
Signed by untrusted user: JonasFranzDEV <info@jonasfranz.software> GPG Key ID: 506AEEBE80BEDECD
8 changed files with 76 additions and 51 deletions
  1. +12
    -3
      .drone.yml
  2. +37
    -0
      cmd/flags.go
  3. +6
    -4
      cmd/migrate-all.go
  4. +12
    -42
      cmd/migrate.go
  5. +1
    -1
      main.go
  6. +6
    -1
      migrations/issue.go
  7. +1
    -0
      migrations/migratory.go
  8. +1
    -0
      migrations/repo.go

+ 12
- 3
.drone.yml View File

@@ -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 ]


+ 37
- 0
cmd/flags.go View File

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

+ 6
- 4
cmd/migrate-all.go View File

@@ -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)
}



+ 12
- 42
cmd/migrate.go View File

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


+ 1
- 1
main.go View File

@@ -1,11 +1,11 @@
package main

import (
"fmt"
"os"

"github.com/urfave/cli"

"fmt"
"git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/cmd"
)



+ 6
- 1
migrations/issue.go View File

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


+ 1
- 0
migrations/migratory.go View File

@@ -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


+ 1
- 0
migrations/repo.go View File

@@ -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{


Loading…
Cancel
Save