-
Notifications
You must be signed in to change notification settings - Fork 0
/
send-jira-update-via-slack.js
executable file
·133 lines (112 loc) · 3.61 KB
/
send-jira-update-via-slack.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
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
const https = require('https');
const config = require('./config')
/**
* SLACK's PART
*/
/**
* SLACK HELPERS
*/
const sendSlackMessage = (filteredJiraTickets) => {
let message = ''
filteredJiraTickets.forEach((ticket) => {
const { status, summary, ticketNumber } = ticket
message += ` • <${SLACK_TICKET_URI.replace('{ticketNumber}', ticketNumber)}|${ticketNumber}>: ${summary} - *${getJiraStatusWording(status)}*\n`
})
const slackBlocks = `[{"type": "section", "text": {"type": "mrkdwn", "text": "${message}"}}]`
const slackRequestOptions = {
headers: {
'Authorization': `Bearer ${config.SLACK_TOKEN}`,
},
hostname: 'slack.com',
method: 'GET',
path: `/api/chat.postMessage?channel=${config.SLACK_CHANNEL_ID}&blocks=${encodeURI(slackBlocks)}&pretty=1`,
}
const slackRequest = https.request(slackRequestOptions, (res) => {
res.setEncoding('utf8');
res.on('data', (slackAPIResult) => {
const { error, ok } = JSON.parse(slackAPIResult)
if (ok) {
console.log("Your update has been sent, enjoy the 30 seconds you've just saved!")
} else {
console.log("Something went wrong, you'll have to write your update by hand :(", error)
}
})
})
slackRequest.end()
}
/**
* JIRA's PART
*/
/**
* SLACK VARIABLES
*/
const SLACK_TICKET_URI = 'https://fubotv.atlassian.net/browse/{ticketNumber}'
/**
* JIRA VARIABLES
*/
const JIRA_STATUS_URL = {
done: 'https://fubotv.atlassian.net/rest/api/3/status/10005',
onGoing: 'https://fubotv.atlassian.net/rest/api/3/status/3',
review: 'https://fubotv.atlassian.net/rest/api/3/status/10317',
}
/**
* JIRA HELPERS
*/
const getJiraStatusWording = (statusUrl) => {
switch(statusUrl) {
case JIRA_STATUS_URL.onGoing:
return 'On going'
case JIRA_STATUS_URL.review:
return 'In review'
default:
return 'On going'
}
}
const getJiraTickets = async () => {
const jiraRequest0ptions = {
auth: `${config.JIRA_USER_MAIL}:${config.JIRA_TOKEN}`,
hostname: 'fubotv.atlassian.net',
method: 'GET',
path: `/rest/api/3/search?jql=assignee%20%3D%20currentUser%28%29%20`, // <= Get all your assigned tickets
}
return new Promise((resolve) => {
const jiraRequest = https.request(jiraRequest0ptions, (res) => {
let chuncks = ''
res.setEncoding('utf8');
res.on('data', (jiraAPIResult) => {
chuncks += jiraAPIResult
})
res.on('end', () => {
const decodedJiraAPIResult = JSON.parse(chuncks)
const filteredDecodedJiraAPIResult = []
decodedJiraAPIResult?.issues?.forEach((issue) => {
const updateDate = new Date(issue?.fields?.updated).getTime() /* TR => */ + (28 * 1000 * 60 * 60)
const differenceFromNowInHours = (new Date().getTime() - updateDate) / (1000 * 60 * 60)
if (
issue?.fields?.status?.self === JIRA_STATUS_URL.onGoing
|| (issue?.fields?.status?.self === JIRA_STATUS_URL.review && differenceFromNowInHours < 24) // <= Include only the last 24 hours tickets in review
) {
filteredDecodedJiraAPIResult.push({
ticketNumber: issue.key,
status: issue.fields.status.self,
summary: issue.fields.summary
})
}
})
resolve(filteredDecodedJiraAPIResult)
})
})
jiraRequest.end()
})
}
/**
* MAIN
*/
getJiraTickets().then((filteredJiraTickets) => {
if (filteredJiraTickets.length <= 0) {
console.log('You have nothing no tickets with updates... Do you really work ?')
return
}
sendSlackMessage(filteredJiraTickets)
})