A tool to migrate GitHub Repositories to Gitea including all issues
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

93 lines
2.0 KiB

  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "sync"
  6. "code.gitea.io/sdk/gitea"
  7. "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/migrations"
  8. "github.com/google/go-github/github"
  9. "github.com/urfave/cli"
  10. "golang.org/x/oauth2"
  11. )
  12. // CmdMigrateAll is a command to migrate all repositories of an user
  13. var CmdMigrateAll = cli.Command{
  14. Name: "migrate-all",
  15. Usage: "migrates all repositories of an user from github to a gitea repository",
  16. Action: runMigrateAll,
  17. Flags: append(defaultMigrateFlags,
  18. cli.StringFlag{
  19. Name: "gh-user",
  20. EnvVar: "GH_USER",
  21. Usage: "GitHub Username",
  22. },
  23. ),
  24. }
  25. func runMigrateAll(ctx *cli.Context) error {
  26. m := &migrations.Migratory{
  27. Client: gitea.NewClient(ctx.String("url"), ctx.String("token")),
  28. Private: ctx.Bool("private"),
  29. NewOwnerID: ctx.Int64("owner"),
  30. }
  31. if m.NewOwnerID == 0 {
  32. usr, err := m.Client.GetMyUserInfo()
  33. if err != nil {
  34. return fmt.Errorf("cannot fetch user info about current user: %v", err)
  35. }
  36. m.NewOwnerID = usr.ID
  37. }
  38. c := context.Background()
  39. var gc *github.Client
  40. if ctx.IsSet("gh-token") {
  41. ts := oauth2.StaticTokenSource(
  42. &oauth2.Token{AccessToken: ctx.String("gh-token")},
  43. )
  44. tc := oauth2.NewClient(c, ts)
  45. gc = github.NewClient(tc)
  46. } else {
  47. gc = github.NewClient(nil)
  48. }
  49. opt := &github.RepositoryListOptions{
  50. ListOptions: github.ListOptions{PerPage: 100},
  51. }
  52. // get all pages of results
  53. var allRepos []*github.Repository
  54. for {
  55. repos, resp, err := gc.Repositories.List(c, ctx.String("gh-user"), opt)
  56. if err != nil {
  57. return err
  58. }
  59. allRepos = append(allRepos, repos...)
  60. if resp.NextPage == 0 {
  61. break
  62. }
  63. opt.Page = resp.NextPage
  64. }
  65. errs := make(chan error, 1)
  66. var wg sync.WaitGroup
  67. wg.Add(len(allRepos))
  68. for _, repo := range allRepos {
  69. go func(r *github.Repository) {
  70. defer wg.Done()
  71. errs <- migrate(c, gc, m, r.Owner.GetLogin(), r.GetName(), ctx.Bool("only-repo"))
  72. }(repo)
  73. }
  74. go func() {
  75. for i := range errs {
  76. if i != nil {
  77. fmt.Printf("error: %v", i)
  78. }
  79. }
  80. }()
  81. wg.Wait()
  82. return nil
  83. }