-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.ts
113 lines (97 loc) · 3.2 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
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
import { getBooleanInput, getInput } from '@actions/core';
import { error, info } from 'console';
import { writeFile } from 'fs-extra';
import fetch, { Response, fileFrom, FormData } from 'node-fetch';
// https://www.conventionalcommits.org/en/v1.0.0/
const CONVENTIONAL_COMMIT_REGEX = new RegExp(
/^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(.*\))?(!?): .*$/,
);
export const isValidConventionalCommitMessage = (message: string) => {
return CONVENTIONAL_COMMIT_REGEX.test(message);
};
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 guessConfig = getBooleanInput('guess_config', { required: false });
const branch = getInput('branch', { required: false });
const outputPath = getInput('output_path');
if (configPath && guessConfig) {
const errorMsg = "Can't set both configPath and guessConfig";
error(errorMsg);
throw Error(errorMsg);
}
if (commitMessage && !isValidConventionalCommitMessage(commitMessage)) {
const errorMsg =
'Invalid commit message format. Please follow the Conventional Commits format: https://www.conventionalcommits.org/en/v1.0.0/';
error(errorMsg);
throw Error(errorMsg);
}
info(configPath ? 'Uploading spec and config files...' : 'Uploading spec file...');
const response = await uploadSpecAndConfig(
inputPath,
configPath,
stainless_api_key,
projectName,
commitMessage,
guessConfig,
branch,
);
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,
guessConfig: boolean,
branch: string,
): Promise<Response> {
const formData = new FormData();
formData.set('projectName', projectName);
if (commitMessage) {
formData.set('commitMessage', 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'));
}
if (guessConfig) {
formData.set('guessConfig', 'true');
}
if (branch) {
formData.set('branch', branch);
}
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);
});
}