Docs menu

Platforms

GitHub

This page shows how to test a GitHub App: how to point it at the fake, which people and repositories a test creates, what those people do, and what you check afterwards. It assumes you have run Getting started once.

import { expect, test } from "vitest";test("a mention in an issue gets one comment, and no self-trigger loop", async ({ github }) => {  const ana = await github.createUser({ login: "ana" });  const repo = await github.createRepo({ collaborators: [ana], apps: [github.app] });  const issue = await ana.openIssue({ title: "Login fails on Safari", body: "Steps: …" }, { repo });  await ana.comment("@my-agent can you triage this?", { issue });  await expect(issue).toHaveCommentFrom(github.app, { text: /triage/i });  await expect(issue).toHaveCommentsFrom(github.app, { count: 1, quietFor: "2s" });});

What the fake covers#

The fake GitHub is one organization (acme) with one GitHub App in it, my-agent, installed on the repositories you choose, and any other App a test adds (several Apps). It serves GitHub's REST API and sends the App's webhooks. Repositories hold real git objects, so commits have real SHAs and diffs are real diffs.

AreaWhat's there
The AppGET /app, installations, POST /app/installations/{id}/access_tokens, GET /installation/repositories, GET /repos/{owner}/{repo}/installation, the webhook delivery log (/app/hook/deliveries) and redelivery
IssuesIssues, comments, labels, assignees, reactions, listing with state and labels filters
Pull requestsPull requests, their files and commits, merging, requested reviewers, reviews, review comments and replies
CodeContents API, Git Data API (blobs, trees, commits, refs), branches, commits, compare
ChecksCheck runs with annotations and requested actions, commit statuses
Webhooksissues, issue_comment, pull_request, pull_request_review, pull_request_review_comment, pull_request_review_thread, push, check_run, installation_repositories

The App has issues, pull_requests, contents, checks and statuses write permissions, and administration write, as the live test org's App has to make each session's repositories. Every response and webhook is checked in channelstack's own tests against GitHub's published schemas (@octokit/openapi and @octokit/openapi-webhooks).

Not covered: the GraphQL API (POST /graphql answers 404), Actions and workflow runs, check suites, releases, milestones, discussions, teams and projects. A path the fake doesn't serve answers GitHub's own 404.

Connecting a GitHub App#

Ask the running server for the App's settings:

npx channelstack env
GITHUB_API_URL=https://github.localhost
GITHUB_APP_ID=…
GITHUB_APP_SLUG=my-agent
GITHUB_INSTALLATION_ID=…
GITHUB_BOT_USER_ID=…
GITHUB_PRIVATE_KEY=-----BEGIN RSA PRIVATE KEY-----\n…\n-----END RSA PRIVATE KEY-----\n
GITHUB_WEBHOOK_SECRET=…

The private key is an RSA key made once for your machine and kept in ~/.channelstack/secrets.json. It's printed on one line with \n for its line breaks, because an .env line is one line; turn them back into line breaks when you read it. The App id, installation id and bot id stay the same over a reset, and over a restart when CHANNELSTACK_SEED is set. Without it, a restart draws new ids, so read channelstack env again.

The host layout#

GITHUB_API_URL is the REST API's root: https://github.localhost with portless, http://127.0.0.1:4500/github with --no-portless. The same API also answers under /api/v3, the GitHub Enterprise Server layout, for SDKs that take a host and add that path themselves. Octokit takes GITHUB_API_URL as it is.

Octokit#

import { createAppAuth } from "@octokit/auth-app";
import { Octokit } from "@octokit/rest";

const privateKey = process.env.GITHUB_PRIVATE_KEY!.replaceAll("\\n", "\n");

export const octokit = new Octokit({
  authStrategy: createAppAuth,
  auth: {
    appId: process.env.GITHUB_APP_ID,
    privateKey,
    installationId: Number(process.env.GITHUB_INSTALLATION_ID),
  },
  ...(process.env.GITHUB_API_URL ? { baseUrl: process.env.GITHUB_API_URL } : {}),
});

Authentication works as on GitHub: the App's own endpoints take a JWT signed with the private key (RS256, iss the App id or client id, ten minutes at most), and everything else takes the installation token the JWT is exchanged for. Verify webhooks with @octokit/webhooks and GITHUB_WEBHOOK_SECRET.

Probot#

import { Probot, Server } from "probot";

const server = new Server({
  Probot: Probot.defaults({
    appId: process.env.GITHUB_APP_ID,
    privateKey: process.env.GITHUB_PRIVATE_KEY!.replaceAll("\\n", "\n"),
    secret: process.env.GITHUB_WEBHOOK_SECRET,
    ...(process.env.GITHUB_API_URL ? { baseUrl: process.env.GITHUB_API_URL } : {}),
  }),
  port: 3000,
  webhookPath: "/api/github/webhooks",
});
await server.load(app);
await server.start();

