-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
140 lines (114 loc) · 3.15 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
package main
import (
"bytes"
"flag"
"fmt"
"github.com/sue445/gitpanda/gitlab"
"github.com/sue445/gitpanda/webhook"
"net/http"
"os"
"strconv"
"strings"
)
var (
// Version represents app version (injected from ldflags)
Version string
// Revision represents app revision (injected from ldflags)
Revision string
)
var isPrintVersion bool
var isDebugLogging bool
var truncateLines = 0
var sentryDsn = ""
func init() {
flag.BoolVar(&isPrintVersion, "version", false, "Whether showing version")
flag.Parse()
if os.Getenv("DEBUG_LOGGING") != "" {
isDebugLogging = true
}
if os.Getenv("SENTRY_DSN") != "" {
sentryDsn = os.Getenv("SENTRY_DSN")
}
if s := os.Getenv("TRUNCATE_LINES"); s != "" {
lines, err := strconv.Atoi(s)
if err != nil {
fmt.Printf("%s is invalid number, error=%v\n", s, err)
os.Exit(1)
}
if lines > 0 {
truncateLines = lines
}
}
}
func checkEnv(name string) {
if os.Getenv(name) != "" {
return
}
fmt.Printf("[ERROR] %s is required\n", name)
fmt.Println("")
printUsage()
os.Exit(1)
}
func printVersion() {
fmt.Printf("gitpanda %s, build %s\n", Version, Revision)
}
func printUsage() {
fmt.Println("[Usage]")
fmt.Println(" PORT=8000 \\")
fmt.Println(" GITLAB_API_ENDPOINT=https://your-gitlab.example.com/api/v4 \\")
fmt.Println(" GITLAB_BASE_URL=https://your-gitlab.example.com \\")
fmt.Println(" GITLAB_PRIVATE_TOKEN=xxxxxxxxxx \\")
fmt.Println(" SLACK_OAUTH_ACCESS_TOKEN=xoxp-0000000000-0000000000-000000000000-00000000000000000000000000000000 \\")
fmt.Println(" ./gitpanda")
}
func normalHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text")
switch r.Method {
case http.MethodGet:
_, err := w.Write([]byte("It works"))
if err != nil {
fmt.Printf("[ERROR] error=%v\n", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error())) //nolint:errcheck
panic(err)
}
case http.MethodPost:
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(r.Body)
if err != nil {
fmt.Printf("[ERROR] error=%v\n", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error())) //nolint:errcheck
panic(err)
}
body := strings.TrimSpace(buf.String())
if isDebugLogging {
fmt.Printf("[DEBUG] normalHandler: body=%s\n", body)
}
s := webhook.NewSlackWebhook(
os.Getenv("SLACK_OAUTH_ACCESS_TOKEN"),
os.Getenv("SLACK_VERIFICATION_TOKEN"),
&gitlab.URLParserParams{
APIEndpoint: os.Getenv("GITLAB_API_ENDPOINT"),
BaseURL: os.Getenv("GITLAB_BASE_URL"),
PrivateToken: os.Getenv("GITLAB_PRIVATE_TOKEN"),
GitPandaVersion: Version,
IsDebugLogging: isDebugLogging,
},
)
response, err := s.Request(body, truncateLines)
if err != nil {
fmt.Printf("[ERROR] body=%s, response=%s, error=%v\n", body, response, err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error())) //nolint:errcheck
panic(err)
}
_, err = w.Write([]byte(response))
if err != nil {
fmt.Printf("[ERROR] body=%s, response=%s, error=%v\n", body, response, err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error())) //nolint:errcheck
panic(err)
}
}
}