forked from nodejs/node-core-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.js
More file actions
155 lines (140 loc) · 3.92 KB
/
Copy pathrequest.js
File metadata and controls
155 lines (140 loc) · 3.92 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
import fs from 'node:fs';
import { fetch } from 'undici';
import { CI_DOMAIN } from './ci/ci_type_parser.js';
import proxy from './proxy.js';
import {
isDebugVerbosity,
debuglog
} from './verbosity.js';
function wrappedFetch(url, options, ...args) {
if (isDebugVerbosity()) {
debuglog('[fetch]', url);
}
return fetch(url, options, ...args);
}
export default class Request {
constructor(credentials) {
this.credentials = credentials;
this.proxyAgent = proxy();
}
loadQuery(file) {
const filePath = new URL(`./queries/${file}.gql`, import.meta.url);
return fs.readFileSync(filePath, 'utf8');
}
async fetch(url, options) {
options.agent = this.proxyAgent;
if (url.startsWith(`https://${CI_DOMAIN}`)) {
options.headers = options.headers || {};
Object.assign(options.headers, this.getJenkinsHeaders());
}
return wrappedFetch(url, options);
}
async buffer(url, options = {}) {
const res = await this.fetch(url, options);
const buffer = await res.arrayBuffer();
return Buffer.from(buffer);
}
async text(url, options = {}) {
return this.fetch(url, options).then(res => res.text());
}
async json(url, options = {}) {
options.headers = options.headers || {};
const text = await this.text(url, options);
try {
return JSON.parse(text);
} catch (e) {
if (isDebugVerbosity()) {
debuglog('[Request] Cannot parse JSON response from',
url, ':\n', text);
}
throw e;
}
}
async gql(name, variables, path) {
const query = this.loadQuery(name);
if (path) {
const result = await this.queryAll(query, variables, path);
return result;
} else {
const result = await this.query(query, variables);
return result;
}
}
getJenkinsHeaders() {
const jenkinsCredentials = this.credentials.jenkins;
if (!jenkinsCredentials) {
throw new Error('The request has not been ' +
'authenticated with a Jenkins token');
}
return {
Authorization: `Basic ${jenkinsCredentials}`,
'User-Agent': 'node-core-utils'
};
}
// This is for github v4 API queries, for other types of queries
// use .text or .json
async query(query, variables) {
const githubCredentials = this.credentials.github;
if (!githubCredentials) {
throw new Error('The request has not been ' +
'authenticated with a GitHub token');
}
const url = 'https://api.github.com/graphql';
const options = {
agent: this.proxyAgent,
method: 'POST',
headers: {
Authorization: `Basic ${githubCredentials}`,
'User-Agent': 'node-core-utils',
Accept: 'application/vnd.github.antiope-preview+json'
},
body: JSON.stringify({
query,
variables
})
};
const result = await this.json(url, options);
if (result.errors) {
const { type, message } = result.errors[0];
const err = new Error(`[${type}] GraphQL request Error: ${message}`);
err.data = {
variables
};
throw err;
}
if (result.message) {
const err = new Error(`GraphQL request Error: ${result.message}`);
err.data = {
variables
};
throw err;
}
return result.data;
}
async queryAll(query, variables, path) {
let after = null;
let all = [];
// first page
do {
const varWithPage = Object.assign({
after
}, variables);
const data = await this.query(query, varWithPage);
let current = data;
for (const step of path) {
current = current[step];
}
// current should have:
// totalCount
// pageInfo { hasNextPage, endCursor }
// nodes
all = all.concat(current.nodes);
if (current.pageInfo.hasNextPage) {
after = current.pageInfo.endCursor;
} else {
after = null;
}
} while (after !== null);
return all;
}
}