Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ type Hook struct {
ResponseHeaders ResponseHeaders `json:"response-headers,omitempty"`
CaptureCommandOutput bool `json:"include-command-output-in-response,omitempty"`
CaptureCommandOutputOnError bool `json:"include-command-output-in-response-on-error,omitempty"`
DisableCommandOutputLogging bool `json:"disable-command-output-logging,omitempty"`
PassEnvironmentToCommand []Argument `json:"pass-environment-to-command,omitempty"`
PassArgumentsToCommand []Argument `json:"pass-arguments-to-command,omitempty"`
PassFileToCommand []Argument `json:"pass-file-to-command,omitempty"`
Expand Down
4 changes: 3 additions & 1 deletion webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,9 @@ func handleHook(h *hook.Hook, r *hook.Request) (string, error) {

out, err := cmd.CombinedOutput()

log.Printf("[%s] command output: %s\n", r.ID, out)
if err != nil || !h.DisableCommandOutputLogging {
log.Printf("[%s] command output: %s\n", r.ID, out)
}

if err != nil {
log.Printf("[%s] error occurred: %+v\n", r.ID, err)
Expand Down
64 changes: 64 additions & 0 deletions webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,70 @@ func TestStaticParams(t *testing.T) {
}
}

func TestDisableCommandOutputLogging(t *testing.T) {
hookecho, cleanHookecho := buildHookecho(t)
defer cleanHookecho()

tests := []struct {
name string
hook *hook.Hook
wantLog bool
wantErr bool
}{
{
name: "logs command output on success by default",
hook: &hook.Hook{
ExecuteCommand: hookecho,
PassArgumentsToCommand: []hook.Argument{
{Source: "string", Name: "passed"},
},
},
wantLog: true,
},
{
name: "does not log command output on success when disabled",
hook: &hook.Hook{
ExecuteCommand: hookecho,
DisableCommandOutputLogging: true,
PassArgumentsToCommand: []hook.Argument{
{Source: "string", Name: "passed"},
},
},
wantLog: false,
},
{
name: "still logs command output on error when disabled",
hook: &hook.Hook{
ExecuteCommand: hookecho,
DisableCommandOutputLogging: true,
PassArgumentsToCommand: []hook.Argument{
{Source: "string", Name: "exit=1"},
},
},
wantLog: true,
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &bytes.Buffer{}
log.SetOutput(b)
t.Cleanup(func() { log.SetOutput(os.Stderr) })

_, err := handleHook(tt.hook, &hook.Request{ID: "test"})
if (err != nil) != tt.wantErr {
t.Fatalf("handleHook() error = %v, wantErr %v", err, tt.wantErr)
}

gotLog := strings.Contains(b.String(), "command output:")
if gotLog != tt.wantLog {
t.Fatalf("command output logged = %v, want %v\nlog output:\n%s", gotLog, tt.wantLog, b.String())
}
})
}
}

func TestWebhook(t *testing.T) {
hookecho, cleanupHookecho := buildHookecho(t)
defer cleanupHookecho()
Expand Down
Loading