1
0
Fork 0

Implement cache deletion

pull/1783/head
PrinsFrank 2024-08-02 20:56:05 +02:00
parent bb6c500939
commit 490fdc61c7
4 changed files with 107 additions and 1 deletions

View File

@ -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 { function checkPaths(paths: string[]): void {
if (!paths || paths.length === 0) { if (!paths || paths.length === 0) {
throw new ValidationError( throw new ValidationError(
@ -258,3 +266,44 @@ export async function saveCache(
return cacheId 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
}

View File

@ -18,7 +18,8 @@ import {
ReserveCacheRequest, ReserveCacheRequest,
ReserveCacheResponse, ReserveCacheResponse,
ITypedResponseWithError, ITypedResponseWithError,
ArtifactCacheList ArtifactCacheList,
DeleteCacheResponse
} from './contracts' } from './contracts'
import { import {
downloadCacheHttpClient, downloadCacheHttpClient,
@ -227,6 +228,37 @@ export async function reserveCache(
return response 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 { function getContentRange(start: number, end: number): string {
// Format: `bytes start-end/filesize // Format: `bytes start-end/filesize
// start and end are inclusive // start and end are inclusive

View File

@ -33,6 +33,10 @@ export interface ReserveCacheResponse {
cacheId: number cacheId: number
} }
export interface DeleteCacheResponse {
totalCount: number
}
export interface InternalCacheOptions { export interface InternalCacheOptions {
compressionMethod?: CompressionMethod compressionMethod?: CompressionMethod
enableCrossOsArchive?: boolean enableCrossOsArchive?: boolean

View File

@ -336,6 +336,27 @@ export class HttpClient {
return this._processResponse<T>(res, this.requestOptions) 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. * Makes a raw http request.
* All other methods such as get, post, patch, and request ultimately call this. * All other methods such as get, post, patch, and request ultimately call this.