Skip to content

Commit b99f9f8

Browse files
committed
fix: improve GetPushEventPayload
1 parent 7a5af25 commit b99f9f8

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

Diff for: models/actions/run.go

+22-6
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,29 @@ func (run *ActionRun) Duration() time.Duration {
119119

120120
func (run *ActionRun) GetPushEventPayload() (*api.PushPayload, error) {
121121
if run.Event == webhook_module.HookEventPush {
122-
var payload api.PushPayload
123-
if err := json.Unmarshal([]byte(run.EventPayload), &payload); err != nil {
124-
return nil, err
125-
}
126-
return &payload, nil
122+
return nil, fmt.Errorf("event %s is not a push event", run.Event)
123+
}
124+
125+
payload := &api.PushPayload{}
126+
if err := json.Unmarshal([]byte(run.EventPayload), payload); err != nil {
127+
return nil, err
128+
}
129+
130+
// Since it comes from json unmarshal, we should check if it's broken
131+
if payload.HeadCommit == nil {
132+
return nil, fmt.Errorf("nil HeadCommit")
133+
}
134+
if payload.Repo == nil {
135+
return nil, fmt.Errorf("nil Repo")
136+
}
137+
if payload.Pusher == nil {
138+
return nil, fmt.Errorf("nil Pusher")
127139
}
128-
return nil, fmt.Errorf("event %s is not a push event", run.Event)
140+
if payload.Sender == nil {
141+
return nil, fmt.Errorf("nil Sender")
142+
}
143+
144+
return payload, nil
129145
}
130146

131147
func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) error {

0 commit comments

Comments
 (0)