Rename createHttpClient to createActionsCacheClient

This commit is contained in:
Sergey Dolin 2023-07-05 09:53:06 +02:00
parent a9c16cbf4d
commit 56c9f4224b
4 changed files with 50 additions and 48 deletions

41
dist/index.js vendored
View File

@ -68,7 +68,7 @@ const getCacheArchiveUrl = (httpClient, cacheKey, cacheVersion) => __awaiter(voi
return cacheDownloadUrl; return cacheDownloadUrl;
}); });
const downloadFileFromActionsCache = (destFileName, cacheKey, cacheVersion) => __awaiter(void 0, void 0, void 0, function* () { const downloadFileFromActionsCache = (destFileName, cacheKey, cacheVersion) => __awaiter(void 0, void 0, void 0, function* () {
const httpClient = (0, http_client_1.createHttpClient)(); const httpClient = (0, http_client_1.createActionsCacheClient)();
const archiveUrl = yield getCacheArchiveUrl(httpClient, cacheKey, cacheVersion); const archiveUrl = yield getCacheArchiveUrl(httpClient, cacheKey, cacheVersion);
if (!archiveUrl) { if (!archiveUrl) {
return undefined; return undefined;
@ -109,7 +109,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result; return result;
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getCacheApiUrl = exports.getGitHubActionsApiUrl = exports.createHttpClient = void 0; exports.getCacheApiUrl = exports.getGitHubActionsApiUrl = exports.createActionsCacheClient = void 0;
const http_client_1 = __nccwpck_require__(6255); const http_client_1 = __nccwpck_require__(6255);
const auth_1 = __nccwpck_require__(5526); const auth_1 = __nccwpck_require__(5526);
const core = __importStar(__nccwpck_require__(2186)); const core = __importStar(__nccwpck_require__(2186));
@ -119,12 +119,12 @@ const getRequestOptions = () => ({
Accept: createAcceptHeader('application/json', '6.0-preview.1') Accept: createAcceptHeader('application/json', '6.0-preview.1')
} }
}); });
const createHttpClient = () => { const createActionsCacheClient = () => {
const token = process.env['ACTIONS_RUNTIME_TOKEN'] || ''; const token = process.env['ACTIONS_RUNTIME_TOKEN'] || '';
const bearerCredentialHandler = new auth_1.BearerCredentialHandler(token); const bearerCredentialHandler = new auth_1.BearerCredentialHandler(token);
return new http_client_1.HttpClient('actions/cache', [bearerCredentialHandler], getRequestOptions()); return new http_client_1.HttpClient('actions/cache', [bearerCredentialHandler], getRequestOptions());
}; };
exports.createHttpClient = createHttpClient; exports.createActionsCacheClient = createActionsCacheClient;
const getGitHubActionsApiUrl = (resource) => { const getGitHubActionsApiUrl = (resource) => {
const baseUrl = process.env['GITHUB_API_URL'] || ''; const baseUrl = process.env['GITHUB_API_URL'] || '';
if (!baseUrl) { if (!baseUrl) {
@ -345,6 +345,7 @@ const retry_1 = __nccwpck_require__(3910);
const github_1 = __nccwpck_require__(5438); const github_1 = __nccwpck_require__(5438);
const plugin_retry_1 = __nccwpck_require__(6298); const plugin_retry_1 = __nccwpck_require__(6298);
const http_client_1 = __nccwpck_require__(8661); const http_client_1 = __nccwpck_require__(8661);
const uploadChunk = (httpClient) => __awaiter(void 0, void 0, void 0, function* () { });
const uploadFile = (httpClient, cacheId, filePath, fileSize) => __awaiter(void 0, void 0, void 0, function* () { const uploadFile = (httpClient, cacheId, filePath, fileSize) => __awaiter(void 0, void 0, void 0, function* () {
if (fileSize <= 0) if (fileSize <= 0)
return; return;
@ -358,19 +359,19 @@ const uploadFile = (httpClient, cacheId, filePath, fileSize) => __awaiter(void 0
}; };
const resourceUrl = (0, http_client_1.getCacheApiUrl)(`caches/${cacheId.toString()}`); const resourceUrl = (0, http_client_1.getCacheApiUrl)(`caches/${cacheId.toString()}`);
const fd = fs_1.default.openSync(filePath, 'r'); const fd = fs_1.default.openSync(filePath, 'r');
const openStream = () => fs_1.default
.createReadStream(filePath, {
fd,
start,
end,
autoClose: false
})
.on('error', error => {
throw new Error(`Cache upload failed because file read failed with ${error.message}`);
});
try { try {
const uploadChunkResponse = yield (0, retry_1.retryHttpClientResponse)(`uploadChunk (start: ${start}, end: ${end})`, () => __awaiter(void 0, void 0, void 0, function* () { const uploadChunkResponse = yield (0, retry_1.retryHttpClientResponse)(`uploadChunk (start: ${start}, end: ${end})`, () => __awaiter(void 0, void 0, void 0, function* () {
const stream = fs_1.default return httpClient.sendStream('PATCH', resourceUrl, openStream(), additionalHeaders);
.createReadStream(filePath, {
fd,
start,
end,
autoClose: false
})
.on('error', error => {
throw new Error(`Cache upload failed because file read failed with ${error.message}`);
});
return httpClient.sendStream('PATCH', resourceUrl, stream, additionalHeaders);
})); }));
if (!(0, http_responses_1.isSuccessStatusCode)(uploadChunkResponse.message.statusCode)) { if (!(0, http_responses_1.isSuccessStatusCode)(uploadChunkResponse.message.statusCode)) {
throw new Error(`Cache service responded with ${uploadChunkResponse.message.statusCode} during upload chunk.`); throw new Error(`Cache service responded with ${uploadChunkResponse.message.statusCode} during upload chunk.`);
@ -379,7 +380,6 @@ const uploadFile = (httpClient, cacheId, filePath, fileSize) => __awaiter(void 0
finally { finally {
fs_1.default.closeSync(fd); fs_1.default.closeSync(fd);
} }
return;
}); });
const resetCacheWithOctokit = (cacheKey) => __awaiter(void 0, void 0, void 0, function* () { const resetCacheWithOctokit = (cacheKey) => __awaiter(void 0, void 0, void 0, function* () {
const token = core.getInput('repo-token'); const token = core.getInput('repo-token');
@ -436,7 +436,7 @@ const uploadFileToActionsCache = (filePath, cacheKey, cacheVersion) => __awaiter
core.info(`the cache ${cacheKey} will be removed`); core.info(`the cache ${cacheKey} will be removed`);
return; return;
} }
const httpClient = (0, http_client_1.createHttpClient)(); const httpClient = (0, http_client_1.createActionsCacheClient)();
const cacheId = yield reserveCache(httpClient, fileSize, cacheKey, cacheVersion); const cacheId = yield reserveCache(httpClient, fileSize, cacheKey, cacheVersion);
yield uploadFile(httpClient, cacheId, filePath, fileSize); yield uploadFile(httpClient, cacheId, filePath, fileSize);
yield commitCache(httpClient, cacheId, fileSize); yield commitCache(httpClient, cacheId, fileSize);
@ -446,12 +446,11 @@ const uploadFileToActionsCache = (filePath, cacheKey, cacheVersion) => __awaiter
if (typedError.name === cache_1.ValidationError.name) { if (typedError.name === cache_1.ValidationError.name) {
throw error; throw error;
} }
else if (typedError.name === cache_1.ReserveCacheError.name) { if (typedError.name === cache_1.ReserveCacheError.name) {
core.info(`Failed to save: ${typedError.message}`); core.info(`Failed to save: ${typedError.message}`);
return;
} }
else { core.warning(`Failed to save: ${typedError.message}`);
core.warning(`Failed to save: ${typedError.message}`);
}
} }
}); });
exports.uploadFileToActionsCache = uploadFileToActionsCache; exports.uploadFileToActionsCache = uploadFileToActionsCache;

View File

@ -1,4 +1,4 @@
import {createHttpClient, getCacheApiUrl} from './http-client'; import {createActionsCacheClient, getCacheApiUrl} from './http-client';
import {retryTypedResponse} from './retry'; import {retryTypedResponse} from './retry';
import {isSuccessStatusCode} from './http-responses'; import {isSuccessStatusCode} from './http-responses';
import {HttpClient} from '@actions/http-client'; import {HttpClient} from '@actions/http-client';
@ -50,7 +50,7 @@ export const downloadFileFromActionsCache = async (
cacheKey: string, cacheKey: string,
cacheVersion: string cacheVersion: string
) => { ) => {
const httpClient = createHttpClient(); const httpClient = createActionsCacheClient();
const archiveUrl = await getCacheArchiveUrl( const archiveUrl = await getCacheArchiveUrl(
httpClient, httpClient,
cacheKey, cacheKey,

View File

@ -11,7 +11,7 @@ const getRequestOptions = (): RequestOptions => ({
} }
}); });
export const createHttpClient = (): HttpClient => { export const createActionsCacheClient = (): HttpClient => {
const token = process.env['ACTIONS_RUNTIME_TOKEN'] || ''; const token = process.env['ACTIONS_RUNTIME_TOKEN'] || '';
const bearerCredentialHandler = new BearerCredentialHandler(token); const bearerCredentialHandler = new BearerCredentialHandler(token);

View File

@ -7,7 +7,9 @@ import {isSuccessStatusCode} from './http-responses';
import {retryHttpClientResponse, retryTypedResponse} from './retry'; import {retryHttpClientResponse, retryTypedResponse} from './retry';
import {getOctokit} from '@actions/github'; import {getOctokit} from '@actions/github';
import {retry as octokitRetry} from '@octokit/plugin-retry'; import {retry as octokitRetry} from '@octokit/plugin-retry';
import {createHttpClient, getCacheApiUrl} from './http-client'; import {createActionsCacheClient, getCacheApiUrl} from './http-client';
const uploadChunk = async (httpClient: HttpClient): Promise<void> => {};
const uploadFile = async ( const uploadFile = async (
httpClient: HttpClient, httpClient: HttpClient,
@ -32,29 +34,30 @@ const uploadFile = async (
const resourceUrl = getCacheApiUrl(`caches/${cacheId.toString()}`); const resourceUrl = getCacheApiUrl(`caches/${cacheId.toString()}`);
const fd = fs.openSync(filePath, 'r'); const fd = fs.openSync(filePath, 'r');
const openStream = () =>
fs
.createReadStream(filePath, {
fd,
start,
end,
autoClose: false
})
.on('error', error => {
throw new Error(
`Cache upload failed because file read failed with ${error.message}`
);
});
try { try {
const uploadChunkResponse = await retryHttpClientResponse( const uploadChunkResponse = await retryHttpClientResponse(
`uploadChunk (start: ${start}, end: ${end})`, `uploadChunk (start: ${start}, end: ${end})`,
async () => { async () =>
const stream = fs httpClient.sendStream(
.createReadStream(filePath, {
fd,
start,
end,
autoClose: false
})
.on('error', error => {
throw new Error(
`Cache upload failed because file read failed with ${error.message}`
);
});
return httpClient.sendStream(
'PATCH', 'PATCH',
resourceUrl, resourceUrl,
stream, openStream(),
additionalHeaders additionalHeaders
); )
}
); );
if (!isSuccessStatusCode(uploadChunkResponse.message.statusCode)) { if (!isSuccessStatusCode(uploadChunkResponse.message.statusCode)) {
@ -65,7 +68,6 @@ const uploadFile = async (
} finally { } finally {
fs.closeSync(fd); fs.closeSync(fd);
} }
return;
}; };
const resetCacheWithOctokit = async (cacheKey: string): Promise<void> => { const resetCacheWithOctokit = async (cacheKey: string): Promise<void> => {
@ -155,7 +157,7 @@ export const uploadFileToActionsCache = async (
return; return;
} }
const httpClient = createHttpClient(); const httpClient = createActionsCacheClient();
const cacheId = await reserveCache( const cacheId = await reserveCache(
httpClient, httpClient,
@ -171,10 +173,11 @@ export const uploadFileToActionsCache = async (
const typedError = error as Error; const typedError = error as Error;
if (typedError.name === ValidationError.name) { if (typedError.name === ValidationError.name) {
throw error; throw error;
} else if (typedError.name === ReserveCacheError.name) {
core.info(`Failed to save: ${typedError.message}`);
} else {
core.warning(`Failed to save: ${typedError.message}`);
} }
if (typedError.name === ReserveCacheError.name) {
core.info(`Failed to save: ${typedError.message}`);
return;
}
core.warning(`Failed to save: ${typedError.message}`);
} }
}; };