mirror of https://github.com/actions/toolkit
New Response Type
parent
b602df7c05
commit
79acd5bac4
|
@ -6,7 +6,7 @@ import * as cacheUtils from '../src/internal/cacheUtils'
|
||||||
import {CacheFilename, CompressionMethod} from '../src/internal/constants'
|
import {CacheFilename, CompressionMethod} from '../src/internal/constants'
|
||||||
import * as tar from '../src/internal/tar'
|
import * as tar from '../src/internal/tar'
|
||||||
import {ITypedResponse} from '@actions/http-client/interfaces'
|
import {ITypedResponse} from '@actions/http-client/interfaces'
|
||||||
import {ReserveCacheResponse} from '../src/internal/contracts'
|
import {ReserveCacheResponse, ITypedResponseWithErrorMessage} from '../src/internal/contracts'
|
||||||
|
|
||||||
jest.mock('../src/internal/cacheHttpClient')
|
jest.mock('../src/internal/cacheHttpClient')
|
||||||
jest.mock('../src/internal/cacheUtils')
|
jest.mock('../src/internal/cacheUtils')
|
||||||
|
@ -60,7 +60,7 @@ test('save with large cache outputs should fail', async () => {
|
||||||
const reserveCacheMock = jest
|
const reserveCacheMock = jest
|
||||||
.spyOn(cacheHttpClient, 'reserveCache')
|
.spyOn(cacheHttpClient, 'reserveCache')
|
||||||
.mockImplementation(async () => {
|
.mockImplementation(async () => {
|
||||||
let response: ITypedResponse<ReserveCacheResponse> = {statusCode:400, result: null, headers:{}}
|
let response: ITypedResponseWithErrorMessage<ReserveCacheResponse> = {statusCode:400, result: null, headers:{}, typeKey:"InvalidReserveCacheRequestException"}
|
||||||
return response
|
return response
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ test('save with reserve cache failure should fail', async () => {
|
||||||
const reserveCacheMock = jest
|
const reserveCacheMock = jest
|
||||||
.spyOn(cacheHttpClient, 'reserveCache')
|
.spyOn(cacheHttpClient, 'reserveCache')
|
||||||
.mockImplementation(async () => {
|
.mockImplementation(async () => {
|
||||||
let response: ITypedResponse<ReserveCacheResponse> = {statusCode:500, result: null, headers:{}}
|
let response: ITypedResponseWithErrorMessage<ReserveCacheResponse> = {statusCode:500, result: null, headers:{}}
|
||||||
return response
|
return response
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -182,8 +182,9 @@ export async function saveCache(
|
||||||
cacheSize
|
cacheSize
|
||||||
})
|
})
|
||||||
|
|
||||||
if(reserveCacheResponse?.statusCode === 400){
|
if(reserveCacheResponse?.statusCode === 400 && reserveCacheResponse?.typeKey === "InvalidReserveCacheRequestException"){
|
||||||
throw new ReserveCacheError(
|
throw new ReserveCacheError(
|
||||||
|
reserveCacheResponse?.message ??
|
||||||
`Cache size of ~${Math.round(archiveFileSize / (1024 * 1024))} MB (${archiveFileSize} B) is over the data cap limit, not saving cache.`
|
`Cache size of ~${Math.round(archiveFileSize / (1024 * 1024))} MB (${archiveFileSize} B) is over the data cap limit, not saving cache.`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,8 @@ import {
|
||||||
InternalCacheOptions,
|
InternalCacheOptions,
|
||||||
CommitCacheRequest,
|
CommitCacheRequest,
|
||||||
ReserveCacheRequest,
|
ReserveCacheRequest,
|
||||||
ReserveCacheResponse
|
ReserveCacheResponse,
|
||||||
|
ITypedResponseWithErrorMessage
|
||||||
} from './contracts'
|
} from './contracts'
|
||||||
import {downloadCacheHttpClient, downloadCacheStorageSDK} from './downloadUtils'
|
import {downloadCacheHttpClient, downloadCacheStorageSDK} from './downloadUtils'
|
||||||
import {
|
import {
|
||||||
|
@ -143,7 +144,7 @@ export async function reserveCache(
|
||||||
key: string,
|
key: string,
|
||||||
paths: string[],
|
paths: string[],
|
||||||
options?: InternalCacheOptions
|
options?: InternalCacheOptions
|
||||||
): Promise<ITypedResponse<ReserveCacheResponse>> {
|
): Promise<ITypedResponseWithErrorMessage<ReserveCacheResponse>> {
|
||||||
const httpClient = createHttpClient()
|
const httpClient = createHttpClient()
|
||||||
const version = getCacheVersion(paths, options?.compressionMethod)
|
const version = getCacheVersion(paths, options?.compressionMethod)
|
||||||
|
|
||||||
|
@ -158,6 +159,7 @@ export async function reserveCache(
|
||||||
reserveCacheRequest
|
reserveCacheRequest
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
console.log(response)
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,12 @@
|
||||||
import {CompressionMethod} from './constants'
|
import {CompressionMethod} from './constants'
|
||||||
|
import {
|
||||||
|
ITypedResponse
|
||||||
|
} from '@actions/http-client/interfaces'
|
||||||
|
|
||||||
|
export interface ITypedResponseWithErrorMessage<T> extends ITypedResponse<T> {
|
||||||
|
message?: string
|
||||||
|
typeKey?: string
|
||||||
|
}
|
||||||
|
|
||||||
export interface ArtifactCacheEntry {
|
export interface ArtifactCacheEntry {
|
||||||
cacheKey?: string
|
cacheKey?: string
|
||||||
|
@ -25,3 +33,7 @@ export interface InternalCacheOptions {
|
||||||
compressionMethod?: CompressionMethod
|
compressionMethod?: CompressionMethod
|
||||||
cacheSize?: number
|
cacheSize?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface CommitCacheRequest {
|
||||||
|
size: number
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import {
|
||||||
ITypedResponse
|
ITypedResponse
|
||||||
} from '@actions/http-client/interfaces'
|
} from '@actions/http-client/interfaces'
|
||||||
import {DefaultRetryDelay, DefaultRetryAttempts} from './constants'
|
import {DefaultRetryDelay, DefaultRetryAttempts} from './constants'
|
||||||
|
import {ITypedResponseWithErrorMessage} from './contracts'
|
||||||
|
|
||||||
export function isSuccessStatusCode(statusCode?: number): boolean {
|
export function isSuccessStatusCode(statusCode?: number): boolean {
|
||||||
if (!statusCode) {
|
if (!statusCode) {
|
||||||
|
@ -94,14 +95,14 @@ export async function retry<T>(
|
||||||
|
|
||||||
export async function retryTypedResponse<T>(
|
export async function retryTypedResponse<T>(
|
||||||
name: string,
|
name: string,
|
||||||
method: () => Promise<ITypedResponse<T>>,
|
method: () => Promise<ITypedResponseWithErrorMessage<T>>,
|
||||||
maxAttempts = DefaultRetryAttempts,
|
maxAttempts = DefaultRetryAttempts,
|
||||||
delay = DefaultRetryDelay
|
delay = DefaultRetryDelay
|
||||||
): Promise<ITypedResponse<T>> {
|
): Promise<ITypedResponseWithErrorMessage<T>> {
|
||||||
return await retry(
|
return await retry(
|
||||||
name,
|
name,
|
||||||
method,
|
method,
|
||||||
(response: ITypedResponse<T>) => response.statusCode,
|
(response: ITypedResponseWithErrorMessage<T>) => response.statusCode,
|
||||||
maxAttempts,
|
maxAttempts,
|
||||||
delay,
|
delay,
|
||||||
// If the error object contains the statusCode property, extract it and return
|
// If the error object contains the statusCode property, extract it and return
|
||||||
|
@ -111,7 +112,9 @@ export async function retryTypedResponse<T>(
|
||||||
return {
|
return {
|
||||||
statusCode: error.statusCode,
|
statusCode: error.statusCode,
|
||||||
result: null,
|
result: null,
|
||||||
headers: {}
|
headers: {},
|
||||||
|
message:error.message,
|
||||||
|
typeKey:error.result.typeKey
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return undefined
|
return undefined
|
||||||
|
|
Loading…
Reference in New Issue