forked from mill6-plat6aux/pathfinder-testbed
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub.js
58 lines (53 loc) · 2.1 KB
/
github.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
/*!
* Copyright 2024 Takuro Okada.
* Released under the MIT License.
*/
// @ts-check
import { Http } from "./http.js";
const GITHUB_API_VERSION = "2022-11-28";
const USER_AGENT = "pact-api-testbed";
/**
* @param {string} owner Repository holders
* @param {string} repository
* @param {string} [path] Path from repository root (no leading slash required)
* @returns {Promise<Array<string>>} File names
*/
export async function getFileList(owner, repository, path) {
let response = await Http.request("get", `https://api.github.com/repos/${owner}/${repository}/contents/${path != null ? path : ""}`, {
Accept: "application/json",
"X-GitHub-Api-Version": GITHUB_API_VERSION,
"User-Agent": USER_AGENT
});
if(response.status != 200) {
throw new Error(`An error occurred when accessing GitHub. Status:${response.status} Body:${response.body}`);
}
if(response.body == null || !Array.isArray(response.body)) {
throw new Error(`The file list retrieved from GitHub was not an array. Body:${response.body}`);
}
return response.body
.filter(entry => entry.type == "file" && /pact-openapi-\d+\.\d+\.\d+\.yaml/.test(entry.name))
.map(entry => entry.name);
}
/**
* @param {string} owner Repository holders
* @param {string} repository
* @param {string} path Path from repository root (no leading slash required)
* @returns {Promise<Buffer>}
*/
export async function getFile(owner, repository, path) {
let response = await Http.request("get", `https://api.github.com/repos/${owner}/${repository}/contents/${path}`, {
Accept: "application/vnd.github.raw+json",
"X-GitHub-Api-Version": GITHUB_API_VERSION,
"User-Agent": USER_AGENT
});
if(response.status != 200) {
throw new Error(`An error occurred when accessing GitHub. Status:${response.status} Body:${response.body}`);
}
if(response.body == null) {
throw new Error(`File data is empty.`);
}
if(!(response.body instanceof Buffer)) {
throw new Error(`File data is not binary. Body:${response.body}`);
}
return response.body;
}