Skip to content

Commit

Permalink
Modify testing if the discussion.json is empty or notpresent
Browse files Browse the repository at this point in the history
  • Loading branch information
dgparmar14 committed Jul 22, 2024
1 parent 1a16748 commit 1f7f36a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions schemas/discussion-data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ required:
- category
- time
- repoName
- participants

properties:
source:
Expand Down
12 changes: 4 additions & 8 deletions scraper/src/github-scraper/discussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,10 @@ export async function scrapeDiscussions(
endDate,
startDate,
);
if (allDiscussions) {
const parsedDiscussions = await parseDiscussionData(
allDiscussions,
endDate,
startDate,
);
await saveDiscussionData(parsedDiscussions, dataDir);
}
const parsedDiscussions =
allDiscussions &&
(await parseDiscussionData(allDiscussions, endDate, startDate));
await saveDiscussionData(parsedDiscussions, dataDir);
} catch (error: any) {
throw new Error(`Error fetching discussions: ${error.message}`);
}
Expand Down
10 changes: 6 additions & 4 deletions scraper/src/github-scraper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ export async function mergeDiscussions(
newDiscussions: ParsedDiscussion[],
) {
const mergedDiscussions = [...oldData];

if (!newDiscussions) {
return mergedDiscussions;
}
newDiscussions.forEach((newDiscussion) => {
const oldIndex = oldData.findIndex(
(oldDiscussion) => oldDiscussion.link === newDiscussion.link,
Expand All @@ -203,14 +205,14 @@ export async function saveDiscussionData(
dataDir: string,
) {
// check data dir present or not and file is present or not if not then create it
await mkdir(dataDir + "/discussions", { recursive: true });
const discussionsDir = path.join(dataDir, "discussions");
await mkdir(discussionsDir, { recursive: true });

const file = path.join(dataDir + "/discussions", "discussions.json");
const file = path.join(discussionsDir, "discussions.json");
try {
// Try reading the file
const response = await readFile(file);
const oldData = JSON.parse(response.toString());

const newData = await mergeDiscussions(oldData, discussions);
const jsonData = JSON.stringify(newData, null, 2);
await writeFile(file, jsonData);
Expand Down
3 changes: 3 additions & 0 deletions tests/github-discussion-schema.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const filesInDir = fs

filesInDir.forEach((file) => {
const content = fs.readFileSync(join(GH_DATA, file)).toString();
if (content === "null") {
process.exit(1);
}
const data = JSON.parse(stripJsonComments(content));

describe(`Validate '${file}'`, function () {
Expand Down

0 comments on commit 1f7f36a

Please sign in to comment.