-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
170 lines (156 loc) · 7.34 KB
/
Copy pathmain.ts
File metadata and controls
170 lines (156 loc) · 7.34 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
import { ZipReader, HttpRangeReader, TextWriter } from "https://deno.land/x/zipjs@v2.7.24/index.js";
import * as mime from "npm:mime-types";
const token = Deno.env.get("GITHUB_TOKEN_NOPERMISSIONS");
console.log("Found token:", !!token)
const zipReaderMap = new Map();
Deno.serve(async (req) => {
const url = new URL(req.url);
const opts = {
headers: {
Authorization: `token ${token}`,
},
};
let remote_url, target_file = '', cache_control = "max-age=31536000";
// Look for url like org/repo/artifacts/xxxxx/file
const groups = url.pathname.match(/\/([^\/]*)\/([^\/]*)\/.*artifacts\/(\d{10})\/?(.*)/);
if (groups !== null) {
const owner = groups[1];
const repo = groups[2];
const artifact = groups[3];
console.log({owner, repo, artifact});
target_file = groups[4];
remote_url = `https://api.github.com/repos/${owner}/${repo}/actions/artifacts/${artifact}/zip`
}
// If not found, look for url like org/repo/assets/xxxxx/file
if (remote_url === undefined) {
const groups = url.pathname.match(/\/([^\/]*)\/([^\/]*)\/.*assets\/(\d{9})\/?(.*)/);
if (groups !== null) {
const owner = groups[1];
const repo = groups[2];
const asset = groups[3];
console.log({owner, repo, asset});
target_file = groups[4];
remote_url = `https://api.github.com/repos/${owner}/${repo}/releases/assets/${asset}`;
opts.headers['Accept'] = 'application/octet-stream';
opts.redirect = 'follow';
}
}
// If not found, look for url like org/repo/branch/file
// If it matches, find the latest action run in the repo on the branch and use its first artifact
if (remote_url === undefined) {
const groups = url.pathname.match(/\/([^\/]*)\/([^\/]*)\/([^\/]*)\/?(.*)/);
if (groups !== null) {
const owner = groups[1];
const repo = groups[2];
const branch = groups[3];
console.log({owner, repo, branch});
target_file = groups[4];
if (branch.match(/^v?[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$/)) {
// branch is a release tag name - fetch from release assets
const tag = branch[0] == 'v' ? branch.slice(1) : branch;
const release = (await (await fetch(
`https://api.github.com/repos/${owner}/${repo}/releases/tags/${tag}`,
opts,
)).json());
if (release.assets_url) {
const assets = await (await fetch(release.assets_url)).json();
for (const asset of assets) {
if (asset.name.match(/documentation.*/)) {
if (target_file == '') {
target_file = `documentation-${tag}/index.html`;
}
console.log(
'Redirecting to',
`${url.origin}/${owner}/${repo}/assets/${asset.id}/${target_file}`,
);
return Response.redirect(
`${url.origin}/${owner}/${repo}/assets/${asset.id}/${target_file}`,
302,
);
}
}
}
}
else {
try {
// Retreive relatively few per page - hopefully we won't need to fetch many anyway, so it won't increase latency much.
const per_page = 10;
// Retreive runs on branch, for each run, retreive the first artifact that has "docs_html" name.
// Assume that one of the first per_page * 10 workflow runs has a docs build.
for (let page=1; page < 10; page++) {
const runs = (await (await fetch(
`https://api.github.com/repos/${owner}/${repo}/actions/runs?branch=${branch}&page=${page}&per_page=${per_page}`,
opts,
)).json()).workflow_runs;
for (const run of runs) {
const artifact_names = (
url.searchParams.has('artifact_name') ?
url.searchParams.get('artifact_name').split(',') :
(repo === 'scipp' ? ['docs_html', 'html', 'DocumentationHTML'] : ['docs_html'])
);
for (const artifact_name of artifact_names) {
const artifacts = (await (await fetch(
`https://api.github.com/repos/${owner}/${repo}/actions/runs/${run.id}/artifacts?name=${artifact_name}&per_page=1`,
opts,
)).json()).artifacts;
if (artifacts.length != 0) {
console.log(
'Redirecting to',
`${url.origin}/${owner}/${repo}/actions/artifacts/${artifacts[0].id}/${target_file}`,
);
return Response.redirect(
`${url.origin}/${owner}/${repo}/actions/artifacts/${artifacts[0].id}/${target_file}`,
302,
);
}
}
}
if (runs.length < per_page) {
// Retreived fewer than requested, we reached the end of the list.
break;
}
}
return new Response(
"No docs artifact was found on that branch", { status: 404 }
);
} catch (e) {
console.log("Error when fetching latest docs from branch ", e);
return new Response(
"Failed to fetch latest docs from branch", { status: 500 }
);
}
}
}
}
if (remote_url === undefined) {
return new Response(
"No artifact path provided. Provide a valid github artifact url.", { status: 404 }
);
}
if (target_file == '') {
target_file = 'index.html';
if (req.url.slice(-1) !== '/') {
return Response.redirect(req.url + '/');
}
}
console.log(remote_url, target_file);
if (!zipReaderMap.has(remote_url)) {
const httpReader = new HttpRangeReader(remote_url, opts);
zipReaderMap.set(remote_url, new ZipReader(httpReader));
}
const zipReader = zipReaderMap.get(remote_url);
const entries = await zipReader.getEntries().catch(err => {console.error(err); return [];});
const targetEntry = entries.find((entry) => entry.filename === target_file);
if (!targetEntry) {
console.error(`File "${target_file}" not found in ZIP archive.`);
return new Response("File not found", { status: 404 });
}
console.log(`Found "${target_file}" in ZIP archive. Streaming...`);
const stream = new TransformStream();
targetEntry.getData(stream).catch(err => console.log("Stream was interruped", err));
const ext = mime.contentType(target_file.split('.').slice(-1)[0]);
return new Response(
stream.readable,
{ headers: { "Content-Type": ext, "Cache-Control": cache_control}}
);
});