forked from JasonEtco/activity-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (63 loc) · 2.13 KB
/
index.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
require('dotenv').config()
const { Toolkit } = require('actions-toolkit')
const { GistBox, MAX_LINES, MAX_LENGTH } = require('gist-box')
const capitalize = str => str.slice(0, 1).toUpperCase() + str.slice(1)
const truncate = str =>
str.length <= MAX_LENGTH ? str : str.slice(0, MAX_LENGTH - 3) + '...'
const serializers = {
IssueCommentEvent: item => {
return `🗣 Commented on #${item.payload.issue.number} in ${item.repo.name}`
},
IssuesEvent: item => {
return `❗️ ${capitalize(item.payload.action)} issue #${
item.payload.issue.number
} in ${item.repo.name}`
},
PullRequestEvent: item => {
const emoji = item.payload.action === 'opened' ? '💪' : '❌'
const line = item.payload.pull_request.merged
? '🎉 Merged'
: `${emoji} ${capitalize(item.payload.action)}`
return `${line} PR #${item.payload.pull_request.number} in ${
item.repo.name
}`
}
}
Toolkit.run(
async tools => {
const { GIST_ID, GH_USERNAME, GH_PAT } = process.env
// Get the user's public events
tools.log.debug(`Getting activity for ${GH_USERNAME}`)
const events = await tools.github.activity.listPublicEventsForUser({
username: GH_USERNAME,
per_page: 100
})
tools.log.debug(
`Activity for ${GH_USERNAME}, ${events.data.length} events found.`
)
const content = events.data
// Filter out any boring activity
.filter(event => serializers.hasOwnProperty(event.type))
// We only have five lines to work with
.slice(0, MAX_LINES)
// Call the serializer to construct a string
.map(item => serializers[item.type](item))
// Truncate if necessary
.map(truncate)
// Join items to one string
.join('\n')
const box = new GistBox({ id: GIST_ID, token: GH_PAT })
try {
tools.log.debug(`Updating Gist ${GIST_ID}`)
await box.update({ content })
tools.exit.success('Gist updated!')
} catch (err) {
tools.log.debug('Error getting or update the Gist:')
return tools.exit.failure(err)
}
},
{
event: 'schedule',
secrets: ['GITHUB_TOKEN', 'GH_PAT', 'GH_USERNAME', 'GIST_ID']
}
)