Repository navigation
Expand file tree
/
Copy pathgithub-actions-utils.js
More file actions
206 lines (181 loc) · 6.85 KB
/
Copy pathgithub-actions-utils.js
File metadata and controls
206 lines (181 loc) · 6.85 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const fs = require('fs-extra');
const axios = require('axios');
const path = require('path');
class GitHubActionsUtils {
constructor() {
this.logsDir = path.join(__dirname, 'logs');
this.ensureLogsDirectory();
}
/**
* Ensure logs directory exists
*/
async ensureLogsDirectory() {
try {
await fs.ensureDir(this.logsDir);
} catch (error) {
console.error('Failed to create logs directory:', error.message);
}
}
/**
* Log message to console and file (GitHub Actions compatible)
*/
async log(message, logFile = 'general.log') {
const timestamp = new Date().toISOString();
const logMessage = `[${timestamp}] ${message}`;
// Log to console (GitHub Actions will capture this)
console.log(logMessage);
// Also log to file for artifact upload
try {
const logPath = path.join(this.logsDir, logFile);
await fs.appendFile(logPath, logMessage + '\n');
} catch (error) {
console.error('Failed to write to log file:', error.message);
}
}
/**
* Read configuration from environment or config file
*/
getConfig() {
// Try environment variables first (GitHub Actions)
if (process.env.ACCESS_TOKEN || process.env.GITHUB_ACTIONS) {
return {
AccessToken: process.env.ACCESS_TOKEN,
RefreshToken: process.env.REFRESH_TOKEN,
Amount: process.env.AMOUNT,
MoneycardId: process.env.MONEYCARD_ID
};
}
// Fallback to config file for local development
try {
const configPath = path.join(__dirname, 'config.json');
if (fs.existsSync(configPath)) {
return fs.readJsonSync(configPath);
}
} catch (error) {
console.error('Error reading config file:', error.message);
}
throw new Error('No configuration found. Set environment variables or create config.json');
}
/**
* Update configuration (for GitHub Actions, output new tokens)
*/
async updateConfig(updates) {
// In GitHub Actions, we output the new tokens for the workflow to handle
if (process.env.GITHUB_ACTIONS) {
if (updates.AccessToken) {
console.log(`::add-mask::${updates.AccessToken}`);
await this.setOutput('access_token', updates.AccessToken);
}
if (updates.RefreshToken) {
console.log(`::add-mask::${updates.RefreshToken}`);
await this.setOutput('refresh_token', updates.RefreshToken);
}
await this.log(`Tokens updated for GitHub Actions: ${Object.keys(updates).join(', ')}`);
return;
}
// For local development, update the config file
try {
const configPath = path.join(__dirname, 'config.json');
let currentConfig = {};
if (fs.existsSync(configPath)) {
currentConfig = fs.readJsonSync(configPath);
}
const newConfig = { ...currentConfig, ...updates };
fs.writeJsonSync(configPath, newConfig, { spaces: 2 });
await this.log(`Configuration updated: ${Object.keys(updates).join(', ')}`);
} catch (error) {
await this.log(`Error updating config: ${error.message}`, 'error.log');
throw error;
}
}
/**
* Make HTTP request with retry logic
*/
async makeHttpRequest(options, retries = 3) {
for (let attempt = 1; attempt <= retries; attempt++) {
try {
await this.log(`Making HTTP request (attempt ${attempt}/${retries}): ${options.method} ${options.url}`);
const response = await axios({
...options,
timeout: 30000, // 30 second timeout
validateStatus: (status) => status < 500 // Don't throw on 4xx errors
});
if (response.status >= 400) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
await this.log(`HTTP request successful: ${response.status}`);
return response;
} catch (error) {
const errorMessage = `HTTP request failed (attempt ${attempt}/${retries}): ${error.message}`;
await this.log(errorMessage, 'error.log');
if (attempt === retries) {
throw new Error(`All ${retries} attempts failed. Last error: ${error.message}`);
}
// Wait before retry (exponential backoff)
const waitTime = Math.pow(2, attempt) * 1000;
await this.log(`Waiting ${waitTime}ms before retry...`);
await this.sleep(waitTime);
}
}
}
/**
* Sleep for specified milliseconds
*/
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* Check if current day is Friday (5) or Saturday (6)
*/
isWeekend() {
const today = new Date();
const dayOfWeek = today.getDay();
return dayOfWeek === 5 || dayOfWeek === 6; // Friday or Saturday
}
/**
* Get current time in Israel timezone
*/
getCurrentTimeIsrael() {
return new Date().toLocaleString('en-US', {
timeZone: 'Asia/Jerusalem',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
}
/**
* Validate required config fields
*/
validateConfig(config, requiredFields) {
const missingFields = requiredFields.filter(field => !config[field] || config[field].trim() === '');
if (missingFields.length > 0) {
throw new Error(`Missing required configuration fields: ${missingFields.join(', ')}`);
}
}
/**
* Set GitHub Actions output using Environment Files
*/
async setOutput(name, value) {
if (process.env.GITHUB_ACTIONS && process.env.GITHUB_OUTPUT) {
try {
await fs.appendFile(process.env.GITHUB_OUTPUT, `${name}=${value}\n`);
} catch (error) {
console.error(`Failed to write to GITHUB_OUTPUT: ${error.message}`);
// Fallback to deprecated method for compatibility
console.log(`::set-output name=${name}::${value}`);
}
}
}
/**
* Mask sensitive value in GitHub Actions
*/
maskValue(value) {
if (process.env.GITHUB_ACTIONS && value) {
console.log(`::add-mask::${value}`);
}
}
}
module.exports = new GitHubActionsUtils();