This repository was archived by the owner on Dec 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
154 lines (132 loc) · 4.66 KB
/
index.js
File metadata and controls
154 lines (132 loc) · 4.66 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const core = require('@actions/core');
const https = require('https');
async function makeRequest(options, data) {
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let body = '';
res.on('data', chunk => body += chunk);
res.on('end', () => {
try {
resolve({
statusCode: res.statusCode,
body: body ? JSON.parse(body) : null
});
} catch (e) {
resolve({
statusCode: res.statusCode,
body: body
});
}
});
});
req.on('error', reject);
if (data) {
req.write(JSON.stringify(data));
}
req.end();
});
}
async function updateEphemeralDatabase(apiHost, apiKey, databaseEntityId, payload) {
const url = new URL(`/api/database/${databaseEntityId}`, apiHost);
core.info(`Updating ephemeral database at url ${url}`);
// Get version from package.json
const version = require('./package.json').version;
const options = {
hostname: url.hostname,
port: url.port || 443,
path: url.pathname,
method: 'PUT',
headers: {
'Authorization': `apikey ${apiKey}`,
'Content-Type': 'application/json',
'User-Agent': 'Tonic-Github-Action',
'X-GitHub-Action': 'update-ephemeral-database',
'X-GitHub-Action-Version': version
}
};
const response = await makeRequest(options, payload);
if (response.statusCode >= 400) {
throw new Error(`HTTP ${response.statusCode}: ${JSON.stringify(response.body)}`);
}
return response.body;
}
function buildExpiryConfig(expiryMode, inputs) {
switch (expiryMode.toLowerCase()) {
case 'static': {
const expiryMinutes = parseInt(inputs.expiryMinutes);
if (!expiryMinutes || expiryMinutes <= 0) {
throw new Error('expiry-minutes is required and must be positive for static expiry mode');
}
return {
expiryType: 'Static',
durationEnd: {
minutesFromStartToExpiry: expiryMinutes,
minutesFromLastActivityToExpiry: 60
}
};
}
case 'inactivity': {
const inactivityMinutes = parseInt(inputs.inactivityMinutes);
if (!inactivityMinutes || inactivityMinutes <= 0) {
throw new Error('inactivity-minutes is required and must be positive for inactivity expiry mode');
}
return {
expiryType: 'Inactivity',
durationEnd: {
minutesFromStartToExpiry: 0,
minutesFromLastActivityToExpiry: inactivityMinutes
}
};
}
case 'business-hours': {
return {
expiryType: 'BusinessHours',
businessHoursEnd: {
timeZone: inputs.businessHoursTimezone,
startDayName: inputs.businessHoursStartDay,
endDayName: inputs.businessHoursEndDay,
startHour: parseInt(inputs.businessHoursStartHour),
endHour: parseInt(inputs.businessHoursEndHour)
}
};
}
case 'never':
return null;
default:
throw new Error(`Invalid expiry-mode: ${expiryMode}. Must be one of: static, inactivity, business-hours, never`);
}
}
async function run() {
try {
const apiHost = core.getInput('ephemeral-url');
const apiKey = core.getInput('ephemeral-api-key');
const databaseEntityId = core.getInput('database-entity-id');
const ephemeralEntityName = core.getInput('entity-name');
const databaseDescription = core.getInput('database-description');
const expiryMode = core.getInput('expiry-mode') || 'never';
const payload = {
name: ephemeralEntityName,
databaseDescription: databaseDescription
};
// Build expiry configuration based on mode
const expiryConfig = buildExpiryConfig(expiryMode, {
expiryMinutes: core.getInput('expiry-minutes'),
inactivityMinutes: core.getInput('inactivity-minutes'),
businessHoursTimezone: core.getInput('business-hours-timezone'),
businessHoursStartDay: core.getInput('business-hours-start-day'),
businessHoursEndDay: core.getInput('business-hours-end-day'),
businessHoursStartHour: core.getInput('business-hours-start-hour'),
businessHoursEndHour: core.getInput('business-hours-end-hour')
});
if (expiryConfig) {
payload.expiry = expiryConfig;
}
core.info(`Updating ephemeral database ${databaseEntityId} with expiry mode: ${expiryMode}`);
core.info(`Payload: ${JSON.stringify(payload, null, 2)}`);
const result = await updateEphemeralDatabase(apiHost, apiKey, databaseEntityId, payload);
core.info(`Ephemeral database updated successfully: ${JSON.stringify(result)}`);
} catch (error) {
core.setFailed(`Failed to update ephemeral database: ${error.message}`);
}
}
run();