generated from italia/js-action-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (58 loc) · 1.8 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
const core = require('@actions/core')
const github = require('@actions/github')
const https = require('https')
const simpleGit = require('simple-git');
const main = async () => {
const currentRepoGit = simpleGit();
const tags = (await currentRepoGit.tags({'--sort' : 'taggerdate'})).all
const version = tags.pop()
const repoName = github.context.payload.repository.full_name
const changelogUrl = `https://github.com/${repoName}/releases/tag/${version}`
const slackToken = core.getInput('slack_token')
const channelId = core.getInput('channel_id')
const projectName = core.getInput('project_name')
const payload = JSON.stringify({
channel: channelId,
token: slackToken,
attachments: [
{
pretext : `Rilasciata la nuova versione di ${projectName}: ${version}!`,
text : `Changelog disponibile qua: ${changelogUrl}`,
},
],
})
const requestOptions = {
method: 'POST',
port: 443,
hostname: 'slack.com',
path: '/api/chat.postMessage',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': payload.length,
Authorization: `Bearer ${slackToken}`,
Accept: 'application/json',
},
}
const messageRequest = https.request(requestOptions, res => {
let responseData = ''
res.on('data', (d) => {
responseData += d
})
res.on('end', () => {
if (res.statusCode === 200) {
const jsonResponse = JSON.parse(responseData)
if (jsonResponse.ok === true) {
core.setOutput('status', '✅ Message sent!')
return
}
}
core.setFailed(`❌ Failed request: ${responseData}`)
})
})
messageRequest.on('error', () => {
core.setFailed('Failed to fetch Slack')
})
messageRequest.write(payload)
messageRequest.end()
}
main()