/**
 * GitHub webhook event shapes used by the ingestion connector.
 * Spec: https://docs.github.com/en/webhooks/webhook-events-and-payloads
 *
 * V1 cares about four event types: issues, issue_comment, pull_request,
 * pull_request_review_comment. Each guard is gated on the X-GitHub-Event
 * header value so a malicious or misrouted payload cannot satisfy a guard
 * for the wrong event type.
 */
function isObject(x) {
    return !!x && typeof x === 'object';
}
function hasSenderShape(x) {
    if (!isObject(x))
        return false;
    return typeof x.login === 'string' && typeof x.id === 'number';
}
export function isGitHubWebhookEnvelope(x) {
    if (!isObject(x))
        return false;
    // Duck-type: an envelope must at least carry an action OR a repository field.
    return typeof x.action === 'string' || isObject(x.repository);
}
export function isGitHubIssueEvent(x, evtHeader) {
    if (evtHeader !== 'issues')
        return false;
    if (!isObject(x))
        return false;
    if (typeof x.action !== 'string')
        return false;
    const issue = x.issue;
    if (!isObject(issue))
        return false;
    if (typeof issue.number !== 'number')
        return false;
    if (!hasSenderShape(issue.user))
        return false;
    return true;
}
export function isGitHubIssueCommentEvent(x, evtHeader) {
    if (evtHeader !== 'issue_comment')
        return false;
    if (!isObject(x))
        return false;
    if (typeof x.action !== 'string')
        return false;
    const issue = x.issue;
    if (!isObject(issue) || typeof issue.number !== 'number')
        return false;
    const comment = x.comment;
    if (!isObject(comment))
        return false;
    if (typeof comment.id !== 'number')
        return false;
    if (!hasSenderShape(comment.user))
        return false;
    return true;
}
export function isGitHubPullRequestEvent(x, evtHeader) {
    if (evtHeader !== 'pull_request')
        return false;
    if (!isObject(x))
        return false;
    if (typeof x.action !== 'string')
        return false;
    const pr = x.pull_request;
    if (!isObject(pr))
        return false;
    if (typeof pr.number !== 'number')
        return false;
    if (!hasSenderShape(pr.user))
        return false;
    return true;
}
export function isGitHubPullRequestReviewCommentEvent(x, evtHeader) {
    if (evtHeader !== 'pull_request_review_comment')
        return false;
    if (!isObject(x))
        return false;
    if (typeof x.action !== 'string')
        return false;
    const pr = x.pull_request;
    if (!isObject(pr) || typeof pr.number !== 'number')
        return false;
    const comment = x.comment;
    if (!isObject(comment))
        return false;
    if (typeof comment.id !== 'number')
        return false;
    if (!hasSenderShape(comment.user))
        return false;
    return true;
}
//# sourceMappingURL=types.js.map