-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathloaders.ts
210 lines (183 loc) · 4.95 KB
/
loaders.ts
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import {NormalizedEnvironment, SchemaPointer, Endpoint} from './config';
import * as probot from 'probot';
import Dataloader from 'dataloader';
import yaml from 'js-yaml';
import axios from 'axios';
import {
getIntrospectionQuery,
buildClientSchema,
printSchema,
Source,
} from 'graphql';
import {isNil, parseEndpoint} from './utils';
const GET_FILE_QUERY = /* GraphQL */ `
query GetFile($repo: String!, $owner: String!, $expression: String!) {
repository(name: $repo, owner: $owner) {
object(expression: $expression) {
... on Blob {
text
}
}
}
}
`.replace(/\s+/g, ' ');
type FileLoaderConfig = {
context: probot.Context;
owner: string;
repo: string;
};
interface FileLoaderInput {
path: string;
ref: string;
throwNotFound?: boolean;
onError?: (error: any) => void;
}
export type FileLoader = (input: FileLoaderInput) => Promise<string | null>;
export type ConfigLoader = () => Promise<object | null | undefined>; // id is the same every time
export function createFileLoader(config: FileLoaderConfig): FileLoader {
const loader = new Dataloader<FileLoaderInput, string | null, string>(
(inputs) => {
return Promise.all(
inputs.map(async (input) => {
const {context, repo, owner} = config;
const {ref, path} = input;
const result = await context.github.graphql(GET_FILE_QUERY, {
repo,
owner,
expression: `${ref}:${path}`,
});
try {
if (!result) {
throw new Error(`No result :(`);
}
if (result.data) {
return result.data.repository.object.text as string;
}
return (result as any).repository.object.text as string;
} catch (error) {
const failure = new Error(`Failed to load '${path}' (ref: ${ref})`);
if (input.throwNotFound === false) {
if (input.onError) {
input.onError(failure);
} else {
console.error(failure);
}
return null;
}
throw failure;
}
}),
);
},
{
batch: false,
cacheKeyFn(obj) {
return `${obj.ref} - ${obj.path}`;
},
},
);
return (input) => loader.load(input);
}
export function createConfigLoader(
config: FileLoaderConfig & {
ref: string;
},
loadFile: FileLoader,
): ConfigLoader {
const loader = new Dataloader<string, object | null, string>(
(ids) => {
const errors: Error[] = [];
const onError = (error: any) => {
errors.push(error);
};
return Promise.all(
ids.map(async (id) => {
const [yamlConfig, ymlConfig, pkgFile] = await Promise.all([
loadFile({
...config,
path: `.github/${id}.yaml`,
throwNotFound: false,
onError,
}),
loadFile({
...config,
path: `.github/${id}.yml`,
throwNotFound: false,
onError,
}),
loadFile({
...config,
path: 'package.json',
throwNotFound: false,
onError,
}),
]);
if (yamlConfig || ymlConfig) {
return yaml.safeLoad((yamlConfig || ymlConfig)!);
}
if (pkgFile) {
try {
const pkg = JSON.parse(pkgFile);
if (pkg[id]) {
return pkg[id];
}
} catch (error) {
errors.push(error);
}
}
console.error([`Failed to load config:`, ...errors].join('\n'));
return null;
}),
);
},
{
batch: false,
},
);
return () => loader.load('graphql-inspector');
}
export async function printSchemaFromEndpoint(endpoint: Endpoint) {
const config = parseEndpoint(endpoint);
const {data: response} = await axios.request({
method: config.method,
url: config.url,
headers: config.headers,
data: {
query: getIntrospectionQuery().replace(/\s+/g, ' ').trim(),
},
});
const introspection = response.data;
return printSchema(
buildClientSchema(introspection, {
assumeValid: true,
}),
);
}
export async function loadSources({
config,
oldPointer,
newPointer,
loadFile,
}: {
config: NormalizedEnvironment;
oldPointer: SchemaPointer;
newPointer: SchemaPointer;
loadFile: FileLoader;
}): Promise<{
old: Source;
new: Source;
}> {
// Here, config.endpoint is defined only if target's branch matches branch of environment
// otherwise it's empty
const useEndpoint = !isNil(config.endpoint);
const [oldFile, newFile] = await Promise.all([
useEndpoint
? printSchemaFromEndpoint(config.endpoint!)
: loadFile(oldPointer),
loadFile(newPointer),
]);
return {
old: new Source(oldFile!),
new: new Source(newFile!),
};
}