mirror of https://github.com/actions/toolkit
Implement cache deletion
parent
bb6c500939
commit
490fdc61c7
|
@ -21,6 +21,14 @@ export class ReserveCacheError extends Error {
|
|||
}
|
||||
}
|
||||
|
||||
export class DeleteCacheError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message)
|
||||
this.name = 'DeleteCacheError'
|
||||
Object.setPrototypeOf(this, DeleteCacheError.prototype)
|
||||
}
|
||||
}
|
||||
|
||||
function checkPaths(paths: string[]): void {
|
||||
if (!paths || paths.length === 0) {
|
||||
throw new ValidationError(
|
||||
|
@ -258,3 +266,44 @@ export async function saveCache(
|
|||
|
||||
return cacheId
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a cache item with the specified key
|
||||
*
|
||||
* @param key an explicit key for the cache to delete
|
||||
* @param ref only delete items with key for this reference The ref for a branch should be formatted as
|
||||
* refs/heads/<branch name>. To reference a pull request use refs/pull/<number>/merge.
|
||||
*/
|
||||
export async function deleteCacheWithKey(
|
||||
key: string,
|
||||
ref?: string
|
||||
): Promise<number> {
|
||||
checkKey(key)
|
||||
|
||||
core.debug(`Deleting Cache with key "${key}" and ref "${ref}"`)
|
||||
const deleteCacheResponse = await cacheHttpClient.deleteCacheWithKey(key, ref)
|
||||
if (deleteCacheResponse?.statusCode !== 200) {
|
||||
throw new DeleteCacheError(
|
||||
`Unable to delete cache with key ${key}. More details: ${deleteCacheResponse?.error?.message}`
|
||||
)
|
||||
}
|
||||
|
||||
return deleteCacheResponse?.result?.totalCount
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a cache item with the specified id
|
||||
*
|
||||
* @param id an explicit id for the cache to delete
|
||||
*/
|
||||
export async function deleteCacheWithId(id: number): Promise<number> {
|
||||
core.debug(`Deleting Cache with id ${id.toString()}`)
|
||||
const deleteCacheResponse = await cacheHttpClient.deleteCacheWithId(id)
|
||||
if (deleteCacheResponse?.statusCode !== 204) {
|
||||
throw new DeleteCacheError(
|
||||
`Unable to delete cache with id ${id}. More details: ${deleteCacheResponse?.error?.message}`
|
||||
)
|
||||
}
|
||||
|
||||
return deleteCacheResponse?.result?.totalCount
|
||||
}
|
||||
|
|
|
@ -18,7 +18,8 @@ import {
|
|||
ReserveCacheRequest,
|
||||
ReserveCacheResponse,
|
||||
ITypedResponseWithError,
|
||||
ArtifactCacheList
|
||||
ArtifactCacheList,
|
||||
DeleteCacheResponse
|
||||
} from './contracts'
|
||||
import {
|
||||
downloadCacheHttpClient,
|
||||
|
@ -227,6 +228,37 @@ export async function reserveCache(
|
|||
return response
|
||||
}
|
||||
|
||||
export async function deleteCacheWithKey(
|
||||
key: string,
|
||||
ref?: string
|
||||
): Promise<ITypedResponseWithError<DeleteCacheResponse>> {
|
||||
const httpClient = createHttpClient()
|
||||
|
||||
const response = await retryTypedResponse('deleteCache', async () =>
|
||||
httpClient.deleteJson<DeleteCacheResponse>(
|
||||
ref != null
|
||||
? getCacheApiUrl(`caches?key=${key}&ref=${ref}`)
|
||||
: getCacheApiUrl(`caches?key=${key}`)
|
||||
)
|
||||
)
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
export async function deleteCacheWithId(
|
||||
id: number
|
||||
): Promise<ITypedResponseWithError<DeleteCacheResponse>> {
|
||||
const httpClient = createHttpClient()
|
||||
|
||||
const response = await retryTypedResponse('deleteCache', async () =>
|
||||
httpClient.deleteJson<DeleteCacheResponse>(
|
||||
getCacheApiUrl(`caches/${id.toString()}`)
|
||||
)
|
||||
)
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
function getContentRange(start: number, end: number): string {
|
||||
// Format: `bytes start-end/filesize
|
||||
// start and end are inclusive
|
||||
|
|
|
@ -33,6 +33,10 @@ export interface ReserveCacheResponse {
|
|||
cacheId: number
|
||||
}
|
||||
|
||||
export interface DeleteCacheResponse {
|
||||
totalCount: number
|
||||
}
|
||||
|
||||
export interface InternalCacheOptions {
|
||||
compressionMethod?: CompressionMethod
|
||||
enableCrossOsArchive?: boolean
|
||||
|
|
|
@ -336,6 +336,27 @@ export class HttpClient {
|
|||
return this._processResponse<T>(res, this.requestOptions)
|
||||
}
|
||||
|
||||
async deleteJson<T>(
|
||||
requestUrl: string,
|
||||
additionalHeaders: http.OutgoingHttpHeaders = {}
|
||||
): Promise<ifm.TypedResponse<T>> {
|
||||
additionalHeaders[Headers.Accept] = this._getExistingOrDefaultHeader(
|
||||
additionalHeaders,
|
||||
Headers.Accept,
|
||||
MediaTypes.ApplicationJson
|
||||
)
|
||||
additionalHeaders[Headers.ContentType] = this._getExistingOrDefaultHeader(
|
||||
additionalHeaders,
|
||||
Headers.ContentType,
|
||||
MediaTypes.ApplicationJson
|
||||
)
|
||||
const res: HttpClientResponse = await this.del(
|
||||
requestUrl,
|
||||
additionalHeaders
|
||||
)
|
||||
return this._processResponse<T>(res, this.requestOptions)
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes a raw http request.
|
||||
* All other methods such as get, post, patch, and request ultimately call this.
|
||||
|
|
Loading…
Reference in New Issue