-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
74 lines (65 loc) · 2.1 KB
/
index.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
import { getInput } from '@actions/core';
import { error, info } from 'console';
import { writeFile } from 'fs-extra';
import fetch, { Response, fileFrom, FormData } from 'node-fetch';
export async function main() {
// inputs
const stainless_api_key = getInput('stainless_api_key', { required: true });
const inputPath = getInput('input_path', { required: true });
const configPath = getInput('config_path', { required: false });
const projectName = getInput('project_name', { required: false });
const commitMessage = getInput('commit_message', { required: false });
const outputPath = getInput('output_path');
info(configPath ? 'Uploading spec and config files...' : 'Uploading spec file...');
const response = await uploadSpecAndConfig(
inputPath,
configPath,
stainless_api_key,
projectName,
commitMessage,
);
if (!response.ok) {
const text = await response.text();
const errorMsg = `Failed to upload files: ${response.statusText} ${text}`;
error(errorMsg);
throw Error(errorMsg);
}
info('Uploaded!');
if (outputPath) {
const decoratedSpec = await response.text();
writeFile(outputPath, decoratedSpec);
info('Wrote decorated spec to', outputPath);
}
}
async function uploadSpecAndConfig(
specPath: string,
configPath: string,
token: string,
projectName: string,
commitMessage: string,
): Promise<Response> {
const formData = new FormData();
formData.set('projectName', projectName);
formData.set('commitMesssage', commitMessage);
// append a spec file
formData.set('oasSpec', await fileFrom(specPath, 'text/plain'));
// append a config file, if present
if (configPath) {
formData.set('stainlessConfig', await fileFrom(configPath, 'text/plain'));
}
const response = await fetch('https://api.stainlessapi.com/api/spec', {
method: 'POST',
body: formData,
headers: {
Authorization: `Bearer ${token}`,
'X-GitHub-Action': 'stainless-api/upload-openapi-spec-action',
},
});
return response;
}
if (require.main === module) {
main().catch((err) => {
console.error(err);
process.exit(1);
});
}