-
Notifications
You must be signed in to change notification settings - Fork 55
/
index.js
131 lines (115 loc) Β· 3.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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");
const PREMIUM = core.getInput("PREMIUM");
async function main() {
// v8 => v9
const stats = await axios(
"https://api.todoist.com/sync/v9/completed/get_stats",
{ headers: { Authorization: `Bearer ${TODOIST_API_KEY}` } }
);
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 = "<!-- TODO-IST:";
const closingTag = "-->";
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<!-- ${tagToLookFor}:START -->\n<!-- ${tagToLookFor}:END -->`
);
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 = "[email protected]";
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();
})();