Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SUPPORT]: How to provide typing for webhook handlers #1062

Closed
1 task done
harryzcy opened this issue Sep 26, 2024 · 2 comments
Closed
1 task done

[SUPPORT]: How to provide typing for webhook handlers #1062

harryzcy opened this issue Sep 26, 2024 · 2 comments
Labels
Type: Support Any questions, information, or general needs around the SDK or GitHub APIs

Comments

@harryzcy
Copy link

harryzcy commented Sep 26, 2024

What happened?

After upgrading octokit to v4.0.2 and @octokit/core to v6.1.2, my previous typing no longer works. How should I fix them here?

import { App, createNodeMiddleware } from 'octokit'

const app = new App();
app.webhooks.on('pull_request.opened', handlePullRequestOpened)
import {
  PullRequestClosedEvent,
  PullRequestOpenedEvent,
  PullRequestSynchronizeEvent
} from '@octokit/webhooks-types'
import { Octokit } from 'octokit'

interface HandlePullRequestOpenedOptions {
  octokit: Octokit
  payload: PullRequestOpenedEvent
}

export async function handlePullRequestOpened(options: HandlePullRequestOpenedOptions) {

}

Versions

Octokit v4.0.2

Relevant log output

No overload matches this call.
  Overload 1 of 2, '(event: "pull_request.opened" | "pull_request.opened"[], callback: HandlerFunction<"pull_request.opened", { octokit: Octokit & { paginate: PaginateInterface; } & paginateGraphQLInterface & Api & { ...; }; }>): void', gave the following error.
    Argument of type '({ payload }: HandlePullRequestOpenedOptions) => Promise<void>' is not assignable to parameter of type 'HandlerFunction<"pull_request.opened", { octokit: Octokit & { paginate: PaginateInterface; } & paginateGraphQLInterface & Api & { ...; }; }>'.
      Types of parameters '__0' and 'event' are incompatible.
        Type 'BaseWebhookEvent<"pull_request"> & { payload: { action: "opened"; }; } & { octokit: Octokit & { paginate: PaginateInterface; } & paginateGraphQLInterface & Api & { ...; }; }' is not assignable to type 'HandlePullRequestOpenedOptions'.
          The types of 'payload.pull_request' are incompatible between these types.
            Type '{ url: string; id: number; node_id: string; html_url: string; diff_url: string; patch_url: string; issue_url: string; commits_url: string; review_comments_url: string; review_comment_url: string; ... 37 more ...; changed_files: number; } & { ...; }' is not assignable to type 'PullRequest & { state: "open"; closed_at: null; merged_at: null; active_lock_reason: null; merged_by: null; }'.
              Type '{ url: string; id: number; node_id: string; html_url: string; diff_url: string; patch_url: string; issue_url: string; commits_url: string; review_comments_url: string; review_comment_url: string; ... 37 more ...; changed_files: number; } & { ...; }' is not assignable to type 'PullRequest'.
                The types of 'user.name' are incompatible between these types.
                  Type 'string | null | undefined' is not assignable to type 'string | undefined'.
                    Type 'null' is not assignable to type 'string | undefined'.
  Overload 2 of 2, '(event: "pull_request.opened" | "pull_request.opened"[], callback: HandlerFunction<"pull_request.opened", { octokit: Octokit; }>): void', gave the following error.
    Argument of type '({ payload }: HandlePullRequestOpenedOptions) => Promise<void>' is not assignable to parameter of type 'HandlerFunction<"pull_request.opened", { octokit: Octokit; }>'.
      Types of parameters '__0' and 'event' are incompatible.
        Type 'BaseWebhookEvent<"pull_request"> & { payload: { action: "opened"; }; } & { octokit: Octokit; }' is not assignable to type 'HandlePullRequestOpenedOptions'.
          Types of property 'octokit' are incompatible.
            Type 'Octokit' is not assignable to type 'Octokit & { paginate: PaginateInterface; } & paginateGraphQLInterface & Api & { retry: { retryRequest: (error: RequestError, retries: number, retryAfter: number) => RequestError; }; }'.
              Property 'paginate' is missing in type 'Octokit' but required in type '{ paginate: PaginateInterface; }'.ts(2769)
octokit.d.ts(5, 5): 'paginate' is declared here.

Code of Conduct

  • I agree to follow this project's Code of Conduct
@harryzcy harryzcy added Status: Triage This is being looked at and prioritized Type: Bug Something isn't working as documented, or is being fixed labels Sep 26, 2024
Copy link
Contributor

👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labeled with Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! 🚀

@wolfy1339 wolfy1339 added Type: Support Any questions, information, or general needs around the SDK or GitHub APIs and removed Type: Bug Something isn't working as documented, or is being fixed Status: Triage This is being looked at and prioritized labels Sep 26, 2024
@wolfy1339 wolfy1339 changed the title [BUG]: How to provide typing foe webhook handlers [SUPPORT]: How to provide typing foe webhook handlers Sep 26, 2024
@wolfy1339
Copy link
Member

The types for Webhooks were completely changed.

We went from community generated types to types generated via GitHub's Open API spec.

@octokit/webhooks-types doesn't work with the new versions of @octokit/webhooks.

There isn't a compatibility layer where you can use the same import names.

You will need the @octokit/openapi-webhooks-types package instead.

import type { webhooks as OpenAPIWebhooks } from "@octokit/openapi-webhooks-types";

type WebhookEventDefinition<TEventName extends keyof OpenAPIWebhooks> =
  OpenAPIWebhooks[TEventName]["post"]["requestBody"]["content"]["application/json"];

type PullRequestOpenedEvent = WebhookEventDefinition<"pull-request-opened">;

@github-project-automation github-project-automation bot moved this from 🆕 Triage to ✅ Done in 🧰 Octokit Active Sep 26, 2024
@harryzcy harryzcy changed the title [SUPPORT]: How to provide typing foe webhook handlers [SUPPORT]: How to provide typing for webhook handlers Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Support Any questions, information, or general needs around the SDK or GitHub APIs
Projects
Archived in project
Development

No branches or pull requests

2 participants