diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 5c4a6a8..c6790cd 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -6,28 +6,24 @@ name: Node.js CI on: workflow_dispatch: push: - branches: [ main ] + branches: [main] jobs: build: - runs-on: ubuntu-latest - strategy: - matrix: - node-version: [16.x] steps: - - uses: actions/checkout@v2 - - name: Use Node.js - uses: actions/setup-node@v2 - with: - node-version: '12.x' - - name: Install dependencies - run: npm install js-yaml node-fetch - - name: Run Daily File - run: node javascript/add_challenge_to_spaces.js - env: - API_TOKEN: ${{ secrets.API_TOKEN }} - COMMUNITY_ID: ${{ secrets.COMMUNITY_ID }} - CIRCLE_COMMUNITY_PATH: ${{ secrets.CIRCLE_COMMUNITY_PATH }} - DAILY_SPACE_ID: ${{ secrets.DAILY_SPACE_ID }} + - uses: actions/checkout@v2 + - name: Use Node.js + uses: actions/setup-node@v2 + with: + node-version: "12.x" + - name: Install dependencies + run: npm install js-yaml node-fetch + - name: Run Daily File + run: node javascript/add_challenge_to_spaces.js + env: + API_TOKEN: ${{ secrets.API_TOKEN }} + COMMUNITY_ID: ${{ secrets.COMMUNITY_ID }} + CIRCLE_COMMUNITY_PATH: ${{ secrets.CIRCLE_COMMUNITY_PATH }} + DAILY_SPACE_ID: ${{ secrets.DAILY_SPACE_ID }} diff --git a/.gitignore b/.gitignore index f6760c7..c290399 100644 --- a/.gitignore +++ b/.gitignore @@ -129,4 +129,4 @@ dmypy.json .pyre/ # Node -/node_modules \ No newline at end of file +/node_modules diff --git a/javascript/add_challenge_to_spaces.js b/javascript/add_challenge_to_spaces.js index 0900458..0b5edb5 100644 --- a/javascript/add_challenge_to_spaces.js +++ b/javascript/add_challenge_to_spaces.js @@ -1,22 +1,16 @@ //Import dependcies -const fs = require("fs"); -const YAML = require("js-yaml"); +import fs from "fs"; +import YAML from "js-yaml"; //import actions -const { postQuestion } = require("./js_actions.js"); +import postQuestion from "./js_actions.js"; try { - const raw = fs.readFileSync("./topics.yaml"); + const raw = fs.readFileSync("././topics.yaml"); const data = YAML.load(raw); - Object.values(data).forEach((item) => { - if (!item.posted) { - postQuestion(item); - item.posted = true; - } - }); - - let newData = YAML.dump(data); - fs.writeFileSync("./topics.yaml", newData, "utf8"); + const lengthOfYAMLfile = Object.values(data).length; + const latestQuestion = data[lengthOfYAMLfile]; + postQuestion(latestQuestion, lengthOfYAMLfile); } catch (err) { console.log(err); } diff --git a/javascript/js_actions.js b/javascript/js_actions.js index 4a14f68..b4527c4 100644 --- a/javascript/js_actions.js +++ b/javascript/js_actions.js @@ -1,49 +1,67 @@ -const fetch = require("node-fetch"); +import fetch from "node-fetch"; const API_TOKEN = process.env.API_TOKEN; const COMMUNITY_ID = process.env.COMMUNITY_ID; const CIRCLE_COMMUNITY_PATH = process.env.CIRCLE_COMMUNITY_PATH; const BEGGINER_SPACE_ID = process.env.BEGGINER_SPACE_ID; const MEDIUM_SPACE_ID = process.env.MEDIUM_SPACE_ID; -const ADVANCED_SPACE_ID = process.env.ADVANCED_SPACE_ID; -const postQuestion = async (question) => { +const postQuestion = async (question, questionNumber) => { + const { title, difficulty, source, body, author_name } = question; -const { title, difficulty, body, author_email } = question; + const requestOptions = { + method: "POST", + headers: { Authorization: API_TOKEN }, + }; -console.log(difficulty); -const requestOptions = { - method: "POST", - headers: { Authorization: API_TOKEN }, -}; + const questionDifficulties = { + Beginner: BEGGINER_SPACE_ID, + Medium: MEDIUM_SPACE_ID, + }; -const questionDifficulties = { - Beginner: BEGGINER_SPACE_ID, - Medium: MEDIUM_SPACE_ID, - Advanced: ADVANCED_SPACE_ID, -}; + const SPACE_ID = questionDifficulties[difficulty]; + + //post sections + let titleHTML = `Interview Prep Challenge ${questionNumber}: ${title}`; + let bodyHTML = ` + ๐Ÿ‘๐Ÿ‘

Thanks to ${author_name} for the question!๐Ÿ‘๐Ÿ‘


+
+ Difficulty Level: ${difficulty}
+ Sourced from: ${source}
+
+ ${body}
+
+ This question was submitted in the YearOne Open Source project, to submit a topic, go to github.com/YearOne-Prep/YearOne-prep-challenges +