Chat SDK#

The Chat SDK's GitHub adapter reads GITHUB_API_URL from the environment by itself:

import { createGitHubAdapter } from "@chat-adapter/github";
import { createMemoryState } from "@chat-adapter/state-memory";
import { Chat } from "chat";

const github = createGitHubAdapter({
  appId: process.env.GITHUB_APP_ID!,
  privateKey: process.env.GITHUB_PRIVATE_KEY!.replaceAll("\\n", "\n"),
  webhookSecret: process.env.GITHUB_WEBHOOK_SECRET!,
  userName: "my-agent",
});
const bot = new Chat({ userName: "my-agent", adapters: { github }, state: createMemoryState() });

// Serve bot.webhooks.github(request) at /api/webhooks/github.

Where webhooks are delivered#

channelstack sends the App's webhooks to the URL you give it when the server starts:

CHANNELSTACK_GITHUB_WEBHOOK_URL=http://localhost:3000/api/github/webhooks npx channelstack start

/api/github/webhooks is Probot's default path; the Chat SDK reference agent serves /api/webhooks/github. Without a webhook URL, nothing is delivered. See Connecting your app for TLS trust and the other runtimes, and proxy mode for a client that hard-codes api.github.com.

People and repositories in a test#

A test creates the people and repositories it needs. Nothing is declared in a config file.

const ana = await github.createUser({ login: "ana" });const ben = await github.createUser({ login: "ben", role: "admin", name: "Ben Ortiz" });
OptionWhat it does
loginRequired. Mentions (@ana) and matchers find the person by it
idA fixed numeric id, for apps that allowlist by id
name, emailProfile fields
rolemember (default), admin or outside_collaborator. An org member can read every repository; writing needs a grant
tokenLive mode only: see Live

A login is unique within a test session, so parallel tests can each have an ana, with different numeric ids.

const repo = await github.createRepo({  name: "web",  files: "./fixtures/web",  collaborators: [ana, { user: ben, permission: "maintain" }],  labels: ["bug", { name: "agent:fix", color: "5319e7" }],  apps: [github.app],});
OptionWhat it does
nameWithout the owner. Generated (test-<id>) if omitted. A name is one repository across all open sessions, so a second test asking for web at the same time is refused with invalid_argument
filesThe default branch's first commit: a fixture directory or one fixture file, relative to the test file (in Rust, to the working directory), or a list of files ({ name, content } or { path })
collaboratorsPeople, with write permission unless you give another (read, triage, write, maintain, admin)
labelsNames, or { name, color, description }
appsThe Apps installed on it
privateDefault true
defaultBranchDefault main

Giving the App access#

The App hears about a repository only when it's installed there, which is GitHub's rule. Pass apps: [github.app] to createRepo, or install it later:

const repo = await github.createRepo({ collaborators: [ana] }); // not installedconst issue = await ana.openIssue({ title: "Hello?" }, { repo });const comment = await ana.comment("@my-agent ping", { issue });expect(comment.delivery).toBeUndefined();await repo.installApp(github.app); // sends installation_repositories

For the App's token, a repository it isn't installed on doesn't exist: every call about it answers 404. repo.uninstallApp(github.app) takes it away again.

What people do#

Every action is a method on the person who takes it, with the content first and the place as an option. An action that sends one webhook resolves to that delivery, or to undefined when the App isn't installed. A pull request is also an issue, so { issue } accepts either. The API reference lists every method and option.

Issues and comments#

const issue = await ana.openIssue({ title: "Typo in the README", labels: ["bug"] }, { repo });await ana.comment("@my-agent is this a duplicate?", { issue });await ana.label("agent:fix", { issue });await ana.assign([ben], { issue });await ana.editIssue({ title: "Typo in README.md" }, { issue });await ana.close({ issue });

@login in a body becomes a real mention; @my-agent mentions the App by its slug. comment sends issue_comment (created) and resolves to the comment, with the delivery on comment.delivery. edit, delete, react and minimize act on a comment; unlabel and reopen undo the others.

Pushes and pull requests#

const pr = await ana.openPullRequest(  {    from: "fix-login",    title: "Fix login redirect",    changes: { "src/login.ts": "export const redirect = '/home';\n" },  },  { repo },);const commit = await ana.push(  { "src/app.ts": "export const broken = ;\n" },  { repo, branch: "fix-login", message: "wip" },);

push commits file changes (a map of path to new contents, null to delete, or a list of files) to a branch, creating it from the default branch if it's missing. It sends push, and pull_request (synchronize) for open pull requests on that branch. openPullRequest pushes changes to from first when you pass them; draft: true opens a draft and reviewers requests reviews right away.

Reviews#

