-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (69 loc) · 3.04 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
import { exit } from 'process'
import axios from 'axios'
import {loadDoneIssues, persistDoneIssues} from "./persistance";
import {getAllSprintIssues, getAllSprints, getIssue} from "./jira";
import {sendMsg} from "./slack";
async function getJoke() {
// no I don't get it
const { data: joke } = await axios.get('https://v2.jokeapi.dev/joke/Programming?safe-mode')
if (joke.type === 'single') return joke.joke
return `*${joke.setup}*\n${joke.delivery}`
}
function issueTypeToMsg(issueType) {
if (issueType === 'Story') return `*${issueType}* :large_green_circle: `
if (issueType === 'Bug') return `*${issueType}* :red_circle: `
if (issueType === 'Task') return `*${issueType}* :large_blue_circle: `
else return `*${issueType}*`
}
async function bot(postToSlack = true) {
// await sendMsg('hello world')
const doneIssues = await loadDoneIssues()
// await getAllBoards()
const stuff = await getAllSprintIssues(process.env.sprintId)
// filter for Tasks only (sub-tasks are also returned)
const sprintTasks = stuff.filter(({ fields: { issuetype: { name }} }) => name !== 'Sub-task')
// const nonSprintTasks = stuff.filter(({ fields: { issuetype: { name }} }) => name === 'Sub-task')
// console.log(nonSprintTasks.length)
// console.log(sprintTasks.map(({ fields: { issuetype: { name }, summary } }) => summary))
// return console.log(sprintTasks.map(({ fields: { issuetype: { name }}}) => name))
// console.log(stuff.map(({ fields: { status: { name } } }) => ({ name })))
const dii = sprintTasks.filter(({ fields: { status: { name } } }) => name === 'Done')
const newDoneIssues = dii.filter(({ id }) => !doneIssues.includes(id))
// console.log(newDoneIssues)
if (newDoneIssues.length > 0) {
let msg = 'New tasks are done! :muscle:\n'
newDoneIssues.forEach(async ({ key, fields: { summary, issuetype: { name: issueType } } }) => {
msg += `- *${issueType}*: ${summary}\nhttps://${process.env.jiraHost}/browse/${key}\n`
})
msg += `\n${await getJoke()}`
if (postToSlack) await sendMsg(msg)
else console.log(msg)
await persistDoneIssues([
...doneIssues,
...newDoneIssues.map(({ id }) => id ),
])
}
}
const r = f => f().then(() => console.log('DONE')).catch(e => console.error(e))
function runBot() {
setInterval(
() => r(bot),
1000*JSON.parse(process.env.intervalInSeconds)
)
}
// const quote = getRandomQuote()
// const msg = quote.text + " - " + quote.author
// console.log(msg)
// sendMsg(msg).then(console.log).catch(console.error)
function main() {
const secondLastArg = process.argv[process.argv.length-2]
const lastArg = process.argv[process.argv.length-1]
if (lastArg.startsWith('sprints')) r(getAllSprints)
else if (lastArg.startsWith('sprint-tasks')) getAllSprintIssues(process.env.sprintId).then(console.log)
else if (lastArg.startsWith('init')) bot(false).then()
else if (lastArg.startsWith('start')) runBot()
else if (secondLastArg.startsWith('issue')) getIssue(lastArg)
else if (lastArg.startsWith('cron')) r(bot)
else { console.log('Missing cmd args'); exit(1) }
}
main()