Skip to content

Commit

Permalink
Context: Webhooks propagate request context to store calls (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
dm03514 authored Aug 5, 2019
1 parent 40fbcf5 commit c841730
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PKGS = $(shell go list ./... | grep -v /vendor/)

test-unit:
GO111MODULE=on go test -tags=unit $(PKGS)
GO111MODULE=on go test -tags=unit -coverprofile=coverage.out $(PKGS)

build:
GO111MODULE=on go build ./...
Expand Down
12 changes: 5 additions & 7 deletions github/eventtracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ type EventTracer struct {
traces traces.SpanStore
}

func (et *EventTracer) handleIssue(issue *github.IssuesEvent) error {
ctx := context.Background()
func (et *EventTracer) handleIssue(ctx context.Context, issue *github.IssuesEvent) error {
ie := IssuesEvent{issue}

log.WithFields(log.Fields{
Expand Down Expand Up @@ -63,8 +62,7 @@ func (et *EventTracer) handleIssue(issue *github.IssuesEvent) error {
return nil
}

func (et *EventTracer) handlePullRequest(pr *github.PullRequestEvent) error {
ctx := context.Background()
func (et *EventTracer) handlePullRequest(ctx context.Context, pr *github.PullRequestEvent) error {
pre := PREvent{pr}

log.WithFields(log.Fields{
Expand Down Expand Up @@ -121,13 +119,13 @@ func (et *EventTracer) handlePullRequest(pr *github.PullRequestEvent) error {
return nil
}

func (et *EventTracer) handleEvent(event interface{}) error {
func (et *EventTracer) handleEvent(ctx context.Context, event interface{}) error {
var err error
switch event := event.(type) {
case *github.IssuesEvent:
err = et.handleIssue(event)
err = et.handleIssue(ctx, event)
case *github.PullRequestEvent:
err = et.handlePullRequest(event)
err = et.handlePullRequest(ctx, event)
default:
err = fmt.Errorf("event type not supported, %+v", event)
}
Expand Down
38 changes: 21 additions & 17 deletions github/eventtracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,17 @@ func TestEventTracer_handleIssue_EndStateNoStartFound(t *testing.T) {
closed := "closed"
name := "name"
number := 1
err := gh.handleIssue(&github.IssuesEvent{
Action: &closed,
Repo: &github.Repository{
Name: &name,
},
Issue: &github.Issue{
Number: &number,
},
})
err := gh.handleIssue(
context.Background(),
&github.IssuesEvent{
Action: &closed,
Repo: &github.Repository{
Name: &name,
},
Issue: &github.Issue{
Number: &number,
},
})
assert.IsType(t, traces.SpanMissingError{}, err)
}

Expand Down Expand Up @@ -195,15 +197,17 @@ func TestNewEventTracer_handlePullRequest_TracePresent(t *testing.T) {
openedAction := "opened"
id := int64(1)

err := gh.handlePullRequest(&github.PullRequestEvent{
Action: &openedAction,
PullRequest: &github.PullRequest{
ID: &id,
Head: &github.PullRequestBranch{
Ref: &branchName,
err := gh.handlePullRequest(
context.Background(),
&github.PullRequestEvent{
Action: &openedAction,
PullRequest: &github.PullRequest{
ID: &id,
Head: &github.PullRequestBranch{
Ref: &branchName,
},
},
},
})
})
assert.NoError(t, err)
assert.Equal(t, 1, gh.spans.Count())
span, ok := gh.spans.Get(ctx, "1")
Expand Down
3 changes: 2 additions & 1 deletion github/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"bytes"
"context"
"net/http"
"net/url"
)
Expand All @@ -11,7 +12,7 @@ type StubTracer struct {
calls int
}

func (st *StubTracer) handleEvent(e interface{}) error {
func (st *StubTracer) handleEvent(ctx context.Context, e interface{}) error {
st.calls++
return st.ReturnValue
}
Expand Down
5 changes: 3 additions & 2 deletions github/webhook.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package github

import (
"context"
"github.com/ImpactInsights/valuestream/traces"
"github.com/google/go-github/github"
log "github.com/sirupsen/logrus"
Expand All @@ -9,7 +10,7 @@ import (
)

type tracer interface {
handleEvent(e interface{}) error
handleEvent(ctx context.Context, e interface{}) error
}

type Webhook struct {
Expand Down Expand Up @@ -58,7 +59,7 @@ func (webhook *Webhook) Handler(w http.ResponseWriter, r *http.Request) {
return
}

if err = webhook.et.handleEvent(event); err != nil {
if err = webhook.et.handleEvent(r.Context(), event); err != nil {
switch err.(type) {
case traces.SpanMissingError:
log.WithFields(log.Fields{
Expand Down
3 changes: 1 addition & 2 deletions jenkins/jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ type EventTracer struct {
traces traces.SpanStore
}

func (et *EventTracer) handleBuild(be *BuildEvent) error {
ctx := context.Background()
func (et *EventTracer) handleBuild(ctx context.Context, be *BuildEvent) error {
switch be.State() {
case startState:
parentID, found := be.ParentSpanID()
Expand Down
4 changes: 3 additions & 1 deletion jenkins/testing.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package jenkins

import "context"

type StubEventTracer struct {
ReturnValue error
calls int
}

func (s *StubEventTracer) handleBuild(be *BuildEvent) error {
func (s *StubEventTracer) handleBuild(ctx context.Context, be *BuildEvent) error {
s.calls++
return s.ReturnValue
}
5 changes: 3 additions & 2 deletions jenkins/webhook.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package jenkins

import (
"context"
"encoding/json"
log "github.com/sirupsen/logrus"
"net/http"
)

type tracer interface {
handleBuild(*BuildEvent) error
handleBuild(context.Context, *BuildEvent) error
}

type Webhook struct {
Expand All @@ -25,7 +26,7 @@ func (hook *Webhook) HTTPBuildHandler(w http.ResponseWriter, r *http.Request) {
}
defer r.Body.Close()

if err := hook.et.handleBuild(&build); err != nil {
if err := hook.et.handleBuild(r.Context(), &build); err != nil {
log.Warnf("Error handling build event: %v", err)
http.Error(w, "error", http.StatusBadRequest)
return
Expand Down

0 comments on commit c841730

Please sign in to comment.