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

Feature/refactor project #107

Merged
merged 19 commits into from
Jan 22, 2024
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
85ce8ee
:wrench: config: update packages to latest version
lucasvtiradentes Jan 15, 2024
49cd354
:wrench: config: update vscode configs
lucasvtiradentes Jan 16, 2024
aaa2c6d
:recycle: refactor: split code base to improve maintainability
lucasvtiradentes Jan 16, 2024
a51eb70
:sparkles: feature: add methos to get all ticktick tasks and github c…
lucasvtiradentes Jan 17, 2024
43e7259
:sparkles: feature: add gas properties types
lucasvtiradentes Jan 19, 2024
219628a
:sparkles: feature: add method to get all gcal tasks
lucasvtiradentes Jan 19, 2024
3cdc309
:sparkles: feature: updated sync tasks from ticktick to gcal
lucasvtiradentes Jan 20, 2024
f2f4007
:recycle: refactor: update sync tracking results
lucasvtiradentes Jan 20, 2024
eede958
:recycle: refactor: update email methods
lucasvtiradentes Jan 21, 2024
035fc2d
:recycle: refactor: github commits sync methods
lucasvtiradentes Jan 21, 2024
ef54d15
:art: codestyle: update sync removed github commits
lucasvtiradentes Jan 21, 2024
0182b13
:bug: bugfix: commit date error
lucasvtiradentes Jan 21, 2024
c5674d6
:bug: bugfix: remove commits error
lucasvtiradentes Jan 21, 2024
a317226
:sparkles: feature: update email methods
lucasvtiradentes Jan 21, 2024
3556665
:wrench: config: update configs schema
lucasvtiradentes Jan 21, 2024
d1d8eee
:adhesive_bandage: fix: validation tests
lucasvtiradentes Jan 21, 2024
fb78fc3
:adhesive_bandage: fix: email items datetime format
lucasvtiradentes Jan 21, 2024
3919b65
:wrench: config: update configs example file
lucasvtiradentes Jan 21, 2024
13538d0
:wrench: config: update postbuild script
lucasvtiradentes Jan 22, 2024
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
Prev Previous commit
Next Next commit
✨ feature: add method to get all gcal tasks
lucasvtiradentes committed Jan 19, 2024
commit 219628a32bb6f38e92574dddb6bd104fb5230e37
6 changes: 4 additions & 2 deletions dist/classes/GoogleCalendar.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/// <reference types="google-apps-script" />
type TGoogleEvent = GoogleAppsScript.Calendar.Schema.Event;
type TParsedGoogleEvent = Pick<TGoogleEvent, 'colorId' | 'id' | 'summary' | 'description' | 'htmlLink' | 'attendees' | 'visibility' | 'reminders' | 'start' | 'end' | 'created' | 'updated' | 'extendedProperties'>;
export declare const createMissingCalendars: (allGcalendarsNames: string[]) => void;
export declare const getAllCalendars: () => GoogleAppsScript.Calendar.Schema.CalendarListEntry[];
export declare const checkIfCalendarExists: (calendarName: string) => GoogleAppsScript.Calendar.Schema.CalendarListEntry;
export declare const createCalendar: (calName: string) => GoogleAppsScript.Calendar.Schema.Calendar;
export declare function getTasksFromGoogleCalendars(allCalendars: string[]): TParsedGoogleEvent[];
export {};
1 change: 1 addition & 0 deletions dist/consts/configs.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export declare const CONFIGS: {
readonly DEBUG_MODE: true;
readonly MAX_GCAL_TASKS: 2500;
};
export type TGasPropertiesSchema = {
todayGithubAddedCommits: string;
175 changes: 131 additions & 44 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -61,14 +61,13 @@
const url = `https://api.github.com/search/commits?q=author:${username}&page=${pageNumber}&sort=committer-date&per_page=100`;
let response;
if (personalToken !== '') {
response = yield fetch(url, { headers: { Authorization: `Bearer ${personalToken}` } });
response = UrlFetchApp.fetch(url, { muteHttpExceptions: true, headers: { Authorization: `Bearer ${personalToken}` } });
}
else {
response = yield fetch(url);
response = UrlFetchApp.fetch(url, { muteHttpExceptions: true });
}
console.log(response);
const data = (_a = JSON.parse(yield response.text())) !== null && _a !== void 0 ? _a : {};
if (response.status !== 200) {
const data = (_a = JSON.parse(response.getContentText())) !== null && _a !== void 0 ? _a : {};
if (response.getResponseCode() !== 200) {
if (data.message === 'Validation Failed') {
throw new Error(ERRORS.invalidGithubUsername);
}
@@ -109,6 +108,103 @@
});
}

