# GitHub Triggers for Managed Agent Automations

> Start a Type.com managed agent automation when a pull request, issue, push, review, release, or CI run changes in GitHub.

Canonical page: https://docs.type.com/automations/github/

A GitHub trigger starts a managed agent automation when something changes in a repository. There are two ways to use GitHub events:

- **With instructions.** The managed agent follows your instructions for each GitHub event.
- **With code.** Your TypeScript runs for each GitHub event. It can filter events, read more with the GitHub CLI in the sandbox, and pass a step to an agent stage.

## Before you start

- [Connect GitHub](https://docs.type.com/connectors-and-skills/github/index.md) to Type.com, and add the connection to the Space. In the CLI, run `type-cli integrations connect github`.
- Make sure that the GitHub App can see the repository. It must also subscribe to the events that you need.
- Make sure that the Space has the skills that the managed agent needs, for example a review skill.
- For a code automation, get the GitHub connection ID (`orgint_...`) and the Space ID (`ch_...`). To see Space IDs, run `type-cli spaces list --json`.

## With instructions

In an instruction automation, a GitHub event starts work in the Space. The managed agent follows your instructions, with the Space's connections and skills.

Examples:

- Review new pull requests with the team's review skill.
- Summarize new issues and suggest an owner.
- Post a short note when a release is published.

### Make an instruction automation

The same creation flow handles schedules and incoming events. Existing legacy sources stay under **Synced events**.

1. Open the Space's **Automations** page (or **Space settings → Automations** in the settings layout).
2. Select **New automation**.
3. Choose **GitHub** in **Trigger**.
4. If GitHub has more than one connection, choose the connection.
5. Write the required instructions describing what to do with each event. To use a skill, write its handle, for example `/pr-review`.
6. Select **Create automation**. No test event is required.

In the CLI:

```sh
type-cli automations create --space <space> --source github --integration <org-integration-id> \
  --name "GitHub triage" --instructions "Summarize relevant changes; take no action on other events." --enable
```

Without `--enable`, the CLI creates the automation paused.

The new automation subscribes to every GitHub event from the connection (`github.*`). Each event starts a run:

1. An agent stage follows your instructions, with the GitHub event as its input.
2. A code stage records the agent's result: `completed` or `no_results`, with a short summary.

The automation decides whether to post a message or take another action. You can later change it to use code, agent work, or both.

### Keep runs focused

Every event that GitHub sends starts a run, including events that do not matter to you. Say in the instructions which events matter, and tell the managed agent to stop with no action for the others. To stop runs for other events, change the subscription's `events` or add a [GitHub matcher](https://docs.type.com/automations/reference/matchers/index.md#github-matchers) in the automation's code.

## With code

A code automation runs your TypeScript when a GitHub event comes. To learn the basics, see [Core concepts](https://docs.type.com/automations/how-it-works/index.md). For more complete automations, see [Diagnose CI failures](https://docs.type.com/automations/examples/ci-failure-review/index.md) and [Fix one security finding a day](https://docs.type.com/automations/examples/security-audit-fix/index.md).

This example makes a code automation that posts in a Space when a pull request is merged into `main`.

### Post when a pull request is merged

```ts title="src/main.ts"

const github = "orgint_example"; // the GitHub connection
const space = "ch_example"; // this Space

const pullRequestEvent = z.object({
  type: z.string(),
  id: z.string(),
  resourceId: z.string().nullable(),
  resourceType: z.string().nullable(),
  data: z.unknown(),
});
const pullRequest = z.object({
  repositoryFullName: z.string(),
  title: z.string(),
  url: z.string(),
});

export default automation({
  output: z.object({ messageId: z.string() }),
  deployment: {
    subscriptions: {
      onMerged: {
        title: "Merged into main",
        description: "Post in the Space when a pull request is merged into main",
        source: {
          kind: "integration",
          connectionId: github,
          events: ["github.pull_request.closed"],
        },
        match: {
          "github.pull_request": {
            repositories: ["acme/project"],
            merged: true,
            baseBranches: ["main"],
          },
        },
        entry: "announce",
      },
    },
  },
  stages: {
    announce: code({
      input: pullRequestEvent,
      run: async ({ ctx, input: event }) => {
        const pr = pullRequest.safeParse(event.data);
        if (!pr.success)
          return err(
            new TypeApiError("Unexpected pull request data", "invalid_input"),
          );
        const { repositoryFullName, title, url } = pr.data;
        const sent = await ctx.write.sendMessage(
          { channelId: space, content: `Merged into main: ${title}\n${url}` },
          { effectKey: `merged:${repositoryFullName}/${event.resourceId}` },
        );
        if (sent.isErr()) return err(sent.error);
        return complete({ messageId: sent.value.messageId });
      },
    }),
  },
});
```

- `events: ["github.pull_request.closed"]` selects closed pull requests. GitHub sends `closed` for merged pull requests too.
- The `match` field uses a [GitHub matcher](https://docs.type.com/automations/reference/matchers/index.md#github-matchers). It selects the repository, merged pull requests, and the base branch. An event that it skips ends without running your code.
- With no `schema`, the entry stage gets the generic event envelope, and `data` is unchecked. The code checks the fields that it uses and fails the stage when they are missing.
- The effect key uses the repository and the pull request number (`resourceId`), so each merge posts once. Effect keys can use letters, digits, and `.`, `_`, `:`, `/`, `-`.

### Test the example

The example posts with `ctx.write.sendMessage`. Fixtures cannot answer that call, so a fixture cannot test the path that posts. See [Calls that fixtures cannot answer](https://docs.type.com/automations/testing/index.md#calls-that-fixtures-cannot-answer).

Test the example in two parts:

1. Use fixtures for the paths that end before the post. Test a pull request that the matcher skips. Also test a pull request with data that the code rejects. A GitHub matcher needs the normalized fields of its event, for example `title` for a pull request, so a fixture payload must include them.

    ```json title="fixtures.json"
    [
      {
        "name": "skips a closed pull request that is not merged",
        "subscriptionId": "onMerged",
        "payload": {
          "type": "github.pull_request.closed",
          "id": "delivery-1",
          "resourceType": "pull_request",
          "resourceId": "42",
          "data": { "repositoryFullName": "acme/project", "merged": false, "baseBranch": "main", "title": "Draft idea", "url": "https://github.com/acme/project/pull/42" }
        },
        "toolResponses": []
      },
      {
        "name": "fails on pull request data without a URL",
        "subscriptionId": "onMerged",
        "payload": {
          "type": "github.pull_request.closed",
          "id": "delivery-2",
          "resourceType": "pull_request",
          "resourceId": "43",
          "data": { "repositoryFullName": "acme/project", "merged": true, "baseBranch": "main", "title": "Fix login" }
        },
        "toolResponses": [],
        "expectedError": "Unexpected pull request data"
      }
    ]
    ```

2. Test the post in a live run. First, set `repositories` to a test repository. Merge a test pull request into `main`. Then read the run:

    ```sh
    type-cli automations history --space <space> --automation <automation-id>
    ```

    When the run is correct, change `repositories` to the real repository and deploy again.

### Event names

GitHub event names have the form `github.<event>.<action>`. `github.push` and `github.status` have no action. Branch and tag events are `github.create.branch`, `github.create.tag`, `github.delete.branch`, and `github.delete.tag`. See the [Events reference](https://docs.type.com/automations/reference/events/index.md#github-events) for all events and their `data` fields.

Each event arrives in an envelope:

- `type`: the event name, for example `github.pull_request.closed`.
- `id`: the GitHub delivery ID.
- `resourceType` and `resourceId`: for example `pull_request` and the pull request number.
- `data`: normalized fields, for example `title`, `url`, `merged`, `baseBranch`, and `repositoryFullName`.

Common choices:

| Goal | `events` | `match` |
| --- | --- | --- |
| A pull request opened | `github.pull_request.opened` | `github.pull_request` |
| A pull request merged | `github.pull_request.closed` | `github.pull_request` with `merged: true` |
| A comment on a pull request | `github.issue_comment.created` | `github.issue_comment` with `pullRequest: true` |
| A review requested changes | `github.pull_request_review.submitted` | `github.pull_request_review` with `states: ["changes_requested"]` |
| CI failed | `github.workflow_run.completed` | `github.workflow_run` with `conclusions: ["failure"]` |
| A push to `main` | `github.push` | `github.push` with `refs: ["refs/heads/main"]` |
| A release published | `github.release.published` | `github.release` |

`github.*` selects every GitHub event. Other wildcards, such as `github.pull_request.*`, are not supported.

### Select repositories

Use the `repositories` option of the matcher. The value is `owner/repo`, and the comparison is not case-sensitive.

> **Caution:** The `resources` field of the source does not select repositories. It filters by resource IDs, such as pull request numbers: `resources: { pull_request: ["42"] }`.

### Read more from GitHub

The event has a summary of the change. To read more, for example the files of a pull request, run the GitHub CLI in the [sandbox](https://docs.type.com/automations/sandbox/index.md). Commands get GitHub credentials only for the GitHub connections of the Space.

## Limitations

- Existing legacy sources retain their previous controls. New event automations can be paused and resumed from their automation editor.
- Type.com does not send `deployment`, `deployment_status`, `pull_request_review_thread`, `label`, or `member` events to automations.
- The GitHub App settings decide which events GitHub sends. Saving an automation does not change these settings.
- In code automations, Type.com does not check `<action>` values against a list. A wrong action name deploys, but no event matches it.
- In GitHub matchers, list filters are case-sensitive, except `repositories`.