await ana.markReady({ pr });await ben.review({  pr,  event: "REQUEST_CHANGES",  body: "@my-agent please address these",  comments: [{ path: "src/app.ts", line: 1, body: "name this something clearer" }],});const inline = await ana.reviewComment("@my-agent why is this exported?", { pr, path: "src/app.ts", line: 1 });await ana.reply("thanks", { comment: inline });await ana.resolve({ comment: inline });await ana.requestReview([ben], { pr });await ana.merge({ pr, method: "squash" });

review sends pull_request_review and one pull_request_review_comment per inline comment. A comment on a line outside the diff is refused with 422, as on GitHub. resolve and unresolve send pull_request_review_thread.

Check runs#

await expect(pr).toHaveCheckRun("my-agent", { conclusion: "action_required" });const [run] = await pr.checkRuns();await ana.requestAction("autofix", { checkRun: run! });await ana.rerun({ checkRun: run! });

requestAction clicks one of the buttons the App put on its check run (check_run, action requested_action); run.actions lists their identifiers. rerun clicks Re-run (check_run, action rerequested).

What to check#

The matchers read what a person would see on the issue or pull request page. Positive matchers resolve as soon as the condition holds; .not and exact counts wait until the app is quiet first, so toHaveCommentsFrom(github.app, { count: 1 }) catches an App that answers its own comments. Waiting and quiet explains how. A check run the App started and never completed counts as work in progress, so the app isn't quiet while one is open.

MatcherSubjectReads
toHaveCommentFrom(author, { text })issue, pull requestissue.comments()
toHaveCommentsFrom(author, { count })issue, pull requestissue.comments(), once quiet
toHaveReviewFrom(author, { state })pull requestpr.reviews()
toHaveReviewComment({ path, line, from, text })pull requestpr.reviewComments()
toHaveCheckRun(name, { status, conclusion })pull request, commitcheckRuns()
toHaveLabel(name)issue, pull requestissue.labels()
toBeMerged()pull requestpr.merged()
toContainText(text)issue, pull request, review commenttranscript()
toBeAccepted, toBeRejecteddeliverydelivery.attempts
toHaveReceivedCall(operation, { times, error })githubgithub.calls()

The reads behind them are there for anything a matcher doesn't cover: issue.state(), issue.assignees(), issue.latestComment({ from }), pr.headCommit(), commit.statuses(), comment.reactions(), inline.replies(), inline.resolved(), and repo.readFile(path, { ref }) for a file as the app left it on a branch. github.calls() lists the App's calls about this test's repositories, named like POST /repos/acme/web/issues/1/comments; github.appCalls() lists its calls about no repository (GET /app, the token exchange), which every session shares.

A full test, a review round from a requested change to the merge:

import { expect, test } from "vitest";test("a full review round: changes requested, fixed, approved, merged", async ({ github }) => {  const ana = await github.createUser({ login: "ana" });  const ben = await github.createUser({ login: "ben" });  const repo = await github.createRepo({ files: "./fixtures/web", collaborators: [ana, ben], apps: [github.app] });  const pr = await ana.openPullRequest(    { from: "feat", draft: true, changes: { "src/app.ts": "export const x = 1\n" } },    { repo },  );  await ana.markReady({ pr });  await ben.review({    pr,    event: "REQUEST_CHANGES",    body: "@my-agent please address these",    comments: [{ path: "src/app.ts", line: 1, body: "name this something clearer" }],  });  const fix = await pr.headCommit();  expect(fix.author?.name).toBe(github.app.name);  await ben.review({ pr, event: "APPROVE" });  await ana.merge({ pr, method: "squash" });  await expect(pr).toBeMerged();});

githubChecks() from channelstack/checks adds the checks every GitHub App should pass: one comment per mention, ignores its own comments, redelivery, a bad signature. See Standard checks.

Delivery specifics#

Signatures and headers#

Every webhook carries GitHub's headers, signed over the exact bytes sent:

HeaderValue
X-GitHub-EventThe event, e.g. issue_comment
X-GitHub-DeliveryThe delivery's GUID, the same on a redelivery
X-Hub-Signature-256sha256= and the hex HMAC-SHA256 of the body, keyed with GITHUB_WEBHOOK_SECRET
X-Hub-SignatureThe older sha1= form, which GitHub still sends
X-GitHub-Hook-ID, X-GitHub-Hook-Installation-Target-ID, X-GitHub-Hook-Installation-Target-TypeThe App's hook (integration)
User-AgentGitHub-Hookshot/…

The payload has repository, organization, sender and installation, as on GitHub. The App is told about its own comments, commits and check runs too, so an app has to ignore them itself (check sender.type === "Bot").

Acknowledgement and redelivery#

The App has ten seconds to answer. GitHub doesn't retry a failed delivery on its own, and neither does the fake: a delivery that failed or timed out stays that way until someone redelivers it from the App's delivery log. In a test, that's delivery.redeliver(): the same GUID and payload, a new attempt.

