mirror of
https://git.mirrors.martin98.com/https://github.com/actions/toolkit
synced 2026-01-03 23:12:55 +08:00
Add isGhes gate and refactor to clean up circular dependencies
This commit is contained in:
parent
ab58a59f33
commit
267841d7bd
22
packages/cache/__tests__/cacheUtils.test.ts
vendored
22
packages/cache/__tests__/cacheUtils.test.ts
vendored
@ -1,4 +1,4 @@
|
|||||||
import {promises as fs} from 'fs'
|
import { promises as fs } from 'fs'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
import * as cacheUtils from '../src/internal/cacheUtils'
|
import * as cacheUtils from '../src/internal/cacheUtils'
|
||||||
|
|
||||||
@ -42,23 +42,3 @@ test('resolvePaths works on github workspace directory', async () => {
|
|||||||
const paths = await cacheUtils.resolvePaths([workspace])
|
const paths = await cacheUtils.resolvePaths([workspace])
|
||||||
expect(paths.length).toBeGreaterThan(0)
|
expect(paths.length).toBeGreaterThan(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('isGhes returns false for github.com', async () => {
|
|
||||||
process.env.GITHUB_SERVER_URL = 'https://github.com'
|
|
||||||
expect(cacheUtils.isGhes()).toBe(false)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('isGhes returns false for ghe.com', async () => {
|
|
||||||
process.env.GITHUB_SERVER_URL = 'https://somedomain.ghe.com'
|
|
||||||
expect(cacheUtils.isGhes()).toBe(false)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('isGhes returns true for enterprise URL', async () => {
|
|
||||||
process.env.GITHUB_SERVER_URL = 'https://my-enterprise.github.com'
|
|
||||||
expect(cacheUtils.isGhes()).toBe(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('isGhes returns false for ghe.localhost', () => {
|
|
||||||
process.env.GITHUB_SERVER_URL = 'https://my.domain.ghe.localhost'
|
|
||||||
expect(cacheUtils.isGhes()).toBe(false)
|
|
||||||
})
|
|
||||||
|
|||||||
26
packages/cache/__tests__/config.test.ts
vendored
Normal file
26
packages/cache/__tests__/config.test.ts
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import { promises as fs } from 'fs'
|
||||||
|
import * as config from '../src/internal/config'
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.resetModules()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('isGhes returns false for github.com', async () => {
|
||||||
|
process.env.GITHUB_SERVER_URL = 'https://github.com'
|
||||||
|
expect(config.isGhes()).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('isGhes returns false for ghe.com', async () => {
|
||||||
|
process.env.GITHUB_SERVER_URL = 'https://somedomain.ghe.com'
|
||||||
|
expect(config.isGhes()).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('isGhes returns true for enterprise URL', async () => {
|
||||||
|
process.env.GITHUB_SERVER_URL = 'https://my-enterprise.github.com'
|
||||||
|
expect(config.isGhes()).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('isGhes returns false for ghe.localhost', () => {
|
||||||
|
process.env.GITHUB_SERVER_URL = 'https://my.domain.ghe.localhost'
|
||||||
|
expect(config.isGhes()).toBe(false)
|
||||||
|
})
|
||||||
28
packages/cache/__tests__/saveCache.test.ts
vendored
28
packages/cache/__tests__/saveCache.test.ts
vendored
@ -1,27 +1,29 @@
|
|||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
import {saveCache} from '../src/cache'
|
import { saveCache } from '../src/cache'
|
||||||
import * as cacheHttpClient from '../src/internal/cacheHttpClient'
|
import * as cacheHttpClient from '../src/internal/cacheHttpClient'
|
||||||
import * as cacheUtils from '../src/internal/cacheUtils'
|
import * as cacheUtils from '../src/internal/cacheUtils'
|
||||||
import {CacheFilename, CompressionMethod} from '../src/internal/constants'
|
import * as config from '../src/internal/config'
|
||||||
|
import { CacheFilename, CompressionMethod } from '../src/internal/constants'
|
||||||
import * as tar from '../src/internal/tar'
|
import * as tar from '../src/internal/tar'
|
||||||
import {TypedResponse} from '@actions/http-client/lib/interfaces'
|
import { TypedResponse } from '@actions/http-client/lib/interfaces'
|
||||||
import {
|
import {
|
||||||
ReserveCacheResponse,
|
ReserveCacheResponse,
|
||||||
ITypedResponseWithError
|
ITypedResponseWithError
|
||||||
} from '../src/internal/contracts'
|
} from '../src/internal/contracts'
|
||||||
import {HttpClientError} from '@actions/http-client'
|
import { HttpClientError } from '@actions/http-client'
|
||||||
|
|
||||||
jest.mock('../src/internal/cacheHttpClient')
|
jest.mock('../src/internal/cacheHttpClient')
|
||||||
jest.mock('../src/internal/cacheUtils')
|
jest.mock('../src/internal/cacheUtils')
|
||||||
|
jest.mock('../src/internal/config')
|
||||||
jest.mock('../src/internal/tar')
|
jest.mock('../src/internal/tar')
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
jest.spyOn(console, 'log').mockImplementation(() => {})
|
jest.spyOn(console, 'log').mockImplementation(() => { })
|
||||||
jest.spyOn(core, 'debug').mockImplementation(() => {})
|
jest.spyOn(core, 'debug').mockImplementation(() => { })
|
||||||
jest.spyOn(core, 'info').mockImplementation(() => {})
|
jest.spyOn(core, 'info').mockImplementation(() => { })
|
||||||
jest.spyOn(core, 'warning').mockImplementation(() => {})
|
jest.spyOn(core, 'warning').mockImplementation(() => { })
|
||||||
jest.spyOn(core, 'error').mockImplementation(() => {})
|
jest.spyOn(core, 'error').mockImplementation(() => { })
|
||||||
jest.spyOn(cacheUtils, 'getCacheFileName').mockImplementation(cm => {
|
jest.spyOn(cacheUtils, 'getCacheFileName').mockImplementation(cm => {
|
||||||
const actualUtils = jest.requireActual('../src/internal/cacheUtils')
|
const actualUtils = jest.requireActual('../src/internal/cacheUtils')
|
||||||
return actualUtils.getCacheFileName(cm)
|
return actualUtils.getCacheFileName(cm)
|
||||||
@ -94,7 +96,7 @@ test('save with large cache outputs should fail in GHES with error message', asy
|
|||||||
.spyOn(cacheUtils, 'getCompressionMethod')
|
.spyOn(cacheUtils, 'getCompressionMethod')
|
||||||
.mockReturnValueOnce(Promise.resolve(compression))
|
.mockReturnValueOnce(Promise.resolve(compression))
|
||||||
|
|
||||||
jest.spyOn(cacheUtils, 'isGhes').mockReturnValueOnce(true)
|
jest.spyOn(config, 'isGhes').mockReturnValueOnce(true)
|
||||||
|
|
||||||
const reserveCacheMock = jest
|
const reserveCacheMock = jest
|
||||||
.spyOn(cacheHttpClient, 'reserveCache')
|
.spyOn(cacheHttpClient, 'reserveCache')
|
||||||
@ -146,7 +148,7 @@ test('save with large cache outputs should fail in GHES without error message',
|
|||||||
.spyOn(cacheUtils, 'getCompressionMethod')
|
.spyOn(cacheUtils, 'getCompressionMethod')
|
||||||
.mockReturnValueOnce(Promise.resolve(compression))
|
.mockReturnValueOnce(Promise.resolve(compression))
|
||||||
|
|
||||||
jest.spyOn(cacheUtils, 'isGhes').mockReturnValueOnce(true)
|
jest.spyOn(config, 'isGhes').mockReturnValueOnce(true)
|
||||||
|
|
||||||
const reserveCacheMock = jest
|
const reserveCacheMock = jest
|
||||||
.spyOn(cacheHttpClient, 'reserveCache')
|
.spyOn(cacheHttpClient, 'reserveCache')
|
||||||
@ -229,7 +231,7 @@ test('save with server error should fail', async () => {
|
|||||||
.mockImplementation(async () => {
|
.mockImplementation(async () => {
|
||||||
const response: TypedResponse<ReserveCacheResponse> = {
|
const response: TypedResponse<ReserveCacheResponse> = {
|
||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
result: {cacheId},
|
result: { cacheId },
|
||||||
headers: {}
|
headers: {}
|
||||||
}
|
}
|
||||||
return response
|
return response
|
||||||
@ -283,7 +285,7 @@ test('save with valid inputs uploads a cache', async () => {
|
|||||||
.mockImplementation(async () => {
|
.mockImplementation(async () => {
|
||||||
const response: TypedResponse<ReserveCacheResponse> = {
|
const response: TypedResponse<ReserveCacheResponse> = {
|
||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
result: {cacheId},
|
result: { cacheId },
|
||||||
headers: {}
|
headers: {}
|
||||||
}
|
}
|
||||||
return response
|
return response
|
||||||
|
|||||||
24
packages/cache/src/cache.ts
vendored
24
packages/cache/src/cache.ts
vendored
@ -1,20 +1,20 @@
|
|||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
import * as config from './internal/config'
|
|
||||||
import * as utils from './internal/cacheUtils'
|
import * as utils from './internal/cacheUtils'
|
||||||
import * as cacheHttpClient from './internal/cacheHttpClient'
|
import * as cacheHttpClient from './internal/cacheHttpClient'
|
||||||
import * as cacheTwirpClient from './internal/shared/cacheTwirpClient'
|
import * as cacheTwirpClient from './internal/shared/cacheTwirpClient'
|
||||||
import {DownloadOptions, UploadOptions} from './options'
|
import { getCacheServiceVersion, isGhes } from './internal/config'
|
||||||
import {createTar, extractTar, listTar} from './internal/tar'
|
import { DownloadOptions, UploadOptions } from './options'
|
||||||
|
import { createTar, extractTar, listTar } from './internal/tar'
|
||||||
import {
|
import {
|
||||||
CreateCacheEntryRequest,
|
CreateCacheEntryRequest,
|
||||||
FinalizeCacheEntryUploadRequest,
|
FinalizeCacheEntryUploadRequest,
|
||||||
FinalizeCacheEntryUploadResponse,
|
FinalizeCacheEntryUploadResponse,
|
||||||
GetCacheEntryDownloadURLRequest
|
GetCacheEntryDownloadURLRequest
|
||||||
} from './generated/results/api/v1/cache'
|
} from './generated/results/api/v1/cache'
|
||||||
import {CacheFileSizeLimit} from './internal/constants'
|
import { CacheFileSizeLimit } from './internal/constants'
|
||||||
import {UploadCacheFile} from './internal/blob/upload-cache'
|
import { uploadCacheFile } from './internal/blob/upload-cache'
|
||||||
import {DownloadCacheFile} from './internal/blob/download-cache'
|
import { downloadCacheFile } from './internal/blob/download-cache'
|
||||||
export class ValidationError extends Error {
|
export class ValidationError extends Error {
|
||||||
constructor(message: string) {
|
constructor(message: string) {
|
||||||
super(message)
|
super(message)
|
||||||
@ -81,7 +81,7 @@ export async function restoreCache(
|
|||||||
): Promise<string | undefined> {
|
): Promise<string | undefined> {
|
||||||
checkPaths(paths)
|
checkPaths(paths)
|
||||||
|
|
||||||
const cacheServiceVersion: string = config.getCacheServiceVersion()
|
const cacheServiceVersion: string = getCacheServiceVersion()
|
||||||
switch (cacheServiceVersion) {
|
switch (cacheServiceVersion) {
|
||||||
case 'v2':
|
case 'v2':
|
||||||
return await restoreCacheV2(
|
return await restoreCacheV2(
|
||||||
@ -269,7 +269,7 @@ async function restoreCacheV2(
|
|||||||
core.debug(`Archive path: ${archivePath}`)
|
core.debug(`Archive path: ${archivePath}`)
|
||||||
core.debug(`Starting download of archive to: ${archivePath}`)
|
core.debug(`Starting download of archive to: ${archivePath}`)
|
||||||
|
|
||||||
await DownloadCacheFile(response.signedDownloadUrl, archivePath)
|
await downloadCacheFile(response.signedDownloadUrl, archivePath)
|
||||||
|
|
||||||
const archiveFileSize = utils.getArchiveFileSizeInBytes(archivePath)
|
const archiveFileSize = utils.getArchiveFileSizeInBytes(archivePath)
|
||||||
core.info(
|
core.info(
|
||||||
@ -317,7 +317,7 @@ export async function saveCache(
|
|||||||
checkPaths(paths)
|
checkPaths(paths)
|
||||||
checkKey(key)
|
checkKey(key)
|
||||||
|
|
||||||
const cacheServiceVersion: string = config.getCacheServiceVersion()
|
const cacheServiceVersion: string = getCacheServiceVersion()
|
||||||
switch (cacheServiceVersion) {
|
switch (cacheServiceVersion) {
|
||||||
case 'v2':
|
case 'v2':
|
||||||
return await saveCacheV2(paths, key, options, enableCrossOsArchive)
|
return await saveCacheV2(paths, key, options, enableCrossOsArchive)
|
||||||
@ -373,7 +373,7 @@ async function saveCacheV1(
|
|||||||
core.debug(`File Size: ${archiveFileSize}`)
|
core.debug(`File Size: ${archiveFileSize}`)
|
||||||
|
|
||||||
// For GHES, this check will take place in ReserveCache API with enterprise file size limit
|
// For GHES, this check will take place in ReserveCache API with enterprise file size limit
|
||||||
if (archiveFileSize > fileSizeLimit && !utils.isGhes()) {
|
if (archiveFileSize > fileSizeLimit && !isGhes()) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Cache size of ~${Math.round(
|
`Cache size of ~${Math.round(
|
||||||
archiveFileSize / (1024 * 1024)
|
archiveFileSize / (1024 * 1024)
|
||||||
@ -477,7 +477,7 @@ async function saveCacheV2(
|
|||||||
core.debug(`File Size: ${archiveFileSize}`)
|
core.debug(`File Size: ${archiveFileSize}`)
|
||||||
|
|
||||||
// For GHES, this check will take place in ReserveCache API with enterprise file size limit
|
// For GHES, this check will take place in ReserveCache API with enterprise file size limit
|
||||||
if (archiveFileSize > CacheFileSizeLimit && !utils.isGhes()) {
|
if (archiveFileSize > CacheFileSizeLimit && !isGhes()) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Cache size of ~${Math.round(
|
`Cache size of ~${Math.round(
|
||||||
archiveFileSize / (1024 * 1024)
|
archiveFileSize / (1024 * 1024)
|
||||||
@ -504,7 +504,7 @@ async function saveCacheV2(
|
|||||||
}
|
}
|
||||||
|
|
||||||
core.debug(`Attempting to upload cache located at: ${archivePath}`)
|
core.debug(`Attempting to upload cache located at: ${archivePath}`)
|
||||||
await UploadCacheFile(response.signedUploadUrl, archivePath)
|
await uploadCacheFile(response.signedUploadUrl, archivePath)
|
||||||
|
|
||||||
const finalizeRequest: FinalizeCacheEntryUploadRequest = {
|
const finalizeRequest: FinalizeCacheEntryUploadRequest = {
|
||||||
key,
|
key,
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import {
|
|||||||
BlobDownloadOptions
|
BlobDownloadOptions
|
||||||
} from '@azure/storage-blob'
|
} from '@azure/storage-blob'
|
||||||
|
|
||||||
export async function DownloadCacheFile(
|
export async function downloadCacheFile(
|
||||||
signedUploadURL: string,
|
signedUploadURL: string,
|
||||||
archivePath: string
|
archivePath: string
|
||||||
): Promise<{}> {
|
): Promise<{}> {
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import {
|
|||||||
BlockBlobParallelUploadOptions
|
BlockBlobParallelUploadOptions
|
||||||
} from '@azure/storage-blob'
|
} from '@azure/storage-blob'
|
||||||
|
|
||||||
export async function UploadCacheFile(
|
export async function uploadCacheFile(
|
||||||
signedUploadURL: string,
|
signedUploadURL: string,
|
||||||
archivePath: string
|
archivePath: string
|
||||||
): Promise<{}> {
|
): Promise<{}> {
|
||||||
|
|||||||
13
packages/cache/src/internal/cacheUtils.ts
vendored
13
packages/cache/src/internal/cacheUtils.ts
vendored
@ -133,19 +133,6 @@ export function assertDefined<T>(name: string, value?: T): T {
|
|||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isGhes(): boolean {
|
|
||||||
const ghUrl = new URL(
|
|
||||||
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
|
|
||||||
)
|
|
||||||
|
|
||||||
const hostname = ghUrl.hostname.trimEnd().toUpperCase()
|
|
||||||
const isGitHubHost = hostname === 'GITHUB.COM'
|
|
||||||
const isGheHost =
|
|
||||||
hostname.endsWith('.GHE.COM') || hostname.endsWith('.GHE.LOCALHOST')
|
|
||||||
|
|
||||||
return !isGitHubHost && !isGheHost
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getCacheVersion(
|
export function getCacheVersion(
|
||||||
paths: string[],
|
paths: string[],
|
||||||
compressionMethod?: CompressionMethod,
|
compressionMethod?: CompressionMethod,
|
||||||
|
|||||||
20
packages/cache/src/internal/config.ts
vendored
20
packages/cache/src/internal/config.ts
vendored
@ -1,9 +1,29 @@
|
|||||||
|
export function isGhes(): boolean {
|
||||||
|
const ghUrl = new URL(
|
||||||
|
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
|
||||||
|
)
|
||||||
|
|
||||||
|
const hostname = ghUrl.hostname.trimEnd().toUpperCase()
|
||||||
|
const isGitHubHost = hostname === 'GITHUB.COM'
|
||||||
|
const isGheHost = hostname.endsWith('.GHE.COM')
|
||||||
|
const isLocalHost = hostname.endsWith('.LOCALHOST')
|
||||||
|
|
||||||
|
return !isGitHubHost && !isGheHost && !isLocalHost
|
||||||
|
}
|
||||||
|
|
||||||
export function getCacheServiceVersion(): string {
|
export function getCacheServiceVersion(): string {
|
||||||
|
// Cache service v2 is not supported on GHES. We will default to
|
||||||
|
// cache service v1 even if the feature flag was enabled by user.
|
||||||
|
if (isGhes()) return 'v1'
|
||||||
|
|
||||||
return process.env['ACTIONS_CACHE_SERVICE_V2'] ? 'v2' : 'v1'
|
return process.env['ACTIONS_CACHE_SERVICE_V2'] ? 'v2' : 'v1'
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCacheServiceURL(): string {
|
export function getCacheServiceURL(): string {
|
||||||
const version = getCacheServiceVersion()
|
const version = getCacheServiceVersion()
|
||||||
|
|
||||||
|
// Based on the version of the cache service, we will determine which
|
||||||
|
// URL to use.
|
||||||
switch (version) {
|
switch (version) {
|
||||||
case 'v1':
|
case 'v1':
|
||||||
return (
|
return (
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user