diff --git a/origin.js b/origin.js new file mode 100644 index 0000000..aca76a8 --- /dev/null +++ b/origin.js @@ -0,0 +1,141 @@ +const core = require("@actions/core"); +const axios = require("axios"); +const Humanize = require("humanize-plus"); +const fs = require("fs"); +const exec = require("./exec"); + +const TODOIST_API_KEY = core.getInput("TODOIST_API_KEY") || process.env.TODOIST_API_KEY; +const PREMIUM = core.getInput("PREMIUM"); + +async function main() { + // v8 => v9 + const stats = await axios( + "https://api.todoist.com/sync/v9/completed/get_stats", + { + method: "POST", + headers: { + Authorization: `Bearer ${TODOIST_API_KEY}`, + "Content-Type": "application/json", + }, + data: { + resource_types: '["all"]', + // limit: 20, + }, + } + ); + await updateReadme(stats.data); +} + +let todoist = []; +let jobFailFlag = false; +const README_FILE_PATH = "./README.md"; + +async function updateReadme(data) { + const { karma, completed_count, days_items, goals, week_items } = data; + + const karmaPoint = [`🏆 **${Humanize.intComma(karma)}** Karma Points`]; + todoist.push(karmaPoint); + + const dailyGoal = [ + `🌸 Completed **${days_items[0].total_completed.toString()}** tasks today`, + ]; + todoist.push(dailyGoal); + + if (PREMIUM == "true") { + const weekItems = [ + `🗓 Completed **${week_items[0].total_completed.toString()}** tasks this week`, + ]; + todoist.push(weekItems); + } + + const totalTasks = [ + `✅ Completed **${Humanize.intComma(completed_count)}** tasks so far`, + ]; + todoist.push(totalTasks); + + const longestStreak = [ + `⏳ Longest streak is **${goals.max_daily_streak.count}** days`, + ]; + todoist.push(longestStreak); + + if (todoist.length == 0) return; + + if (todoist.length > 0) { + // console.log(todoist.length); + // const showTasks = todoist.reduce((todo, cur, index) => { + // return todo + `\n${cur} ` + (((index + 1) === todoist.length) ? '\n' : ''); + // }) + const readmeData = fs.readFileSync(README_FILE_PATH, "utf8"); + + const newReadme = buildReadme(readmeData, todoist.join(" \n")); + if (newReadme !== readmeData) { + core.info("Writing to " + README_FILE_PATH); + fs.writeFileSync(README_FILE_PATH, newReadme); + if (!process.env.TEST_MODE) { + commitReadme(); + } + } else { + core.info("No change detected, skipping"); + process.exit(0); + } + } else { + core.info("Nothing fetched"); + process.exit(jobFailFlag ? 1 : 0); + } +} + +// console.log(todoist.length); + +const buildReadme = (prevReadmeContent, newReadmeContent) => { + const tagToLookFor = ""; + const startOfOpeningTagIndex = prevReadmeContent.indexOf( + `${tagToLookFor}START` + ); + const endOfOpeningTagIndex = prevReadmeContent.indexOf( + closingTag, + startOfOpeningTagIndex + ); + const startOfClosingTagIndex = prevReadmeContent.indexOf( + `${tagToLookFor}END`, + endOfOpeningTagIndex + ); + if ( + startOfOpeningTagIndex === -1 || + endOfOpeningTagIndex === -1 || + startOfClosingTagIndex === -1 + ) { + core.error( + `Cannot find the comment tag on the readme:\n\n` + ); + process.exit(1); + } + return [ + prevReadmeContent.slice(0, endOfOpeningTagIndex + closingTag.length), + "\n", + newReadmeContent, + "\n", + prevReadmeContent.slice(startOfClosingTagIndex), + ].join(""); +}; + +const commitReadme = async () => { + // Getting config + const committerUsername = "Hasan"; + const committerEmail = "aldoyh@gmail.com"; + const commitMessage = "Todoist updated."; + // Doing commit and push + await exec("git", ["config", "--global", "user.email", committerEmail]); + await exec("git", ["config", "--global", "user.name", committerUsername]); + await exec("git", ["add", README_FILE_PATH]); + await exec("git", ["commit", "-m", commitMessage]); + // await exec('git', ['fetch']); + await exec("git", ["push"]); + core.info("Readme updated successfully."); + // Making job fail if one of the source fails + process.exit(jobFailFlag ? 1 : 0); +}; + +(async () => { + await main(); +})(); \ No newline at end of file