const comment = await ana.comment("@my-agent investigate", { issue });await expect(issue).toHaveCommentFrom(github.app);await comment.delivery!.redeliver();await expect(issue).toHaveCommentsFrom(github.app, { count: 1, quietFor: "2s" });

delivery.resend({ badSignature: true }) sends it signed with the wrong secret, and toBeRejected() checks the app refused it. GitHub webhooks carry no timestamp, so there's no stale option here. The app can read the same log itself through GET /app/hook/deliveries and redeliver with POST /app/hook/deliveries/{id}/attempts. Delivery and failures covers what each of these proves.

What the fake refuses#

The fake answers with GitHub's status codes and its error object (message, documentation_url, and errors for a 422):

StatusMessageWhen
401Bad credentialsNo token, a token that isn't the App's, or the App's JWT on an endpoint that needs the installation token
403Resource not accessible by integrationA call outside the App's permissions, or GET /user as an installation
403Must have … access to …A person writing where they only have read
404Not FoundA repository the App isn't installed on, an unknown issue or comment, a path the fake doesn't serve
404Label does not existRemoving a label the issue doesn't have
405Pull Request is not mergeableMerging a closed or conflicting pull request
405Pull Request is still a draftMerging a draft
409<path> does not match <sha>A Contents API write with an outdated sha
422Validation FailedA missing field, a review comment outside the diff, a check run with more than three actions
400Problems parsing JSONA body that isn't JSON

GitHub drops, without a word, an assignee who can't be assigned, the App's bot among them. The errors reference lists every code.

Several Apps#

A test adds another GitHub App to the org with github.createApp({ name }): its own App id, slug, <name>[bot] bot user, private key, webhook secret and installation, with my-agent's permissions and events unless permissions and events say otherwise. It hears about a repository once it is installed there: createRepo({ apps: [github.app, reviewer] }) or repo.installApp(reviewer), which tells that App alone. Its webhooks go to CHANNELSTACK_GITHUB_APP_<NAME>_WEBHOOK_URL; channelstack env --app reviewer --channel github prints its GITHUB_APP_ID, GITHUB_PRIVATE_KEY and the rest. See several apps.

const coder = await github.createApp({ name: "coder" });
const ana = await github.createUser({ login: "ana" });
const repo = await github.createRepo({ collaborators: [ana], apps: [github.app, coder] });
const issue = await ana.openIssue({ title: "Login fails on Safari" }, { repo });

await ana.comment("@coder can you triage this?", { issue });

await expect(issue).toHaveCommentFrom(coder, { text: /triage/i });
await expect(issue).not.toHaveCommentFrom(github.app);

As GitHub does it:

  • An event two Apps hear is two deliveries, each with its own X-GitHub-Delivery, its own App's installation.id and X-GitHub-Hook-Installation-Target-ID, signed with its own secret. Each delivery says which App it went to (delivery.app).
  • Another App's comment, issue, label, pull request, review or push arrives with sender: { login: "<slug>[bot]", type: "Bot" }, and an App's own actions come back to it: nothing stops two Apps from answering each other.
  • Each App's JWT and installation token answer as that App, and reach only what its installation covers.
  • check_run created and completed go to every subscribed App; rerequested and requested_action only to the App that made the run.

A test can act as an App it made, through the fake's REST API with its installation token: reviewer.comment, openIssue, openPullRequest, review, reviewComment, label, push and the rest. Acting as my-agent is refused, because your agent is that App.

Live#

The same tests can run in a real test organization. Set the App's credentials (CHANNELSTACK_GITHUB_APP_ID, CHANNELSTACK_GITHUB_PRIVATE_KEY) and allowlist the org with CHANNELSTACK_LIVE_GITHUB_ORGS. The App must be installed on all of the org's repositories, with the Administration permission, because it creates and deletes each session's repositories (channelstack-run-<run>-<world>-<n>). Each person acts with their own fine-grained token from a machine account, CHANNELSTACK_GITHUB_USER_<LOGIN>_TOKEN, and the login you pass must be that account's.

Live, GitHub delivers to your App itself. channelstack acts, reads the results back, and reads the App's delivery log, so github.deliveries(), toBeAccepted() and redeliver() work live too. The log lags a few seconds behind, so poll it:

await expect  .poll(async () => (await github.deliveries({ type: "issue_comment", resource: repo })).length, {    timeout: 30_000,  })  .toBeGreaterThan(0);

Built live today: repositories, collaborators, issues, comments, labels, assignees, reactions, reads and the delivery log. Pushes, pull requests, reviews and check runs aren't live yet; live runs skip those steps and say why. resend({ badSignature }) stays fake only. See Live mode and the API reference.

Suggest changes to this page