@relaymesh/githook
v0.0.11
Published
This SDK consumes githook events from Relaybus and decodes the protobuf `EventPayload` into a usable `Event` object.
Readme
TypeScript Worker SDK
This SDK consumes githook events from Relaybus and decodes the protobuf
EventPayload into a usable Event object.
Supported drivers: amqp, kafka, nats.
Install
npm install @relaymesh/githookInstall the Relaybus adapter(s) you plan to use:
npm install @relaymesh/relaybus-amqp
npm install @relaymesh/relaybus-kafka
npm install @relaymesh/relaybus-natsGithook Worker Example (rule-id only)
import * as worker from "@relaymesh/githook";
async function main() {
const wk = worker.New(
worker.WithEndpoint("http://localhost:8080"),
);
wk.HandleRule("rule-id", (ctx, event) => {
console.log(ctx.tenantId, event.provider, event.type, event.topic);
console.log(event.payload.toString("utf8"));
});
await wk.Run();
}
main().catch(console.error);Go-style example
import * as worker from "@relaymesh/githook";
const controller = new AbortController();
process.on("SIGINT", () => controller.abort());
process.on("SIGTERM", () => controller.abort());
const wk = worker.New(
worker.WithEndpoint("https://githook-app.vercel.app/api/connect"),
);
wk.HandleRule("85101e9f-3bcf-4ed0-b561-750c270ef6c3", (ctx, evt) => {
if (!evt) {
return;
}
console.log(
`topic=${evt.topic} provider=${evt.provider} type=${evt.type} installation=${evt.metadata["installation_id"]}`,
);
if (evt.payload.length > 0) {
console.log(`payload bytes=${evt.payload.length}`);
}
});
await wk.Run(controller.signal);API-driven drivers
Rule handlers are the recommended way to subscribe. If you need to bind handlers to explicit topics or driver IDs, you can still do so.
import * as worker from "@relaymesh/githook";
async function main() {
const subscriber = worker.buildSubscriber({
driver: "amqp",
amqp: { url: "amqp://guest:guest@localhost:5672/" },
});
const wk = worker.New(
worker.WithSubscriber(subscriber),
worker.WithEndpoint("http://localhost:8080"),
worker.WithAPIKey(process.env.GITHOOK_API_KEY ?? ""),
worker.WithTopics("pr.opened.ready"),
);
wk.HandleTopic("pr.opened.ready", "driver-id", (ctx, event) => {
console.log(ctx.requestId, event.topic);
});
await wk.Run();
}
main().catch(console.error);Rule handlers
const wk = worker.New();
wk.HandleRule("rule-id", (ctx, event) => {
console.log(event.topic, event.provider);
});OAuth2 for API calls
If you use OAuth2 instead of API keys for control-plane calls:
const wk = worker.New(
worker.WithEndpoint("http://localhost:8080"),
worker.WithOAuth2Config({
enabled: true,
clientId: process.env.GITHOOK_OAUTH2_CLIENT_ID ?? "",
clientSecret: process.env.GITHOOK_OAUTH2_CLIENT_SECRET ?? "",
tokenUrl: process.env.GITHOOK_OAUTH2_TOKEN_URL ?? "",
scopes: ["githook.read", "githook.write"],
}),
);Server-resolved SCM clients
const wk = worker.New(
worker.WithEndpoint("http://localhost:8080"),
worker.WithClientProvider(worker.NewRemoteSCMClientProvider()),
);
wk.HandleRule("rule-id", async (_ctx, evt) => {
const gh = worker.GitHubClient(evt);
if (gh) {
const repo = await gh.requestJSON("GET", "/repos/org/repo");
console.log(repo);
}
});