Skip to content

Commit

Permalink
Add ping endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
qwtel committed Sep 7, 2024
1 parent 60f2147 commit d32bd39
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions functions/api/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/// <reference types="@cloudflare/workers-types/2023-07-01" />

const SellerID = 'MzsHym72gcntzEb5BQygpQ==';
const ProductId = 'nuOluY';

const Org = 'hydecorp';
const TeamSlug = 'pro-customers';
const GitHubApiVersion = '2022-11-28';

interface Env {
GUMROAD_ACCESS_TOKEN: string
GITHUB_ADMIN_PAT: string
KV?: KVNamespace
}

export const onRequestPost: PagesFunction<Env> = async (context) => {
const { request: _request, env } = context;
const request = _request as Request
console.debug("content-type", request.headers.get('content-type'));
if (!request.headers.get('content-type')?.includes('x-www-form-urlencoded')) {
console.error("Invalid content-type", request.headers.get('content-type'));
return new Response(null, { status: 400 });
}
const formData = await request.formData();
const payload = Object.fromEntries(formData);
console.debug("payload", payload);

if (payload.seller_id !== SellerID) {
console.error("Invalid seller_id, this should never happen");
return new Response(null, { status: 400 });
}
if (payload.product_permalink !== ProductId) {
console.warn("Unsupported product_permalink");
return new Response(); // ok
}
if (!payload.custom_fields) {
console.error("No custom_fields");
return new Response(null, { status: 400 });
}
console.debug("payload.custom_fields", payload.custom_fields);

const customFields = payload.custom_fields && JSON.parse(payload.custom_fields as string) as Record<string, string>|undefined|null;
const githubHandle = customFields && Object.entries(customFields).find(([k]) => k.trim().toLowerCase().startsWith('github'))?.[1];
console.debug("githubHandle", githubHandle);

if (githubHandle) {
const url = `https://api.github.com/orgs/${Org}/teams/${TeamSlug}/memberships/${githubHandle}`;
const ghResponse = await fetch(url, {
method: 'PUT',
headers: {
'accept': 'application/vnd.github+json',
'authorization': `Bearer ${env.GITHUB_ADMIN_PAT}`,
'x-github-api-version': GitHubApiVersion,
'content-type': 'application/json'
},
body: JSON.stringify({ role: 'member' })
});
console.debug("ghResponse/status", ghResponse.status);
console.debug("ghResponse/body", await ghResponse.json());
return new Response(null, { status: ghResponse.status });
}

return new Response();
};

0 comments on commit d32bd39

Please sign in to comment.