Skip to content

Commit

Permalink
Fix crash with bad payload
Browse files Browse the repository at this point in the history
We were getting this crash on the controller:

```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x1f99bc7]
pipelines-as-code/pkg/provider/github/parse_payload.go:277
```
TestSkippedEvent shows it but it wasn't detected that the controller was
crashing and hapily returned ok

Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel committed Dec 18, 2024
1 parent b1df262 commit ff7c7be
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
15 changes: 15 additions & 0 deletions pkg/provider/github/parse_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ func (v *Provider) processEvent(ctx context.Context, event *info.Event, eventInt
return nil, err
}
case *github.PushEvent:
if gitEvent.GetRepo() == nil {
return nil, errors.New("error parsing payload the repository should not be nil")
}
processedEvent.Organization = gitEvent.GetRepo().GetOwner().GetLogin()
processedEvent.Repository = gitEvent.GetRepo().GetName()
processedEvent.DefaultBranch = gitEvent.GetRepo().GetDefaultBranch()
Expand All @@ -274,6 +277,9 @@ func (v *Provider) processEvent(ctx context.Context, event *info.Event, eventInt
processedEvent.HeadURL = processedEvent.BaseURL // in push events Head URL is the same as BaseURL
case *github.PullRequestEvent:
processedEvent.Repository = gitEvent.GetRepo().GetName()
if gitEvent.GetRepo() == nil {
return nil, errors.New("error parsing payload the repository should not be nil")
}
processedEvent.Organization = gitEvent.GetRepo().Owner.GetLogin()
processedEvent.DefaultBranch = gitEvent.GetRepo().GetDefaultBranch()
processedEvent.SHA = gitEvent.GetPullRequest().Head.GetSHA()
Expand Down Expand Up @@ -309,6 +315,9 @@ func (v *Provider) processEvent(ctx context.Context, event *info.Event, eventInt

func (v *Provider) handleReRequestEvent(ctx context.Context, event *github.CheckRunEvent) (*info.Event, error) {
runevent := info.NewEvent()
if event.GetRepo() == nil {
return nil, errors.New("error parsing payload the repository should not be nil")
}
runevent.Organization = event.GetRepo().GetOwner().GetLogin()
runevent.Repository = event.GetRepo().GetName()
runevent.URL = event.GetRepo().GetHTMLURL()
Expand All @@ -334,6 +343,9 @@ func (v *Provider) handleReRequestEvent(ctx context.Context, event *github.Check

func (v *Provider) handleCheckSuites(ctx context.Context, event *github.CheckSuiteEvent) (*info.Event, error) {
runevent := info.NewEvent()
if event.GetRepo() == nil {
return nil, errors.New("error parsing payload the repository should not be nil")
}
runevent.Organization = event.GetRepo().GetOwner().GetLogin()
runevent.Repository = event.GetRepo().GetName()
runevent.URL = event.GetRepo().GetHTMLURL()
Expand Down Expand Up @@ -396,6 +408,9 @@ func (v *Provider) handleIssueCommentEvent(ctx context.Context, event *github.Is
func (v *Provider) handleCommitCommentEvent(ctx context.Context, event *github.CommitCommentEvent) (*info.Event, error) {
action := "push"
runevent := info.NewEvent()
if event.GetRepo() == nil {
return nil, errors.New("error parsing payload the repository should not be nil")
}
runevent.Organization = event.GetRepo().GetOwner().GetLogin()
runevent.Repository = event.GetRepo().GetName()
runevent.Sender = event.GetSender().GetLogin()
Expand Down
18 changes: 18 additions & 0 deletions pkg/provider/github/parse_payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ var samplePRAnother = github.PullRequest{
func TestParsePayLoad(t *testing.T) {
samplePrEventClosed := samplePRevent
samplePrEventClosed.Action = github.String("closed")

samplePRNoRepo := samplePRevent
samplePRNoRepo.Repo = nil

tests := []struct {
name string
wantErrString string
Expand Down Expand Up @@ -188,6 +192,20 @@ func TestParsePayLoad(t *testing.T) {
},
shaRet: "samplePRsha",
},
{
name: "bad/pull request",
eventType: "pull_request",
triggerTarget: triggertype.PullRequest.String(),
payloadEventStruct: samplePRNoRepo,
wantErrString: "error parsing payload the repository should not be nil",
},
{
name: "bad/push",
eventType: "push",
triggerTarget: triggertype.Push.String(),
payloadEventStruct: github.PushEvent{},
wantErrString: "error parsing payload the repository should not be nil",
},
{
// specific run from a check_suite
name: "good/rerequest check_run on pull request",
Expand Down
2 changes: 1 addition & 1 deletion test/invalid_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestSkippedEvent(t *testing.T) {
assert.NilError(t, err)
defer resp.Body.Close()

assert.Equal(t, resp.StatusCode, http.StatusOK, "%s reply expected 200 OK", elURL)
assert.Equal(t, resp.StatusCode, http.StatusAccepted, "%s reply expected 202 OK", elURL)
}

func TestGETCall(t *testing.T) {
Expand Down

0 comments on commit ff7c7be

Please sign in to comment.