-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrequests.go
115 lines (88 loc) · 2.15 KB
/
requests.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
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"sort"
"strings"
)
func parseGetRequest(r *http.Request, filematch bool) (string, string, []string, error) {
repo := ""
branch := ""
files := []string{}
log.Print("parsing get request")
reqRepo, ok := r.URL.Query()["repo"]
if !ok || len(reqRepo) < 1 {
log.Print("repo is missing")
log.Print("aborting request handling")
return repo, branch, files, errors.New("repo is missing")
}
repo = reqRepo[0]
log.Print("parsed repo: ", repo)
reqBranch, ok := r.URL.Query()["branch"]
if !ok || len(reqBranch) < 1 {
log.Print("branch is missing. Assuming master")
branch = "master"
} else {
branch = reqBranch[0]
}
log.Print("parsed branch: ", branch)
if filematch {
reqFiles, ok := r.URL.Query()["files"]
if ok && len(reqFiles) > 0 {
files = reqFiles
}
}
return repo, branch, files, nil
}
func parseJSONRequest(r *http.Request, filematch bool) ([]string, string, []string, error) {
repo := []string{}
branch := "master"
files := []string{}
type gitlabProject struct {
Gitsshurl string `json:"git_ssh_url"`
Githttpurl string `json:"git_http_url"`
}
type gitlabCommit struct {
Added []string
Modified []string
Removed []string
}
type gitlabWebhook struct {
Ref string
Project gitlabProject
Commits []gitlabCommit
}
log.Print("parsing json request")
var h gitlabWebhook
body, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(body, &h)
if err != nil {
return repo, branch, files, errors.New("bad request")
}
if h.Project.Githttpurl != "" {
repo = append(repo, h.Project.Githttpurl)
}
if h.Project.Gitsshurl != "" {
repo = append(repo, h.Project.Gitsshurl)
}
if strings.Contains(h.Ref, "refs/heads/") {
branch = strings.ReplaceAll(h.Ref, "refs/heads/", "")
}
for _, commit := range h.Commits {
for _, file := range commit.Added {
files = append(files, file)
}
for _, file := range commit.Modified {
files = append(files, file)
}
for _, file := range commit.Removed {
files = append(files, file)
}
}
files = uniqueNonEmptyElementsOf(files)
sort.Strings(files)
return repo, branch, files, nil
}