/
GithubActivity.js
63 lines (54 loc) · 1.42 KB
/
GithubActivity.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
const request = require('request-promise-native')
class GithubActivity {
constructor(username, organizations = []) {
if (!username) {
throw new Error('You need to supply an username')
}
this.username = username
this.organizations = organizations
}
async filter(beforeDate) {
const activities = await request({
url: `https://api.github.com/users/${this.username}/events`,
headers: {
'User-Agent': 'Geekbot fill'
},
json: true
})
return activities.filter(
activity =>
!beforeDate ||
new Date(activity.created_at).getTime() >= beforeDate.getTime()
)
}
getRepoName(activity) {
let name = ''
if (!activity) {
name = ''
} else if (activity.repository && activity.repository.name) {
name = activity.repository.name
} else if (activity.repo && activity.repo.name) {
name = activity.repo.name
} else if (activity.ref) {
name = activity.ref
} else {
name = ''
}
return this.sanitizeRepoName(name)
}
sanitizeRepoName(name) {
if (this.organizations.length === 0) {
return name
} else {
for (const orgName of this.organizations) {
const orgPrefix = `${orgName}/`
const hasOrg = name.search(orgPrefix) !== -1
if (hasOrg) {
return name.replace(orgPrefix, '')
}
}
return ''
}
}
}
module.exports = GithubActivity