From 5f18190ea55ae9b2bbdf87cde65a1a6e3f210457 Mon Sep 17 00:00:00 2001 From: abhisheknaiidu Date: Fri, 27 Nov 2020 19:29:51 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A6=20Fix=20premium=20issue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 208 +++++++++++++++++++++++++++---------------------------- 1 file changed, 101 insertions(+), 107 deletions(-) diff --git a/index.js b/index.js index dac99b0..133de57 100644 --- a/index.js +++ b/index.js @@ -8,128 +8,122 @@ const TODOIST_API_KEY = core.getInput("TODOIST_API_KEY"); const PREMIUM = core.getInput("PREMIUM"); async function main() { - const stats = await axios(`https://api.todoist.com/sync/v8.3/completed/get_stats?token=${TODOIST_API_KEY}`); - await updateReadme(stats.data); + const stats = await axios( + `https://api.todoist.com/sync/v8.3/completed/get_stats?token=${TODOIST_API_KEY}` + ); + await updateReadme(stats.data); } let todoist = []; let jobFailFlag = false; -const README_FILE_PATH = './README.md'; +const README_FILE_PATH = "./README.md"; async function updateReadme(data) { + const { karma, completed_count, days_items, goals, week_items } = 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); + const karmaPoint = [`🏆 **${Humanize.intComma(karma)}** Karma Points`]; + todoist.push(karmaPoint); - if(PREMIUM) { - 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 dailyGoal = [ + `🌸 Completed **${days_items[0].total_completed.toString()}** tasks today`, + ]; + todoist.push(dailyGoal); - const longestStreak = [ - `⏳ Longest streak is **${goals.max_daily_streak.count}** days`, + if (PREMIUM === "true") { + const weekItems = [ + `🗓 Completed **${week_items[0].total_completed.toString()}** tasks this week`, ]; - 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); - } + todoist.push(weekItems); } - // console.log(todoist.length); + 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; - - 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); + 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); } - return [ - prevReadmeContent.slice(0, endOfOpeningTagIndex + closingTag.length), - '\n', - newReadmeContent, - '\n', - prevReadmeContent.slice(startOfClosingTagIndex), - ].join(''); - }; - - const commitReadme = async () => { - // Getting config - const committerUsername = 'Abhishek Naidu'; - const committerEmail = 'example@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 + } 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 = "Abhishek Naidu"; + const committerEmail = "example@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 +(async () => { + await main(); +})();