+ Don't forget to let us know that you've completed this question! + Leave a comment below ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡ + `; + + //url creation + const url = encodeURI(formatPostBody(titleHTML, bodyHTML)); + + function formatPostBody(titleSection, bodySection) { + const post_url = `${CIRCLE_COMMUNITY_PATH}/api/v1/posts?community_id=${COMMUNITY_ID}&SPACE_ID=${SPACE_ID}&`; + const post_title = `name=${titleSection}&`; + const post_body = `internal_custom_html=${bodySection}&`; + const post_url_ending_params = `is_comments_enabled=true&is_liking_enabled=true&is_truncation_disabled=false`; + + const url_pieces = [ + post_url, + post_title, + post_body, + post_url_ending_params, + ]; + + return url_pieces.join(""); + } -const SPACE_ID = questionDifficulties[difficulty]; - -// url creation -const post_url = `${CIRCLE_COMMUNITY_PATH}/api/v1/posts?community_id=${COMMUNITY_ID}&SPACE_ID=${SPACE_ID}&`; -const post_title = `name=${title}&`; -const post_body = `body=${body}&`; -const post_url_ending_params = `is_comments_enabled=true&is_liking_enabled=true&is_truncation_disabled=true`; -const post_author = `&user_email=${author_email}`; - -const url_pieces = [post_url, post_title, post_body, post_url_ending_params]; -if (author_email) { - url_pieces.append(post_author); -} -const url = encodeURI(url_pieces.join("")); - -await fetch(url, requestOptions) - .then((response) => response.json()) - .then((result) => { - console.log(result); - }) - .catch((error) => console.log("error", error)); + await fetch(url, requestOptions) + .then((response) => response.json()) + .then((result) => { + console.log(result); + }) + .catch((error) => console.log("error", error)); }; -module.exports = { postQuestion }; +export default postQuestion; diff --git a/javascript/package.json b/javascript/package.json index d6a08cb..38956df 100644 --- a/javascript/package.json +++ b/javascript/package.json @@ -2,5 +2,6 @@ "dependencies": { "js-yaml": "^4.1.0", "node-fetch": "^2.6.1" - } -} + }, + "type": "module" +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..2319d17 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,94 @@ +{ + "name": "yearOneโ€”Challenges", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "dependencies": { + "node-fetch": "^3.0.0" + } + }, + "node_modules/data-uri-to-buffer": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-3.0.1.tgz", + "integrity": "sha512-WboRycPNsVw3B3TL559F7kuBUM4d8CgMEvk6xEJlOp7OBPjt6G7z8WMWlD2rOFZLk6OYfFIUGsCOWzcQH9K2og==", + "engines": { + "node": ">= 6" + } + }, + "node_modules/fetch-blob": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.1.2.tgz", + "integrity": "sha512-hunJbvy/6OLjCD0uuhLdp0mMPzP/yd2ssd1t2FCJsaA7wkWhpbp9xfuNVpv7Ll4jFhzp6T4LAupSiV9uOeg0VQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "paypal", + "url": "https://paypal.me/jimmywarting" + } + ], + "dependencies": { + "web-streams-polyfill": "^3.0.3" + }, + "engines": { + "node": "^12.20 || >= 14.13" + } + }, + "node_modules/node-fetch": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.0.0.tgz", + "integrity": "sha512-bKMI+C7/T/SPU1lKnbQbwxptpCrG9ashG+VkytmXCPZyuM9jB6VU+hY0oi4lC8LxTtAeWdckNCTa3nrGsAdA3Q==", + "dependencies": { + "data-uri-to-buffer": "^3.0.1", + "fetch-blob": "^3.1.2" + }, + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/node-fetch" + } + }, + "node_modules/web-streams-polyfill": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.1.0.tgz", + "integrity": "sha512-wO9r1YnYe7kFBLHyyVEhV1H8VRWoNiNnuP+v/HUUmSTaRF8F93Kmd3JMrETx0f11GXxRek6OcL2QtjFIdc5WYw==", + "engines": { + "node": ">= 8" + } + } + }, + "dependencies": { + "data-uri-to-buffer": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-3.0.1.tgz", + "integrity": "sha512-WboRycPNsVw3B3TL559F7kuBUM4d8CgMEvk6xEJlOp7OBPjt6G7z8WMWlD2rOFZLk6OYfFIUGsCOWzcQH9K2og==" + }, + "fetch-blob": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.1.2.tgz", + "integrity": "sha512-hunJbvy/6OLjCD0uuhLdp0mMPzP/yd2ssd1t2FCJsaA7wkWhpbp9xfuNVpv7Ll4jFhzp6T4LAupSiV9uOeg0VQ==", + "requires": { + "web-streams-polyfill": "^3.0.3" + } + }, + "node-fetch": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.0.0.tgz", + "integrity": "sha512-bKMI+C7/T/SPU1lKnbQbwxptpCrG9ashG+VkytmXCPZyuM9jB6VU+hY0oi4lC8LxTtAeWdckNCTa3nrGsAdA3Q==", + "requires": { + "data-uri-to-buffer": "^3.0.1", + "fetch-blob": "^3.1.2" + } + }, + "web-streams-polyfill": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.1.0.tgz", + "integrity": "sha512-wO9r1YnYe7kFBLHyyVEhV1H8VRWoNiNnuP+v/HUUmSTaRF8F93Kmd3JMrETx0f11GXxRek6OcL2QtjFIdc5WYw==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..ad62037 --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "node-fetch": "^3.0.0" + } +}