-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
84 lines (75 loc) · 2.23 KB
/
utils.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
import fetch from "node-fetch";
const contributionCommitMessage = "history: has contributed to a private repo.";
const getContributions = async (token, username) => {
const headers = {
Authorization: `bearer ${token}`,
};
const body = {
query: `query {
user(login: "${username}") {
contributionsCollection {
contributionCalendar {
weeks {
contributionDays {
contributionCount
date
}
}
}
}
}
}`,
};
const response = await fetch('https://api.github.com/graphql', {
method: "POST",
body: JSON.stringify(body),
headers: headers,
});
const data = await response.json();
return data;
};
const isSameDay = async (day1, day2) => {
const firstDate = new Date(day1);
const secondDate = new Date(day2);
return (
firstDate.getDate() === secondDate.getDate() &&
firstDate.getMonth() === secondDate.getMonth() &&
firstDate.getFullYear() === secondDate.getFullYear()
);
};
const asyncFilter = async (arr, predicate) => {
const results = await Promise.all(arr.map(predicate));
return arr.filter((_v, index) => results[index]);
};
const filterCommitsOnMsDate = async (msDateToRefer, c) => {
return new Promise(async (resolve) => {
const isContributionCommit = c.message === contributionCommitMessage;
const commitDateMs = Date.parse(c.date);
const isSameDayCommit = await isSameDay(msDateToRefer, commitDateMs);
resolve(isContributionCommit && isSameDayCommit);
});
};
const getLastContributedDay = async (contributionsPerWeek) => {
let lastContributedDay;
for (let i = contributionsPerWeek.length - 1; i >= 0; i--) {
const { contributionDays } = contributionsPerWeek[i];
for (let j = contributionDays.length - 1; j >= 0; j--) {
if (contributionDays[j].contributionCount > 0) {
lastContributedDay = contributionDays[j];
break;
}
}
if (lastContributedDay) {
break;
}
}
return lastContributedDay;
};
export {
contributionCommitMessage,
getContributions,
isSameDay,
asyncFilter,
getLastContributedDay,
filterCommitsOnMsDate,
};