mirror of
				https://github.com/actions/cache.git
				synced 2025-11-04 07:38:37 +08:00 
			
		
		
		
	Don’t accidentally deleting cache from base branch
Signed-off-by: Gregorio Litenstein <g.litenstein@gmail.com>
This commit is contained in:
		
							parent
							
								
									d35302b225
								
							
						
					
					
						commit
						1328a18e7e
					
				| 
						 | 
					@ -69,6 +69,7 @@ export async function saveImpl(
 | 
				
			||||||
            );
 | 
					            );
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if (utils.isExactKeyMatch(primaryKey, restoredKey)) {
 | 
					        if (utils.isExactKeyMatch(primaryKey, restoredKey)) {
 | 
				
			||||||
 | 
					            /* istanbul ignore next */
 | 
				
			||||||
            const { GITHUB_TOKEN, GITHUB_REPOSITORY } = process.env || null;
 | 
					            const { GITHUB_TOKEN, GITHUB_REPOSITORY } = process.env || null;
 | 
				
			||||||
            if (GITHUB_TOKEN && GITHUB_REPOSITORY && refreshCache === true) {
 | 
					            if (GITHUB_TOKEN && GITHUB_REPOSITORY && refreshCache === true) {
 | 
				
			||||||
                core.info(
 | 
					                core.info(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -28,32 +28,61 @@ export function isExactKeyMatch(key: string, cacheKey?: string): boolean {
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export async function deleteCacheByKey(key: string, owner: string, repo: string) {
 | 
					export function logWarning(message: string): void {
 | 
				
			||||||
 | 
					    const warningPrefix = "[warning]";
 | 
				
			||||||
 | 
					    core.info(`${warningPrefix}${message}`);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export async function deleteCacheByKey(key: string, owner: string, repo: string) : Promise <number | void> {
 | 
				
			||||||
    const octokit = new Octokit();
 | 
					    const octokit = new Octokit();
 | 
				
			||||||
    let response;
 | 
					    let response;
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
        response = await octokit.rest.actions.deleteActionsCacheByKey({
 | 
					        const gitRef = process.env[RefKey];
 | 
				
			||||||
 | 
					        let cacheEntry = await octokit.rest.actions.getActionsCacheList({
 | 
				
			||||||
            owner: owner,
 | 
					            owner: owner,
 | 
				
			||||||
            repo: repo,
 | 
					            repo: repo,
 | 
				
			||||||
            key: key
 | 
					            key: key,
 | 
				
			||||||
 | 
					            ref: gitRef
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
        if (response.status === 200) {
 | 
					        const { data: {
 | 
				
			||||||
            core.info(`Succesfully deleted cache with key: ${response.data.actions_caches[0].key}`);
 | 
					            total_count,
 | 
				
			||||||
 | 
					            actions_caches
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        } = cacheEntry;
 | 
				
			||||||
 | 
					        if (total_count !== 1 || total_count !== actions_caches.length) { // leave all find logic to the actual cache implementation. We just want to make sure we're returned a single element so we don't accidentally delete an entry that belongs to a different gitref.
 | 
				
			||||||
 | 
					            if (total_count > 1) {
 | 
				
			||||||
 | 
					                exports.logWarning(`More than one cache entry found for key ${key}`);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else if (total_count === 0 || actions_caches.length === 0) {
 | 
				
			||||||
 | 
					                exports.logWarning(`No cache entries for key ${key} belong to gitref ${gitRef}.`);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            // This situation is likely never actually going to come up.
 | 
				
			||||||
 | 
					            // Istanbul is being dumb and I can't ignore this path.
 | 
				
			||||||
 | 
					            else if (total_count !== actions_caches.length) {
 | 
				
			||||||
 | 
					                exports.logWarning(`Reported cache entry matches for ${key} does not match length of 'actions_caches' array in API response.`);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            core.info(`Skip trying to delete cache entry for key ${key}.`)
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        let id = actions_caches[0].id;
 | 
				
			||||||
 | 
					        response = await octokit.rest.actions.deleteActionsCacheById({
 | 
				
			||||||
 | 
					            owner: owner,
 | 
				
			||||||
 | 
					            repo: repo,
 | 
				
			||||||
 | 
					            cache_id: id
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					        if (response.status === 204) {
 | 
				
			||||||
 | 
					            core.info(`Succesfully deleted cache with key: ${key}, id: ${id}`);
 | 
				
			||||||
 | 
					             return 204;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    } catch (e) {
 | 
					    } catch (e) {
 | 
				
			||||||
        if (e instanceof RequestError) {
 | 
					        if (e instanceof RequestError) {
 | 
				
			||||||
            let err = e as RequestError;
 | 
					            let err = e as RequestError;
 | 
				
			||||||
            let errData = err.response?.data as any | undefined;
 | 
					            let errData = err.response?.data as any | undefined;
 | 
				
			||||||
            exports.logWarning(`${err.name} '${err.status}: ${errData?.message}' trying to delete cache with key: ${key}`);
 | 
					            exports.logWarning(`Github API reported error: ${err.name} '${err.status}: ${errData?.message}'`);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        response = e;
 | 
					        core.info(`Couldn't delete cache entry for key ${key}.`)
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return response;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export function logWarning(message: string): void {
 | 
					 | 
				
			||||||
    const warningPrefix = "[warning]";
 | 
					 | 
				
			||||||
    core.info(`${warningPrefix}${message}`);
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Cache token authorized for all events that are tied to a ref
 | 
					// Cache token authorized for all events that are tied to a ref
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user