forked from travis-ci/worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_transform_build_json.go
72 lines (55 loc) · 1.74 KB
/
step_transform_build_json.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
package worker
import (
"bytes"
"fmt"
"os/exec"
"strings"
"github.com/mitchellh/multistep"
"github.com/travis-ci/worker/context"
"go.opencensus.io/trace"
gocontext "golang.org/x/net/context"
)
type stepTransformBuildJSON struct {
payloadFilterExecutable string
}
type EnvVar struct {
Name string
Public bool
Value string
}
func (s *stepTransformBuildJSON) Run(state multistep.StateBag) multistep.StepAction {
buildJob := state.Get("buildJob").(Job)
ctx := state.Get("ctx").(gocontext.Context)
ctx, span := trace.StartSpan(ctx, "TransformBuildJSON.Run")
defer span.End()
if s.payloadFilterExecutable == "" {
context.LoggerFromContext(ctx).Info("skipping json transformation, no filter executable defined")
return multistep.ActionContinue
}
context.LoggerFromContext(ctx).Info(fmt.Sprintf("calling filter executable: %s", s.payloadFilterExecutable))
payload := buildJob.RawPayload()
cmd := exec.Command(s.payloadFilterExecutable)
rawJson, err := payload.MarshalJSON()
if err != nil {
context.LoggerFromContext(ctx).Info(fmt.Sprintf("failed to marshal json: %v", err))
return multistep.ActionContinue
}
cmd.Stdin = strings.NewReader(string(rawJson))
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
context.LoggerFromContext(ctx).Info(fmt.Sprintf("failed to run filter executable: %v", err))
return multistep.ActionContinue
}
err = payload.UnmarshalJSON(out.Bytes())
if err != nil {
context.LoggerFromContext(ctx).Info(fmt.Sprintf("failed to unmarshal json: %v", err))
return multistep.ActionContinue
}
context.LoggerFromContext(ctx).Info("replaced the build json")
return multistep.ActionContinue
}
func (s *stepTransformBuildJSON) Cleanup(multistep.StateBag) {
// Nothing to clean up
}