From b4639928698a6bfe1c4bdae4b2bfdad1cb75016d Mon Sep 17 00:00:00 2001 From: Shubham Tiwari <64764738+tiwarishub@users.noreply.github.com> Date: Fri, 25 Mar 2022 14:40:02 +0530 Subject: [PATCH] Adding support in cache package to check if Artifact Cache service is enabled or not (#1028) * Added support to check if Artifact cache service is enabled or not. * enablingForGHES * added ACTIONS_CACHE_URL in fixtures * Fix CI * CI fix * changed function name * Function rename * Updated release * added test case * Update RELEASES.md * Lint errors * lint * linting * lint * update name to actions service * Update packages/cache/src/internal/cacheUtils.ts Co-authored-by: Brian Cristante <33549821+brcrista@users.noreply.github.com> * review comments * linting * linting * push to start CI * Update RELEASES.md * remove extra spaces * reverting version update * Revert "reverting version update" This reverts commit af84eba61ef8c119458dc0ce1da85b01d61b9d13. * Update RELEASES.md Co-authored-by: Brian Cristante <33549821+brcrista@users.noreply.github.com> --- packages/cache/RELEASES.md | 3 +++ packages/cache/__tests__/__fixtures__/index.js | 4 ++-- packages/cache/__tests__/cache.test.ts | 14 ++++++++++++++ packages/cache/package-lock.json | 4 ++-- packages/cache/package.json | 2 +- packages/cache/src/cache.ts | 10 ++++++++++ packages/cache/src/internal/cacheHttpClient.ts | 7 +------ 7 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 packages/cache/__tests__/cache.test.ts diff --git a/packages/cache/RELEASES.md b/packages/cache/RELEASES.md index a6fdec78..ec77c2bc 100644 --- a/packages/cache/RELEASES.md +++ b/packages/cache/RELEASES.md @@ -53,3 +53,6 @@ ### 1.0.11 - Fix file downloads > 2GB([issue](https://github.com/actions/cache/issues/773)) + +### 2.0.0 +- Added support to check if Actions cache service feature is available or not [#1028](https://github.com/actions/toolkit/pull/1028) diff --git a/packages/cache/__tests__/__fixtures__/index.js b/packages/cache/__tests__/__fixtures__/index.js index 2b21bf80..b8057c2a 100644 --- a/packages/cache/__tests__/__fixtures__/index.js +++ b/packages/cache/__tests__/__fixtures__/index.js @@ -3,10 +3,10 @@ const fs = require('fs'); const os = require('os'); const filePath = process.env[`GITHUB_ENV`] -fs.appendFileSync(filePath, `ACTIONS_RUNTIME_URL=${process.env.ACTIONS_RUNTIME_URL}${os.EOL}`, { +fs.appendFileSync(filePath, `ACTIONS_RUNTIME_TOKEN=${process.env.ACTIONS_RUNTIME_TOKEN}${os.EOL}`, { encoding: 'utf8' }) -fs.appendFileSync(filePath, `ACTIONS_RUNTIME_TOKEN=${process.env.ACTIONS_RUNTIME_TOKEN}${os.EOL}`, { +fs.appendFileSync(filePath, `ACTIONS_CACHE_URL=${process.env.ACTIONS_CACHE_URL}${os.EOL}`, { encoding: 'utf8' }) fs.appendFileSync(filePath, `GITHUB_RUN_ID=${process.env.GITHUB_RUN_ID}${os.EOL}`, { diff --git a/packages/cache/__tests__/cache.test.ts b/packages/cache/__tests__/cache.test.ts new file mode 100644 index 00000000..80375305 --- /dev/null +++ b/packages/cache/__tests__/cache.test.ts @@ -0,0 +1,14 @@ +import * as cache from '../src/cache' + +test('isFeatureAvailable returns true if server url is set', () => { + try { + process.env['ACTIONS_CACHE_URL'] = 'http://cache.com' + expect(cache.isFeatureAvailable()).toBe(true) + } finally { + delete process.env['ACTIONS_CACHE_URL'] + } +}) + +test('isFeatureAvailable returns false if server url is not set', () => { + expect(cache.isFeatureAvailable()).toBe(false) +}) diff --git a/packages/cache/package-lock.json b/packages/cache/package-lock.json index 2d7af851..48516b94 100644 --- a/packages/cache/package-lock.json +++ b/packages/cache/package-lock.json @@ -1,12 +1,12 @@ { "name": "@actions/cache", - "version": "1.0.11", + "version": "2.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@actions/cache", - "version": "1.0.11", + "version": "2.0.0", "license": "MIT", "dependencies": { "@actions/core": "^1.2.6", diff --git a/packages/cache/package.json b/packages/cache/package.json index 7b7c31a4..3cfbae1a 100644 --- a/packages/cache/package.json +++ b/packages/cache/package.json @@ -1,6 +1,6 @@ { "name": "@actions/cache", - "version": "1.0.11", + "version": "2.0.0", "preview": true, "description": "Actions cache lib", "keywords": [ diff --git a/packages/cache/src/cache.ts b/packages/cache/src/cache.ts index 540d639c..9356e838 100644 --- a/packages/cache/src/cache.ts +++ b/packages/cache/src/cache.ts @@ -43,6 +43,16 @@ function checkKey(key: string): void { } } +/** + * isFeatureAvailable to check the presence of Actions cache service + * + * @returns boolean return true if Actions cache service feature is available, otherwise false + */ + +export function isFeatureAvailable(): boolean { + return !!process.env['ACTIONS_CACHE_URL'] +} + /** * Restores cache from keys * diff --git a/packages/cache/src/internal/cacheHttpClient.ts b/packages/cache/src/internal/cacheHttpClient.ts index 6a46f736..65d7bd08 100644 --- a/packages/cache/src/internal/cacheHttpClient.ts +++ b/packages/cache/src/internal/cacheHttpClient.ts @@ -31,12 +31,7 @@ import { const versionSalt = '1.0' function getCacheApiUrl(resource: string): string { - // Ideally we just use ACTIONS_CACHE_URL - const baseUrl: string = ( - process.env['ACTIONS_CACHE_URL'] || - process.env['ACTIONS_RUNTIME_URL'] || - '' - ).replace('pipelines', 'artifactcache') + const baseUrl: string = process.env['ACTIONS_CACHE_URL'] || '' if (!baseUrl) { throw new Error('Cache Service Url not found, unable to restore cache.') }