-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
110 lines (99 loc) · 2.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"flag"
"fmt"
"os"
"github.com/ewoutp/go-gitlab-client"
"github.com/gogits/go-gogs-client"
)
var (
gitlabHost string
gitlabApiPath string
gitlabUser string
gitlabPassword string
gitlabToken string
gogsUrl string
gogsToken string
gogsUser string
)
func init() {
flag.StringVar(&gitlabHost, "gitlab-host", "", "")
flag.StringVar(&gitlabApiPath, "gitlab-api-path", "", "")
flag.StringVar(&gitlabUser, "gitlab-user", "", "")
flag.StringVar(&gitlabPassword, "gitlab-password", "", "")
flag.StringVar(&gitlabToken, "gitlab-token", "", "")
flag.StringVar(&gogsUrl, "gogs-url", "", "")
flag.StringVar(&gogsToken, "gogs-token", "", "")
flag.StringVar(&gogsUser, "gogs-user", "", "")
}
func main() {
flag.Parse()
gc := gogs.NewClient(gogsUrl, gogsToken)
orgMap := make(map[string]*gogs.Organization)
getOrg := func(name string) *gogs.Organization {
org, ok := orgMap[name]
if ok {
return org
}
org, err := gc.GetOrg(name)
if err == nil {
orgMap[name] = org
return org
}
createOpt := gogs.CreateOrgOption{
UserName: name,
}
org, err = gc.AdminCreateOrg(gogsUser, createOpt)
if err != nil {
exitf("Failed to create organization '%s': %v\n", name, err)
}
orgMap[name] = org
return org
}
migrate := func(p *gogitlab.Project) {
_, err := gc.GetRepo(p.Namespace.Name, p.Name)
if err == nil {
fmt.Printf("%s | %s already exists\n", p.Namespace.Name, p.Name)
} else {
org := getOrg(p.Namespace.Name)
name := fixName(p.Name)
fmt.Printf("%s | %s migrating as '%s'...\n", p.Namespace.Name, p.Name, name)
opts := gogs.MigrateRepoOption{
CloneAddr: p.HttpRepoUrl,
AuthUsername: gitlabUser,
AuthPassword: gitlabPassword,
UID: int(org.ID),
RepoName: name,
Private: !p.Public,
Description: p.Description,
}
_, err := gc.MigrateRepo(opts)
if err != nil {
exitf("Failed to migrate '%s | %s': %v\n", p.Namespace.Name, p.Name, err)
}
}
}
gitlab := gogitlab.NewGitlab(gitlabHost, gitlabApiPath, gitlabToken)
projects, err := gitlab.AllProjects()
if err != nil {
exitf("Cannot get gitlab projects: %v\n", err)
}
for _, p := range projects {
if p.Archived {
continue
}
migrate(p)
}
}
func fixName(name string) string {
switch name {
case "api": // reserved
return "theapi"
default:
return name
}
}
func exitf(format string, args ...interface{}) {
fmt.Printf(format, args...)
os.Exit(1)
}