From 9f3c3b5b0f41a6a77fc48f9af5b562eae45b6f1d Mon Sep 17 00:00:00 2001 From: Jaspinder Singh Date: Tue, 18 Jul 2023 17:54:30 -0230 Subject: [PATCH 1/3] Added Problem data with solution at the end, changed file type to markdown for easy readability --- src/action.js | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/src/action.js b/src/action.js index f1d16637..9c42412d 100644 --- a/src/action.js +++ b/src/action.js @@ -1,5 +1,6 @@ const axios = require('axios'); const { Octokit } = require('@octokit/rest'); +const fetch = require("node-fetch"); const COMMIT_MESSAGE = 'Sync LeetCode submission'; const LANG_TO_EXTENSION = { @@ -45,7 +46,8 @@ async function commit(params) { treeSHA, latestCommitSHA, submission, - destinationFolder + destinationFolder, + question_data } = params; const name = normalizeName(submission.title); @@ -56,13 +58,13 @@ async function commit(params) { } const prefix = !!destinationFolder ? `${destinationFolder}/` : ''; - const path = `${prefix}problems/${name}/solution.${LANG_TO_EXTENSION[submission.lang]}` + const path = `${prefix}problems/${name}/solution.md` // Markdown file for the problem with the solution at the end const treeData = [ { path, mode: '100644', - content: submission.code, + content: question_data + '\n' + '# Solution' + '\n' + '```' + submission.lang + ' \n' + submission.code + '\n' + '```', } ]; @@ -105,6 +107,40 @@ async function commit(params) { return [treeResponse.data.sha, commitResponse.data.sha]; } +async function getQuestionData(titleSlug, leetcodeSession) { + log(`Getting question data for ${titleSlug}...`); + + const headers = { + "Content-Type": "application/json", + "Cookie": `LEETCODE_SESSION=${leetcodeSession};` + } + + const graphql = JSON.stringify({ + query: `query getQuestionDetail($titleSlug: String!) { + question(titleSlug: $titleSlug) { + content + } + }`, + variables: {"titleSlug": titleSlug}, + }) + + const requestOptions = { + method: 'POST', + headers, + body: graphql, + redirect: 'follow' + }; + + try { + const response = await fetch("https://leetcode.com/graphql/", requestOptions); + const result = await response.text(); + const parsedResult = JSON.parse(result); + return parsedResult.data.question.content; + } catch (error) { + console.log('error', error); + } +} + // Returns false if no more submissions should be added. function addToSubmissions(params) { const { @@ -238,7 +274,10 @@ async function sync(inputs) { let treeSHA = commits.data[0].commit.tree.sha; for (i = submissions.length - 1; i >= 0; i--) { submission = submissions[i]; - [treeSHA, latestCommitSHA] = await commit({ octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission, destinationFolder }); + + // Get the question data for the submission. + const question_data = await getQuestionData(submission.title_slug, leetcodeSession); + [treeSHA, latestCommitSHA] = await commit({ octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission, destinationFolder, question_data }); } log('Done syncing all submissions.'); } From 1fb8c607a6007baf8243b823012c23034972ced7 Mon Sep 17 00:00:00 2001 From: Jaspinder Singh Date: Thu, 3 Aug 2023 11:19:33 -0230 Subject: [PATCH 2/3] re-used axios for fetching question data and removed node-fetch --- src/action.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/action.js b/src/action.js index 9c42412d..3029b75c 100644 --- a/src/action.js +++ b/src/action.js @@ -1,6 +1,5 @@ const axios = require('axios'); const { Octokit } = require('@octokit/rest'); -const fetch = require("node-fetch"); const COMMIT_MESSAGE = 'Sync LeetCode submission'; const LANG_TO_EXTENSION = { @@ -64,7 +63,7 @@ async function commit(params) { { path, mode: '100644', - content: question_data + '\n' + '# Solution' + '\n' + '```' + submission.lang + ' \n' + submission.code + '\n' + '```', + content: question_data + '\n \n ' + '# Solution' + '\n' + '```' + submission.lang + ' \n' + submission.code + '\n' + '```', } ]; @@ -124,18 +123,11 @@ async function getQuestionData(titleSlug, leetcodeSession) { variables: {"titleSlug": titleSlug}, }) - const requestOptions = { - method: 'POST', - headers, - body: graphql, - redirect: 'follow' - }; try { - const response = await fetch("https://leetcode.com/graphql/", requestOptions); - const result = await response.text(); - const parsedResult = JSON.parse(result); - return parsedResult.data.question.content; + const response = await axios.post("https://leetcode.com/graphql/", graphql, {headers}); + const result = await response.data; + return result.data.question.content; } catch (error) { console.log('error', error); } From 48def2df80958a628158009b961421928506f435 Mon Sep 17 00:00:00 2001 From: Jaspinder Singh Date: Thu, 3 Aug 2023 11:40:27 -0230 Subject: [PATCH 3/3] separate file for solution and question --- src/action.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/action.js b/src/action.js index 3029b75c..ccb1fd76 100644 --- a/src/action.js +++ b/src/action.js @@ -57,14 +57,21 @@ async function commit(params) { } const prefix = !!destinationFolder ? `${destinationFolder}/` : ''; - const path = `${prefix}problems/${name}/solution.md` // Markdown file for the problem with the solution at the end + const questionPath = `${prefix}problems/${name}/question.md`; // Markdown file for the problem with question data + const solutionPath = `${prefix}problems/${name}/solution.${LANG_TO_EXTENSION[submission.lang]}`; // Separate file for the solution + const treeData = [ { - path, + path: questionPath, mode: '100644', - content: question_data + '\n \n ' + '# Solution' + '\n' + '```' + submission.lang + ' \n' + submission.code + '\n' + '```', - } + content: question_data, + }, + { + path: solutionPath, + mode: '100644', + content: submission.code, + }, ]; const treeResponse = await octokit.git.createTree({