-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |