Skip to content

Commit 4013bd7

Browse files
committed
update api 触发构建
1 parent cb0e603 commit 4013bd7

2 files changed

Lines changed: 37 additions & 71 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,5 @@ bunfig.toml
8585
gemini.md
8686
node_modules
8787
a.html
88-
src/data/comments.json
88+
src/data/comments.json
89+
a.json

src/scripts/fetch-comments.js

Lines changed: 35 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import fs from 'fs';
22

3-
const GITHUB_TOKEN = process.env.GITHUB_TOKEN_READ
4-
const DISCUSSION_CATEGORY_NAME = 'General';
5-
const REPO_OWNER = 'catcodeme';
3+
// Correct configuration based on the provided a.json
4+
const GITHUB_TOKEN = process.env.GITHUB_TOKEN_READ;
5+
const REPO_OWNER = 'CatCodeMe';
66
const REPO_NAME = 'catcodeme.github.io';
77

8-
async function fetchDiscussions(categoryId) {
8+
async function fetchAllDiscussions() {
99
const response = await fetch('https://api.github.com/graphql', {
1010
method: 'POST',
1111
headers: {
@@ -14,9 +14,9 @@ async function fetchDiscussions(categoryId) {
1414
},
1515
body: JSON.stringify({
1616
query: `
17-
query($repoOwner: String!, $repoName: String!, $categoryId: ID!) {
17+
query($repoOwner: String!, $repoName: String!) {
1818
repository(owner: $repoOwner, name: $repoName) {
19-
discussions(first: 100, categoryId: $categoryId, orderBy: {field: CREATED_AT, direction: DESC}) {
19+
discussions(first: 100, orderBy: {field: CREATED_AT, direction: DESC}) {
2020
nodes {
2121
id
2222
title
@@ -61,96 +61,61 @@ async function fetchDiscussions(categoryId) {
6161
variables: {
6262
repoOwner: REPO_OWNER,
6363
repoName: REPO_NAME,
64-
categoryId: categoryId
6564
}
6665
})
6766
});
6867

6968
const data = await response.json();
7069

71-
// Check for API errors in the response
7270
if (data.errors) {
7371
console.error("GitHub API returned errors:", JSON.stringify(data.errors, null, 2));
7472
throw new Error("Failed to fetch discussions due to API errors.");
7573
}
7674

77-
// Check for unexpected data structure
78-
if (!data.data || !data.data.repository || !data.data.repository.discussionCategory) {
75+
if (!data.data || !data.data.repository || !data.data.repository.discussions) {
7976
console.error("Unexpected data structure from GitHub API:", JSON.stringify(data, null, 2));
80-
if (data.data && data.data.repository && !data.data.repository.discussionCategory) {
81-
throw new Error(`Could not find the discussion category: '${DISCUSSION_CATEGORY_NAME}'. Please check the name.`);
82-
}
83-
throw new Error("Unexpected data structure received from GitHub API.");
77+
throw new Error("Unexpected data structure. Expected 'data.repository.discussions'.");
8478
}
8579

86-
return data.data.repository.discussionCategory.discussions.nodes;
87-
}
88-
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;
80+
return data.data.repository.discussions.nodes;
12581
}
12682

12783
async function main() {
128-
const categoryId = await getDiscussionCategoryId();
129-
const discussions = await fetchDiscussions(categoryId);
130-
const comments = discussions.flatMap(discussion => {
131-
return [
132-
{
84+
try {
85+
const discussions = await fetchAllDiscussions();
86+
const allComments = discussions.flatMap(discussion => {
87+
// Map the main discussion post
88+
const mainComment = {
13389
id: discussion.id,
13490
url: discussion.url,
13591
createdAt: discussion.createdAt,
13692
author: discussion.author,
13793
bodyHTML: discussion.bodyHTML,
13894
reactionGroups: discussion.reactionGroups,
13995
isDiscussion: true,
140-
},
141-
...discussion.comments.nodes.map(comment => ({
142-
id: comment.id,
143-
url: comment.url,
144-
createdAt: comment.createdAt,
145-
author: comment.author,
146-
bodyHTML: comment.bodyHTML,
147-
reactionGroups: comment.reactionGroups,
96+
title: discussion.title
97+
};
98+
99+
// Map the replies to the discussion
100+
const replies = discussion.comments.nodes.map(comment => ({
101+
...comment,
148102
isDiscussion: false,
149-
}))
150-
];
151-
});
103+
title: discussion.title // Carry over title to replies
104+
}));
152105

153-
fs.writeFileSync('src/data/comments.json', JSON.stringify(comments, null, 2));
106+
return [mainComment, ...replies];
107+
});
108+
109+
// Sort all comments and replies together by date
110+
allComments.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
111+
112+
fs.writeFileSync('src/data/comments.json', JSON.stringify(allComments, null, 2));
113+
console.log(`Successfully fetched and wrote ${allComments.length} comments to src/data/comments.json`);
114+
115+
} catch (error) {
116+
console.error("An error occurred during the fetch process:", error.message);
117+
process.exit(1); // Exit with an error code to fail the GitHub Action
118+
}
154119
}
155120

156121
main();

0 commit comments

Comments
 (0)