-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
169 lines (144 loc) · 4.91 KB
/
index.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
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
const core = require("@actions/core");
const fs = require("fs");
const path = require("path");
const glob = require("glob");
function getInput(name, options) {
// Check if running in GitHub Actions environment
if (process.env.GITHUB_ACTIONS) {
return core.getInput(name, options);
}
// CLI argument parsing
const args = process.argv.slice(2);
const flagIndex = args.findIndex(
(arg) => arg === `--${name}` || arg === `-${name.charAt(0)}`
);
if (flagIndex === -1) {
if (options?.required) {
throw new Error(`Input required and not supplied: ${name}`);
}
return "";
}
// Return the value after the flag
const value = args[flagIndex + 1];
if (!value || value.startsWith("-")) {
throw new Error(`Value not provided for argument: ${name}`);
}
return value;
}
function getFiles(pattern) {
let adjustedPattern = pathPattern;
try {
// Check if the pattern exactly matches a directory
const stats = fs.statSync(pathPattern);
if (stats.isDirectory()) {
adjustedPattern = path.join(pathPattern, "**", "*.yaml");
}
} catch (err) {
// If path doesn't exist, keep the original pattern
// This allows glob patterns that don't match actual paths to still work
}
const files = glob.sync(adjustedPattern).filter((file) => {
try {
const isFile = fs.statSync(file).isFile();
const hasYamlExtension =
file.toLowerCase().endsWith(".yaml") ||
file.toLowerCase().endsWith(".yml");
return isFile && hasYamlExtension;
} catch (err) {
core.warning(`Unable to check file: ${file}. Error: ${err.message}`);
return false;
}
});
return files;
}
async function run() {
try {
const configYamlUtils = await import("@continuedev/config-yaml");
// Get inputs
const pathPattern = getInput("paths", { required: true });
const ownerSlug = getInput("owner-slug", { required: true });
const continueApiDomain =
getInput("continue-api-domain") || "api.continue.dev";
const apiKey = getInput("api-key", { required: true });
const isAssistant = getInput("is-assistant") === "true";
// Handle both old 'private' and new 'visibility' parameters
const privateInput = getInput("private");
const visibilityInput = getInput("visibility") || "public";
// Determine visibility value
let visibility;
if (privateInput !== "") {
// If private input is provided, use it for backward compatibility
visibility = privateInput === "true" ? "private" : "public";
} else {
// Otherwise use the new visibility input
visibility = visibilityInput;
}
const files = getFiles(pathPattern);
if (files.length === 0) {
console.log("No yaml files found matching the pattern");
return;
}
console.log("Uploading...");
for (const filepath of files) {
const packageSlug = path.basename(filepath, ".yaml");
const fullSlug = `${ownerSlug}/${packageSlug}`;
const protocol = continueApiDomain.startsWith("localhost")
? "http"
: "https";
const packagePageUrl = `${protocol}://hub.continue.dev/platform/${fullSlug}`;
const url = `${protocol}://${continueApiDomain}/packages/${fullSlug}/versions/new`;
// Resolve the absolute path of the file
const absoluteFilePath = path.isAbsolute(filepath)
? filepath
: path.join(process.cwd(), filepath);
// Check if the file exists
if (!fs.existsSync(absoluteFilePath)) {
core.warning(`File not found at path: ${absoluteFilePath}`);
continue; // Skip to next file
}
// Read file contents
const content = fs.readFileSync(absoluteFilePath, "utf8");
// Lint the file
try {
configYamlUtils.parseConfigYaml(content);
} catch (err) {
core.setFailed(`⚠️ Invalid YAML file ${filepath}: ${err.message}`);
continue;
}
// Make POST request using built-in fetch
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({ content, isAssistant, visibility }),
});
// Check for errors
if (!response.ok) {
const errorText = await response.text();
if (
errorText.includes("Version") &&
errorText.includes("already exists")
) {
console.log(
`Version from ${filepath} already exists at ${packagePageUrl}`
);
} else {
core.setFailed(
`Failed to upload ${filepath} to ${fullSlug}: HTTP error ${response.status}: ${errorText}`
);
}
continue;
}
const data = await response.json();
console.log(
`Successfully published new version from ${filepath} to ${packagePageUrl}:`,
data.versionId
);
}
} catch (error) {
core.setFailed(error.message);
}
}
run();