|
| 1 | +# Connector Developer’s Guide |
| 2 | + |
| 3 | +## Build a connector (step-by-step guide) |
| 4 | + |
| 5 | +In this tutorial, we’ll explain how to create a connector that will |
| 6 | +allow you to extract data from a source and produce records in an |
| 7 | +structure data form. |
| 8 | + |
| 9 | +To get you up and running quickly, we have built a [scaffolding |
| 10 | +template tool](https://github.com/codeclimate/create-codeclimate-connector) |
| 11 | +that will create a new project with some scaffolding and stubbed |
| 12 | +unit tests, and is setup to use TypeScript. |
| 13 | + |
| 14 | +For the purposes of this exercise, we're going to build a Connector |
| 15 | +with PagerDuty. |
| 16 | + |
| 17 | +1. Create a new Connector by running `yarn create codeclimate-connector <connector-slug>`. |
| 18 | +For this example, we will run ``yarn create codeclimate-connector pagerduty`. The project |
| 19 | +will be created in a directory called `./codeclimate-connector-pagerduty`. |
| 20 | + |
| 21 | +2. Take a look at the created `README.md` and `src/Client.ts` in your project to get |
| 22 | +familiar with a Connector implementation. |
| 23 | + |
| 24 | +3. Write a valid configuration to `connector-config.json`. This will depend of the |
| 25 | +data source you're integrating with. In this case, PagerDuty requires a valid |
| 26 | +[API token](https://v2.developer.pagerduty.com/docs/authentication) to be able to make |
| 27 | +requests to their API. Implement the `verifyConfiguration()` method to validate |
| 28 | +the token: |
| 29 | + |
| 30 | +``` |
| 31 | + if (!this.apiTokenPresent()) { |
| 32 | + return Promise.resolve({ |
| 33 | + isValid: false, |
| 34 | + errorMessages: ["apiToken must be present"], |
| 35 | + }) |
| 36 | + } |
| 37 | +
|
| 38 | + return this.buildApiClient().get("me"). |
| 39 | + then(() => { |
| 40 | + return { isValid: true } |
| 41 | + }). |
| 42 | + catch((err) => { |
| 43 | + let msg = "An error occurred" |
| 44 | +
|
| 45 | + if (err instanceof ResponseError) { |
| 46 | + msg = err.message |
| 47 | + } |
| 48 | +
|
| 49 | + return { |
| 50 | + isValid: false, |
| 51 | + errorMessages: [msg], |
| 52 | + } |
| 53 | + }) |
| 54 | +``` |
| 55 | + |
| 56 | +4. A connector is able to consume from a stream or streams. In the case of |
| 57 | +PagerDuty, there's no concept of "projects" the connector can subscribe too, |
| 58 | +so we will emit a single Stream for the account the configured API token by |
| 59 | +implement the `discoverStream()` method: |
| 60 | + |
| 61 | +``` |
| 62 | + return new Promise((resolve, _reject) => { |
| 63 | + this.recordProducer.produce({ |
| 64 | + record: { |
| 65 | + _type: "Stream", |
| 66 | + id: "unavailable", |
| 67 | + self: "https://pagerduty.com", |
| 68 | + name: "PagerDuty Account", |
| 69 | + } |
| 70 | + }) |
| 71 | +
|
| 72 | + resolve() |
| 73 | + }) |
| 74 | +``` |
| 75 | + |
| 76 | +5. Sync data from PagerDuty from an earliest data cutoff data for the |
| 77 | + stream returned by `discoverStream()`. The recommended pattern for |
| 78 | + Connectors is to start by fetching recent data, and walk backwards in time. When |
| 79 | + implementing that pattern, the `Client` should stop when it encounters data |
| 80 | + from before `earliestDataCutoff`. When publishing records, an exception will be |
| 81 | + raised with details when an invalid record is produced. |
| 82 | + |
| 83 | +6. To ensure the project's current code has been compiled into `./lib` for |
| 84 | + execution, run `yarn build`. |
| 85 | +7. Then, to run a sync, run the following (replace `YYYY-MM-DD` with the date |
| 86 | + you want to sync back to): |
| 87 | + |
| 88 | + ``` |
| 89 | + yarn run \ |
| 90 | + codeclimate-connector sync-stream \ |
| 91 | + pagerduty \ |
| 92 | + connector-config.json \ |
| 93 | + '$(shell cat stream.json)' \ |
| 94 | + YYYY-MM-DD |
| 95 | + ``` |
| 96 | +
|
| 97 | +Go to [PagerDuty Connector repository](https://github.com/codeclimate/codeclimate-connector-pagerduty) to dive in the implementation. |
| 98 | +
|
| 99 | +## Publishing your connector |
| 100 | +
|
| 101 | +Use `yarn publish` to release a new version of the package. This will prompt you to |
| 102 | +enter a new version number, release it on npm, and then push the new version to GitHub. |
0 commit comments