Created
February 27, 2024 01:53
-
-
Save Lissy93/75dfacd19b714429759ec082c21bf81a to your computer and use it in GitHub Desktop.
A simple REST API, to fetch all comments on a given GitHub discussion thread. Built as a Cloudflare Worker.
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
/** | |
* Fetch all comments on a given GitHub discussion | |
* A simple REST API, built for Cloudflare Workers | |
* Code licensed under MIT, (C) Alicia Sykes 2024 | |
*/ | |
addEventListener('fetch', event => { | |
event.respondWith(handleRequest(event.request)) | |
}) | |
async function handleRequest(request) { | |
const url = new URL(request.url); | |
const username = url.searchParams.get('username'); | |
const repo = url.searchParams.get('repo'); | |
const discussionId = url.searchParams.get('discussionId'); | |
if (!username || !repo || !discussionId) { | |
return new Response('Missing required parameters', { status: 400 }); | |
} | |
const query = ` | |
query { | |
repository(owner: "${username}", name: "${repo}") { | |
discussion(number: ${discussionId}) { | |
comments(first: 100) { | |
nodes { | |
id | |
body | |
bodyHTML | |
author { | |
login | |
avatarUrl | |
... on User { | |
name | |
location | |
websiteUrl | |
createdAt | |
followers { | |
totalCount | |
} | |
} | |
} | |
createdAt | |
lastEditedAt | |
upvoteCount | |
reactionGroups { | |
content | |
users { | |
totalCount | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
`; | |
try { | |
const apiResponse = await fetch('https://api.github.com/graphql', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `bearer ${GH_API_TOKEN}`, | |
'User-Agent': 'GetDiscussionComments' | |
}, | |
body: JSON.stringify({ query }) | |
}); | |
const result = await apiResponse.json(); | |
if (!apiResponse.ok) { | |
throw new Error(result.message || 'Failed to fetch GitHub data'); | |
} | |
return new Response(JSON.stringify(result.data.repository.discussion.comments.nodes), { | |
headers: { | |
'Content-Type': 'application/json', | |
'Access-Control-Allow-Origin': '*', | |
'Access-Control-Allow-Methods': 'GET, OPTIONS', | |
'Access-Control-Allow-Headers': 'Content-Type' | |
}, | |
}); | |
} catch (error) { | |
return new Response(error.message, { status: 500 }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
About
A quick REST API, for Cloudflare Workers, to fetch all comments on a given GitHub Discussions thread.
Example
An example use case for this, is here, where I'm fetching user reviews from GH discussions.
I've also written a web component in Lit, which can display a full thread's comments.
Usage
Just send a GET request to:
https://discussion-comments.as93.net/?username=[USERNAME]&repo=[REPOSITORY]&discussionId=[ID]
You'll get a response containing an array of comments, for example see: here
Deployment
npm i -g wrangler
wrangler init sponsors-api
cd sponsors-api
andnpm install
wrangler.toml
add your API key to theGITHUB_TOKEN
env varwrangler dev
wrangler deploy
to deploy your app to the world!Alternatively, just log in to Cloudflare, GO to "Workers" then "New" / "Quick Edit".
And then paste the above script in, and then hit "Save & Deploy". Done :)
You can now make requests to
https://[project-name].workers.dev
🚀License
Licensed under MIT, © Alicia Sykes 2024