Skip to content

Convert github source to cloud events.#340

Merged
knative-prow-robot merged 3 commits into
knative:masterfrom
n3wscott:github-cloudevents
Aug 13, 2018
Merged

Convert github source to cloud events.#340
knative-prow-robot merged 3 commits into
knative:masterfrom
n3wscott:github-cloudevents

Conversation

@n3wscott
Copy link
Copy Markdown
Contributor

@n3wscott n3wscott commented Aug 10, 2018

This is a followup to #276. The focus is to migrate the Source and Sample to use the cloudevent naming style and the eventing framework.

Fixes #334

Fixes #332

@knative-prow-robot knative-prow-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 10, 2018

- dev.knative.github.pullrequest

Uses two images, the feedlet and the receive adapter.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does the github source require any parameters to be set? If so, what do they mean?

Comment thread pkg/sources/github/feedlet/feedlet.go Outdated
}
switch eventType {
case "pullrequest":
case pullrequestEvent:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This feels like it should be a map[string]string, or a map[string][]string, rather than a switch.

Using a map would also make it easier to report the known event types on line 462.

source := pl.PullRequest.HTMLURL

var eventType string
if len(header["X-GitHub-Event"]) == 1 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Try:

// global
ceTypes = map[string]string{"pull_request": github.PullrequestEvent, "": "unknown"}

...
eventType := ceTypes[header.Get("x-github-event")]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oh nice!


}

eventID := "unknown"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Again, prefer Header.Get


}

eventID := "unknown"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also, should this be a UUID or something if not supplied, rather than a deterministic value?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added uuid

return err
}

func postMessage(target string, m *gh.PullRequestPayload, source, eventType, eventID string) error {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you leave m as an interface{}, can you avoid the cast in 59 (which could panic), and support multiple payload types idiomatically? (This may not work, I haven't tested.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this is good.

}
defer resp.Body.Close()
log.Printf("Error: response Status: %s", resp.Status)
log.Printf("response Status: %s", resp.Status)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we want to/should we log these?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I moved it to only log on error.

@n3wscott
Copy link
Copy Markdown
Contributor Author

/retest

2 similar comments
@n3wscott
Copy link
Copy Markdown
Contributor Author

/retest

@srinivashegde86
Copy link
Copy Markdown
Contributor

/retest


```

The Feedlet requires a ServiceAccount to run as a cluster admin for the targeted namespace:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's unfortunate that we can't put this in a yaml file somewhere to happen at the same time as the controller is installed/the source is created.

"": UnsupportedEvent,
}

var GithubEventType = map[string]string{
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this just the reverse of CloudEventType?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, is there a better way?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Build it automatically, like:

var GithubEventType map[string]string
for k, v := range(CloudEventType) {
  GitHubEventType[v] = k
}

kind: EventType
metadata:
name: pullrequest
name: dev.knative.github.pullrequest
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Self-registration would make it easier to make this name align with the one in events.go

// TODO: Add more supported event types.
default:
event, ok := github.GithubEventType[eventType]
if !ok {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You may also be able to check if event == ""

pl := payload.(github.PullRequestPayload)
postMessage(h.target, &pl)

hdr := http.Header(header)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think a webhooks.Header is an http.Header, so it should already have .Get().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I do not have access to .Get() from the webhooks.Header object for some reason, it was a compile error when I was trying.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Aha -- http.Header is an http.Header, which is a map[string[]string. But since it has a different type name, methods which take an http.Header as a receiver don't apply to webhooks.Header.

So glad they "minimize imports": https://github.com/go-playground/webhooks/blob/v3.13.0/webhooks.go#L8

It looks like this declaration was removed in v5.


source := pl.PullRequest.HTMLURL

eventType, ok := github.CloudEventType[hdr.Get("X-GitHub-Event")]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

github.CloudEventType should map "" to UnsupportedEvent

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It does, this catches all other github events that have a real name but are not "" and "pull_request"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, right. I was thinking about the "header missing" case.

// TODO: in general, receive adapters may have to be able to retry for error cases.
log.Printf("response Status: %s", resp.Status)
body, _ := ioutil.ReadAll(resp.Body)
log.Printf("response Body: %s", string(body))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If this is a 500, should we return an error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

/idk

There are some questions here that are bigger than this PR around what the Receive Adapter should do if it gets an error from the "action". Should it try again? How many times?...

@n3wscott
Copy link
Copy Markdown
Contributor Author

Grant for LGTM:
/cc @grantr

Evan for Approval:
/assign @evankanderson

Copy link
Copy Markdown
Member

@evankanderson evankanderson 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

"": UnsupportedEvent,
}

var GithubEventType = map[string]string{
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Build it automatically, like:

var GithubEventType map[string]string
for k, v := range(CloudEventType) {
  GitHubEventType[v] = k
}

pl := payload.(github.PullRequestPayload)
postMessage(h.target, &pl)

hdr := http.Header(header)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Aha -- http.Header is an http.Header, which is a map[string[]string. But since it has a different type name, methods which take an http.Header as a receiver don't apply to webhooks.Header.

So glad they "minimize imports": https://github.com/go-playground/webhooks/blob/v3.13.0/webhooks.go#L8

It looks like this declaration was removed in v5.


source := pl.PullRequest.HTMLURL

eventType, ok := github.CloudEventType[hdr.Get("X-GitHub-Event")]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, right. I was thinking about the "header missing" case.

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Aug 13, 2018
@knative-prow-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: evankanderson, n3wscott

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details 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

@knative-prow-robot knative-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 13, 2018
@knative-prow-robot knative-prow-robot merged commit 8536f0e into knative:master Aug 13, 2018
n3wscott added a commit to n3wscott/eventing that referenced this pull request Aug 14, 2018
* Convert github source to cloud events.

* Add more words to the readme.

* Use cloud event map and updat the readme.
matzew added a commit to matzew/eventing that referenced this pull request Nov 20, 2019
mgencur pushed a commit to mgencur/eventing that referenced this pull request Sep 21, 2023
* Added hostnames to tls certificates



* Update broker-filter-tls-certificate.yaml



---------

Signed-off-by: Calum Murray <cmurray@redhat.com>
Co-authored-by: Pierangelo Di Pilato <pierangelodipilato@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants