Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions packages/webhook-ingestion/src/github/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ const optionalProviderId = (record: JsonRecord, key: string): string | undefined
const withOptional = <K extends string, V>(key: K, value: V | undefined): Partial<Record<K, V>> =>
value === undefined ? {} : ({ [key]: value } as Record<K, V>);

/** Returns the account associated with a GitHub App installation webhook payload. */
const installationAccount = (payload: JsonRecord): JsonRecord => {
const installation = asRecord(payload.installation, "installation");
const account =
optionalRecord(installation.account) ??
optionalRecord(optionalRecord(payload.repository)?.owner) ??
optionalRecord(payload.sender);

if (!account) {
throw new WebhookPayloadError("GitHub payload is missing installation.account.");
}

return account;
};

const numberValue = (record: JsonRecord, key: string): number => {
const value = record[key];
if (typeof value === "number") {
Expand Down Expand Up @@ -165,9 +180,8 @@ export type NormalizedGitHubFeedback = {

/** Extracts a GitHub installation account. */
export function normalizeGitHubAccount(payload: JsonRecord): NormalizedGitHubAccount {
const installation = asRecord(payload.installation, "installation");
const account = asRecord(installation.account, "installation.account");
const providerAccountId = stringValue(account, "id");
const account = installationAccount(payload);
const providerAccountId = optionalProviderId(account, "id") ?? stringValue(account, "login");
const login = stringValue(account, "login");
const rawType = optionalString(account, "type")?.toLowerCase();
const accountType =
Expand Down
2 changes: 2 additions & 0 deletions packages/webhook-ingestion/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export const installationPayload = {
default_branch: "main",
clone_url: "https://github.com/acme/heimdall.git",
owner: {
id: 42,
login: "acme",
type: "Organization",
},
},
],
Expand Down
19 changes: 19 additions & 0 deletions packages/webhook-ingestion/test/github-normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ describe("GitHub webhook normalization", () => {
expect(pullRequest.snapshot.diffHash).toMatch(/^sha256:[a-f0-9]{64}$/u);
});

it("normalizes pull request payloads when installation account is omitted", () => {
const payload = {
...pullRequestPayload,
installation: {
id: pullRequestPayload.installation.id,
},
};

const installation = normalizeGitHubInstallation(payload);
const pullRequest = normalizeGitHubPullRequest(payload);

expect(installation).toMatchObject({
accountLogin: "acme",
accountType: "organization",
providerInstallationId: "123456",
});
expect(pullRequest.snapshot.pullRequestNumber).toBe(7);
});

it("normalizes pull request comments and reactions as feedback", () => {
const comment = normalizeGitHubFeedback(issueCommentPayload, "issue_comment");
const reaction = normalizeGitHubFeedback(reactionPayload, "reaction");
Expand Down
Loading