-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
85 lines (70 loc) · 2.15 KB
/
main.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
const core = require('@actions/core')
const ics = require('ics')
const ical = require('node-ical')
const { Translate } = require('@google-cloud/translate').v2
function calculateDuration(start, end) {
const timeDifference = end - start
const minutes = Math.floor((timeDifference / (1000 * 60)) % 60)
const hours = Math.floor((timeDifference / (1000 * 60 * 60)) % 24)
const duration = {
hours: hours,
minutes: minutes,
}
return duration
}
async function run() {
try {
const calendarURL = core.getInput('calendar_url')
const projectId = core.getInput('google_cloud_project_id')
const key = core.getInput('google_cloud_api_key')
const targetLanguage = core.getInput('target_language')
const translate = new Translate({ projectId, key })
const events = []
const webEvents = await ical.async.fromURL(calendarURL)
for (const event of Object.values(webEvents)) {
if (!event.start) {
continue
}
const { summary, description, ...other } = event
const [translatedSummary] = await translate.translate(summary, {
from: 'hu',
to: targetLanguage,
})
const start = [
other.start.getFullYear(),
other.start.getMonth() + 1, // Months are zero-based, so add 1
other.start.getDate(),
other.start.getHours(),
other.start.getMinutes(),
]
events.push({
title: translatedSummary,
description,
start,
duration: calculateDuration(other.start, other.end),
location: other.location,
busyStatus: 'BUSY',
transp: other.transparency,
uid: other.uid,
method: other.method,
sequence: parseFloat(other.sequence ?? 0),
alarms: [
{
action: 'display',
description: 'Reminder',
trigger: '-PT15M',
},
],
})
}
const { error, value } = ics.createEvents(events)
if (error) {
throw error
}
core.setOutput('result', value)
} catch (error) {
core.error(`Error ${error}, action may still succeed though`);
core.setFailed(`Action failed with error ${error}`)
}
}
run()