Skip to content

Commit e046225

Browse files
committed
update api
1 parent 7c78533 commit e046225

1 file changed

Lines changed: 74 additions & 35 deletions

File tree

src/scripts/fetch-comments.js

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const DISCUSSION_CATEGORY_NAME = 'General';
55
const REPO_OWNER = 'catcodeme';
66
const REPO_NAME = 'catcodeme.github.io';
77

8-
async function fetchDiscussions() {
8+
async function fetchDiscussions(categoryId) {
99
const response = await fetch('https://api.github.com/graphql', {
1010
method: 'POST',
1111
headers: {
@@ -16,41 +16,39 @@ async function fetchDiscussions() {
1616
query: `
1717
query($repoOwner: String!, $repoName: String!, $categoryName: String!) {
1818
repository(owner: $repoOwner, name: $repoName) {
19-
discussionCategory(slug: $categoryName) {
20-
discussions(first: 100, orderBy: {field: CREATED_AT, direction: DESC}) {
21-
nodes {
22-
id
23-
title
19+
discussions(first: 100, categoryId: $categoryName, orderBy: {field: CREATED_AT, direction: DESC}) {
20+
nodes {
21+
id
22+
title
23+
url
24+
createdAt
25+
author {
26+
login
27+
avatarUrl
2428
url
25-
createdAt
26-
author {
27-
login
28-
avatarUrl
29-
url
30-
}
31-
bodyHTML
32-
reactionGroups {
33-
content
34-
users {
35-
totalCount
36-
}
29+
}
30+
bodyHTML
31+
reactionGroups {
32+
content
33+
users {
34+
totalCount
3735
}
38-
comments(first: 100) {
39-
nodes {
40-
id
36+
}
37+
comments(first: 100) {
38+
nodes {
39+
id
40+
url
41+
createdAt
42+
author {
43+
login
44+
avatarUrl
4145
url
42-
createdAt
43-
author {
44-
login
45-
avatarUrl
46-
url
47-
}
48-
bodyHTML
49-
reactionGroups {
50-
content
51-
users {
52-
totalCount
53-
}
46+
}
47+
bodyHTML
48+
reactionGroups {
49+
content
50+
users {
51+
totalCount
5452
}
5553
}
5654
}
@@ -63,7 +61,7 @@ async function fetchDiscussions() {
6361
variables: {
6462
repoOwner: REPO_OWNER,
6563
repoName: REPO_NAME,
66-
categoryName: DISCUSSION_CATEGORY_NAME
64+
categoryId: categoryId
6765
}
6866
})
6967
});
@@ -88,8 +86,47 @@ async function fetchDiscussions() {
8886
return data.data.repository.discussionCategory.discussions.nodes;
8987
}
9088

89+
async function getDiscussionCategoryId() {
90+
const response = await fetch('https://api.github.com/graphql', {
91+
method: 'POST',
92+
headers: {
93+
'Authorization': `bearer ${GITHUB_TOKEN}`,
94+
'Content-Type': 'application/json'
95+
},
96+
body: JSON.stringify({
97+
query: `
98+
query($repoOwner: String!, $repoName: String!) {
99+
repository(owner: $repoOwner, name: $repoName) {
100+
discussionCategories(first: 10) {
101+
nodes {
102+
id
103+
name
104+
}
105+
}
106+
}
107+
}
108+
`,
109+
variables: {
110+
repoOwner: REPO_OWNER,
111+
repoName: REPO_NAME
112+
}
113+
})
114+
});
115+
const data = await response.json();
116+
if (data.errors) {
117+
console.error("GitHub API returned errors while fetching categories:", JSON.stringify(data.errors, null, 2));
118+
throw new Error("Failed to fetch discussion categories.");
119+
}
120+
const category = data.data.repository.discussionCategories.nodes.find(c => c.name === DISCUSSION_CATEGORY_NAME);
121+
if (!category) {
122+
throw new Error(`Discussion category '${DISCUSSION_CATEGORY_NAME}' not found.`);
123+
}
124+
return category.id;
125+
}
126+
91127
async function main() {
92-
const discussions = await fetchDiscussions();
128+
const categoryId = await getDiscussionCategoryId();
129+
const discussions = await fetchDiscussions(categoryId);
93130
const comments = discussions.flatMap(discussion => {
94131
return [
95132
{
@@ -98,6 +135,7 @@ async function main() {
98135
createdAt: discussion.createdAt,
99136
author: discussion.author,
100137
bodyHTML: discussion.bodyHTML,
138+
reactionGroups: discussion.reactionGroups,
101139
isDiscussion: true,
102140
},
103141
...discussion.comments.nodes.map(comment => ({
@@ -106,6 +144,7 @@ async function main() {
106144
createdAt: comment.createdAt,
107145
author: comment.author,
108146
bodyHTML: comment.bodyHTML,
147+
reactionGroups: comment.reactionGroups,
109148
isDiscussion: false,
110149
}))
111150
];

0 commit comments

Comments
 (0)