-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
123 lines (111 loc) · 3.52 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
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
require('isomorphic-fetch')
const https = require('https')
const { GiphyFetch } = require('@giphy/js-fetch-api')
const { Octokit } = require('@octokit/rest')
const webhookURL = process.env.SLACK_WEBHOOK || 'https://hooks.slack.com/services/TEST/CHANNEL/TOKEN'
const gf = new GiphyFetch(process.env.GIPHY_KEY || 'GIPHY_SDK_API_KEY')
const peddler = (app) => {
const octokit = new Octokit()
const sendMessage = async (context) => {
const pr = context.payload.pull_request
const defaultBranch = pr.head.repo.default_branch || null
const baseBranch = pr.base.ref || null
const prTitle = pr.title
let text
let gif = ''
if (process.env.GIPHY_KEY) {
const bannedWords = ['cert', 'remove', 'manage', 'manager']
const gifLookup = prTitle.split(' ').filter((word) => !bannedWords.includes(word)).join(' ')
const { data: gifs } = await gf.search(`${gifLookup.substring(0, 49)}`, { sort: 'relevant', limit: 1, rating: 'pg' })
gif = gifs[0].url || ''
}
if (prTitle.includes('Revert')) {
text = `:revertit_parrot: ${pr.html_url} :revertit_parrot:`
} else {
text = `Can I get :eyes: on ${pr.html_url}
${gif}`
}
if (!pr.user.login.includes('[bot]') && !pr.draft && baseBranch === defaultBranch) {
const messageBody = {
username: pr.user.login, // This will appear as user name who posts the message
text,
icon_url: pr.user.avatar_url,
attachments: [{
fallback: 'test',
color: '#eed140',
fields: [
{
title: 'PR Title',
value: `${prTitle}`,
short: true
},
{
title: 'Repo',
value: `${pr.head.repo.name}`,
short: true
},
{
title: 'Commits',
value: `${pr.commits}`,
short: true
},
{
title: 'Additions',
value: `${pr.additions}`,
short: true
},
{
title: 'Deletions',
value: `${pr.deletions}`,
short: true
},
{
title: 'Changed files',
value: `${pr.changed_files}`,
short: true
}
]
}]
}
const payload = JSON.stringify(messageBody)
await new Promise((resolve, reject) => {
const requestOptions = {
method: 'POST',
header: {
'Content-Type': 'application/json'
}
}
const req = https.request(webhookURL, requestOptions, (res) => {
let response = ''
res.on('data', (d) => {
response += d
})
res.on('end', () => {
resolve(response)
})
})
req.on('error', (e) => {
reject(e)
})
req.write(payload)
req.end()
})
return {
message: 'Success',
statusCode: 200
}
}
}
app.on('issues.opened', async context => {
const issueComment = context.issue({ body: 'Thanks for opening this issue!' })
return octokit.issues.createComment({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
issue_number: context.payload.issue.number,
body: issueComment
})
})
app.on('pull_request.opened', async context => sendMessage(context))
app.on('pull_request.ready_for_review', async context => sendMessage(context))
}
exports.peddler = peddler