Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions streaming/webhooks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.chiseld.db*
/.eventgen
/.routegen
/node_modules
5 changes: 5 additions & 0 deletions streaming/webhooks/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"chiselstrike"
]
}
5 changes: 5 additions & 0 deletions streaming/webhooks/Chisel.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
models = ["models"]
endpoints = ["endpoints"]
events = ["events"]
policies = ["policies"]
modules = "node"
28 changes: 28 additions & 0 deletions streaming/webhooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# GitHub Webhooks to Kafka

## Getting Started

First, start up the server:

```console
npm run dev -- --kafka-connection localhost:909
```

Then, set up a HTTP tunnel to the server (we need this for Github to send webhooks to):

```console
ssh -R 80:localhost:8080 localhost.run
```

Create a Github test repository and cnfigure webhooks on your Github repository via `Settings > Webhooks > Add webhook`:

* Set the "Payload URL" to the HTTP tunnel you created. For example `https://7380ea147b74b6.lhr.life/dev/github-webhook`.
* Set the "Content type" to "JSON".

Perform a `git push` on the git repository.

A new deployment is created:

```console
curl localhost:8080/dev/deployment
```
3 changes: 3 additions & 0 deletions streaming/webhooks/endpoints/deployment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Deployment } from "../models/Deployment.ts";

export default Deployment.crud();
18 changes: 18 additions & 0 deletions streaming/webhooks/endpoints/github-webhook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { publishEvent } from "@chiselstrike/api";

export default async function (req: Request) {
const event = await req.json();
const repository = event["repository"]["full_name"];
const pusher = event["pusher"]["name"];
const head = event["head_commit"]["id"];
const deployment = {
pusher,
head,
};
await publishEvent({
topic: "deployment",
key: repository,
value: JSON.stringify(deployment),
});
return new Response("ok");
}
13 changes: 13 additions & 0 deletions streaming/webhooks/events/deployment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ChiselEvent } from "@chiselstrike/api";
import { Deployment } from "../models/Deployment.ts";

export default async function (event: ChiselEvent) {
const repository = await event.key.text();
const deployment = JSON.parse(await event.value.text());
console.log(`Deploying to ${repository} ...`);
await Deployment.upsert({
restrictions: { repository },
create: { repository, ...deployment },
update: deployment,
});
}
10 changes: 10 additions & 0 deletions streaming/webhooks/models/Deployment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ChiselEntity } from "@chiselstrike/api";

export class Deployment extends ChiselEntity {
/** The GitHub repository to deploy from. */
repository: string;
/** The username that inititated the deployment. */
pusher: string;
/** The head git commit ID of the deployment. */
head: string;
}
Loading