-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (114 loc) · 3.88 KB
/
index.js
File metadata and controls
129 lines (114 loc) · 3.88 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
const core = require("@actions/core");
const os = require("os");
const tc = require("@actions/tool-cache");
const https = require("https");
const { join } = require("path");
async function run() {
try {
// Get the version input or the latest release
let version = core.getInput("version") || "latest";
// Get the download URL for the release
const releaseUrl = `https://api.github.com/repos/apppackio/apppack/releases/${version}`;
const data = await downloadJson(releaseUrl);
// Validate the API response
if (!data || !data.tag_name) {
throw new Error(
`Failed to fetch release information for version '${version}'. ` +
`Please check if the version exists or try 'latest'.`,
);
}
if (!data.assets || data.assets.length === 0) {
throw new Error(
`No assets found for release ${data.tag_name}. ` +
`This may be a draft release or the release process may have failed.`,
);
}
// strip the leading "v" from the tag name
version = data.tag_name.slice(1);
// Determine the platform-specific asset name
const arch = process.arch === "x64" ? "x86_64" : process.arch;
const platform =
process.platform.charAt(0).toUpperCase() +
process.platform.slice(1) +
"_" +
arch;
const assetName = `apppack_${version}_${platform}.tar.gz`;
const asset = data.assets.find((a) => a.name === assetName);
if (!asset) {
throw new Error(
`Could not find AppPack CLI asset in release ${data.tag_name}`,
);
}
// Download and cache the AppPack CLI tool
const downloadUrl = asset.browser_download_url;
const pathToTarball = await tc.downloadTool(downloadUrl);
const tmpDir = process.env.RUNNER_TEMP || os.tmpdir();
const pathToCLI = await tc.extractTar(pathToTarball, tmpDir);
// Cache the downloaded tool
const toolPath = await tc.cacheFile(
join(pathToCLI, "apppack"),
"apppack",
"apppack",
data.tag_name,
);
// Add the AppPack CLI directory to the PATH
core.addPath(toolPath);
// Set the version output
core.setOutput("version", version);
} catch (error) {
core.setFailed(error.message);
}
}
// Download a JSON file from a URL
async function downloadJson(url) {
return new Promise((resolve, reject) => {
const headers = {
"User-Agent": "apppackio/setup-apppack",
Accept: "application/vnd.github+json",
};
// Use GitHub token if available for higher rate limits
const token = process.env.GITHUB_TOKEN;
if (token) {
headers["Authorization"] = `Bearer ${token}`;
}
const req = https.get(url, { headers }, (res) => {
let data = "";
// Check for non-success status codes
if (res.statusCode !== 200) {
if (res.statusCode === 404) {
reject(
new Error(
`Release not found (404). Please check if the version exists.`,
),
);
} else if (res.statusCode === 403) {
const rateLimitMsg = token
? `GitHub API rate limit exceeded (403). Even with authentication, you may have hit the limit.`
: `GitHub API rate limit exceeded (403). Consider setting GITHUB_TOKEN to increase rate limits from 60 to 5000 requests/hour.`;
reject(new Error(rateLimitMsg));
} else {
reject(
new Error(
`GitHub API request failed with status ${res.statusCode}`,
),
);
}
return;
}
res.on("data", (chunk) => (data += chunk));
res.on("end", () => {
try {
resolve(JSON.parse(data));
} catch (e) {
reject(
new Error(`Failed to parse GitHub API response: ${e.message}`),
);
}
});
});
req.on("error", (e) => {
reject(new Error(`Network error accessing GitHub API: ${e.message}`));
});
});
}
run();