Skip to content
This repository has been archived by the owner on Jun 4, 2021. It is now read-only.

Port Github source to adapter/v2 #1230

Merged
merged 1 commit into from
May 25, 2020
Merged

Conversation

MIBc
Copy link
Contributor

@MIBc MIBc commented May 14, 2020

Fixes: #1199

Proposed Changes

  • Using v2 adapter in github source.

@googlebot googlebot added the cla: yes Indicates the PR's author has signed the CLA. label May 14, 2020
@knative-prow-robot knative-prow-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label May 14, 2020
@knative-prow-robot knative-prow-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label May 14, 2020
@knative-prow-robot
Copy link
Contributor

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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

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.

@MIBc
Copy link
Contributor Author

MIBc commented May 14, 2020

/assign @slinkydeveloper

@@ -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)
Copy link
Contributor

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

Copy link
Contributor

@slinkydeveloper slinkydeveloper left a 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?

@matzew
Copy link
Member

matzew commented May 15, 2020

/ok-to-test

@knative-prow-robot knative-prow-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels May 15, 2020
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)
Copy link
Contributor

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.

Comment on lines 50 to 124
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())
}
Copy link
Contributor

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.

@@ -47,13 +46,20 @@ type Adapter struct {
func New(sinkURI, ownerRepo string) (*Adapter, error) {
a := new(Adapter)
var err error
Copy link
Contributor

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.

@knative-prow-robot knative-prow-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels May 18, 2020
Copy link
Contributor

@slinkydeveloper slinkydeveloper left a 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?

}
}

// mockTransport is a simple fake HTTP transport
type mockTransport func(req *http.Request) (*http.Response, error)
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

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 {
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@slinkydeveloper
Copy link
Contributor

/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 K_SINK, NAME, NAMESPACE, METRICS_DOMAIN.

Check as always the GitlabSource for this 😄

@knative-prow-robot knative-prow-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label May 18, 2020
}
}
if string(got) != string(data) {
return fmt.Errorf("Expected %q event to be sent, got %q", data, string(got))
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if err != nil {
t.Fatal(err)
t.Error(err)
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines 486 to 487
req.Header.Set("X-GitHub-Event", tc.eventType)
req.Header.Set("X-GitHub-Delivery", eventID)
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if err != nil {
http.Error(w, "can not read body", http.StatusBadRequest)
return
return fmt.Errorf("Could not marshal sent payload: %v", err)
Copy link
Contributor

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.

Copy link
Contributor Author

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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"
Copy link
Contributor

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.

Copy link
Contributor Author

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"
Copy link
Contributor

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.

Copy link
Contributor Author

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. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@MIBc
Copy link
Contributor Author

MIBc commented May 19, 2020

/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 K_SINK, NAME, NAMESPACE, METRICS_DOMAIN.

Check as always the GitlabSource for this

All right. I will test it asap.

@MIBc
Copy link
Contributor Author

MIBc commented May 23, 2020

I have fixed the code. It can run normally. @slinkydeveloper

@antoineco
Copy link
Contributor

/lgtm

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label May 23, 2020
gotSubject := event.Context.GetSubject()
if tc.wantCloudEventSubject != "" && tc.wantCloudEventSubject != gotSubject {
return nil, fmt.Errorf("want subject %s, got %s", tc.wantCloudEventSubject, gotSubject)
t.Helper()
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@knative-prow-robot knative-prow-robot removed the lgtm Indicates that a PR is ready to be merged. label May 25, 2020
Copy link
Contributor

@slinkydeveloper slinkydeveloper left a 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


cloudEventType := sourcesv1alpha1.GitHubEventType(gitHubEventType)
subject := subjectFromGitHubEvent(gh.Event(gitHubEventType), payload)
subject := subjectFromGitHubEvent(gh.Event(gitHubEventType), payload, a.logger)

event := cloudevents.NewEvent(cloudevents.VersionV1)
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


func (h *fakeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
data, err := json.Marshal(tc.payload)
Copy link
Contributor

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)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@antoineco
Copy link
Contributor

/lgtm

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label May 25, 2020
@knative-prow-robot knative-prow-robot removed the lgtm Indicates that a PR is ready to be merged. label May 25, 2020
Copy link
Contributor

@slinkydeveloper slinkydeveloper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/approve

@knative-prow-robot knative-prow-robot added lgtm Indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels May 25, 2020
@knative-metrics-robot
Copy link

The following is the coverage report on the affected files.
Say /test pull-knative-eventing-contrib-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
github/pkg/adapter/adapter.go 96.5% 88.2% -8.4

@MIBc
Copy link
Contributor Author

MIBc commented May 25, 2020

/retest

@slinkydeveloper
Copy link
Contributor

/unhold
/lgtm
/approve

@knative-prow-robot knative-prow-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label May 25, 2020
@knative-prow-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cla: yes Indicates the PR's author has signed the CLA. lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Port Github source to adapter/v2
7 participants