Skip to content

Commit 0e040fb

Browse files
committed
Re-add authenticated fetches
1 parent 6dfc438 commit 0e040fb

1 file changed

Lines changed: 50 additions & 7 deletions

File tree

‎js/main.js‎

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ const envValueOrUnset = (name) => {
3737
};
3838

3939
const logActionSourceEnv = () => {
40-
core.info(`GITHUB_ACTION_REPOSITORY=${envValueOrUnset("GITHUB_ACTION_REPOSITORY")}`);
40+
core.info(
41+
`GITHUB_ACTION_REPOSITORY=${envValueOrUnset("GITHUB_ACTION_REPOSITORY")}`,
42+
);
4143
core.info(`GITHUB_ACTION_REF=${envValueOrUnset("GITHUB_ACTION_REF")}`);
4244
};
4345

@@ -147,26 +149,67 @@ const releaseTag = async (actionRepository, actionRef, githubToken) => {
147149
/**
148150
* @param {string} assetName
149151
* @param {string} [githubToken]
150-
* @returns {Promise<string>}
152+
* @returns {Promise<{ url: string, headers: Record<string, string> }>}
151153
*/
152-
const releaseAssetUrl = async (assetName, githubToken) => {
154+
const releaseAssetRequest = async (assetName, githubToken) => {
153155
// const actionRepository = requiredEnv("GITHUB_ACTION_REPOSITORY");
154156
// const actionRef = requiredEnv("GITHUB_ACTION_REF");
155157
const actionRepository = "sub60/cache-action";
156158
const actionRef = "v0.0.2";
157159
const tag = await releaseTag(actionRepository, actionRef, githubToken);
158160

159-
return `https://github.com/${actionRepository}/releases/download/${tag}/${assetName}`;
161+
if (!githubToken) {
162+
return {
163+
url: `https://github.com/${actionRepository}/releases/download/${tag}/${assetName}`,
164+
headers: {},
165+
};
166+
}
167+
168+
const apiUrl = requiredEnv("GITHUB_API_URL");
169+
170+
const response = await fetch(
171+
`${apiUrl}/repos/${actionRepository}/releases/tags/${encodeURIComponent(tag)}`,
172+
{
173+
headers: githubApiHeaders(githubToken),
174+
redirect: "follow",
175+
},
176+
);
177+
178+
if (!response.ok) {
179+
throw new Error(
180+
`Failed to resolve release '${tag}' in '${actionRepository}': ${response.status} ${response.statusText}`,
181+
);
182+
}
183+
184+
const release = await response.json();
185+
const asset = release.assets.find(({ name }) => name === assetName);
186+
187+
if (!asset) {
188+
throw new Error(
189+
`Release '${tag}' in '${actionRepository}' does not contain asset '${assetName}'`,
190+
);
191+
}
192+
193+
return {
194+
url: asset.url,
195+
headers: {
196+
accept: "application/octet-stream",
197+
authorization: `Bearer ${githubToken}`,
198+
"x-github-api-version": "2022-11-28",
199+
},
200+
};
160201
};
161202

162203
/**
163204
* @param {string} url
164205
* @param {string} destinationPath
206+
* @param {Record<string, string>} [headers]
165207
* @returns {Promise<void>}
166208
*/
167-
const downloadFile = async (url, destinationPath) => {
209+
const downloadFile = async (url, destinationPath, headers = {}) => {
168210
const response = await fetch(url, {
169211
redirect: "follow",
212+
headers,
170213
});
171214

172215
if (!response.ok) {
@@ -291,10 +334,10 @@ const main = async () => {
291334

292335
logActionSourceEnv();
293336

294-
const url = await releaseAssetUrl(assetName, githubToken);
337+
const { url, headers } = await releaseAssetRequest(assetName, githubToken);
295338

296339
core.info(`Downloading daemon '${assetName}' from '${url}'`);
297-
await downloadFile(url, binaryPath);
340+
await downloadFile(url, binaryPath, headers);
298341

299342
core.saveState(STATE_SOCKET_PATH, socketPath);
300343
core.saveState(STATE_BINARY_PATH, binaryPath);

0 commit comments

Comments
 (0)