const CONFIGS = {
DEBUG_MODE: true,
MAX_GCAL_TASKS: 2500
};

const logger = {
info: (message, ...optionalParams) => {
{
console.log(message, ...optionalParams);
}
},
error: (message, ...optionalParams) => {
{
console.error(message, ...optionalParams);
}
}
};

function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

// =============================================================================
const createMissingCalendars = (allGcalendarsNames) => {
let createdCalendar = false;
allGcalendarsNames.forEach((calName) => {
if (!checkIfCalendarExists(calName)) {
createCalendar(calName);
logger.info(`created google calendar: [${calName}]`);
createdCalendar = true;
}
});
if (createdCalendar) {
sleep(2000);
}
};
const getAllCalendars = () => {
var _a;
const calendars = (_a = Calendar.CalendarList.list({ showHidden: true }).items) !== null && _a !== void 0 ? _a : [];
return calendars;
};
const checkIfCalendarExists = (calendarName) => {
const allCalendars = getAllCalendars();
const calendar = allCalendars.find((cal) => cal.summary === calendarName);
return calendar;
};
const createCalendar = (calName) => {
const calendarObj = Calendar;
const owenedCalendars = calendarObj.CalendarList.list({ showHidden: true }).items.filter((cal) => cal.accessRole === 'owner');
const doesCalendarExists = owenedCalendars.map((cal) => cal.summary).includes(calName);
if (doesCalendarExists) {
throw new Error(`calendar ${calName} already exists!`);
}
const tmpCalendar = calendarObj.newCalendar();
tmpCalendar.summary = calName;
tmpCalendar.timeZone = calendarObj.Settings.get('timezone').value;
const calendar = calendarObj.Calendars.insert(tmpCalendar);
return calendar;
};
function getCalendarByName(calName) {
const calendar = getAllCalendars().find((cal) => cal.summary === calName);
return calendar;
}
function parseGoogleEvent(ev) {
var _a, _b, _c, _d, _e;
const parsedGoogleEvent = {
id: ev.id,
summary: ev.summary,
description: (_a = ev.description) !== null && _a !== void 0 ? _a : '',
htmlLink: ev.htmlLink,
attendees: (_b = ev.attendees) !== null && _b !== void 0 ? _b : [],
reminders: (_c = ev.reminders) !== null && _c !== void 0 ? _c : {},
visibility: (_d = ev.visibility) !== null && _d !== void 0 ? _d : 'default',
start: ev.start,
end: ev.end,
created: ev.created,
updated: ev.updated,
colorId: ev.colorId,
extendedProperties: (_e = ev.extendedProperties) !== null && _e !== void 0 ? _e : {}
};
return parsedGoogleEvent;
}
function getEventsFromCalendar(calendar) {
const allEvents = Calendar.Events.list(calendar.id, { maxResults: CONFIGS.MAX_GCAL_TASKS }).items;
const parsedEventsArr = allEvents.map((ev) => parseGoogleEvent(ev));
return parsedEventsArr;
}
function getTasksFromGoogleCalendars(allCalendars) {
const tasks = allCalendars.reduce((acc, cur) => {
const taskCalendar = cur;
const calendar = getCalendarByName(taskCalendar);
const tasksArray = getEventsFromCalendar(calendar);
return [...acc, ...tasksArray];
}, []);
return tasks;
}

