/
Geekbot.js
118 lines (94 loc) · 2.95 KB
/
Geekbot.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
const faker = require('faker')
const chrono = require('chrono-node')
const GithubActivity = require('./GithubActivity')
const utils = require('./utils')
class Geekbot {
constructor(username, organizations = [], beforeDateText) {
organizations = organizations.filter(orgName => !!orgName)
this.githubActivity = new GithubActivity(username, organizations)
this.beforeDateText = beforeDateText
this.activities = []
}
getHowDoYouFeel() {
this.resetCache()
const howDowYouFeel = faker.commerce.productAdjective()
return utils.capitalize(howDowYouFeel)
}
async getWhatDidYouDo(separator = '\n') {
let text = 'nope'
if (await this.hasActivities()) {
text = await this.activitiesToText(separator)
}
return text
}
async getWhatWillYouDo() {
const activities = await this.getActivities()
if (activities.length === 0) return 'nope'
let repoNames = activities.map(activity =>
this.githubActivity.getRepoName(activity)
)
repoNames = Array.from(new Set(repoNames))
const reposText =
repoNames.length > 1
? `${repoNames.slice(0, -1).join(', ')} and ${repoNames.slice(-1)[0]}`
: repoNames[0]
return `Probably more work on ${reposText}`
}
getBlocking() {
this.resetCache()
return utils.capitalize(faker.hacker.phrase())
}
async getActivities() {
if (this.activities.length === 0) {
const beforeDate = chrono.parseDate(this.beforeDateText, new Date())
const activities = await this.githubActivity.filter(beforeDate)
for (const activity of activities) {
if (this.inOrganizations(activity)) {
this.activities.push(activity)
}
}
}
return this.activities
}
async hasActivities() {
return (await this.getActivities()).length > 0
}
inOrganizations(activity) {
return !!this.githubActivity.getRepoName(activity)
}
async activitiesToText(separator = ', ') {
const activities = await this.getActivities()
const repoNames = new Set()
let texts = []
for (const activity of activities) {
const repoName = this.githubActivity.getRepoName(activity)
if (repoNames.has(repoName)) continue
const activityText = this.activityToText(activity)
if (!activityText) continue
repoNames.add(repoName)
texts.push(activityText)
}
return texts.join(separator)
}
activityToText(activity) {
const repoName = this.githubActivity.getRepoName(activity)
if (!repoName) return ''
switch (activity.type) {
case 'CreateEvent': {
const description = activity.description
? ` (${activity.description})`
: ''
const verb = activity.ref_type === 'repository' ? 'Created' : 'Worked on'
return `${verb} ${repoName}${description}`
}
case 'WatchEvent':
return ''
default:
return `Worked on ${repoName}`
}
}
resetCache() {
this.activities = []
}
}
module.exports = Geekbot