Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: javascript files work with GitHub actions #48

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,4 @@ dmypy.json
.pyre/

# Node
/node_modules
/node_modules
20 changes: 7 additions & 13 deletions javascript/add_challenge_to_spaces.js
Original file line number Diff line number Diff line change
@@ -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);
}
90 changes: 54 additions & 36 deletions javascript/js_actions.js
Original file line number Diff line number Diff line change
@@ -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 = `
👏👏 <h3>Thanks to <u>${author_name}</u> for the question!👏👏</h3><br>
<br>
<strong>Difficulty Level:</strong> ${difficulty}<br>
<strong>Sourced from:</strong> ${source}<br>
<br>
${body} <br>
<br>
This question was submitted in the YearOne Open Source project, to submit a topic, go to github.com/YearOne-Prep/YearOne-prep-challenges
<br><br>
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;
5 changes: 3 additions & 2 deletions javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"dependencies": {
"js-yaml": "^4.1.0",
"node-fetch": "^2.6.1"
}
}
},
"type": "module"
}
94 changes: 94 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"node-fetch": "^3.0.0"
}
}