UNPKG

2.45 kBMarkdownView Raw
1# Event Handlers
2
3Event handlers are triggered by events that create or update entities
4in the Atomist Cortex that meet certain criteria.
5
6## Setting Up a Subscription
7
8First, define a query representing the data you want to match on. You
9typically begin by exploring Cortex data using a GraphQL
10client. GraphiQL is a great choice.
11
12You will get assistance for writing your queries, and see the shape of
13the resulting data.
14
15![GraphiQL browser](images/graphiql.png)
16
17Once you're happy with your query, add it beginning with
18`subscription` to the `/graphql` directory of your project.
19
20Based on your query it is possible to generate TypeScript types that
21you can use in your handler code. In order to allow type generation
22you need to set up your project in the following way:
23
24```
25$ npm install --save-dev graphql-code-generator
26```
27
28And add the following to the `scripts` section of your `package.json`:
29
30```javascript
31 "gql:gen": "ql-gen --file node_modules/@atomist/automation-client/graph/schema.cortex.json --template typescript -m --out ./src/typings/ './graphql/**/*.graphql'"
32```
33
34Next, generate TypeScript types for your query returns and variables:
35
36```
37$ npm run gql:gen
38```
39
40You can rerun this command at any time, if you add or change queries.
41
42Now import the generated types in your event handlers.
43
44A subscription is set up by referencing the appropriate GraphQL file as follows, using the `EventHandler` decorator:
45
46```typescript
47@EventHandler("Event handler that notifies upstream PR of failed downstream build",
48 GraphQL.subscriptionFromFile("graphql/cascadeBuildCompleted"))
49@Tags("cascade", "build", "status")
50export class SetUpstreamStatusOnBuildCompletion implements HandleEvent<graphql.BuildCompleted.Subscription> {
51
52 @Secret(Secrets.ORG_TOKEN)
53 public githubToken: string;
54
55 public handle(root: EventFired<graphql.BuildCompleted.Subscription>,
56 ctx: HandlerContext): Promise<HandlerResult> {
57 // ... implementation
58 }
59```
60
61An event handler may do further filtering on the argument passed in,
62if it needs to apply criteria that cannot be expressed in GraphQL.
63
64Otherwise, the implementation of an event handler is similar to a
65command handler. It also returns a promise. However, the concept of
66a response message does not apply, as an event handler is not invoked
67from a command issued in a Slack channel. Even handlers can send
68messages directed to specific channels and/or users.