This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
starfish.js
163 lines (145 loc) · 5.41 KB
/
starfish.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const {
alternateIdColumnNumber,
githubIdColumnNumber,
githubImportantEvents,
githubToken,
ignoreSelfOwnedEvents,
minimumNumberOfContributions,
} = require('./globals');
const { createLuxonDateTimeFromIso } = require('./dateTimes');
const fetch = require('node-fetch');
const parse = require('parse-link-header');
function isEventImportant(event) {
const type = event.type;
if (githubImportantEvents.indexOf(type) >= 0) {
return true;
}
if (event.payload) {
const typeWithAction = `${type}.${event.payload.action}`;
if (githubImportantEvents.indexOf(typeWithAction) >= 0) {
return true;
}
}
return false;
}
function filterResponseForImportantEvents(allEventsFromFetch) {
return allEventsFromFetch.filter((event) => {
return isEventImportant(event);
});
}
function repoIsNotSelfOwned(eventType) {
const isAuthorAlsoTheOwner = eventType.author_association === 'OWNER';
return !isAuthorAlsoTheOwner;
}
function filterOutSelfOwnedEvents(events) {
const filteredEvents = events.filter((event) => {
switch (event.type) {
case 'PullRequestEvent':
case 'PullRequestReviewEvent':
return repoIsNotSelfOwned(event.payload.pull_request);
case 'CommitCommentEvent':
case 'IssueCommentEvent':
case 'PullRequestReviewCommentEvent':
return repoIsNotSelfOwned(event.payload.comment);
case 'IssuesEvent':
return repoIsNotSelfOwned(event.payload.issue);
default:
return false;
}
});
return filteredEvents;
}
function fetchPageOfDataAndFilter(url) {
return new Promise((resolve) => {
fetch(url, {
method: 'GET',
headers: {
Authorization: `Basic ${githubToken}`,
},
})
.then((response) => {
if (!response.ok) {
console.error(`Error: ${response.status} ${response.statusText} \nFor: ${url}`);
throw new Error(response.statusText);
}
let parsed = parse(response.headers.get('link'));
let importantEvents = [];
response
.json()
.then((json) => {
let filteredForImportant = filterResponseForImportantEvents(json);
importantEvents = importantEvents.concat(filteredForImportant);
if (ignoreSelfOwnedEvents === 'true') {
importantEvents = filterOutSelfOwnedEvents(importantEvents);
}
if (parsed && parsed.next && parsed.next.url) {
fetchPageOfDataAndFilter(parsed.next.url)
.then((newEvents) => {
return resolve(importantEvents.concat(newEvents));
})
.catch((err) => {
console.error(
`Error fetching page of data for ${parsed.next.url}: ${err}`
);
throw err;
});
} else {
return resolve(importantEvents);
}
})
.catch((err) => {
console.error('Error turning response into JSON:', err);
});
})
.catch((err) => console.error('ERROR GRABBING INFO FROM GITHUB!', err));
});
}
function createIdObject(row, importantEvents) {
return {
alternateId: row[alternateIdColumnNumber],
github: row[githubIdColumnNumber],
contributions: importantEvents,
};
}
function isContributionInTimeRange(createdAt, startMoment, endMoment) {
const momentOfContribution = createLuxonDateTimeFromIso(createdAt, 'Etc/UTC');
return (
momentOfContribution.toMillis() >= startMoment.toMillis() &&
momentOfContribution.toMillis() < endMoment.toMillis()
);
}
function didTheyQualify(idObject, dateTimes) {
const startMoment = dateTimes[0];
const endMoment = dateTimes[1];
let numberOfQualifyingContributions = 0;
for (let i = 0; i < idObject.contributions.length; i++) {
const createdAtString = idObject.contributions[i].created_at;
if (isContributionInTimeRange(createdAtString, startMoment, endMoment)) {
numberOfQualifyingContributions++;
}
if (numberOfQualifyingContributions >= minimumNumberOfContributions) {
return true;
}
}
}
function fetchUserDataAndAddToOutput(row, dateTimes) {
const url = `https://api.github.com/users/${row[githubIdColumnNumber]}/events`;
fetchPageOfDataAndFilter(url)
.then((importantEvents) => {
const idObject = createIdObject(row, importantEvents);
if (didTheyQualify(idObject, dateTimes)) {
process.stdout.write(`${idObject.alternateId}\n`);
}
})
.catch((err) => {
console.error('error', err);
});
}
module.exports = {
createIdObject,
didTheyQualify,
fetchPageOfDataAndFilter,
fetchUserDataAndAddToOutput,
filterResponseForImportantEvents,
isContributionInTimeRange,
};