Skip to content

Instantly share code, notes, and snippets.

@Lissy93
Created February 27, 2024 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lissy93/75dfacd19b714429759ec082c21bf81a to your computer and use it in GitHub Desktop.
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.
/**
* 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 });
}
}
@Lissy93
Copy link
Author

Lissy93 commented Feb 27, 2024

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

  1. Setup Node.js, if you haven't already done so, then install wrangler, npm i -g wrangler
  2. Create a new worker project, with wrangler init sponsors-api
  3. Navigate into your project, and install deps with cd sponsors-api and npm install
  4. In your wrangler.toml add your API key to the GITHUB_TOKEN env var
  5. Start the development server, and test everything's working with wrangler dev
  6. Finally, run 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment