forked from luanche/leigod-auto-pause
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (57 loc) · 1.87 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
import 'dotenv/config';
import { LeigodAPI } from './leigod.js';
import * as core from '@actions/core';
const api = new LeigodAPI();
const usernamesStr = process.env.USERNAME_ARR;
const passwordsStr = process.env.PASSWORD_ARR;
if (!usernamesStr || !passwordsStr) {
core.setFailed(
"Please set USERNAME_ARR and PASSWORD_ARR in envs (split multi user by ',')",
);
}
const usernames = usernamesStr.split(',');
const passwords = passwordsStr.split(',');
const users = [];
for (let idx = 0; idx < usernames.length; idx++) {
const username = (usernames[idx] || '').trim();
const password = (passwords[idx] || '').trim();
users.push({ username, password });
}
function hide(str) {
if (!str) return '';
return str.substring(0, 3) + '****' + str.substr(str.length - 4) || '';
}
async function pause(username, password) {
const hideName = hide(username);
if (username && password) {
try {
core.info(hideName + ': Logging in');
await api.login(username, password);
let isPaused = await api.isTimePaused();
core.info(hideName + ': Getting pause status: ' + isPaused);
if (!isPaused) {
core.warning(hideName + ': Time is not paused, trying to pause time');
await api.pauseTime();
isPaused = await api.isTimePaused();
core.info(hideName + ': Getting pause status again: ' + isPaused);
}
} catch (error) {
core.error(hideName + ': ' + error);
return false;
}
} else {
core.error(
hideName + ': The username or password is empty, please check envs',
);
return false;
}
return true;
}
let flag = true;
for (let idx = 0; idx < users.length; idx++) {
const { username, password } = users[idx];
const res = await pause(username, password);
flag = flag && res;
core.info('-----------------------');
}
if (!flag) core.setFailed('Something went wrong! please check the logs.');