-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.go
168 lines (152 loc) · 4.55 KB
/
hooks.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
// Copyright 2018 Julien Schmidt. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be found
// in the LICENSE file.
package main
import (
"encoding/json"
"log"
"net/http"
)
type systemHook struct {
Changes []struct {
Ref string `json:"ref"`
} `json:"changes"`
EventName string `json:"event_name"`
Name string `json:"name"`
OldPathWithNamespace string `json:"old_path_with_namespace"`
Path string `json:"path"`
PathWithNamespace string `json:"path_with_namespace"`
Project struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
PathWithNamespace string `json:"path_with_namespace"`
VisibilityLevel int `json:"visibility_level"`
} `json:"project"`
ProjectID int `json:"project_id"`
ProjectVisibility string `json:"project_visibility"`
}
func sendErr(w http.ResponseWriter, code int) {
http.Error(w, http.StatusText(code), code)
}
func mirrorRepo(name string, id int) {
details, err := getGitlabRepo(id)
if err != nil {
log.Println(id, name, err)
return
}
err = createGithubRepo(details.ID, details.Name, details.Description, details.Path)
if err != nil {
log.Println(details.ID, details.Name, err)
return
}
repos.add(details.Name)
}
type hooksHandler struct{}
func (h *hooksHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
if r.Method != "POST" || r.URL.Path != "/mirror" {
sendErr(rw, http.StatusTeapot)
return
}
if r.Header.Get("X-Gitlab-Token") != cfg.HookSecret {
sendErr(rw, http.StatusForbidden)
return
}
switch r.Header.Get("X-Gitlab-Event") {
case "System Hook":
var update systemHook
err := json.NewDecoder(r.Body).Decode(&update)
if err != nil {
log.Println(err)
sendErr(rw, http.StatusBadRequest)
return
}
switch update.EventName {
case "project_create":
nsPath := update.PathWithNamespace
org := nsPath[:len(nsPath)-len(update.Path)-1]
if org == cfg.OrgName &&
update.ProjectVisibility == "public" {
log.Println("create", update.Name)
mirrorRepo(update.Name, update.ProjectID)
}
case "project_destroy":
nsPath := update.PathWithNamespace
org := nsPath[:len(nsPath)-len(update.Path)-1]
if org == cfg.OrgName &&
update.ProjectVisibility == "public" {
log.Println("delete", update.Name)
repos.delete(update.Name)
if err := deleteGithubRepo(update.ProjectID, update.Name); err != nil {
log.Println(update.Name, err)
return
}
}
case "project_transfer":
if update.ProjectVisibility == "public" {
nsPath := update.PathWithNamespace
org := nsPath[:len(nsPath)-len(update.Path)-1]
oldNsPath := update.OldPathWithNamespace
oldOrg := oldNsPath[:len(oldNsPath)-len(update.Path)-1]
if org == cfg.OrgName && org != oldOrg {
log.Println("create", update.Name)
mirrorRepo(update.Name, update.ProjectID)
} else if oldOrg == cfg.OrgName && org != oldOrg {
log.Println("delete", update.Name)
repos.delete(update.Name)
if err := deleteGithubRepo(update.ProjectID, update.Name); err != nil {
log.Println(update.Name, err)
return
}
}
}
case "project_update":
nsPath := update.PathWithNamespace
org := nsPath[:len(nsPath)-len(update.Path)-1]
if org == cfg.OrgName {
name := update.Name
mirrored := repos.contains(name)
if mirrored {
if update.ProjectVisibility != "public" {
log.Println("delete", update.ProjectID, name)
repos.delete(name)
if err := deleteGithubRepo(update.ProjectID, name); err != nil {
log.Println(name, err)
return
}
} else {
log.Println("update", update.ProjectID, name)
details, err := getGitlabRepo(update.ProjectID)
if err != nil {
log.Println(name, err)
return
}
err = updateGithubRepo(update.ProjectID, name, details.Description, details.Path)
if err != nil {
log.Println(name, err)
return
}
}
} else {
if update.ProjectVisibility == "public" {
log.Println("create", update.ProjectID, name)
mirrorRepo(name, update.ProjectID)
}
}
}
case "repository_update":
if update.Project.VisibilityLevel == visibilityPublic && update.Project.Namespace == cfg.OrgName {
name := update.Project.Name
if len(update.Changes) > 0 {
log.Println("push", update.ProjectID, name)
if err := pushRepo(update.ProjectID, name); err != nil {
log.Println(name, err)
return
}
}
}
}
default:
sendErr(rw, http.StatusBadRequest)
return
}
}