-
Notifications
You must be signed in to change notification settings - Fork 225
Conversation
Hi @MIBc. Thanks for your PR. I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
/assign @slinkydeveloper |
github/pkg/adapter/adapter.go
Outdated
@@ -85,10 +91,16 @@ func (a *Adapter) handleEvent(payload interface{}, hdr http.Header) error { | |||
event.SetSource(a.source) | |||
event.SetSubject(subject) | |||
event.SetDataContentType(cloudevents.ApplicationJSON) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need this line anymore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're not using adapter/v2, you ported to sdk-go v2. Can you check how the adapter works in gitlab and try to do the same?
/ok-to-test |
github/pkg/adapter/adapter.go
Outdated
result := a.client.Send(context.Background(), event) | ||
if !cloudevents.IsACK(result) { | ||
log.Printf("Cloud Event delivery error: %v", result) | ||
return fmt.Errorf("failed to send cloudevent: %v", result) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
%v
-> %w
so that the caller can unwrap the result if needed.
github/pkg/adapter/adapter.go
Outdated
if err != nil { | ||
log.Fatalf("failed to create http protocol: %s", err.Error()) | ||
} | ||
a.client, err = cloudevents.NewClient(p, cloudevents.WithUUIDs(), cloudevents.WithTimeNow()) | ||
if err != nil { | ||
log.Fatalf("failed to create client: %s", err.Error()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a mix of fatal and returned errors in this function.
Since the function signature includes an error return value, we should always return the error, and let the caller fail however it wants to fail.
github/pkg/adapter/adapter.go
Outdated
@@ -47,13 +46,20 @@ type Adapter struct { | |||
func New(sinkURI, ownerRepo string) (*Adapter, error) { | |||
a := new(Adapter) | |||
var err error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove this since you assign to a new err
below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now it looks good! @antoineco can you do another review pass please?
github/pkg/adapter/adapter_test.go
Outdated
} | ||
} | ||
|
||
// mockTransport is a simple fake HTTP transport | ||
type mockTransport func(req *http.Request) (*http.Response, error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like mockTransport is dead code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
github/pkg/adapter/adapter_test.go
Outdated
if err != nil { | ||
if diff := cmp.Diff(tc.wantErrMsg, err.Error()); diff != "" { | ||
return fmt.Errorf("incorrect error (-want, +got): %v", diff) | ||
if err := tc.validateAcceptedPayload(t, ceClient); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add asserts here also for ce attributes like subject, id, type, source, type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
/hold @MIBc can you try the source to check if it's working? I think the receive adapter implementation could break if you don't pass some/all variables like Check as always the GitlabSource for this 😄 |
github/pkg/adapter/adapter_test.go
Outdated
} | ||
} | ||
if string(got) != string(data) { | ||
return fmt.Errorf("Expected %q event to be sent, got %q", data, string(got)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data
will be printed as a byte array here unless you cast it to string as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
github/pkg/adapter/adapter_test.go
Outdated
if err != nil { | ||
t.Fatal(err) | ||
t.Error(err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fatal
was correct. If this step fails, the rest of the test has no chance to succeed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
github/pkg/adapter/adapter_test.go
Outdated
req.Header.Set("X-GitHub-Event", tc.eventType) | ||
req.Header.Set("X-GitHub-Delivery", eventID) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"X-GitHub-Event" -> GHHeaderEvent
"X-GitHub-Delivery" -> GHHeaderDelivery
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
github/pkg/adapter/adapter_test.go
Outdated
if err != nil { | ||
if diff := cmp.Diff(tc.wantErrMsg, err.Error()); diff != "" { | ||
return fmt.Errorf("incorrect error (-want, +got): %v", diff) | ||
if err := tc.validateAcceptedPayload(t, ceClient); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of returning an error here just to call t.Error
on it, you could simply fail inside the validateAcceptedPayload
function using t.Error
or t.Fatal
. This is a very common pattern in tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it
github/pkg/adapter/adapter_test.go
Outdated
h := &fakeHandler{ | ||
//handler: tc.sink, | ||
handler: sinkAccepted, // No tests expect the sink to do anything interesting | ||
func (tc *testCase) validateAcceptedPayload(t *testing.T, ce *adaptertest.TestCloudEventsClient) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is a test helper, so it should start with t.Helper()
so that the lines where error occur are correctly reported in logs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
github/pkg/adapter/adapter_test.go
Outdated
if err != nil { | ||
http.Error(w, "can not read body", http.StatusBadRequest) | ||
return | ||
return fmt.Errorf("Could not marshal sent payload: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like I said in a previous comment, it would be better to fail here by calling t.Fatal
instead of returning an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@@ -407,231 +409,108 @@ var testCases = []testCase{ | |||
}, | |||
} | |||
|
|||
// mockTransport is a simple fake HTTP transport | |||
type mockTransport func(req *http.Request) (*http.Response, error) | |||
func newTestAdapter(t *testing.T, ce cloudevents.Client) *gitHubAdapter { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The t
variable is unused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The t
is used to set fake context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I didn't even see that :)
"testing" | ||
"time" | ||
|
||
"knative.dev/eventing-contrib/github/pkg/apis/sources/v1alpha1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's group this import together with the other third-party packages. Or even better: group all knative.dev
imports together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
||
cloudevents "github.com/cloudevents/sdk-go" | ||
sourcesv1alpha1 "knative.dev/eventing-contrib/github/pkg/apis/sources/v1alpha1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's group this import together with the other third-party packages. Or even better: group all knative.dev
imports together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank for your review. I wil think about them. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
All right. I will test it asap. |
I have fixed the code. It can run normally. @slinkydeveloper |
/lgtm |
github/pkg/adapter/adapter_test.go
Outdated
gotSubject := event.Context.GetSubject() | ||
if tc.wantCloudEventSubject != "" && tc.wantCloudEventSubject != gotSubject { | ||
return nil, fmt.Errorf("want subject %s, got %s", tc.wantCloudEventSubject, gotSubject) | ||
t.Helper() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be inside the validateAcceptedPayload
, on the very first line of the function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two nits and we're ready to go
github/pkg/adapter/adapter.go
Outdated
|
||
cloudEventType := sourcesv1alpha1.GitHubEventType(gitHubEventType) | ||
subject := subjectFromGitHubEvent(gh.Event(gitHubEventType), payload) | ||
subject := subjectFromGitHubEvent(gh.Event(gitHubEventType), payload, a.logger) | ||
|
||
event := cloudevents.NewEvent(cloudevents.VersionV1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove the VersionV1
in order to use always the latest version? just invoke NewEvent
without parameters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
github/pkg/adapter/adapter_test.go
Outdated
|
||
func (h *fakeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
body, err := ioutil.ReadAll(r.Body) | ||
data, err := json.Marshal(tc.payload) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's a json always avoid byte array/string comparisons since the serialization order of the fields could be not deterministic, so the assertion below string(got) != string(data)
could fail even if the JSONs are semantically equivalent.
Parse both tc.payload and sent event data into interface{}
and then use reflect.DeepEqual
or cmp.Diff
:
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("unexpected data (-want, +got) = %v", diff)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You didn't parsed the json:
var want interface{}
json.Unmarshal(tc.payload, &want)
etc...
@@ -47,13 +47,24 @@ func MakeService(args *ServiceArgs) *v1.Service { | |||
SecretKeyRef: args.Source.Spec.SecretToken.SecretKeyRef, | |||
}, | |||
}, { | |||
Name: "SINK", | |||
Name: "K_SINK", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
/lgtm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
/approve
The following is the coverage report on the affected files.
|
/retest |
/unhold |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: MIBc, slinkydeveloper The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Fixes: #1199
Proposed Changes