Skip to content

Instantly share code, notes, and snippets.

@Kreozot
Last active April 14, 2023 09:46
Show Gist options
  • Save Kreozot/b66094c418e175aa62928f8c7f3ece08 to your computer and use it in GitHub Desktop.
Save Kreozot/b66094c418e175aa62928f8c7f3ece08 to your computer and use it in GitHub Desktop.
const taskIdRegex = /([A-Z]+-[0-9]+)/;
const contributionRegex = /(pushed to branch|pushed new branch)/;
const SELECT_DATE_INTERVAL = 2000;
const getDateTitle = (date) => {
const month = date.toLocaleString('default', { month: 'short' });
const day = date.getDate();
const year = date.getFullYear();
return `${month} ${day}, ${year}`;
};
const getDateElement = (date) => {
const dateTitle = getDateTitle(date);
return document.querySelector(`[data-testid="user-contrib-cell"][title*="${dateTitle}"]`);
};
const clickElement = (element) => {
const clickEvent = new MouseEvent('click', { bubbles: true });
element.dispatchEvent(clickEvent);
};
const selectDate = (date) => new Promise((resolve) => {
const dateElement = getDateElement(date);
if (dateElement) {
clickElement(dateElement);
}
setTimeout(() => {
resolve();
}, SELECT_DATE_INTERVAL);
});
const getContributionElements = () => document.querySelectorAll('.bordered-list > li');
const getContributionsTaskIds = () => {
const contributionElements = getContributionElements();
const taskIds = Array.from(contributionElements)
.map((contributionElement) => contributionElement.innerText)
.filter((taskId) => contributionRegex.test(taskId))
.map((contributionText) => contributionText.match(taskIdRegex)?.[1])
.filter((taskId) => taskId);
const uniqueTaskIds = [...new Set(taskIds)];
return uniqueTaskIds;
};
const getDatesBetween = (startDate, endDate) => {
const dates = [];
for (let date = startDate; date <= endDate; date.setDate(date.getDate() + 1)) {
dates.push(new Date(date));
}
return dates;
};
const getDateFromDateString = (dateString) => {
const [year, month, day] = dateString.split('-');
return new Date(year, month - 1, day);
};
const getContributionTaskIdsBetween = async (startDateString, endDateString) => {
const startDate = getDateFromDateString(startDateString);
const endDate = getDateFromDateString(endDateString);
const dates = getDatesBetween(startDate, endDate);
const taskIds = [];
for (const date of dates) {
await selectDate(date);
taskIds.push({
date: getDateTitle(date),
taskIds: getContributionsTaskIds().join(', '),
});
}
console.log(taskIds);
return taskIds;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment