-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·139 lines (118 loc) · 4 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
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
try { require('dotenv/config'); } catch (error) {}
const SteamUser = require("steam-user");
const fs = require('fs');
const axios = require('axios');
const APP_ID = 730;
const DEPOT_IDS = {'2347771': 'windows', '2347773': 'linux'}
const BRANCH = 'public';
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const GITHUB_REPOSITORY = process.env.GITHUB_REPOSITORY || 'cs2-analysis/cs2-analysis';
const GITHUB_BRANCH = process.env.GITHUB_BRANCH || 'master';
const GITHUB_WORKFLOW = process.env.GITHUB_WORKFLOW || 'update.yml';
const VERSION_FILE = process.env.VERSION_FILE || 'version.json';
if (!GITHUB_TOKEN) {
console.error('GITHUB_TOKEN is required');
process.exit(1);
}
const versions = JSON.parse(fs.readFileSync(VERSION_FILE));
Object.keys(DEPOT_IDS).forEach(depotId => {
const manifest = versions[depotId];
if (manifest == null || isNaN(manifest)) {
console.error(`Manifest for depot ${depotId} not found in ${VERSION_FILE}`);
process.exit(1);
}
});
function mapAppInfo(appinfo) {
return Object.entries(appinfo.depots)
.filter(([depotId, depot]) => Number(depotId) && depot.manifests)
.flatMap(([depotId, depot]) => {
return Object.entries(depot.manifests).map(([branch, manifest]) => [branch, depotId, manifest.gid]);
}).reduce((acc, [branch, depotId, gid]) => {
acc[branch] ??= {};
acc[branch][depotId] = gid;
return acc;
}, {});
}
const client = new SteamUser({
picsCacheAll: true,
enablePicsCache: true,
changelistUpdateInterval: 10000,
});
client.on('appUpdate', (appId, data) => {
if (!data.appinfo.depots) {
return;
}
onAppUpdate(appId, mapAppInfo(data.appinfo)).catch(error => {
console.error(error);
process.exit(2);
});
});
client.on('error', (err) => {
console.error(err);
process.exit(1);
});
let initPromiseResolve;
const initPromise = new Promise(resolve => initPromiseResolve = resolve);
client.on('loggedOn', async () => {
try {
console.log('Logged into Steam');
const res = await client.getProductInfo([APP_ID], [], true);
const data = mapAppInfo(res.apps[APP_ID].appinfo)[BRANCH];
Object.entries(versions).forEach(([depotId, manifest]) => {
if (data[depotId] !== manifest) {
console.error(`Manifest for depot ${depotId} changed while offline (expected: ${manifest}, found: ${data[depotId]})`);
process.exit(2);
}
});
console.log('Manifests match, starting...');
initPromiseResolve();
} catch (error) {
console.error(error);
process.exit(1);
}
})
client.logOn({anonymous: true});
async function onAppUpdate(appId, depots) {
if (appId != APP_ID) {
return;
}
console.log(`App ${appId} updated`);
console.dir(depots, {depth: null});
await initPromise;
const data = depots[BRANCH];
if (!data) {
console.warn(`No data for branch ${BRANCH}`);
return;
}
for (const [depotId, manifest] of Object.entries(data)) {
const oldManifest = versions[depotId];
if (oldManifest == null || oldManifest === manifest) {
continue;
}
console.log(`Depot ${depotId} updated (old: ${oldManifest}, new: ${manifest})`, versions);
await onDepotUpdate(appId, depotId, manifest);
versions[depotId] = manifest;
fs.writeFileSync(VERSION_FILE, JSON.stringify(versions, null, 2));
}
}
async function onDepotUpdate(appId, depotId, manifestId) {
const res = await axios.request({
method: 'POST',
url: `https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/workflows/${GITHUB_WORKFLOW}/dispatches`,
headers: {
'Authorization': `Bearer ${GITHUB_TOKEN}`,
'Accept': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
"User-Agent": 'curl/8.11.0', // probably placebo, but I think we look more legit this way :)
},
data: {
ref: GITHUB_BRANCH,
inputs: {
depotId: depotId,
manifestId: manifestId,
gitBranch: DEPOT_IDS[depotId],
},
},
});
console.log(`Dispatched workflow for depot ${depotId} with manifest ${manifestId} (status: ${res.status})`);
}