-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlab2gs.go
More file actions
170 lines (139 loc) · 3.89 KB
/
Copy pathgitlab2gs.go
File metadata and controls
170 lines (139 loc) · 3.89 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/bugagazavr/go-gitlab-client"
"github.com/gogits/go-gogs-client"
"io/ioutil"
"os"
)
var usage = `Usage: gitlab2gs [options...]
Options:
-config Specify your config file
`
type Config struct {
GitlabHost string `json:"gitlabHost"`
GitlabApiPath string `json:"gitlabApiPath"`
GitlabToken string `json:"gitlabToken"`
GitlabUser string `json:"gitlabUser"`
GitlabPassword string `json:"gitlabPassword"`
GitlabProjects []string `json:"gitlabProjects"`
GogsUrl string `json:"gogsUrl"`
GogsToken string `json:"gogsToken"`
GogsApiPath string `json:"gogsApiPath"`
}
var configFile = flag.String("config", "./config.json.sample", "json config file")
var userMap = make(map[string]*gogs.Organization)
var gitlab *gogitlab.Gitlab
var gs *gogs.Client
func main() {
flag.Usage = func() {
fmt.Fprint(os.Stderr, usage)
}
flag.Parse()
if *configFile == "" {
usageAndExit("Please specify your json config file")
}
config := loadConfig(*configFile)
//judge config file args is legal or not
isLegalConfigArg(config)
gitlab = gogitlab.NewGitlab(config.GitlabHost, config.GitlabApiPath, config.GitlabToken)
gs = gogs.NewClient(config.GogsUrl, config.GogsToken)
migrateProjects := getProjects(config.GitlabProjects)
for _, project := range migrateProjects {
doMigrate(project, config)
}
}
func loadConfig(configfile string) *Config {
file, err := ioutil.ReadFile(*configFile)
checkErr("Config file error", err)
var config Config
json.Unmarshal(file, &config)
return &config
}
func isLegalConfigArg(config *Config) {
if config.GogsUrl == "" || config.GogsToken == "" || config.GitlabHost == "" ||
config.GitlabToken == "" || config.GogsApiPath == "" || config.GitlabApiPath == "" {
usageAndExit("JSON config file field error, please check your config file")
}
}
func getProjects(projects []string) []*gogitlab.Project {
if len(projects) != 0 {
var projectss []*gogitlab.Project
for _, projectID := range projects {
project, err := gitlab.Project(projectID)
checkErr("Get project from ID error", err)
projectss = append(projectss, project)
}
return projectss
}
allProjects, err := gitlab.AllProjects()
checkErr("Get gitlab projects error", err)
return allProjects
}
func getGogsUID(name string) int {
org, ok := userMap[name]
if ok {
return int(org.ID)
}
orgUserName, err := gs.GetOrg(name)
if err == nil {
return int(orgUserName.ID)
}
createOrg := gogs.CreateOrgOption{
UserName: name,
}
org, err = gs.AdminCreateOrg("root", createOrg)
checkErr("Failed to create org", err)
userMap[name] = org
return int(org.ID)
}
func doMigrate(project *gogitlab.Project, config *Config) {
t, err := gs.GetRepo(project.Namespace.Name, project.Name)
fmt.Printf("t: %+v\n", t)
if err == nil {
fmt.Printf("%s # %s already in your gogs\n", project.Namespace.Name, project.Name)
} else {
orgID := getGogsUID(project.Namespace.Name)
// api is reserved
var name string = ""
if project.Name == "api" {
name = "myapi"
} else {
name = project.Name
}
fmt.Printf("%s # %s migrating to gogs # %s #...\n",
project.Namespace.Name, project.Name, name)
opts := gogs.MigrateRepoOption{
CloneAddr: project.HttpRepoUrl,
AuthUsername: config.GitlabUser,
AuthPassword: config.GitlabPassword,
UID: orgID,
RepoName: name,
Private: !project.Public,
Description: project.Description,
}
_, err := gs.MigrateRepo(opts)
checkErr("", err)
}
}
func usageAndExit(message string) {
if message != "" {
fmt.Fprintf(os.Stderr, message)
fmt.Fprintf(os.Stderr, "\n\n")
}
flag.Usage()
fmt.Fprintf(os.Stderr, "\n")
os.Exit(1)
}
func checkErr(errMessage string, err error) {
if err != nil {
if errMessage != "" {
fmt.Fprintf(os.Stderr, errMessage)
fmt.Fprint(os.Stderr, "\n\n")
}
fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
}