Skip to content

Commit f24d999

Browse files
committed
Workflow update
1 parent 504c5d1 commit f24d999

File tree

1 file changed

+41
-46
lines changed

1 file changed

+41
-46
lines changed

scripts/fetch-github-data.js

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -65,79 +65,74 @@ async function getRepoTopics(owner, repo_name) {
6565
}
6666

6767
/**
68-
* Get top repositories for a specific topic in an organization
68+
* Get all repositories for an organization (paginated)
69+
* @param {string} org - Organization name
70+
* @returns {Promise<Array>} - List of repositories
71+
*/
72+
async function getAllOrgRepos(org) {
73+
let repos = [];
74+
let page = 1;
75+
const per_page = 100;
76+
while (true) {
77+
const url = `https://api.github.com/orgs/${org}/repos?per_page=${per_page}&page=${page}`;
78+
const headers = {
79+
'Accept': 'application/vnd.github.v3+json',
80+
'Authorization': `Bearer ${GITHUB_TOKEN}`
81+
};
82+
const response = await fetch(url, { headers });
83+
if (!response.ok) {
84+
throw new Error(`Failed to fetch repos: ${response.status}`);
85+
}
86+
const data = await response.json();
87+
repos = repos.concat(data);
88+
if (data.length < per_page) break;
89+
page++;
90+
}
91+
return repos;
92+
}
93+
94+
/**
95+
* Get top repositories for a specific topic in an organization (using repo topics API)
6996
* @param {string} org - Organization name
7097
* @param {string} topic - Topic to search for
7198
* @param {number} limit - Maximum number of repos to return
7299
* @returns {Promise<Object>} - Repository data
73100
*/
74101
async function getTopRepositories(org, topic, limit = 3) {
75-
let all_repos = [];
76-
let page = 1;
77-
78102
try {
79-
while (true) {
80-
const params = new URLSearchParams({
81-
'q': `org:${org} topic:"${topic}"`,
82-
'sort': 'stars',
83-
'order': 'desc',
84-
'per_page': '100',
85-
'page': page.toString()
86-
});
87-
88-
const headers = {
89-
'Accept': 'application/vnd.github.v3+json',
90-
'Authorization': `Bearer ${GITHUB_TOKEN}`
91-
};
92-
93-
const response = await fetch(`${GITHUB_API_SEARCH_URL}?${params}`, { headers });
94-
95-
if (!response.ok) {
96-
const errorData = await response.json();
97-
throw new Error(`HTTP error! Status: ${response.status}, Message: ${JSON.stringify(errorData)}`);
98-
}
99-
100-
const data = await response.json();
101-
const public_repos = data.items.filter(repo => !repo.private);
102-
all_repos.push(...public_repos);
103-
104-
if (data.items.length < 100) {
105-
break;
103+
// Get all repos in the org
104+
const all_repos = await getAllOrgRepos(org);
105+
// For each repo, fetch topics and filter by topic
106+
const reposWithTopic = [];
107+
for (const repo of all_repos) {
108+
if (repo.private) continue; // skip private repos
109+
const repoTopics = await getRepoTopics(org, repo.name);
110+
if (repoTopics.map(t => t.toLowerCase()).includes(topic.toLowerCase())) {
111+
reposWithTopic.push({ ...repo, topics: repoTopics });
106112
}
107-
108-
page++;
109113
}
110-
111114
// Sort by stars and limit results
112-
const top_repos = all_repos
115+
const top_repos = reposWithTopic
113116
.sort((a, b) => b.stargazers_count - a.stargazers_count)
114117
.slice(0, limit);
115-
116118
const total_stars = top_repos.reduce((sum, repo) => sum + repo.stargazers_count, 0);
117119
const total_repositories = top_repos.length;
118-
119120
let all_topics = new Set();
120121
const formatted_repos = [];
121-
122-
// Get topics for each repository
123122
for (const repo of top_repos) {
124-
const repo_name = repo.name;
125-
const topics = await getRepoTopics(org, repo_name);
126-
topics.forEach(topic => all_topics.add(topic));
127-
123+
all_topics = new Set([...all_topics, ...repo.topics]);
128124
formatted_repos.push({
129-
"name": repo_name,
125+
"name": repo.name,
130126
"url": repo.html_url,
131127
"description": repo.description,
132128
"stars": repo.stargazers_count,
133129
"forks": repo.forks_count,
134130
"is_fork": repo.fork,
135-
"topics": topics,
131+
"topics": repo.topics,
136132
"language": repo.language,
137133
"updated_at": repo.updated_at
138134
});
139135
}
140-
141136
return {
142137
"top_repositories": formatted_repos,
143138
"total_stars": total_stars,

0 commit comments

Comments
 (0)