-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (87 loc) · 2.93 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
const core = require('@actions/core')
const github = require('@actions/github')
const { IncomingWebhook } = require('@slack/webhook')
const parse = require('parse-diff')
function getAdditions (diff, fileToWatch) {
const files = parse(diff)
const additions = []
files.filter(file => file.to.includes(fileToWatch.toLowerCase()))
.forEach((file) => {
file.chunks.forEach(chunk => {
chunk.changes.filter(change => change.type === 'add')
.forEach(change => {
additions.push(change.content)
})
})
})
return additions
}
async function getPRDiff (githubToken) {
const octoKit = github.getOctokit(githubToken)
const { data: diff } = await octoKit.rest.pulls.get({
repo: github.context.repo.repo,
owner: github.context.repo.owner,
pull_number: github.context.payload.pull_request.number,
mediaType: { format: 'diff' }
})
return diff
}
async function notifySlack (slackChannel, slackWebhook, title, text) {
const webhook = new IncomingWebhook(slackWebhook, {
channel: slackChannel,
username: title
})
await webhook.send({
text: text
})
}
function getInputs () {
const fileToWatch = core.getInput('file', { required: true })
const slackTitle = core.getInput('slack-title', { required: false })
const slackChannel = core.getInput('slack-channel', { required: true })
const slackWebhook = core.getInput('slack-webhook', { required: true })
const githubToken = core.getInput('github-token', { required: true })
const includePRLinkString = core.getInput('include-pr-link', { required: false })
const includePRLink = includePRLinkString === 'true'
const eventName = github.context.eventName
return { fileToWatch, slackTitle, slackChannel, slackWebhook, githubToken, includePRLink, eventName }
}
function getTitle (slackTitle, fileToWatch) {
if (slackTitle) {
return slackTitle
} else {
return 'Additions to: ' + fileToWatch
}
}
async function run () {
try {
const { fileToWatch, slackTitle, slackChannel, slackWebhook, githubToken, includePRLink, eventName } = getInputs()
let didNotify
if (eventName === 'pull_request') {
const diff = await getPRDiff(githubToken)
const additions = getAdditions(diff, fileToWatch)
if (additions.length === 0) {
console.log('No additions found')
didNotify = false
} else {
console.log('Found ' + additions.length + '. Notifying channel: ' + slackChannel)
const title = getTitle(slackTitle, fileToWatch)
let text = additions.join('\r\n')
if (includePRLink) {
text += '\r\n \r\n' + github.context.payload.pull_request.html_url
}
await notifySlack(slackChannel, slackWebhook, title, text)
didNotify = true
}
} else {
didNotify = false
}
core.setOutput('didNotify', didNotify)
} catch (error) {
console.log(error)
core.setFailed(error.message)
}
}
(async () => {
await run()
})()