-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.ts
More file actions
140 lines (114 loc) · 3.19 KB
/
client.ts
File metadata and controls
140 lines (114 loc) · 3.19 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
import WebSocket from "ws";
import { parse } from "node-html-parser";
import got from "got";
import "dotenv/config";
import {
decodeSession,
generatePresenceId,
getWebsocketSessionId,
sendPacket,
subscribe,
unsubscribe,
} from "./util.js";
import got from "got";
const { USER_SESSION } = process.env;
if (!USER_SESSION) throw new Error("you need a USER_SESSION");
// get dom
const body = await got("https://github.com/Hacksore/Hacksore/actions", {
headers: {
Cookie: `user_session=${USER_SESSION}`,
},
}).text();
const html = parse(body);
// get all teh things in the DOM to sub to
const elements = html.querySelectorAll(".js-socket-channel[data-channel]");
const subscriptions = elements.map((item) => ({
id: item.attrs["data-channel"],
description: item.attrs["data-tooltip-global"],
url: item.attrs["data-url"],
job: item.id
}))
// .filter(item => !item.job.includes("check_suite"));
// console.log(subscriptions)
const [event, idkYet] = decodeSession(sessionId);
// console.log("initial event", event);
// console.log("idk what this is yet", idkYet);
const jobParams = {
commitHash: "d8a3b73ad5b6a643bd218d482b296182d8622ae8",
// TODO: how do programmatically get this
jobId: "9237147042",
};
// const getRunningJobs
const getSocketUrl = async ({
commitHash,
jobId,
}: {
commitHash: string;
jobId: string;
}) => {
let liveLogs: any;
try {
liveLogs = await got(
`https://github.com/Hacksore/test/commit/${commitHash}/checks/${jobId}/live_logs`,
{
headers: {
accept: "application/json",
cookie: `user_session=${USER_SESSION}`,
},
}
).json();
} catch (err: any) {
console.log(err.message);
throw new Error("could not get the live_logs response");
}
try {
const url = liveLogs.data.authenticated_url;
const res2 = await got(url, {
headers: {
accept: "application/json",
},
});
if (res2.statusCode !== 200) {
throw new Error("could not get the authenticated_url response");
}
const body = JSON.parse(res2.body);
return body.logStreamWebSocketUrl;
} catch (err) {
console.log(err);
throw new Error("Error getting authenticated_url");
}
};
const socketUrl = await getSocketUrl(jobParams);
const rawParams = socketUrl.split("?")[1];
const params = new URLSearchParams(rawParams);
const runId = Number(params.get("runId"));
const tenantId = params.get("tenantId");
const ws = new WebSocket(socketUrl, {
headers: {
Host: "pipelines.actions.githubusercontent.com",
Origin: "https://github.com",
},
});
const sendPacket = (ws: WebSocket, packet: any) => {
// there is some strange termination string required
const payload = JSON.stringify(packet) + "\x1e";
ws.send(payload);
};
ws.on("open", () => {
console.log("Socket opened");
sendPacket(ws, { protocol: "json", version: 1 });
sendPacket(ws, {
arguments: [tenantId, runId],
target: "WatchRunAsync",
type: 1,
});
// Figure out what other packets we can send
});
// print out all messages
ws.on("message", (data) => {
console.log(data.toString());
});
// print out all messages
ws.on("close", (errorCode) => {
console.log(`Socket closed with code ${errorCode}`);
});