function getDateFixedByTimezone(timeZoneIndex) {
const date = new Date();
date.setHours(date.getHours() + timeZoneIndex);
@@ -127,11 +223,11 @@

const getIcsCalendarTasks = (icsLink, timezoneCorrection) => __awaiter(void 0, void 0, void 0, function* () {
const parsedLink = icsLink.replace('webcal://', 'https://');
console.log({ parsedLink });
const response = yield fetch(parsedLink);
console.log({ response });
const data = yield response.text();
console.log({ data });
const urlResponse = UrlFetchApp.fetch(parsedLink, { validateHttpsCertificates: false, muteHttpExceptions: true });
const data = urlResponse.getContentText() || '';
if (urlResponse.getResponseCode() !== 200) {
throw new Error(ERRORS.httpsError + parsedLink);
}
if (data.search('BEGIN:VCALENDAR') === -1) {
throw new Error('RESPOSTA INVALIDA PRA UM ICS');
}
@@ -155,7 +251,6 @@
};
return [...acc, eventObj];
}, []);
console.log({ allEventsArr });
const allEventsParsedArr = allEventsArr.map((item) => {
const parsedDateTime = getParsedIcsDatetimes(item.DTSTART, item.DTEND, item.TZID, timezoneCorrection);
return {
@@ -167,7 +262,6 @@
end: parsedDateTime.finalDtend
};
});
console.log({ allEventsParsedArr });
return allEventsParsedArr;
});
const getStrBetween = (str, substr1, substr2) => {
@@ -224,19 +318,6 @@
return typeof obj === 'object' && obj !== null;
}

const logger = {
info: (message, ...optionalParams) => {
{
console.log(message, ...optionalParams);
}
},
error: (message, ...optionalParams) => {
{
console.error(message, ...optionalParams);
}
}
};

function validateNestedObject(obj, requiredConfigs) {
if (!isObject(obj)) {
return false;
@@ -330,31 +411,37 @@
return __awaiter(this, void 0, void 0, function* () {
const shouldSyncGithub = this.configs[githubConfigsKey];
const shouldSyncTicktick = this.configs[ticktickConfigsKey];
if (!shouldSyncGithub && !shouldSyncTicktick) {
logger.info('nothing to sync');
return;
}
const info = {
githubCommits: [],
ticktickTasks: [],
ticktickGcalTasks: [],
allIcsLinks: [],
allGcalTasks: []
};
// prettier-ignore
const allGoogleCalendars = [...new Set([]
.concat(shouldSyncGithub ? [this.configs[githubConfigsKey].commits_configs.commits_calendar, this.configs[githubConfigsKey].issues_configs.issues_calendar] : [])
.concat(shouldSyncTicktick ? [...this.configs[ticktickConfigsKey].ics_calendars.map((item) => item.gcal), ...this.configs[ticktickConfigsKey].ics_calendars.map((item) => item.dcal_done)] : []))
];
console.log({ allGoogleCalendars });
// createMissingCalendars(allGoogleCalendars);
const allIcsLinks = this.configs[ticktickConfigsKey].ics_calendars.map((item) => item.link);
console.log({ allIcsLinks });
for (const ics of allIcsLinks) {
console.log({ ics1: ics });
const tasks = yield getIcsCalendarTasks(ics, this.configs.settings.timezone_correction);
console.log({ tasks });
createMissingCalendars(allGoogleCalendars);
info.allGcalTasks = getTasksFromGoogleCalendars(allGoogleCalendars);
if (shouldSyncTicktick) {
const icsCalendarsConfigs = this.configs[ticktickConfigsKey].ics_calendars;
info.allIcsLinks = icsCalendarsConfigs.map((item) => item.link);
info.ticktickGcalTasks = getTasksFromGoogleCalendars([...new Set(icsCalendarsConfigs.map((item) => item.gcal))]);
info.ticktickTasks = mergeArraysOfArrays(yield Promise.all(info.allIcsLinks.map((ics) => __awaiter(this, void 0, void 0, function* () {
const tasks = yield getIcsCalendarTasks(ics, this.configs.settings.timezone_correction);
return tasks;
}))));
}
if (shouldSyncGithub) {
info.githubCommits = yield getAllGithubCommits(this.configs[githubConfigsKey].username, this.configs[githubConfigsKey].personal_token);
}
const ticktickTasks = mergeArraysOfArrays(yield Promise.all(allIcsLinks.map((ics) => __awaiter(this, void 0, void 0, function* () {
console.log({ ics });
const tasks = yield getIcsCalendarTasks(ics, this.configs.settings.timezone_correction);
console.log({ tasks });
return tasks;
}))));
console.log(ticktickTasks);
console.log(1);
const githubCommits = yield getAllGithubCommits(this.configs[githubConfigsKey].username, this.configs[githubConfigsKey].personal_token);
console.log(3);
console.log(githubCommits);
console.log(info);
});
}
}
Loading