-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
127 lines (103 loc) · 3.08 KB
/
client.ts
File metadata and controls
127 lines (103 loc) · 3.08 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
import WebSocket from "ws";
import "dotenv/config";
import got from "got";
const { USER_SESSION } = process.env;
if (!USER_SESSION) throw new Error("you need a USER_SESSION");
const user = "hacksore";
const repo = "test";
const getFirstCommit = async ({
user,
repo,
}: {
user: string;
repo: string;
}) => {
// get job id
const commitRes = await got(
`https://api.github.com/repos/${user}/${repo}/commits`
);
const commitResData = JSON.parse(commitRes.body);
return commitResData[0].sha;
};
const getSocketUrl = async () => {
let liveLogs: any;
const commitHash = await getFirstCommit({ user, repo });
console.log("loaded commit", commitHash);
// get job id
const jobAPIResp = await got(
`https://api.github.com/repos/${user}/${repo}/actions/runs?status=in_progress`
);
const jobAPIdata = JSON.parse(jobAPIResp.body);
console.log("jobapi", jobAPIdata);
if (jobAPIdata.workflow_runs.length === 0) {
console.log("fuck off this aint ready yet bruv");
return;
}
const jobHtmlResponse = await got(jobAPIdata.workflow_runs[0].html_url);
const jobIdRegex = new RegExp(/data-item-id="job_(\d+)"/);
const jobId = jobHtmlResponse.body.match(jobIdRegex)![1];
console.log("Found job id", jobId);
const liveLogUrl = `https://github.com/${user}/${repo}/commit/${commitHash}/checks/${jobId}/live_logs`;
console.log(liveLogUrl);
try {
liveLogs = await got(liveLogUrl, {
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();
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}`);
});