-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
91 lines (74 loc) · 3.19 KB
/
index.ts
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
import * as ynab from "ynab"
import * as core from "@actions/core";
import * as cache from "@actions/cache"
import { buildAutomationMap } from "./automation"
import { parse } from "./parsers"
import { ApproverAutomation } from "./automations/approve"
import { BottomlessAutomation } from "./automations/bottomless"
import { ReplicateAutomation } from "./automations/replicate"
import { StockAutomation } from "./automations/stocks"
import { Yahoo } from "./datasources/yahoo";
import { DataSource } from "./datasources/datasource";
async function run() {
const cacheEnabled = core.getBooleanInput("cache", { required: false })
try
{
const apiKey = core.getInput("api-key", { trimWhitespace: true, required: true })
const api = new ynab.API(apiKey)
if (cacheEnabled) {
await cache.restoreCache([DataSource.cacheDirectory], "ynab-cache")
}
const yahoo = new Yahoo()
if (cacheEnabled) {
core.debug("Removing old entries from the cache")
try
{
await yahoo.cleanCache()
}
catch (err)
{
core.warning("Failed to clean cache, proceeding with current state (this shouldn't cause any problems)")
core.warning(err)
}
}
const automations = buildAutomationMap([
new ApproverAutomation(api),
new BottomlessAutomation(api),
new ReplicateAutomation(api),
new StockAutomation(api, yahoo, yahoo),
])
const budgetId = core.getInput("budget-id", { trimWhitespace: true, required: false }) || "default"
const budget = await api.budgets.getBudgetById(budgetId)
const accounts = await api.accounts.getAccounts(budget.data.budget.id)
await Promise.all(accounts.data.accounts.map(async account => {
const triggers = parse(account.note || "")
if (!triggers.length) return;
await Promise.all(triggers.map(async trigger => {
const automation = automations[trigger.kind]
if (!automation) {
core.error(`Unknown automation kind '${trigger.kind}' in account '${account.name}'`)
return;
}
core.info(`Running automation '${automation.kind}' in account '${account.name}'`)
try {
await automation.run(budget.data.budget, account, trigger.options)
core.info(`Finished running automation '${automation.kind}' in account '${account.name}'`)
} catch (err) {
core.error(`Failed to run automation '${automation.kind}' in account '${account.name}'`)
core.error(err)
throw err
}
}))
}))
core.setOutput("success", true)
} catch (err) {
core.debug(err.message)
core.debug(err.stack)
core.setFailed(err)
} finally {
if (cacheEnabled) {
await cache.saveCache([DataSource.cacheDirectory], "ynab-cache")
}
}
}
run()