diff --git a/sample/README.md b/sample/README.md index 53bc605247f..fb9e4fd4aba 100644 --- a/sample/README.md +++ b/sample/README.md @@ -24,6 +24,7 @@ See [the docs repo](https://github.com/knative/docs/tree/master/eventing/samples A simple handler for processing k8s events from the local cluster. * [Binding running services to an IoT core](https://github.com/knative/docs/tree/master/eventing/samples/event-flow) - A sample using Google PubSub to read events from Google's IoT core. +* [Github Pull Request Handler](https://github.com/knative/docs/tree/master/eventing/samples/github-events) - + A simple handler for Github Pull Requests -* [Github Pull Request Handler](./github) - A simple handler for Github Pull Requests * [GCP PubSub Receiver Handler](./gcp_pubsub_function) - A simple handler for processing GCP PubSub events diff --git a/sample/github/README.md b/sample/github/README.md deleted file mode 100644 index af796115b44..00000000000 --- a/sample/github/README.md +++ /dev/null @@ -1,161 +0,0 @@ -# GitHub Flow - -A GitHub webhook will be created on a repository and a Knative `Service` will be -deployed to receive the webhook's event deliveries and forward them into a -`Channel`, through a `Bus`, and out to the consumer via a `Subscription`. The -`Flow` resource takes care of provisioning the webhook, the `Service`, the -`Channel`, and the `Subscription`. - -## Prerequisites - -1. [Setup your development environment](../../DEVELOPMENT.md#getting-started) -2. [Start Knative](../../README.md#start-knative) -3. For GitHub to be able to call into the cluster, - [configure a custom domain](https://github.com/knative/docs/blob/master/serving/using-a-custom-domain.md) and - [assign a static IP address](https://github.com/knative/docs/blob/master/serving/gke-assigning-static-ip-address.md). -4. Install a ClusterBus - -update `knative/eventing/config/buses/stub-bus.yaml`, changing `kind` to `ClusterBus`, like: - -```yaml -apiVersion: channels.knative.dev/v1alpha1 -kind: ClusterBus -metadata: - name: stub -spec: - dispatcher: - name: dispatcher - image: github.com/knative/eventing/pkg/buses/stub - args: [ - "-logtostderr", - "-stderrthreshold", "INFO", - ] -``` - -and apply the stub bus - -```shell -ko apply -f config/buses/stub -``` - -5. Install GitHub as an event source - -```shell -ko apply -f pkg/sources/github/ -``` - -5. Check that the GitHub is now showing up as an event source and there's an event type for *pullrequests* - -```shell -kubectl get eventsources -kubectl get eventtypes -``` - -6. Create a [personal access token](https://github.com/settings/tokens) to GitHub repo that you can use to register - webhooks with the GitHub API. Also decide on a token that your code will use to authenticate the incoming webhooks - from GitHub (*accessToken*). Update `sample/github/githubsecret.yaml` with those values. If your generated access - token is `'asdfasfdsaf'` and you choose your *secretToken* as `'password'`, you'd modify - `sample/github/githubsecret.yaml` like so: - -```yaml -apiVersion: v1 -kind: Secret -metadata: - name: githubsecret -type: Opaque -stringData: - githubCredentials: > - { - "accessToken": "asdfasfdsaf", - "secretToken": "password" - } -``` - -## Running - -In response to a pull request event, the _legit_ Service will add `(looks pretty legit)` to the PR title. - -Deploy the _legit_ service via: - -```shell -ko apply sample/github/legit-service.yaml -``` - -Once deployed, you can inspect the created resources with `kubectl` commands: - -```shell -# This will show the Service that we created: -kubectl get service.serving.knative.dev -o yaml - -# This will show the Route that the Service created: -kubectl get route -o yaml - -# This will show the Configuration that the Service created: -kubectl get configurations -o yaml - -# This will show the Revision that was created by the Configuration: -kubectl get revisions -o yaml -``` - -The `Flow` will accept the Webhook calls from GitHub and pass them to the _legit_ service. - -You can inspect those resources with the following `kubectl` commands: - -```shell -# This will show the available EventSources that you can generate feeds from: -kubectl get eventsources -o yaml - -# This will show the available EventTypes that you can generate feeds from: -kubectl get eventtypes -o yaml -``` - -Configure the `Flow` to point to a GitHub repository you are able to control and update -`spec.trigger.resource` in `samples/github/flow.yaml`. - -And then apply the resources: - -```shell -ko apply -f sample/github/auth.yaml -ko apply -f sample/github/flow.yaml -``` - -Once deployed, you can inspect the created resources with `kubectl` commands: - -```shell -# This will show the available Flows we created: -kubectl get flows -o yaml - -# This will show the available Feeds created from the Flow: -kubectl get feeds -o yaml - -# This will show the available Channels created from the Flow: -kubectl get channels -o yaml - -# This will show the available Subscriptions created from the Flow: -kubectl get subscriptions -o yaml - -# This will show the available ClusterBuses we created in the prerequisites: -kubectl get clusterbuses -o yaml -``` - -Then create the secret so that you can see changes: - -```shell - ko apply -f sample/github/githubsecret.yaml -``` - -Then create a PR for the repo you configured the webhook for, and you'll see that the Title -will be modified with the suffix '(looks pretty legit)' - -## Cleaning up - -To clean up the sample `Flow`: -```shell - ko delete -f sample/github/flow.yaml -``` - -And you can check the repo and see the webhook has been removed - -```shell -ko delete -f sample/github/ -``` diff --git a/sample/github/auth.yaml b/sample/github/auth.yaml deleted file mode 100644 index 45a5a8c15a0..00000000000 --- a/sample/github/auth.yaml +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright 2018 The Knative Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -apiVersion: v1 -kind: ServiceAccount -metadata: - name: feed-sa - namespace: default ---- -apiVersion: rbac.authorization.k8s.io/v1beta1 -kind: ClusterRoleBinding -metadata: - name: feed-admin -subjects: - - kind: ServiceAccount - name: feed-sa - namespace: default -roleRef: - kind: ClusterRole - name: cluster-admin - apiGroup: rbac.authorization.k8s.io diff --git a/sample/github/flow.yaml b/sample/github/flow.yaml deleted file mode 100644 index 150bfcd693e..00000000000 --- a/sample/github/flow.yaml +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright 2018 The Knative Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -apiVersion: flows.knative.dev/v1alpha1 -kind: Flow -metadata: - name: github-flow - namespace: default -spec: - serviceAccountName: feed-sa - trigger: - eventType: pullrequest - resource: / # TODO: Fill this out - service: github - parameters: - secretName: githubsecret - secretKey: githubCredentials - parametersFrom: - - secretKeyRef: - name: githubsecret - key: githubCredentials - action: - target: - kind: Route - apiVersion: serving.knative.dev/v1alpha1 - name: legit diff --git a/sample/github/githubsecret.yaml b/sample/github/githubsecret.yaml deleted file mode 100644 index 0a7f3da5e3b..00000000000 --- a/sample/github/githubsecret.yaml +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2018 The Knative Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -apiVersion: v1 -kind: Secret -metadata: - name: githubsecret -type: Opaque -stringData: - githubCredentials: > - { - "accessToken": "", - "secretToken": "" - } diff --git a/sample/github/legit-service.yaml b/sample/github/legit-service.yaml deleted file mode 100644 index 28fabed5aca..00000000000 --- a/sample/github/legit-service.yaml +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright 2018 The Knative Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -apiVersion: serving.knative.dev/v1alpha1 -kind: Service -metadata: - name: legit -spec: - runLatest: - configuration: - revisionTemplate: - metadata: - labels: - knative.dev/type: function - spec: - container: - image: github.com/knative/eventing/sample/github - env: - - name: GITHUB_SECRET - valueFrom: - secretKeyRef: - key: githubCredentials - name: githubsecret diff --git a/sample/github/legit.go b/sample/github/legit.go deleted file mode 100644 index bde49f65c79..00000000000 --- a/sample/github/legit.go +++ /dev/null @@ -1,108 +0,0 @@ -/* -Copyright 2018 The Knative Authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "context" - "encoding/json" - "flag" - "fmt" - ghclient "github.com/google/go-github/github" - "github.com/knative/eventing/pkg/event" - "golang.org/x/oauth2" - "gopkg.in/go-playground/webhooks.v3/github" - "log" - "net/http" - "os" - "strings" -) - -const ( - // Environment variable containing json credentials - envSecret = "GITHUB_SECRET" - // this is what we tack onto each PR title if not there already - titleSuffix = "looks pretty legit" -) - -// GithubHandler holds necessary objects for communicating with the Github. -type GithubHandler struct { - client *ghclient.Client - ctx context.Context -} - -type GithubSecrets struct { - AccessToken string `json:"accessToken"` - SecretToken string `json:"secretToken"` -} - -func (h *GithubHandler) newPullRequestPayload(ctx context.Context, pl *github.PullRequestPayload) { - - title := pl.PullRequest.Title - log.Printf("GOT PR with Title: %q", title) - - // Check the title and if it contains 'looks pretty legit' leave it alone - if strings.Contains(title, titleSuffix) { - // already modified, leave it alone. - return - } - - newTitle := fmt.Sprintf("%s (%s)", title, titleSuffix) - updatedPR := ghclient.PullRequest{ - Title: &newTitle, - } - newPR, response, err := h.client.PullRequests.Edit(h.ctx, - pl.Repository.Owner.Login, pl.Repository.Name, int(pl.Number), &updatedPR) - if err != nil { - log.Printf("Failed to update PR: %s\n%s", err, response) - return - } - if newPR.Title != nil { - log.Printf("New PR Title: %q", *newPR.Title) - } else { - log.Printf("New PR title is nil") - } -} - -func main() { - flag.Parse() - githubSecrets := os.Getenv(envSecret) - - var credentials GithubSecrets - err := json.Unmarshal([]byte(githubSecrets), &credentials) - if err != nil { - log.Fatalf("Failed to unmarshal credentials: %s", err) - return - } - - // Set up the auth for being able to talk to Github. It's - // odd that you have to also pass context around for the - // calls even after giving it to client. But, whatever. - ctx := context.Background() - ts := oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: credentials.AccessToken}, - ) - tc := oauth2.NewClient(ctx, ts) - - client := ghclient.NewClient(tc) - - h := &GithubHandler{ - client: client, - ctx: ctx, - } - - log.Fatal(http.ListenAndServe(":8080", event.Handler(h.newPullRequestPayload))) -}