Skip to content

Commit 86cef3e

Browse files
authored
Merge pull request #101 from adnanh/development
webhook 2.5.0
2 parents 032c744 + 75cf895 commit 86cef3e

File tree

6 files changed

+56
-33
lines changed

6 files changed

+56
-33
lines changed

Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
OS = darwin freebsd linux openbsd windows
2+
ARCHS = 386 arm amd64 arm64
3+
4+
all: build release
5+
6+
build: deps
7+
go build
8+
9+
release: clean deps
10+
@for arch in $(ARCHS);\
11+
do \
12+
for os in $(OS);\
13+
do \
14+
echo "Building $$os-$$arch"; \
15+
mkdir -p build/webhook-$$os-$$arch/; \
16+
GOOS=$$os GOARCH=$$arch go build -o build/webhook-$$os-$$arch/webhook; \
17+
tar cz -C build -f build/webhook-$$os-$$arch.tar.gz webhook-$$os-$$arch; \
18+
done \
19+
done
20+
21+
test: deps
22+
go test ./...
23+
24+
deps:
25+
go get -d -v -t ./...
26+
27+
clean:
28+
rm -rf build
29+
rm -f webhook

hook/hook.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ func (h *Hook) ExtractCommandArgumentsForEnv(headers, query, payload *map[string
367367
if arg, ok := h.PassEnvironmentToCommand[i].Get(headers, query, payload); ok {
368368
if h.PassEnvironmentToCommand[i].EnvName != "" {
369369
// first try to use the EnvName if specified
370-
args = append(args, EnvNamespace+h.PassEnvironmentToCommand[i].EnvName+"="+arg)
370+
args = append(args, h.PassEnvironmentToCommand[i].EnvName+"="+arg)
371371
} else {
372372
// then fallback on the name
373373
args = append(args, EnvNamespace+h.PassEnvironmentToCommand[i].Name+"="+arg)
@@ -521,10 +521,3 @@ func (r MatchRule) Evaluate(headers, query, payload *map[string]interface{}, bod
521521
}
522522
return false, nil
523523
}
524-
525-
// CommandStatusResponse type encapsulates the executed command exit code, message, stdout and stderr
526-
type CommandStatusResponse struct {
527-
ResponseMessage string `json:"message,omitempty"`
528-
Output string `json:"output,omitempty"`
529-
Error string `json:"error,omitempty"`
530-
}

hook/hook_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ var hookExtractCommandArgumentsForEnvTests = []struct {
179179
"test",
180180
[]Argument{Argument{"header", "a", "MYKEY"}},
181181
&map[string]interface{}{"a": "z"}, nil, nil,
182-
[]string{"HOOK_MYKEY=z"},
182+
[]string{"MYKEY=z"},
183183
true,
184184
},
185185
// failures

test/hooks.json.tmpl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"id": "bitbucket",
5858
"execute-command": "{{ .Hookecho }}",
5959
"command-working-directory": "/",
60-
"include-command-output-in-response": true,
60+
"include-command-output-in-response": false,
6161
"response-message": "success",
6262
"parse-parameters-as-json": [
6363
{
@@ -136,4 +136,3 @@
136136
}
137137
}
138138
]
139-

webhook.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
const (
24-
version = "2.4.0"
24+
version = "2.5.0"
2525
)
2626

2727
var (
@@ -222,8 +222,15 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
222222
}
223223

224224
if matchedHook.CaptureCommandOutput {
225-
response := handleHook(matchedHook, &headers, &query, &payload, &body)
226-
fmt.Fprintf(w, response)
225+
response, err := handleHook(matchedHook, &headers, &query, &payload, &body)
226+
227+
if err != nil {
228+
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
229+
w.WriteHeader(http.StatusInternalServerError)
230+
fmt.Fprintf(w, "Error occurred while executing the hook's command. Please check your logs for more details.")
231+
} else {
232+
fmt.Fprintf(w, response)
233+
}
227234
} else {
228235
go handleHook(matchedHook, &headers, &query, &payload, &body)
229236
fmt.Fprintf(w, matchedHook.ResponseMessage)
@@ -241,7 +248,7 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
241248
}
242249
}
243250

244-
func handleHook(h *hook.Hook, headers, query, payload *map[string]interface{}, body *[]byte) string {
251+
func handleHook(h *hook.Hook, headers, query, payload *map[string]interface{}, body *[]byte) (string, error) {
245252
var err error
246253

247254
cmd := exec.Command(h.ExecuteCommand)
@@ -261,28 +268,17 @@ func handleHook(h *hook.Hook, headers, query, payload *map[string]interface{}, b
261268

262269
log.Printf("executing %s (%s) with arguments %q and environment %s using %s as cwd\n", h.ExecuteCommand, cmd.Path, cmd.Args, envs, cmd.Dir)
263270

264-
out, err := cmd.CombinedOutput()
271+
out, err := cmd.Output()
265272

266273
log.Printf("command output: %s\n", out)
267274

268-
var errorResponse string
269-
270275
if err != nil {
271276
log.Printf("error occurred: %+v\n", err)
272-
errorResponse = fmt.Sprintf("%+v", err)
273277
}
274278

275279
log.Printf("finished handling %s\n", h.ID)
276280

277-
var response []byte
278-
response, err = json.Marshal(&hook.CommandStatusResponse{ResponseMessage: h.ResponseMessage, Output: string(out), Error: errorResponse})
279-
280-
if err != nil {
281-
log.Printf("error marshalling response: %+v", err)
282-
return h.ResponseMessage
283-
}
284-
285-
return string(response)
281+
return string(out), err
286282
}
287283

288284
func reloadHooks() {

webhook_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,9 @@ var hookHandlerTests = []struct {
372372
}`,
373373
false,
374374
http.StatusOK,
375-
`{"output":"arg: 1481a2de7b2a7d02428ad93446ab166be7793fbb [email protected]\nenv: HOOK_head_commit.timestamp=2013-03-12T08:14:29-07:00\n"}`,
375+
`arg: 1481a2de7b2a7d02428ad93446ab166be7793fbb [email protected]
376+
env: HOOK_head_commit.timestamp=2013-03-12T08:14:29-07:00
377+
`,
376378
},
377379
{
378380
"bitbucket", // bitbucket sends their payload using uriencoded params.
@@ -381,7 +383,7 @@ var hookHandlerTests = []struct {
381383
`payload={"canon_url": "https://bitbucket.org","commits": [{"author": "marcus","branch": "master","files": [{"file": "somefile.py","type": "modified"}],"message": "Added some more things to somefile.py\n","node": "620ade18607a","parents": ["702c70160afc"],"raw_author": "Marcus Bertrand <[email protected]>","raw_node": "620ade18607ac42d872b568bb92acaa9a28620e9","revision": null,"size": -1,"timestamp": "2012-05-30 05:58:56","utctimestamp": "2014-11-07 15:19:02+00:00"}],"repository": {"absolute_url": "/webhook/testing/","fork": false,"is_private": true,"name": "Project X","owner": "marcus","scm": "git","slug": "project-x","website": "https://atlassian.com/"},"user": "marcus"}`,
382384
true,
383385
http.StatusOK,
384-
`{"message":"success"}`,
386+
`success`,
385387
},
386388
{
387389
"gitlab",
@@ -431,7 +433,8 @@ var hookHandlerTests = []struct {
431433
}`,
432434
false,
433435
http.StatusOK,
434-
`{"message":"success","output":"arg: b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327 John Smith [email protected]\n"}`,
436+
`arg: b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327 John Smith [email protected]
437+
`,
435438
},
436439

437440
{
@@ -469,7 +472,9 @@ var hookHandlerTests = []struct {
469472
}`,
470473
false,
471474
http.StatusOK,
472-
`{"output":"arg: 1481a2de7b2a7d02428ad93446ab166be7793fbb [email protected]\nenv: HOOK_head_commit.timestamp=2013-03-12T08:14:29-07:00\n"}`,
475+
`arg: 1481a2de7b2a7d02428ad93446ab166be7793fbb [email protected]
476+
env: HOOK_head_commit.timestamp=2013-03-12T08:14:29-07:00
477+
`,
473478
},
474479

475480
{
@@ -506,7 +511,8 @@ var hookHandlerTests = []struct {
506511
}`,
507512
false,
508513
http.StatusOK,
509-
`{"output":"arg: 1481a2de7b2a7d02428ad93446ab166be7793fbb [email protected]\n"}`,
514+
`arg: 1481a2de7b2a7d02428ad93446ab166be7793fbb [email protected]
515+
`,
510516
},
511517

512518
{"empty payload", "github", nil, `{}`, false, http.StatusOK, `Hook rules were not satisfied.`},

0 commit comments

Comments
 (0)