main.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  5. */
  6. package main
  7. import (
  8. "context"
  9. "fmt"
  10. "log"
  11. "os"
  12. "os/signal"
  13. "path/filepath"
  14. "golang.org/x/sys/unix"
  15. )
  16. func main() {
  17. ctx, cancel := context.WithCancel(context.Background())
  18. // Catch signals
  19. go func() {
  20. sigChan := make(chan os.Signal, 1)
  21. signal.Notify(sigChan, unix.SIGINT, unix.SIGTERM)
  22. _ = <-sigChan
  23. log.Printf("caught signal; shutting down...")
  24. cancel()
  25. }()
  26. accessToken, ok := os.LookupEnv("GOGS_ACCESS_TOKEN")
  27. if !ok {
  28. log.Fatal("environment variable GOGS_ACCESS_TOKEN is unset")
  29. }
  30. gitlab := NewGitLabClient(ctx)
  31. gogs := NewGogsClient(ctx, accessToken)
  32. log.Printf("looking up authenticated gogs user...")
  33. user := &GogsUser{}
  34. err := FetchGogsUserAuthenticated(gogs, user)
  35. if err != nil {
  36. log.Fatal(err)
  37. }
  38. input := &MigrateToGogsRepoInput{}
  39. repo := &GogsRepo{}
  40. log.Printf("fetching gitlab projects...")
  41. org := "idio.link"
  42. projects := FetchGitLabGroupProjects(gitlab, org)
  43. for p := range projects.Iter() {
  44. input.SrcURI = p.RepoURL
  45. input.TgtUID = user.Id
  46. input.TgtRepoName = filepath.Base(p.PathWithNamespace)
  47. input.TgtRepoMirror = false
  48. input.TgtRepoPrivate = false
  49. input.TgtRepoDescr = p.Description
  50. log.Printf("migrating %s...", p.PathWithNamespace)
  51. err = MigrateToGogsRepo(gogs, input, repo)
  52. if err != nil {
  53. log.Printf("%v", err)
  54. }
  55. }
  56. if err := projects.LastErr(); err != nil {
  57. log.Fatal(fmt.Sprintf("fetch gitlab group projects: %v", err))
  58. }
  59. }