Your current code used-by.ts
is using Vercel (previously called Zeit) to create a serverless function to return badge data.
I would recommend having a more precise search query. For example, searching for the exact file (filename:javadoc-publisher.yml
) might yield more accurate results than a general search.
And do add proper error handling to avoid potential issues when making requests to the GitHub API.
import { VercelRequest, VercelResponse } from '@vercel/node';
import { Octokit } from '@octokit/core';
const octokit = new Octokit({
auth: process.env.GITHUB_SEARCH_TOKEN
});
/**
* Fetches the number of repositories using the specified GitHub Action.
*
* @param action - The GitHub Action to search for.
* @returns - The number of repositories using the action.
*/
async function getUsageCount(action: string): Promise<number> {
// Replace special characters and build the search query
const searchQuery = `${action.replace(
/[\.,:;/\\`'"=*!?#$&+^|~<>(){}\[\]]/g,
'+'
)} filename:javadoc-publisher.yml path:.github/workflows/ language:YAML fork:true`;
const response = await octokit.request('GET /search/code', { q: searchQuery });
return response.data.total_count;
}
export default async (req: VercelRequest, res: VercelResponse) => {
const { action, badge, ...badgeStuff } = req.query;
if (!action || typeof action !== 'string') {
return res.status(400).send("Invalid action parameter.");
}
try {
const count = await getUsageCount(action);
if (badge === 'true') {
res.send({
schemaVersion: 1,
label: 'used by',
message: `${count}`,
color: '#2088FF',
namedLogo: 'GitHub Actions',
logoColor: 'white',
...badgeStuff
});
} else {
res.send(count.toString());
}
} catch (error) {
console.error("Error fetching GitHub data:", error.message);
res.status(500).send("Error fetching data from GitHub.");
}
};
But if it is still incorrect, try a more hard-coded getUsageCount()
, just for testing/debugging:
const { Octokit } = require('@octokit/core');
const octokit = new Octokit({
auth: 'YOUR_GITHUB_TOKEN' // Replace with your actual token or better yet, use an environment variable.
});
async function getUsageCount() {
try {
const response = await octokit.request('GET /search/code', {
q: 'MathieuSoysal+javadoc-publisher filename:javadoc-publisher.yml path:.github/workflows/ language:YAML'
});
return response.data.total_count;
} catch (error) {
throw new Error('Failed to fetch data from GitHub: ' + error.message);
}
}
// To test
getUsageCount()
.then(count => console.log(count))
.catch(err => console.error(err));