forked from ewoutp/gitlab2gogs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
148 lines (138 loc) · 3.61 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"flag"
"fmt"
"os"
"strings"
"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
lcNames bool
glNamespace string
glProject string
isMirror bool
)
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", "", "")
flag.BoolVar(&lcNames, "lc-names", false, "")
flag.StringVar(&glNamespace, "namespace", "", "")
flag.StringVar(&glProject, "project", "", "")
flag.BoolVar(&isMirror, "mirror", false, "")
}
func main() {
flag.Parse()
gc := gogs.NewClient(gogsUrl, gogsToken)
orgMap := make(map[string]*gogs.Organization)
getOrg := func(o *gogitlab.Namespace) *gogs.Organization {
name := fixName(o.Name)
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,
FullName: o.Name,
Description: o.Description,
}
fmt.Printf("Creating organization '%s' as '%s'...\n", o.Name, 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) {
name := fixName(p.Name)
ns := fixName(p.Namespace.Name)
_, err := gc.GetRepo(ns, name)
if err == nil {
fmt.Printf("Repository '%s/%s' already exists.\n", ns, name)
} else {
org := getOrg(p.Namespace)
fmt.Printf("Migrating '%s/%s' as '%s/%s'...\n", p.Namespace.Name, p.Name, ns, name)
opts := gogs.MigrateRepoOption{
CloneAddr: p.HttpRepoUrl,
AuthUsername: gitlabUser,
AuthPassword: gitlabPassword,
UID: int(org.ID),
RepoName: name,
Private: !p.Public,
Description: p.Description,
Mirror: isMirror,
}
_, 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)
}
if glNamespace != "" {
glNamespace = strings.ToLower(glNamespace)
}
if glProject != "" {
glProject = strings.ToLower(glProject)
}
fmt.Println("\nMigration Started...\n")
for _, p := range projects {
if p.Archived {
fmt.Printf("Archived Repository '%s/%s' skipped.\n", p.Namespace.Name, p.Name)
continue
}
if glNamespace != "" {
if glNamespace == strings.ToLower(p.Namespace.Name) {
if glProject != "" && glProject != strings.ToLower(p.Name) {
fmt.Printf("Skipping Project: '%s/%s'.\n", p.Namespace.Name, p.Name)
continue
}
} else {
fmt.Printf("Skipping Namespace: '%s'.\n", p.Namespace.Name)
continue
}
}
migrate(p)
}
fmt.Println("\nMigration Completed.\n")
}
func fixName(name string) string {
switch name {
case "api": // reserved
return "theapi"
default:
if lcNames {
return strings.ToLower(name)
} else {
return name
}
}
}
func exitf(format string, args ...interface{}) {
fmt.Printf(format, args...)
os.Exit(1)
}