forked from ayufan/gitlab-mirror-post-fetch
/
gitlab-create-mirror.go
314 lines (271 loc) · 8.48 KB
/
gitlab-create-mirror.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/dustin/httputil"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"time"
)
var (
address = flag.String("gitlab-url", getEnvOrDefault("GITLAB_URL", ""), "GitLab URL [GITLAB_URL]")
api_path = flag.String("gitlab-api-path", "/api/v3", "GitLab API path")
group = flag.String("gitlab-group", getEnvOrDefault("GITLAB_GROUP", ""), "GitLab Group [GITLAB_GROUP]")
trim_name = flag.String("trim-name", getEnvOrDefault("TRIM_NAME", ""), "Trim prefix from project name [TRIM_NAME]")
private_token = flag.String("gitlab-private-token", getEnvOrDefault("GITLAB_PRIVATE_TOKEN", ""), "GitLab Mirror Private Token [GITLAB_PRIVATE_TOKEN]")
visibility_level = flag.String("gitlab-visibility-level", getEnvOrDefault("GITLAB_VISIBILITIY_LEVEL", "private"), "Select private, internal or public [GITLAB_VISIBILITIY_LEVEL]")
git = flag.String("git", "/usr/bin/git", "path to git")
origin_remote = flag.String("origin-remote", "origin", "Source remote name")
gitlab_remote = flag.String("gitlab-remote", "gitlab", "Git remote name")
)
const (
groups_url = "/groups"
projects_url = "/projects"
)
type Group struct {
Id int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Path string `json:"path,omitempty"`
OwnerId int `json:"owner_id,omitempty"`
}
type CreateProject struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Path string `json:"path,omitempty"`
IssuesEnabled bool `json:"issues_enabled"`
MergeRequestsEnabled bool `json:"merge_requests_enabled"`
WikiEnabled bool `json:"wiki_enabled"`
SnippetsEnabled bool `json:"snippets_enabled"`
NamespaceId int `json:"namespace_id,omitempty"`
VisibilityLevel int `json:"visibility_level"`
}
type Namespace struct {
Id int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Path string `json:"path,omitempty"`
}
type Project struct {
Id int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Public bool `json:"public,omitempty"`
Path string `json:"path,omitempty"`
SshRepoUrl string `json:"ssh_url_to_repo"`
HttpRepoUrl string `json:"http_url_to_repo"`
Namespace *Namespace `json:"namespace"`
}
func getEnvOrDefault(env string, defaultValue string) string {
value := os.Getenv(env)
if value == "" {
value = defaultValue
}
return value
}
func readPayload(r io.Reader) ([]byte, error) {
maxPayloadSize := int64(1<<63 - 1)
maxPayloadSize = int64(10 << 20) // 10 MB is a lot of text.
b, err := ioutil.ReadAll(io.LimitReader(r, maxPayloadSize+1))
if err != nil {
return nil, err
}
if int64(len(b)) > maxPayloadSize {
err = errors.New("http: POST too large")
return nil, err
}
return b, nil
}
func sendJsonRequest(name string, st int, req *http.Request, jd interface{}) {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("PRIVATE-TOKEN", *private_token)
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("Couldn't execute %v against %s: %v", req.Method, req.URL, err)
return
}
defer res.Body.Close()
if res.StatusCode == st {
if jd != nil {
d := json.NewDecoder(res.Body)
err = d.Decode(jd)
if err != nil {
log.Fatalf("Error decoding json payload %v", err)
}
}
return
}
log.Fatalf("Couldn't execute %v against %s: %v", req.Method, req.URL, httputil.HTTPError(res))
}
func getURL(path string) string {
return fmt.Sprintf("%v/%v/%v", *address, *api_path, path)
}
func groups() []*Group {
url := getURL(groups_url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatalf("Failed to create NewRequest", err)
}
var groups []*Group
sendJsonRequest("get groups", 200, req, &groups)
return groups
}
func findGroup(name string) *Group {
if name == "" {
return nil
}
groups := groups()
for _, group := range groups {
if group.Name == name {
return group
}
}
return nil
}
func projects() []*Project {
url := getURL(projects_url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatalf("Failed to create NewRequest", err)
}
var projects []*Project
sendJsonRequest("get groups", 200, req, &projects)
return projects
}
func findProject(namespace string, project_name string) *Project {
projects := projects()
for _, project := range projects {
// this is hack for project of existing name
nsMatch := project.Namespace.Name == namespace || namespace == ""
if nsMatch && project.Name == project_name {
return project
}
}
return nil
}
func createProject(new_project CreateProject) Project {
body, err := json.Marshal(&new_project)
if err != nil {
log.Fatalf("Failed to marshal project object: %v", err)
}
url := getURL(projects_url)
req, err := http.NewRequest("POST", url, bytes.NewReader(body))
if err != nil {
log.Fatalf("Failed to create NewRequest", err)
}
req.Header.Set("Content-Type", "application/json")
req.ContentLength = int64(len(body))
var created_project Project
sendJsonRequest("create project", 201, req, &created_project)
return created_project
}
func readOriginRemote() url.URL {
out, err := exec.Command(*git, "config", fmt.Sprintf("remote.%v.url", *origin_remote)).Output()
if err != nil {
log.Fatalf("No URL defined for %v.", *origin_remote)
}
rawurl := strings.TrimSpace(string(out))
if !strings.Contains(rawurl, "://") {
rawurl = strings.Replace(rawurl, ":", "/", 1)
rawurl = "ssh://" + rawurl
}
result, err := url.Parse(rawurl)
if err != nil {
log.Fatalf("Invalid URL for %v: %s", *origin_remote, out)
}
result.User = nil
result.RawQuery = ""
result.Fragment = ""
return *result
}
func doCreate(repo_name string, repo_url string) *Project {
log.Printf("Looking for group %v...", *group)
group_data := findGroup(*group)
if group_data == nil && *group != "" {
log.Fatalf("No group %v found.", *group)
}
log.Printf("Creating project %v in %v...", repo_name, *group)
new_project := CreateProject{}
new_project.Name = repo_name
new_project.Description = fmt.Sprintf("Mirror of %v", repo_url)
new_project.IssuesEnabled = false
new_project.MergeRequestsEnabled = false
new_project.WikiEnabled = false
new_project.SnippetsEnabled = false
if group_data != nil {
new_project.NamespaceId = group_data.Id
}
switch *visibility_level {
case "private":
new_project.VisibilityLevel = 0
case "internal":
new_project.VisibilityLevel = 10
case "public":
new_project.VisibilityLevel = 20
default:
log.Fatalf("Unsupported visibility_level: %v", *visibility_level)
}
created_project := createProject(new_project)
return &created_project
}
func doCheckRemote() bool {
log.Printf("Verifying existence of git remote %v...", *gitlab_remote)
err := exec.Command(*git, "config", fmt.Sprintf("remote.%v.url", *gitlab_remote)).Run()
if err != nil {
return false
}
return true
}
func doCreateRemote() {
repo_url := readOriginRemote()
repo_name := repo_url.Path
repo_name = strings.TrimPrefix(repo_name, "/")
repo_name = strings.TrimSuffix(repo_name, ".git")
repo_name = strings.TrimPrefix(repo_name, *group+"/")
repo_name = strings.Replace(repo_name, "/", "-", -1)
repo_name = strings.TrimPrefix(repo_name, *trim_name)
log.Printf("Looking for project %v in %v...", repo_name, *group)
project_data := findProject(*group, repo_name)
if project_data == nil {
project_data = doCreate(repo_name, repo_url.String())
}
log.Printf("Adding remote %v as %v...", project_data.SshRepoUrl, *gitlab_remote)
err := exec.Command(*git, "remote", "add", "--mirror=push", *gitlab_remote, project_data.SshRepoUrl).Run()
if err != nil {
log.Fatalf("Failed to add git remote %v to %v", *gitlab_remote, project_data.SshRepoUrl)
}
log.Printf("We are waiting 3 seconds to settle down...")
time.Sleep(3000 * time.Millisecond)
}
func doPush() {
log.Printf("Pushing changes to %v...", *gitlab_remote)
cmd := exec.Command(*git, "push", *gitlab_remote)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatalf("Failed to push data to %v", *gitlab_remote)
}
}
func main() {
flag.Parse()
if *address == "" {
log.Fatalf("Address is required!")
}
if *private_token == "" {
log.Fatalf("Private token is required!")
}
log.SetFlags(0)
if !doCheckRemote() {
doCreateRemote()
}
doPush()
}