mirror of
https://github.com/game-ci/unity-builder.git
synced 2026-05-31 22:06:16 -07:00
Compare commits
7 Commits
feature/or
...
feature/pr
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dcb0894d0e | ||
|
|
90b9b0c7b0 | ||
|
|
e9c247f04f | ||
|
|
cff759721a | ||
|
|
7e9d0bf53e | ||
|
|
8194790728 | ||
|
|
786ee3799c |
9
.github/workflows/orchestrator-integrity.yml
vendored
9
.github/workflows/orchestrator-integrity.yml
vendored
@@ -196,15 +196,6 @@ jobs:
|
||||
fi
|
||||
- run: yarn install --frozen-lockfile
|
||||
# ==========================================
|
||||
# FAST UNIT TESTS (no infra required, fast-fail gate)
|
||||
# ==========================================
|
||||
- name: Run orchestrator unit tests (fast, no infra)
|
||||
timeout-minutes: 2
|
||||
run: >-
|
||||
yarn run test
|
||||
--testPathPattern="orchestrator-guid|orchestrator-folders|task-parameter-serializer|follow-log-stream-service|runner-availability-service|provider-url-parser|provider-loader|provider-git-manager|orchestrator-image|orchestrator-hooks|orchestrator-github-checks"
|
||||
--verbose --detectOpenHandles --forceExit --runInBand
|
||||
# ==========================================
|
||||
# K8S TESTS SECTION
|
||||
# ==========================================
|
||||
- name: Clean up disk space before K8s tests
|
||||
|
||||
36
action.yml
36
action.yml
@@ -194,6 +194,42 @@ inputs:
|
||||
description:
|
||||
'[Orchestrator] Either local, k8s or aws can be used to run builds on a remote cluster. Additional parameters must
|
||||
be configured.'
|
||||
fallbackProviderStrategy:
|
||||
default: ''
|
||||
required: false
|
||||
description:
|
||||
'[Orchestrator] Fallback provider when the primary is unavailable. Used with runnerCheckEnabled for automatic
|
||||
failover, or as a catch-all if the primary provider fails to initialize.'
|
||||
runnerCheckEnabled:
|
||||
default: 'false'
|
||||
required: false
|
||||
description:
|
||||
'[Orchestrator] Check GitHub Actions runner availability before starting a build. When no suitable runners are
|
||||
available and fallbackProviderStrategy is set, automatically routes to the fallback provider.'
|
||||
runnerCheckLabels:
|
||||
default: ''
|
||||
required: false
|
||||
description:
|
||||
'[Orchestrator] Comma-separated runner labels to filter when checking availability (e.g. self-hosted,linux).
|
||||
When empty, checks all runners in the repository.'
|
||||
runnerCheckMinAvailable:
|
||||
default: '1'
|
||||
required: false
|
||||
description:
|
||||
'[Orchestrator] Minimum number of idle runners required for the primary provider. If fewer are available,
|
||||
routes to fallbackProviderStrategy.'
|
||||
retryOnFallback:
|
||||
default: 'false'
|
||||
required: false
|
||||
description:
|
||||
'[Orchestrator] When true and fallbackProviderStrategy is set, automatically retry the build on the fallback
|
||||
provider if the primary provider fails. Useful for long builds where transient cloud failures are common.'
|
||||
providerInitTimeout:
|
||||
default: '0'
|
||||
required: false
|
||||
description:
|
||||
'[Orchestrator] Maximum seconds to wait for the primary provider to initialize (setupWorkflow). If exceeded
|
||||
and fallbackProviderStrategy is set, switches to the fallback. Set to 0 to disable (default).'
|
||||
resourceTracking:
|
||||
default: 'false'
|
||||
required: false
|
||||
|
||||
256
dist/index.js
generated
vendored
256
dist/index.js
generated
vendored
@@ -327,6 +327,12 @@ class BuildParameters {
|
||||
containerRegistryRepository: input_1.default.containerRegistryRepository,
|
||||
containerRegistryImageVersion: input_1.default.containerRegistryImageVersion,
|
||||
providerStrategy: orchestrator_options_1.default.providerStrategy,
|
||||
fallbackProviderStrategy: orchestrator_options_1.default.fallbackProviderStrategy,
|
||||
runnerCheckEnabled: orchestrator_options_1.default.runnerCheckEnabled,
|
||||
runnerCheckLabels: orchestrator_options_1.default.runnerCheckLabels,
|
||||
runnerCheckMinAvailable: orchestrator_options_1.default.runnerCheckMinAvailable,
|
||||
retryOnFallback: orchestrator_options_1.default.retryOnFallback,
|
||||
providerInitTimeout: orchestrator_options_1.default.providerInitTimeout,
|
||||
buildPlatform: orchestrator_options_1.default.buildPlatform,
|
||||
kubeConfig: orchestrator_options_1.default.kubeConfig,
|
||||
containerMemory: orchestrator_options_1.default.containerMemory,
|
||||
@@ -2204,6 +2210,25 @@ class OrchestratorOptions {
|
||||
}
|
||||
return provider || 'local';
|
||||
}
|
||||
static get fallbackProviderStrategy() {
|
||||
return OrchestratorOptions.getInput('fallbackProviderStrategy') || '';
|
||||
}
|
||||
static get runnerCheckEnabled() {
|
||||
return OrchestratorOptions.getInput('runnerCheckEnabled') === 'true';
|
||||
}
|
||||
static get runnerCheckLabels() {
|
||||
const labels = OrchestratorOptions.getInput('runnerCheckLabels');
|
||||
return labels ? labels.split(',').map((l) => l.trim()) : [];
|
||||
}
|
||||
static get runnerCheckMinAvailable() {
|
||||
return Number(OrchestratorOptions.getInput('runnerCheckMinAvailable')) || 1;
|
||||
}
|
||||
static get retryOnFallback() {
|
||||
return OrchestratorOptions.getInput('retryOnFallback') === 'true';
|
||||
}
|
||||
static get providerInitTimeout() {
|
||||
return Number(OrchestratorOptions.getInput('providerInitTimeout')) || 0;
|
||||
}
|
||||
static get containerCpu() {
|
||||
return OrchestratorOptions.getInput('containerCpu') || `1024`;
|
||||
}
|
||||
@@ -2508,6 +2533,7 @@ const follow_log_stream_service_1 = __nccwpck_require__(36149);
|
||||
const orchestrator_result_1 = __importDefault(__nccwpck_require__(86819));
|
||||
const orchestrator_options_1 = __importDefault(__nccwpck_require__(82473));
|
||||
const resource_tracking_1 = __importDefault(__nccwpck_require__(42604));
|
||||
const runner_availability_service_1 = __nccwpck_require__(18876);
|
||||
class Orchestrator {
|
||||
static get isOrchestratorEnvironment() {
|
||||
return process.env[`GITHUB_ACTIONS`] !== `true`;
|
||||
@@ -2544,6 +2570,26 @@ class Orchestrator {
|
||||
}
|
||||
static async setupSelectedBuildPlatform() {
|
||||
orchestrator_logger_1.default.log(`Orchestrator platform selected ${Orchestrator.buildParameters.providerStrategy}`);
|
||||
// Check runner availability and apply fallback if needed
|
||||
if (Orchestrator.buildParameters.runnerCheckEnabled && Orchestrator.buildParameters.fallbackProviderStrategy) {
|
||||
const owner = orchestrator_options_1.default.githubOwner;
|
||||
const repo = orchestrator_options_1.default.githubRepoName;
|
||||
const token = Orchestrator.buildParameters.gitPrivateToken || process.env.GITHUB_TOKEN || '';
|
||||
orchestrator_logger_1.default.log(`Checking runner availability (labels: [${Orchestrator.buildParameters.runnerCheckLabels.join(', ')}], min: ${Orchestrator.buildParameters.runnerCheckMinAvailable})`);
|
||||
const result = await runner_availability_service_1.RunnerAvailabilityService.checkAvailability(owner, repo, token, Orchestrator.buildParameters.runnerCheckLabels, Orchestrator.buildParameters.runnerCheckMinAvailable);
|
||||
orchestrator_logger_1.default.log(`Runner check: ${result.totalRunners} total, ${result.matchingRunners} matching, ${result.idleRunners} idle — ${result.reason}`);
|
||||
if (result.shouldFallback) {
|
||||
const original = Orchestrator.buildParameters.providerStrategy;
|
||||
const fallback = Orchestrator.buildParameters.fallbackProviderStrategy;
|
||||
orchestrator_logger_1.default.log(`Falling back from '${original}' to '${fallback}' — ${result.reason}`);
|
||||
Orchestrator.buildParameters.providerStrategy = fallback;
|
||||
core.setOutput('providerFallbackUsed', 'true');
|
||||
core.setOutput('providerFallbackReason', result.reason);
|
||||
}
|
||||
else {
|
||||
core.setOutput('providerFallbackUsed', 'false');
|
||||
}
|
||||
}
|
||||
// Detect LocalStack endpoints and handle AWS provider appropriately
|
||||
// AWS_FORCE_PROVIDER options:
|
||||
// - 'aws': Force AWS provider (requires LocalStack Pro with ECS support)
|
||||
@@ -2642,13 +2688,33 @@ class Orchestrator {
|
||||
if (baseImage.includes(`undefined`)) {
|
||||
throw new Error(`baseImage is undefined`);
|
||||
}
|
||||
try {
|
||||
return await Orchestrator.runWithProvider(buildParameters, baseImage);
|
||||
}
|
||||
catch (primaryError) {
|
||||
// Retry on fallback provider if enabled and a fallback is configured
|
||||
const fallback = buildParameters.fallbackProviderStrategy;
|
||||
const alreadyOnFallback = buildParameters.providerStrategy === fallback;
|
||||
if (buildParameters.retryOnFallback && fallback && !alreadyOnFallback) {
|
||||
orchestrator_logger_1.default.log(`Primary provider '${buildParameters.providerStrategy}' failed: ${primaryError.message}`);
|
||||
orchestrator_logger_1.default.log(`Retrying build on fallback provider '${fallback}'...`);
|
||||
buildParameters.providerStrategy = fallback;
|
||||
core.setOutput('providerFallbackUsed', 'true');
|
||||
core.setOutput('providerFallbackReason', `Primary provider failed: ${primaryError.message}`);
|
||||
return await Orchestrator.runWithProvider(buildParameters, baseImage);
|
||||
}
|
||||
throw primaryError;
|
||||
}
|
||||
}
|
||||
static async runWithProvider(buildParameters, baseImage) {
|
||||
await Orchestrator.setup(buildParameters);
|
||||
// When aws-local mode is enabled, validate AWS CloudFormation templates
|
||||
// This ensures AWS templates are correct even when executing via local-docker
|
||||
if (Orchestrator.validateAwsTemplates) {
|
||||
await Orchestrator.validateAwsCloudFormationTemplates();
|
||||
}
|
||||
await Orchestrator.Provider.setupWorkflow(Orchestrator.buildParameters.buildGuid, Orchestrator.buildParameters, Orchestrator.buildParameters.branch, Orchestrator.defaultSecrets);
|
||||
// Setup workflow with optional init timeout
|
||||
await Orchestrator.setupWorkflowWithTimeout();
|
||||
try {
|
||||
if (buildParameters.maxRetainedWorkspaces > 0) {
|
||||
Orchestrator.lockedWorkspace = shared_workspace_locking_1.default.NewWorkspaceName();
|
||||
@@ -2698,6 +2764,24 @@ class Orchestrator {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Runs setupWorkflow with an optional timeout. If providerInitTimeout is set and the
|
||||
* provider takes longer than that to initialize, throws an error that triggers
|
||||
* retry-on-fallback (if enabled).
|
||||
*/
|
||||
static async setupWorkflowWithTimeout() {
|
||||
const timeoutSeconds = Orchestrator.buildParameters.providerInitTimeout;
|
||||
const setupPromise = Orchestrator.Provider.setupWorkflow(Orchestrator.buildParameters.buildGuid, Orchestrator.buildParameters, Orchestrator.buildParameters.branch, Orchestrator.defaultSecrets);
|
||||
if (timeoutSeconds <= 0) {
|
||||
await setupPromise;
|
||||
return;
|
||||
}
|
||||
orchestrator_logger_1.default.log(`Provider init timeout: ${timeoutSeconds}s`);
|
||||
const timeoutPromise = new Promise((_, reject) => {
|
||||
setTimeout(() => reject(new Error(`Provider initialization timed out after ${timeoutSeconds}s`)), timeoutSeconds * 1000);
|
||||
});
|
||||
await Promise.race([setupPromise, timeoutPromise]);
|
||||
}
|
||||
static async updateStatusWithBuildParameters() {
|
||||
const content = { ...Orchestrator.buildParameters };
|
||||
content.gitPrivateToken = ``;
|
||||
@@ -8639,6 +8723,176 @@ class ResourceTracking {
|
||||
exports["default"] = ResourceTracking;
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 18876:
|
||||
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
exports.RunnerAvailabilityService = void 0;
|
||||
const core_1 = __nccwpck_require__(76762);
|
||||
const orchestrator_logger_1 = __importDefault(__nccwpck_require__(32549));
|
||||
/**
|
||||
* Maximum number of pages to fetch when paginating through GitHub API results.
|
||||
* 100 pages * 100 per page = 10,000 runners maximum.
|
||||
*/
|
||||
const MAX_PAGINATION_PAGES = 100;
|
||||
/**
|
||||
* Total timeout in milliseconds for the pagination loop.
|
||||
* Prevents indefinite API calls if GitHub is slow or pagination is unexpectedly deep.
|
||||
*/
|
||||
const PAGINATION_TIMEOUT_MS = 30000;
|
||||
/**
|
||||
* Checks GitHub Actions runner availability to support automatic provider fallback.
|
||||
*
|
||||
* When a user configures `runnerCheckEnabled: true` with a `fallbackProviderStrategy`,
|
||||
* this service queries the GitHub API for runner status before the build starts.
|
||||
* If insufficient runners are available, the orchestrator routes to the fallback provider.
|
||||
*/
|
||||
class RunnerAvailabilityService {
|
||||
/**
|
||||
* Check if enough runners are available to handle the build.
|
||||
*
|
||||
* @param owner - GitHub repository owner
|
||||
* @param repo - GitHub repository name
|
||||
* @param token - GitHub token with repo/actions scope
|
||||
* @param requiredLabels - Labels runners must have (empty = any runner)
|
||||
* @param minAvailable - Minimum idle runners required
|
||||
* @returns RunnerCheckResult with decision and diagnostics
|
||||
*/
|
||||
static async checkAvailability(owner, repo, token, requiredLabels, minAvailable) {
|
||||
if (!token) {
|
||||
return {
|
||||
shouldFallback: false,
|
||||
reason: 'No GitHub token available — skipping runner check',
|
||||
totalRunners: 0,
|
||||
matchingRunners: 0,
|
||||
idleRunners: 0,
|
||||
};
|
||||
}
|
||||
try {
|
||||
const octokit = new core_1.Octokit({ auth: token });
|
||||
// Fetch all runners for the repository
|
||||
const runners = await RunnerAvailabilityService.fetchRunners(octokit, owner, repo);
|
||||
if (runners.length === 0) {
|
||||
return {
|
||||
shouldFallback: true,
|
||||
reason: 'No runners registered for this repository',
|
||||
totalRunners: 0,
|
||||
matchingRunners: 0,
|
||||
idleRunners: 0,
|
||||
};
|
||||
}
|
||||
// Filter by required labels
|
||||
const matching = RunnerAvailabilityService.filterByLabels(runners, requiredLabels);
|
||||
// Count idle (online + not busy)
|
||||
const idle = matching.filter((r) => r.status === 'online' && !r.busy);
|
||||
const result = {
|
||||
shouldFallback: idle.length < minAvailable,
|
||||
reason: idle.length >= minAvailable
|
||||
? `${idle.length} idle runner(s) available (need ${minAvailable})`
|
||||
: `Only ${idle.length} idle runner(s) available, need ${minAvailable}`,
|
||||
totalRunners: runners.length,
|
||||
matchingRunners: matching.length,
|
||||
idleRunners: idle.length,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
catch (error) {
|
||||
// If the API call fails (permissions, rate limit, etc.), don't block the build
|
||||
orchestrator_logger_1.default.log(`Runner availability check failed: ${error.message}`);
|
||||
return {
|
||||
shouldFallback: false,
|
||||
reason: `Runner check failed (${error.message}) — proceeding with primary provider`,
|
||||
totalRunners: 0,
|
||||
matchingRunners: 0,
|
||||
idleRunners: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Fetch all runners for a repository, handling pagination.
|
||||
*
|
||||
* Includes defensive limits:
|
||||
* - Maximum page count (MAX_PAGINATION_PAGES) to prevent infinite loops
|
||||
* - Total timeout (PAGINATION_TIMEOUT_MS) to prevent indefinite API calls
|
||||
* - Rate-limit detection (HTTP 403/429 with X-RateLimit-Remaining header)
|
||||
*/
|
||||
static async fetchRunners(octokit, owner, repo) {
|
||||
const allRunners = [];
|
||||
let page = 1;
|
||||
const perPage = 100;
|
||||
const startTime = Date.now();
|
||||
while (page <= MAX_PAGINATION_PAGES) {
|
||||
// Check total timeout
|
||||
if (Date.now() - startTime > PAGINATION_TIMEOUT_MS) {
|
||||
orchestrator_logger_1.default.logWarning(`[RunnerAvailability] Pagination timeout reached after ${page - 1} pages and ${Date.now() - startTime}ms. ` +
|
||||
`Using ${allRunners.length} runners found so far.`);
|
||||
break;
|
||||
}
|
||||
let response;
|
||||
try {
|
||||
response = await octokit.request('GET /repos/{owner}/{repo}/actions/runners', {
|
||||
owner,
|
||||
repo,
|
||||
per_page: perPage,
|
||||
page,
|
||||
});
|
||||
}
|
||||
catch (requestError) {
|
||||
// Octokit throws for non-2xx responses. Check if this is a rate limit error.
|
||||
const status = requestError.status ?? requestError.response?.status;
|
||||
if (status === 403 || status === 429) {
|
||||
const resetTime = requestError.response?.headers?.['x-ratelimit-reset'] ?? requestError.headers?.['x-ratelimit-reset'];
|
||||
const resetMessage = resetTime
|
||||
? ` Resets at ${new Date(Number.parseInt(String(resetTime), 10) * 1000).toISOString()}`
|
||||
: '';
|
||||
orchestrator_logger_1.default.logWarning(`[RunnerAvailability] GitHub API rate limit reached (HTTP ${status}).${resetMessage} ` +
|
||||
`Using ${allRunners.length} runners found so far.`);
|
||||
break;
|
||||
}
|
||||
// Re-throw non-rate-limit errors to be handled by the outer catch
|
||||
throw requestError;
|
||||
}
|
||||
const runners = (response.data.runners || []);
|
||||
allRunners.push(...runners);
|
||||
if (runners.length < perPage)
|
||||
break;
|
||||
page++;
|
||||
}
|
||||
if (page > MAX_PAGINATION_PAGES) {
|
||||
orchestrator_logger_1.default.logWarning(`[RunnerAvailability] Maximum pagination limit reached (${MAX_PAGINATION_PAGES} pages). ` +
|
||||
`Using ${allRunners.length} runners found so far.`);
|
||||
}
|
||||
if (allRunners.length === 0) {
|
||||
orchestrator_logger_1.default.log('[RunnerAvailability] No runners found. Possible causes: ' +
|
||||
'wrong token permissions (needs repo or actions scope), ' +
|
||||
'no self-hosted runners registered, ' +
|
||||
'or runners are registered at the organization level instead of the repository.');
|
||||
}
|
||||
return allRunners;
|
||||
}
|
||||
/**
|
||||
* Filter runners by required labels. A runner matches if it has ALL required labels.
|
||||
* If requiredLabels is empty, all runners match.
|
||||
*/
|
||||
static filterByLabels(runners, requiredLabels) {
|
||||
if (requiredLabels.length === 0)
|
||||
return runners;
|
||||
return runners.filter((runner) => {
|
||||
const runnerLabelNames = runner.labels.map((l) => l.name.toLowerCase());
|
||||
return requiredLabels.every((required) => runnerLabelNames.includes(required.toLowerCase()));
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.RunnerAvailabilityService = RunnerAvailabilityService;
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 54222:
|
||||
|
||||
2
dist/index.js.map
generated
vendored
2
dist/index.js.map
generated
vendored
File diff suppressed because one or more lines are too long
@@ -54,6 +54,12 @@ class BuildParameters {
|
||||
public sshAgent!: string;
|
||||
public sshPublicKeysDirectoryPath!: string;
|
||||
public providerStrategy!: string;
|
||||
public fallbackProviderStrategy!: string;
|
||||
public runnerCheckEnabled!: boolean;
|
||||
public runnerCheckLabels!: string[];
|
||||
public runnerCheckMinAvailable!: number;
|
||||
public retryOnFallback!: boolean;
|
||||
public providerInitTimeout!: number;
|
||||
public gitPrivateToken!: string;
|
||||
public awsStackName!: string;
|
||||
public awsEndpoint?: string;
|
||||
@@ -194,6 +200,12 @@ class BuildParameters {
|
||||
containerRegistryRepository: Input.containerRegistryRepository,
|
||||
containerRegistryImageVersion: Input.containerRegistryImageVersion,
|
||||
providerStrategy: OrchestratorOptions.providerStrategy,
|
||||
fallbackProviderStrategy: OrchestratorOptions.fallbackProviderStrategy,
|
||||
runnerCheckEnabled: OrchestratorOptions.runnerCheckEnabled,
|
||||
runnerCheckLabels: OrchestratorOptions.runnerCheckLabels,
|
||||
runnerCheckMinAvailable: OrchestratorOptions.runnerCheckMinAvailable,
|
||||
retryOnFallback: OrchestratorOptions.retryOnFallback,
|
||||
providerInitTimeout: OrchestratorOptions.providerInitTimeout,
|
||||
buildPlatform: OrchestratorOptions.buildPlatform,
|
||||
kubeConfig: OrchestratorOptions.kubeConfig,
|
||||
containerMemory: OrchestratorOptions.containerMemory,
|
||||
|
||||
@@ -1,161 +0,0 @@
|
||||
import { OrchestratorFolders } from './orchestrator-folders';
|
||||
|
||||
// Mock Orchestrator
|
||||
jest.mock('../orchestrator', () => ({
|
||||
__esModule: true,
|
||||
default: {
|
||||
buildParameters: {
|
||||
buildGuid: 'test-guid-abc',
|
||||
cacheKey: 'my-cache-key',
|
||||
projectPath: 'test-project',
|
||||
buildPath: 'Builds',
|
||||
maxRetainedWorkspaces: 0,
|
||||
gitPrivateToken: 'ghp_test123',
|
||||
orchestratorRepoName: 'game-ci/unity-builder',
|
||||
githubRepo: 'user/my-game',
|
||||
},
|
||||
lockedWorkspace: '',
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('../../build-parameters', () => ({
|
||||
__esModule: true,
|
||||
default: {
|
||||
shouldUseRetainedWorkspaceMode: jest.fn().mockReturnValue(false),
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('./orchestrator-options', () => ({
|
||||
__esModule: true,
|
||||
default: {
|
||||
useSharedBuilder: false,
|
||||
},
|
||||
}));
|
||||
|
||||
// Normalize paths for cross-platform test compatibility
|
||||
const normalize = (p: string) => p.replace(/\\/g, '/');
|
||||
|
||||
describe('OrchestratorFolders', () => {
|
||||
describe('static constants', () => {
|
||||
it('repositoryFolder is "repo"', () => {
|
||||
expect(OrchestratorFolders.repositoryFolder).toBe('repo');
|
||||
});
|
||||
|
||||
it('buildVolumeFolder is "data"', () => {
|
||||
expect(OrchestratorFolders.buildVolumeFolder).toBe('data');
|
||||
});
|
||||
|
||||
it('cacheFolder is "cache"', () => {
|
||||
expect(OrchestratorFolders.cacheFolder).toBe('cache');
|
||||
});
|
||||
});
|
||||
|
||||
describe('ToLinuxFolder', () => {
|
||||
it('converts backslashes to forward slashes', () => {
|
||||
expect(OrchestratorFolders.ToLinuxFolder('C:\\Users\\test\\project')).toBe('C:/Users/test/project');
|
||||
});
|
||||
|
||||
it('preserves forward slashes', () => {
|
||||
expect(OrchestratorFolders.ToLinuxFolder('/home/user/project')).toBe('/home/user/project');
|
||||
});
|
||||
|
||||
it('handles mixed slashes', () => {
|
||||
expect(OrchestratorFolders.ToLinuxFolder('some/path\\mixed/slashes\\here')).toBe('some/path/mixed/slashes/here');
|
||||
});
|
||||
|
||||
it('handles empty string', () => {
|
||||
expect(OrchestratorFolders.ToLinuxFolder('')).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('path computations (non-retained workspace mode)', () => {
|
||||
it('uniqueOrchestratorJobFolderAbsolute uses buildGuid', () => {
|
||||
const result = normalize(OrchestratorFolders.uniqueOrchestratorJobFolderAbsolute);
|
||||
expect(result).toBe('/data/test-guid-abc');
|
||||
});
|
||||
|
||||
it('cacheFolderForAllFull returns /data/cache', () => {
|
||||
const result = normalize(OrchestratorFolders.cacheFolderForAllFull);
|
||||
expect(result).toBe('/data/cache');
|
||||
});
|
||||
|
||||
it('cacheFolderForCacheKeyFull includes cache key', () => {
|
||||
const result = normalize(OrchestratorFolders.cacheFolderForCacheKeyFull);
|
||||
expect(result).toBe('/data/cache/my-cache-key');
|
||||
});
|
||||
|
||||
it('repoPathAbsolute is under job folder', () => {
|
||||
const result = normalize(OrchestratorFolders.repoPathAbsolute);
|
||||
expect(result).toBe('/data/test-guid-abc/repo');
|
||||
});
|
||||
|
||||
it('projectPathAbsolute includes project path', () => {
|
||||
const result = normalize(OrchestratorFolders.projectPathAbsolute);
|
||||
expect(result).toBe('/data/test-guid-abc/repo/test-project');
|
||||
});
|
||||
|
||||
it('libraryFolderAbsolute is under project path', () => {
|
||||
const result = normalize(OrchestratorFolders.libraryFolderAbsolute);
|
||||
expect(result).toBe('/data/test-guid-abc/repo/test-project/Library');
|
||||
});
|
||||
|
||||
it('projectBuildFolderAbsolute uses buildPath', () => {
|
||||
const result = normalize(OrchestratorFolders.projectBuildFolderAbsolute);
|
||||
expect(result).toBe('/data/test-guid-abc/repo/Builds');
|
||||
});
|
||||
|
||||
it('lfsFolderAbsolute is under .git/lfs', () => {
|
||||
const result = normalize(OrchestratorFolders.lfsFolderAbsolute);
|
||||
expect(result).toBe('/data/test-guid-abc/repo/.git/lfs');
|
||||
});
|
||||
|
||||
it('lfsCacheFolderFull is under cache key', () => {
|
||||
const result = normalize(OrchestratorFolders.lfsCacheFolderFull);
|
||||
expect(result).toBe('/data/cache/my-cache-key/lfs');
|
||||
});
|
||||
|
||||
it('libraryCacheFolderFull is under cache key', () => {
|
||||
const result = normalize(OrchestratorFolders.libraryCacheFolderFull);
|
||||
expect(result).toBe('/data/cache/my-cache-key/Library');
|
||||
});
|
||||
});
|
||||
|
||||
describe('builderPathAbsolute', () => {
|
||||
it('uses job folder when shared builder is disabled', () => {
|
||||
const result = normalize(OrchestratorFolders.builderPathAbsolute);
|
||||
expect(result).toBe('/data/test-guid-abc/builder');
|
||||
});
|
||||
});
|
||||
|
||||
describe('repo URLs', () => {
|
||||
it('unityBuilderRepoUrl includes token and repo name', () => {
|
||||
const url = OrchestratorFolders.unityBuilderRepoUrl;
|
||||
expect(url).toBe('https://ghp_test123@github.com/game-ci/unity-builder.git');
|
||||
});
|
||||
|
||||
it('targetBuildRepoUrl includes token and github repo', () => {
|
||||
const url = OrchestratorFolders.targetBuildRepoUrl;
|
||||
expect(url).toBe('https://ghp_test123@github.com/user/my-game.git');
|
||||
});
|
||||
});
|
||||
|
||||
describe('purgeRemoteCaching', () => {
|
||||
it('returns false when env var is not set', () => {
|
||||
const original = process.env.PURGE_REMOTE_BUILDER_CACHE;
|
||||
delete process.env.PURGE_REMOTE_BUILDER_CACHE;
|
||||
expect(OrchestratorFolders.purgeRemoteCaching).toBe(false);
|
||||
if (original !== undefined) process.env.PURGE_REMOTE_BUILDER_CACHE = original;
|
||||
});
|
||||
|
||||
it('returns true when env var is set', () => {
|
||||
const original = process.env.PURGE_REMOTE_BUILDER_CACHE;
|
||||
process.env.PURGE_REMOTE_BUILDER_CACHE = 'true';
|
||||
expect(OrchestratorFolders.purgeRemoteCaching).toBe(true);
|
||||
if (original !== undefined) {
|
||||
process.env.PURGE_REMOTE_BUILDER_CACHE = original;
|
||||
} else {
|
||||
delete process.env.PURGE_REMOTE_BUILDER_CACHE;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,53 +0,0 @@
|
||||
import OrchestratorNamespace from './orchestrator-guid';
|
||||
|
||||
describe('OrchestratorNamespace', () => {
|
||||
describe('generateGuid', () => {
|
||||
it('generates a guid with correct format', () => {
|
||||
const guid = OrchestratorNamespace.generateGuid('42', 'StandaloneLinux64');
|
||||
// Format: {runNumber}-{platform}-{nanoid4}
|
||||
expect(guid).toMatch(/^42-linux64-[a-z0-9]{4}$/);
|
||||
});
|
||||
|
||||
it('strips "standalone" prefix from platform (case-insensitive)', () => {
|
||||
const guid = OrchestratorNamespace.generateGuid('1', 'StandaloneWindows64');
|
||||
expect(guid).toMatch(/^1-windows64-[a-z0-9]{4}$/);
|
||||
});
|
||||
|
||||
it('lowercases platform name', () => {
|
||||
const guid = OrchestratorNamespace.generateGuid('5', 'Android');
|
||||
expect(guid).toMatch(/^5-android-[a-z0-9]{4}$/);
|
||||
});
|
||||
|
||||
it('handles numeric run number', () => {
|
||||
const guid = OrchestratorNamespace.generateGuid(100, 'iOS');
|
||||
expect(guid).toMatch(/^100-ios-[a-z0-9]{4}$/);
|
||||
});
|
||||
|
||||
it('generates unique guids on repeated calls', () => {
|
||||
const guids = new Set<string>();
|
||||
for (let i = 0; i < 20; i++) {
|
||||
guids.add(OrchestratorNamespace.generateGuid('1', 'StandaloneLinux64'));
|
||||
}
|
||||
// With 4 alphanumeric chars (36^4 = ~1.7M possibilities), 20 calls should almost certainly be unique
|
||||
expect(guids.size).toBeGreaterThan(1);
|
||||
});
|
||||
|
||||
it('handles StandaloneOSX platform', () => {
|
||||
const guid = OrchestratorNamespace.generateGuid('7', 'StandaloneOSX');
|
||||
expect(guid).toMatch(/^7-osx-[a-z0-9]{4}$/);
|
||||
});
|
||||
|
||||
it('handles WebGL platform (no standalone prefix)', () => {
|
||||
const guid = OrchestratorNamespace.generateGuid('3', 'WebGL');
|
||||
expect(guid).toMatch(/^3-webgl-[a-z0-9]{4}$/);
|
||||
});
|
||||
|
||||
it('uses only lowercase alphanumeric characters in nanoid portion', () => {
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const guid = OrchestratorNamespace.generateGuid('1', 'test');
|
||||
const nanoidPart = guid.split('-').pop()!;
|
||||
expect(nanoidPart).toMatch(/^[0-9a-z]{4}$/);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -138,6 +138,32 @@ class OrchestratorOptions {
|
||||
return provider || 'local';
|
||||
}
|
||||
|
||||
static get fallbackProviderStrategy(): string {
|
||||
return OrchestratorOptions.getInput('fallbackProviderStrategy') || '';
|
||||
}
|
||||
|
||||
static get runnerCheckEnabled(): boolean {
|
||||
return OrchestratorOptions.getInput('runnerCheckEnabled') === 'true';
|
||||
}
|
||||
|
||||
static get runnerCheckLabels(): string[] {
|
||||
const labels = OrchestratorOptions.getInput('runnerCheckLabels');
|
||||
|
||||
return labels ? labels.split(',').map((l) => l.trim()) : [];
|
||||
}
|
||||
|
||||
static get runnerCheckMinAvailable(): number {
|
||||
return Number(OrchestratorOptions.getInput('runnerCheckMinAvailable')) || 1;
|
||||
}
|
||||
|
||||
static get retryOnFallback(): boolean {
|
||||
return OrchestratorOptions.getInput('retryOnFallback') === 'true';
|
||||
}
|
||||
|
||||
static get providerInitTimeout(): number {
|
||||
return Number(OrchestratorOptions.getInput('providerInitTimeout')) || 0;
|
||||
}
|
||||
|
||||
static get containerCpu(): string {
|
||||
return OrchestratorOptions.getInput('containerCpu') || `1024`;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import { FollowLogStreamService } from './services/core/follow-log-stream-servic
|
||||
import OrchestratorResult from './services/core/orchestrator-result';
|
||||
import OrchestratorOptions from './options/orchestrator-options';
|
||||
import ResourceTracking from './services/core/resource-tracking';
|
||||
import { RunnerAvailabilityService } from './services/core/runner-availability-service';
|
||||
|
||||
class Orchestrator {
|
||||
public static Provider: ProviderInterface;
|
||||
@@ -76,6 +77,42 @@ class Orchestrator {
|
||||
private static async setupSelectedBuildPlatform() {
|
||||
OrchestratorLogger.log(`Orchestrator platform selected ${Orchestrator.buildParameters.providerStrategy}`);
|
||||
|
||||
// Check runner availability and apply fallback if needed
|
||||
if (Orchestrator.buildParameters.runnerCheckEnabled && Orchestrator.buildParameters.fallbackProviderStrategy) {
|
||||
const owner = OrchestratorOptions.githubOwner;
|
||||
const repo = OrchestratorOptions.githubRepoName;
|
||||
const token = Orchestrator.buildParameters.gitPrivateToken || process.env.GITHUB_TOKEN || '';
|
||||
|
||||
OrchestratorLogger.log(
|
||||
`Checking runner availability (labels: [${Orchestrator.buildParameters.runnerCheckLabels.join(', ')}], min: ${
|
||||
Orchestrator.buildParameters.runnerCheckMinAvailable
|
||||
})`,
|
||||
);
|
||||
|
||||
const result = await RunnerAvailabilityService.checkAvailability(
|
||||
owner,
|
||||
repo,
|
||||
token,
|
||||
Orchestrator.buildParameters.runnerCheckLabels,
|
||||
Orchestrator.buildParameters.runnerCheckMinAvailable,
|
||||
);
|
||||
|
||||
OrchestratorLogger.log(
|
||||
`Runner check: ${result.totalRunners} total, ${result.matchingRunners} matching, ${result.idleRunners} idle — ${result.reason}`,
|
||||
);
|
||||
|
||||
if (result.shouldFallback) {
|
||||
const original = Orchestrator.buildParameters.providerStrategy;
|
||||
const fallback = Orchestrator.buildParameters.fallbackProviderStrategy;
|
||||
OrchestratorLogger.log(`Falling back from '${original}' to '${fallback}' — ${result.reason}`);
|
||||
Orchestrator.buildParameters.providerStrategy = fallback;
|
||||
core.setOutput('providerFallbackUsed', 'true');
|
||||
core.setOutput('providerFallbackReason', result.reason);
|
||||
} else {
|
||||
core.setOutput('providerFallbackUsed', 'false');
|
||||
}
|
||||
}
|
||||
|
||||
// Detect LocalStack endpoints and handle AWS provider appropriately
|
||||
// AWS_FORCE_PROVIDER options:
|
||||
// - 'aws': Force AWS provider (requires LocalStack Pro with ECS support)
|
||||
@@ -182,6 +219,30 @@ class Orchestrator {
|
||||
if (baseImage.includes(`undefined`)) {
|
||||
throw new Error(`baseImage is undefined`);
|
||||
}
|
||||
|
||||
try {
|
||||
return await Orchestrator.runWithProvider(buildParameters, baseImage);
|
||||
} catch (primaryError: any) {
|
||||
// Retry on fallback provider if enabled and a fallback is configured
|
||||
const fallback = buildParameters.fallbackProviderStrategy;
|
||||
const alreadyOnFallback = buildParameters.providerStrategy === fallback;
|
||||
if (buildParameters.retryOnFallback && fallback && !alreadyOnFallback) {
|
||||
OrchestratorLogger.log(
|
||||
`Primary provider '${buildParameters.providerStrategy}' failed: ${primaryError.message}`,
|
||||
);
|
||||
OrchestratorLogger.log(`Retrying build on fallback provider '${fallback}'...`);
|
||||
buildParameters.providerStrategy = fallback;
|
||||
core.setOutput('providerFallbackUsed', 'true');
|
||||
core.setOutput('providerFallbackReason', `Primary provider failed: ${primaryError.message}`);
|
||||
|
||||
return await Orchestrator.runWithProvider(buildParameters, baseImage);
|
||||
}
|
||||
|
||||
throw primaryError;
|
||||
}
|
||||
}
|
||||
|
||||
private static async runWithProvider(buildParameters: BuildParameters, baseImage: string) {
|
||||
await Orchestrator.setup(buildParameters);
|
||||
|
||||
// When aws-local mode is enabled, validate AWS CloudFormation templates
|
||||
@@ -189,12 +250,10 @@ class Orchestrator {
|
||||
if (Orchestrator.validateAwsTemplates) {
|
||||
await Orchestrator.validateAwsCloudFormationTemplates();
|
||||
}
|
||||
await Orchestrator.Provider.setupWorkflow(
|
||||
Orchestrator.buildParameters.buildGuid,
|
||||
Orchestrator.buildParameters,
|
||||
Orchestrator.buildParameters.branch,
|
||||
Orchestrator.defaultSecrets,
|
||||
);
|
||||
|
||||
// Setup workflow with optional init timeout
|
||||
await Orchestrator.setupWorkflowWithTimeout();
|
||||
|
||||
try {
|
||||
if (buildParameters.maxRetainedWorkspaces > 0) {
|
||||
Orchestrator.lockedWorkspace = SharedWorkspaceLocking.NewWorkspaceName();
|
||||
@@ -275,6 +334,39 @@ class Orchestrator {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs setupWorkflow with an optional timeout. If providerInitTimeout is set and the
|
||||
* provider takes longer than that to initialize, throws an error that triggers
|
||||
* retry-on-fallback (if enabled).
|
||||
*/
|
||||
private static async setupWorkflowWithTimeout() {
|
||||
const timeoutSeconds = Orchestrator.buildParameters.providerInitTimeout;
|
||||
|
||||
const setupPromise = Orchestrator.Provider.setupWorkflow(
|
||||
Orchestrator.buildParameters.buildGuid,
|
||||
Orchestrator.buildParameters,
|
||||
Orchestrator.buildParameters.branch,
|
||||
Orchestrator.defaultSecrets,
|
||||
);
|
||||
|
||||
if (timeoutSeconds <= 0) {
|
||||
await setupPromise;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
OrchestratorLogger.log(`Provider init timeout: ${timeoutSeconds}s`);
|
||||
|
||||
const timeoutPromise = new Promise<never>((_, reject) => {
|
||||
setTimeout(
|
||||
() => reject(new Error(`Provider initialization timed out after ${timeoutSeconds}s`)),
|
||||
timeoutSeconds * 1000,
|
||||
);
|
||||
});
|
||||
|
||||
await Promise.race([setupPromise, timeoutPromise]);
|
||||
}
|
||||
|
||||
private static async updateStatusWithBuildParameters() {
|
||||
const content = { ...Orchestrator.buildParameters };
|
||||
content.gitPrivateToken = ``;
|
||||
|
||||
@@ -1,147 +0,0 @@
|
||||
import { FollowLogStreamService } from './follow-log-stream-service';
|
||||
import * as core from '@actions/core';
|
||||
import GitHub from '../../../github';
|
||||
|
||||
// Mock dependencies
|
||||
jest.mock('../../../github', () => ({
|
||||
__esModule: true,
|
||||
default: {
|
||||
updateGitHubCheck: jest.fn(),
|
||||
githubInputEnabled: false,
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('@actions/core', () => ({
|
||||
warning: jest.fn(),
|
||||
setOutput: jest.fn(),
|
||||
setFailed: jest.fn(),
|
||||
error: jest.fn(),
|
||||
getInput: jest.fn().mockReturnValue(''),
|
||||
}));
|
||||
|
||||
jest.mock('../../orchestrator', () => ({
|
||||
__esModule: true,
|
||||
default: {
|
||||
buildParameters: {
|
||||
logId: 'test-log-id-123',
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('../../options/orchestrator-statics', () => ({
|
||||
OrchestratorStatics: {
|
||||
logPrefix: 'TEST',
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('./orchestrator-logger', () => ({
|
||||
__esModule: true,
|
||||
default: {
|
||||
log: jest.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
describe('FollowLogStreamService', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
FollowLogStreamService.Reset();
|
||||
FollowLogStreamService.errors = '';
|
||||
});
|
||||
|
||||
describe('Reset', () => {
|
||||
it('resets DidReceiveEndOfTransmission to false', () => {
|
||||
FollowLogStreamService.DidReceiveEndOfTransmission = true;
|
||||
FollowLogStreamService.Reset();
|
||||
expect(FollowLogStreamService.DidReceiveEndOfTransmission).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('handleIteration', () => {
|
||||
it('detects end of transmission marker', () => {
|
||||
const result = FollowLogStreamService.handleIteration('---test-log-id-123', true, false, '');
|
||||
expect(FollowLogStreamService.DidReceiveEndOfTransmission).toBe(true);
|
||||
expect(result.shouldReadLogs).toBe(false);
|
||||
});
|
||||
|
||||
it('does not trigger end of transmission for non-matching log ID', () => {
|
||||
const result = FollowLogStreamService.handleIteration('---different-log-id', true, false, '');
|
||||
expect(FollowLogStreamService.DidReceiveEndOfTransmission).toBe(false);
|
||||
expect(result.shouldReadLogs).toBe(true);
|
||||
});
|
||||
|
||||
it('detects Library rebuild message', () => {
|
||||
FollowLogStreamService.handleIteration(
|
||||
'Rebuilding Library because the asset database could not be found!',
|
||||
true,
|
||||
false,
|
||||
'',
|
||||
);
|
||||
expect(GitHub.updateGitHubCheck).toHaveBeenCalledWith('Library was not found, importing new Library', '');
|
||||
expect(core.warning).toHaveBeenCalledWith('LIBRARY NOT FOUND!');
|
||||
expect(core.setOutput).toHaveBeenCalledWith('library-found', 'false');
|
||||
});
|
||||
|
||||
it('detects Build succeeded message', () => {
|
||||
FollowLogStreamService.handleIteration('Build succeeded', true, false, '');
|
||||
expect(GitHub.updateGitHubCheck).toHaveBeenCalledWith('Build succeeded', 'Build succeeded');
|
||||
expect(core.setOutput).toHaveBeenCalledWith('build-result', 'success');
|
||||
});
|
||||
|
||||
it('detects Build fail message', () => {
|
||||
FollowLogStreamService.handleIteration('Build fail', true, false, '');
|
||||
expect(GitHub.updateGitHubCheck).toHaveBeenCalled();
|
||||
expect(core.setOutput).toHaveBeenCalledWith('build-result', 'failed');
|
||||
expect(core.setFailed).toHaveBeenCalledWith('unity build failed');
|
||||
expect(core.error).toHaveBeenCalledWith('BUILD FAILED!');
|
||||
});
|
||||
|
||||
it('accumulates error messages with "error " pattern', () => {
|
||||
FollowLogStreamService.handleIteration('error CS0001: Something went wrong', true, false, '');
|
||||
expect(FollowLogStreamService.errors).toContain('error CS0001: Something went wrong');
|
||||
expect(core.error).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('accumulates error messages with "error: " pattern', () => {
|
||||
FollowLogStreamService.handleIteration('Fatal Error: Out of memory', true, false, '');
|
||||
expect(FollowLogStreamService.errors).toContain('Fatal Error: Out of memory');
|
||||
});
|
||||
|
||||
it('accumulates "command failed: " messages', () => {
|
||||
FollowLogStreamService.handleIteration('command failed: git pull', true, false, '');
|
||||
expect(FollowLogStreamService.errors).toContain('command failed: git pull');
|
||||
});
|
||||
|
||||
it('accumulates "invalid " messages', () => {
|
||||
FollowLogStreamService.handleIteration('invalid configuration value', true, false, '');
|
||||
expect(FollowLogStreamService.errors).toContain('invalid configuration value');
|
||||
});
|
||||
|
||||
it('accumulates "cannot be found" messages', () => {
|
||||
FollowLogStreamService.handleIteration('Assembly cannot be found', true, false, '');
|
||||
expect(FollowLogStreamService.errors).toContain('Assembly cannot be found');
|
||||
});
|
||||
|
||||
it('appends message to output', () => {
|
||||
const result = FollowLogStreamService.handleIteration('Some normal log line', true, false, 'previous output\n');
|
||||
expect(result.output).toContain('Some normal log line');
|
||||
expect(result.output).toContain('previous output');
|
||||
});
|
||||
|
||||
it('preserves shouldCleanup value', () => {
|
||||
const result = FollowLogStreamService.handleIteration('normal message', true, true, '');
|
||||
expect(result.shouldCleanup).toBe(true);
|
||||
});
|
||||
|
||||
it('does not change shouldReadLogs for normal messages', () => {
|
||||
const result = FollowLogStreamService.handleIteration('Just a regular build log', true, false, '');
|
||||
expect(result.shouldReadLogs).toBe(true);
|
||||
});
|
||||
|
||||
it('includes accumulated errors in Build fail GitHub check message', () => {
|
||||
FollowLogStreamService.errors = '\nprevious error';
|
||||
FollowLogStreamService.handleIteration('Build fail', true, false, '');
|
||||
const updateCall = (GitHub.updateGitHubCheck as jest.Mock).mock.calls[0];
|
||||
expect(updateCall[0]).toContain('previous error');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,318 @@
|
||||
import { RunnerAvailabilityService } from './runner-availability-service';
|
||||
|
||||
// Mock @octokit/core
|
||||
jest.mock('@octokit/core', () => ({
|
||||
Octokit: jest.fn().mockImplementation(() => ({
|
||||
request: jest.fn(),
|
||||
})),
|
||||
}));
|
||||
|
||||
jest.mock('./orchestrator-logger', () => ({
|
||||
__esModule: true,
|
||||
default: {
|
||||
log: jest.fn(),
|
||||
logWarning: jest.fn(),
|
||||
error: jest.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
import { Octokit } from '@octokit/core';
|
||||
|
||||
const MockedOctokit = Octokit as jest.MockedClass<typeof Octokit>;
|
||||
|
||||
function createMockRunners(runners: Array<{ name: string; status: string; busy: boolean; labels: string[] }>) {
|
||||
return runners.map((r, i) => ({
|
||||
id: i + 1,
|
||||
name: r.name,
|
||||
status: r.status,
|
||||
busy: r.busy,
|
||||
labels: r.labels.map((l) => ({ name: l })),
|
||||
}));
|
||||
}
|
||||
|
||||
describe('RunnerAvailabilityService', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('checkAvailability', () => {
|
||||
it('should skip check and not fallback when no token is provided', async () => {
|
||||
const result = await RunnerAvailabilityService.checkAvailability('owner', 'repo', '', [], 1);
|
||||
expect(result.shouldFallback).toBe(false);
|
||||
expect(result.reason).toContain('No GitHub token');
|
||||
});
|
||||
|
||||
it('should fallback when no runners are registered', async () => {
|
||||
const mockRequest = jest.fn().mockResolvedValue({ status: 200, data: { runners: [] } });
|
||||
MockedOctokit.mockImplementation(() => ({ request: mockRequest } as any));
|
||||
|
||||
const result = await RunnerAvailabilityService.checkAvailability('owner', 'repo', 'token', [], 1);
|
||||
expect(result.shouldFallback).toBe(true);
|
||||
expect(result.reason).toContain('No runners registered');
|
||||
expect(result.totalRunners).toBe(0);
|
||||
});
|
||||
|
||||
it('should not fallback when enough idle runners are available', async () => {
|
||||
const runners = createMockRunners([
|
||||
{ name: 'runner-1', status: 'online', busy: false, labels: ['self-hosted', 'linux'] },
|
||||
{ name: 'runner-2', status: 'online', busy: false, labels: ['self-hosted', 'linux'] },
|
||||
]);
|
||||
const mockRequest = jest.fn().mockResolvedValue({ status: 200, data: { runners } });
|
||||
MockedOctokit.mockImplementation(() => ({ request: mockRequest } as any));
|
||||
|
||||
const result = await RunnerAvailabilityService.checkAvailability('owner', 'repo', 'token', [], 1);
|
||||
expect(result.shouldFallback).toBe(false);
|
||||
expect(result.idleRunners).toBe(2);
|
||||
expect(result.totalRunners).toBe(2);
|
||||
});
|
||||
|
||||
it('should fallback when all runners are busy', async () => {
|
||||
const runners = createMockRunners([
|
||||
{ name: 'runner-1', status: 'online', busy: true, labels: ['self-hosted'] },
|
||||
{ name: 'runner-2', status: 'online', busy: true, labels: ['self-hosted'] },
|
||||
]);
|
||||
const mockRequest = jest.fn().mockResolvedValue({ status: 200, data: { runners } });
|
||||
MockedOctokit.mockImplementation(() => ({ request: mockRequest } as any));
|
||||
|
||||
const result = await RunnerAvailabilityService.checkAvailability('owner', 'repo', 'token', [], 1);
|
||||
expect(result.shouldFallback).toBe(true);
|
||||
expect(result.idleRunners).toBe(0);
|
||||
expect(result.matchingRunners).toBe(2);
|
||||
});
|
||||
|
||||
it('should fallback when all runners are offline', async () => {
|
||||
const runners = createMockRunners([
|
||||
{ name: 'runner-1', status: 'offline', busy: false, labels: ['self-hosted'] },
|
||||
]);
|
||||
const mockRequest = jest.fn().mockResolvedValue({ status: 200, data: { runners } });
|
||||
MockedOctokit.mockImplementation(() => ({ request: mockRequest } as any));
|
||||
|
||||
const result = await RunnerAvailabilityService.checkAvailability('owner', 'repo', 'token', [], 1);
|
||||
expect(result.shouldFallback).toBe(true);
|
||||
expect(result.idleRunners).toBe(0);
|
||||
});
|
||||
|
||||
it('should filter runners by required labels', async () => {
|
||||
const runners = createMockRunners([
|
||||
{ name: 'linux-runner', status: 'online', busy: false, labels: ['self-hosted', 'linux'] },
|
||||
{ name: 'windows-runner', status: 'online', busy: false, labels: ['self-hosted', 'windows'] },
|
||||
]);
|
||||
const mockRequest = jest.fn().mockResolvedValue({ status: 200, data: { runners } });
|
||||
MockedOctokit.mockImplementation(() => ({ request: mockRequest } as any));
|
||||
|
||||
const result = await RunnerAvailabilityService.checkAvailability(
|
||||
'owner',
|
||||
'repo',
|
||||
'token',
|
||||
['self-hosted', 'linux'],
|
||||
1,
|
||||
);
|
||||
|
||||
expect(result.shouldFallback).toBe(false);
|
||||
expect(result.matchingRunners).toBe(1);
|
||||
expect(result.idleRunners).toBe(1);
|
||||
expect(result.totalRunners).toBe(2);
|
||||
});
|
||||
|
||||
it('should fallback when no runners match required labels', async () => {
|
||||
const runners = createMockRunners([
|
||||
{ name: 'windows-runner', status: 'online', busy: false, labels: ['self-hosted', 'windows'] },
|
||||
]);
|
||||
const mockRequest = jest.fn().mockResolvedValue({ status: 200, data: { runners } });
|
||||
MockedOctokit.mockImplementation(() => ({ request: mockRequest } as any));
|
||||
|
||||
const result = await RunnerAvailabilityService.checkAvailability(
|
||||
'owner',
|
||||
'repo',
|
||||
'token',
|
||||
['self-hosted', 'linux'],
|
||||
1,
|
||||
);
|
||||
|
||||
expect(result.shouldFallback).toBe(true);
|
||||
expect(result.matchingRunners).toBe(0);
|
||||
expect(result.idleRunners).toBe(0);
|
||||
});
|
||||
|
||||
it('should respect minAvailable threshold', async () => {
|
||||
const runners = createMockRunners([{ name: 'runner-1', status: 'online', busy: false, labels: ['self-hosted'] }]);
|
||||
const mockRequest = jest.fn().mockResolvedValue({ status: 200, data: { runners } });
|
||||
MockedOctokit.mockImplementation(() => ({ request: mockRequest } as any));
|
||||
|
||||
// Need 2, have 1 — should fallback
|
||||
const result = await RunnerAvailabilityService.checkAvailability('owner', 'repo', 'token', [], 2);
|
||||
expect(result.shouldFallback).toBe(true);
|
||||
expect(result.idleRunners).toBe(1);
|
||||
});
|
||||
|
||||
it('should be case-insensitive for label matching', async () => {
|
||||
const runners = createMockRunners([
|
||||
{ name: 'runner-1', status: 'online', busy: false, labels: ['Self-Hosted', 'Linux'] },
|
||||
]);
|
||||
const mockRequest = jest.fn().mockResolvedValue({ status: 200, data: { runners } });
|
||||
MockedOctokit.mockImplementation(() => ({ request: mockRequest } as any));
|
||||
|
||||
const result = await RunnerAvailabilityService.checkAvailability(
|
||||
'owner',
|
||||
'repo',
|
||||
'token',
|
||||
['self-hosted', 'linux'],
|
||||
1,
|
||||
);
|
||||
expect(result.shouldFallback).toBe(false);
|
||||
expect(result.matchingRunners).toBe(1);
|
||||
});
|
||||
|
||||
it('should not fallback on API error (fail-open)', async () => {
|
||||
const mockRequest = jest.fn().mockRejectedValue(new Error('403 Forbidden'));
|
||||
MockedOctokit.mockImplementation(() => ({ request: mockRequest } as any));
|
||||
|
||||
const result = await RunnerAvailabilityService.checkAvailability('owner', 'repo', 'token', [], 1);
|
||||
expect(result.shouldFallback).toBe(false);
|
||||
expect(result.reason).toContain('Runner check failed');
|
||||
});
|
||||
|
||||
it('should count only online+idle runners', async () => {
|
||||
const runners = createMockRunners([
|
||||
{ name: 'idle', status: 'online', busy: false, labels: ['self-hosted'] },
|
||||
{ name: 'busy', status: 'online', busy: true, labels: ['self-hosted'] },
|
||||
{ name: 'offline', status: 'offline', busy: false, labels: ['self-hosted'] },
|
||||
]);
|
||||
const mockRequest = jest.fn().mockResolvedValue({ status: 200, data: { runners } });
|
||||
MockedOctokit.mockImplementation(() => ({ request: mockRequest } as any));
|
||||
|
||||
const result = await RunnerAvailabilityService.checkAvailability('owner', 'repo', 'token', [], 1);
|
||||
expect(result.shouldFallback).toBe(false);
|
||||
expect(result.totalRunners).toBe(3);
|
||||
expect(result.matchingRunners).toBe(3);
|
||||
expect(result.idleRunners).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pagination limits', () => {
|
||||
it('should stop paginating after reaching the page limit', async () => {
|
||||
// Return full pages (100 runners each) to force continued pagination
|
||||
let callCount = 0;
|
||||
const mockRequest = jest.fn().mockImplementation(() => {
|
||||
callCount++;
|
||||
const runners = createMockRunners(
|
||||
Array.from({ length: 100 }, (_, i) => ({
|
||||
name: `runner-${callCount}-${i}`,
|
||||
status: 'online' as const,
|
||||
busy: false,
|
||||
labels: ['self-hosted'],
|
||||
})),
|
||||
);
|
||||
|
||||
return Promise.resolve({ status: 200, data: { runners } });
|
||||
});
|
||||
MockedOctokit.mockImplementation(() => ({ request: mockRequest } as any));
|
||||
|
||||
const result = await RunnerAvailabilityService.checkAvailability('owner', 'repo', 'token', [], 1);
|
||||
|
||||
// Should have called at most 100 pages (the MAX_PAGINATION_PAGES limit)
|
||||
expect(mockRequest).toHaveBeenCalledTimes(100);
|
||||
// Should still have runners from the pages it did fetch
|
||||
expect(result.totalRunners).toBe(10000);
|
||||
expect(result.shouldFallback).toBe(false);
|
||||
});
|
||||
|
||||
it('should stop paginating on rate limit (HTTP 403)', async () => {
|
||||
let callCount = 0;
|
||||
const mockRequest = jest.fn().mockImplementation(() => {
|
||||
callCount++;
|
||||
if (callCount === 2) {
|
||||
// Octokit throws for non-2xx responses
|
||||
const error: any = new Error('API rate limit exceeded');
|
||||
error.status = 403;
|
||||
error.response = {
|
||||
status: 403,
|
||||
headers: { 'x-ratelimit-reset': String(Math.floor(Date.now() / 1000) + 3600) },
|
||||
};
|
||||
|
||||
return Promise.reject(error);
|
||||
}
|
||||
const runners = createMockRunners(
|
||||
Array.from({ length: 100 }, (_, i) => ({
|
||||
name: `runner-${i}`,
|
||||
status: 'online' as const,
|
||||
busy: false,
|
||||
labels: ['self-hosted'],
|
||||
})),
|
||||
);
|
||||
|
||||
return Promise.resolve({ status: 200, data: { runners } });
|
||||
});
|
||||
MockedOctokit.mockImplementation(() => ({ request: mockRequest } as any));
|
||||
|
||||
const result = await RunnerAvailabilityService.checkAvailability('owner', 'repo', 'token', [], 1);
|
||||
|
||||
// Should have stopped at page 2 (rate limited)
|
||||
expect(mockRequest).toHaveBeenCalledTimes(2);
|
||||
// Should use the 100 runners from the first page
|
||||
expect(result.totalRunners).toBe(100);
|
||||
expect(result.shouldFallback).toBe(false);
|
||||
});
|
||||
|
||||
it('should stop paginating on rate limit (HTTP 429)', async () => {
|
||||
let callCount = 0;
|
||||
const mockRequest = jest.fn().mockImplementation(() => {
|
||||
callCount++;
|
||||
if (callCount === 1) {
|
||||
// Octokit throws for non-2xx responses
|
||||
const error: any = new Error('Too Many Requests');
|
||||
error.status = 429;
|
||||
error.response = { status: 429, headers: {} };
|
||||
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
return Promise.resolve({ status: 200, data: { runners: [] } });
|
||||
});
|
||||
MockedOctokit.mockImplementation(() => ({ request: mockRequest } as any));
|
||||
|
||||
const result = await RunnerAvailabilityService.checkAvailability('owner', 'repo', 'token', [], 1);
|
||||
|
||||
// Should have stopped at first page (rate limited immediately)
|
||||
expect(mockRequest).toHaveBeenCalledTimes(1);
|
||||
// No runners found — should fallback
|
||||
expect(result.totalRunners).toBe(0);
|
||||
expect(result.shouldFallback).toBe(true);
|
||||
});
|
||||
|
||||
it('should handle pagination timeout gracefully', async () => {
|
||||
// Mock Date.now to simulate timeout
|
||||
const originalDateNow = Date.now;
|
||||
let callCount = 0;
|
||||
|
||||
const mockRequest = jest.fn().mockImplementation(() => {
|
||||
callCount++;
|
||||
// After first call, advance time past the timeout
|
||||
if (callCount >= 2) {
|
||||
Date.now = jest.fn(() => originalDateNow() + 31_000);
|
||||
}
|
||||
const runners = createMockRunners(
|
||||
Array.from({ length: 100 }, (_, i) => ({
|
||||
name: `runner-${callCount}-${i}`,
|
||||
status: 'online' as const,
|
||||
busy: false,
|
||||
labels: ['self-hosted'],
|
||||
})),
|
||||
);
|
||||
|
||||
return Promise.resolve({ status: 200, data: { runners } });
|
||||
});
|
||||
MockedOctokit.mockImplementation(() => ({ request: mockRequest } as any));
|
||||
|
||||
const result = await RunnerAvailabilityService.checkAvailability('owner', 'repo', 'token', [], 1);
|
||||
|
||||
// Should have stopped after timeout was detected (2 pages: first succeeds, second triggers timeout check)
|
||||
expect(mockRequest.mock.calls.length).toBeLessThanOrEqual(3);
|
||||
// Should have runners from pages fetched before timeout
|
||||
expect(result.totalRunners).toBeGreaterThan(0);
|
||||
|
||||
// Restore
|
||||
Date.now = originalDateNow;
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,205 @@
|
||||
import { Octokit } from '@octokit/core';
|
||||
import OrchestratorLogger from './orchestrator-logger';
|
||||
|
||||
interface GitHubRunner {
|
||||
id: number;
|
||||
name: string;
|
||||
status: 'online' | 'offline';
|
||||
busy: boolean;
|
||||
labels: Array<{ name: string }>;
|
||||
}
|
||||
|
||||
interface RunnerCheckResult {
|
||||
shouldFallback: boolean;
|
||||
reason: string;
|
||||
totalRunners: number;
|
||||
matchingRunners: number;
|
||||
idleRunners: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maximum number of pages to fetch when paginating through GitHub API results.
|
||||
* 100 pages * 100 per page = 10,000 runners maximum.
|
||||
*/
|
||||
const MAX_PAGINATION_PAGES = 100;
|
||||
|
||||
/**
|
||||
* Total timeout in milliseconds for the pagination loop.
|
||||
* Prevents indefinite API calls if GitHub is slow or pagination is unexpectedly deep.
|
||||
*/
|
||||
const PAGINATION_TIMEOUT_MS = 30_000;
|
||||
|
||||
/**
|
||||
* Checks GitHub Actions runner availability to support automatic provider fallback.
|
||||
*
|
||||
* When a user configures `runnerCheckEnabled: true` with a `fallbackProviderStrategy`,
|
||||
* this service queries the GitHub API for runner status before the build starts.
|
||||
* If insufficient runners are available, the orchestrator routes to the fallback provider.
|
||||
*/
|
||||
export class RunnerAvailabilityService {
|
||||
/**
|
||||
* Check if enough runners are available to handle the build.
|
||||
*
|
||||
* @param owner - GitHub repository owner
|
||||
* @param repo - GitHub repository name
|
||||
* @param token - GitHub token with repo/actions scope
|
||||
* @param requiredLabels - Labels runners must have (empty = any runner)
|
||||
* @param minAvailable - Minimum idle runners required
|
||||
* @returns RunnerCheckResult with decision and diagnostics
|
||||
*/
|
||||
static async checkAvailability(
|
||||
owner: string,
|
||||
repo: string,
|
||||
token: string,
|
||||
requiredLabels: string[],
|
||||
minAvailable: number,
|
||||
): Promise<RunnerCheckResult> {
|
||||
if (!token) {
|
||||
return {
|
||||
shouldFallback: false,
|
||||
reason: 'No GitHub token available — skipping runner check',
|
||||
totalRunners: 0,
|
||||
matchingRunners: 0,
|
||||
idleRunners: 0,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const octokit = new Octokit({ auth: token });
|
||||
|
||||
// Fetch all runners for the repository
|
||||
const runners = await RunnerAvailabilityService.fetchRunners(octokit, owner, repo);
|
||||
|
||||
if (runners.length === 0) {
|
||||
return {
|
||||
shouldFallback: true,
|
||||
reason: 'No runners registered for this repository',
|
||||
totalRunners: 0,
|
||||
matchingRunners: 0,
|
||||
idleRunners: 0,
|
||||
};
|
||||
}
|
||||
|
||||
// Filter by required labels
|
||||
const matching = RunnerAvailabilityService.filterByLabels(runners, requiredLabels);
|
||||
|
||||
// Count idle (online + not busy)
|
||||
const idle = matching.filter((r) => r.status === 'online' && !r.busy);
|
||||
|
||||
const result: RunnerCheckResult = {
|
||||
shouldFallback: idle.length < minAvailable,
|
||||
reason:
|
||||
idle.length >= minAvailable
|
||||
? `${idle.length} idle runner(s) available (need ${minAvailable})`
|
||||
: `Only ${idle.length} idle runner(s) available, need ${minAvailable}`,
|
||||
totalRunners: runners.length,
|
||||
matchingRunners: matching.length,
|
||||
idleRunners: idle.length,
|
||||
};
|
||||
|
||||
return result;
|
||||
} catch (error: any) {
|
||||
// If the API call fails (permissions, rate limit, etc.), don't block the build
|
||||
OrchestratorLogger.log(`Runner availability check failed: ${error.message}`);
|
||||
|
||||
return {
|
||||
shouldFallback: false,
|
||||
reason: `Runner check failed (${error.message}) — proceeding with primary provider`,
|
||||
totalRunners: 0,
|
||||
matchingRunners: 0,
|
||||
idleRunners: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch all runners for a repository, handling pagination.
|
||||
*
|
||||
* Includes defensive limits:
|
||||
* - Maximum page count (MAX_PAGINATION_PAGES) to prevent infinite loops
|
||||
* - Total timeout (PAGINATION_TIMEOUT_MS) to prevent indefinite API calls
|
||||
* - Rate-limit detection (HTTP 403/429 with X-RateLimit-Remaining header)
|
||||
*/
|
||||
private static async fetchRunners(octokit: Octokit, owner: string, repo: string): Promise<GitHubRunner[]> {
|
||||
const allRunners: GitHubRunner[] = [];
|
||||
let page = 1;
|
||||
const perPage = 100;
|
||||
const startTime = Date.now();
|
||||
|
||||
while (page <= MAX_PAGINATION_PAGES) {
|
||||
// Check total timeout
|
||||
if (Date.now() - startTime > PAGINATION_TIMEOUT_MS) {
|
||||
OrchestratorLogger.logWarning(
|
||||
`[RunnerAvailability] Pagination timeout reached after ${page - 1} pages and ${Date.now() - startTime}ms. ` +
|
||||
`Using ${allRunners.length} runners found so far.`,
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
let response: any;
|
||||
try {
|
||||
response = await octokit.request('GET /repos/{owner}/{repo}/actions/runners', {
|
||||
owner,
|
||||
repo,
|
||||
per_page: perPage,
|
||||
page,
|
||||
});
|
||||
} catch (requestError: any) {
|
||||
// Octokit throws for non-2xx responses. Check if this is a rate limit error.
|
||||
const status = requestError.status ?? requestError.response?.status;
|
||||
if (status === 403 || status === 429) {
|
||||
const resetTime =
|
||||
requestError.response?.headers?.['x-ratelimit-reset'] ?? requestError.headers?.['x-ratelimit-reset'];
|
||||
const resetMessage = resetTime
|
||||
? ` Resets at ${new Date(Number.parseInt(String(resetTime), 10) * 1000).toISOString()}`
|
||||
: '';
|
||||
OrchestratorLogger.logWarning(
|
||||
`[RunnerAvailability] GitHub API rate limit reached (HTTP ${status}).${resetMessage} ` +
|
||||
`Using ${allRunners.length} runners found so far.`,
|
||||
);
|
||||
break;
|
||||
}
|
||||
// Re-throw non-rate-limit errors to be handled by the outer catch
|
||||
throw requestError;
|
||||
}
|
||||
|
||||
const runners = (response.data.runners || []) as GitHubRunner[];
|
||||
allRunners.push(...runners);
|
||||
|
||||
if (runners.length < perPage) break;
|
||||
page++;
|
||||
}
|
||||
|
||||
if (page > MAX_PAGINATION_PAGES) {
|
||||
OrchestratorLogger.logWarning(
|
||||
`[RunnerAvailability] Maximum pagination limit reached (${MAX_PAGINATION_PAGES} pages). ` +
|
||||
`Using ${allRunners.length} runners found so far.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (allRunners.length === 0) {
|
||||
OrchestratorLogger.log(
|
||||
'[RunnerAvailability] No runners found. Possible causes: ' +
|
||||
'wrong token permissions (needs repo or actions scope), ' +
|
||||
'no self-hosted runners registered, ' +
|
||||
'or runners are registered at the organization level instead of the repository.',
|
||||
);
|
||||
}
|
||||
|
||||
return allRunners;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter runners by required labels. A runner matches if it has ALL required labels.
|
||||
* If requiredLabels is empty, all runners match.
|
||||
*/
|
||||
private static filterByLabels(runners: GitHubRunner[], requiredLabels: string[]): GitHubRunner[] {
|
||||
if (requiredLabels.length === 0) return runners;
|
||||
|
||||
return runners.filter((runner) => {
|
||||
const runnerLabelNames = runner.labels.map((l) => l.name.toLowerCase());
|
||||
|
||||
return requiredLabels.every((required) => runnerLabelNames.includes(required.toLowerCase()));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,207 +0,0 @@
|
||||
import { TaskParameterSerializer } from './task-parameter-serializer';
|
||||
|
||||
// Mock dependencies that TaskParameterSerializer uses internally
|
||||
jest.mock('@actions/core', () => ({
|
||||
getInput: jest.fn().mockReturnValue(''),
|
||||
setOutput: jest.fn(),
|
||||
info: jest.fn(),
|
||||
warning: jest.fn(),
|
||||
error: jest.fn(),
|
||||
}));
|
||||
|
||||
jest.mock('../../options/orchestrator-options', () => ({
|
||||
__esModule: true,
|
||||
default: {
|
||||
getInput: jest.fn().mockReturnValue(undefined),
|
||||
ToEnvVarFormat: (input: string) => {
|
||||
if (input.toUpperCase() === input) {
|
||||
return input;
|
||||
}
|
||||
return input
|
||||
.replace(/([A-Z])/g, ' $1')
|
||||
.trim()
|
||||
.toUpperCase()
|
||||
.replace(/ /g, '_');
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('../../options/orchestrator-options-reader', () => ({
|
||||
__esModule: true,
|
||||
default: {
|
||||
GetProperties: jest.fn().mockReturnValue([]),
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('../../options/orchestrator-query-override', () => ({
|
||||
__esModule: true,
|
||||
default: {
|
||||
queryOverrides: undefined,
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('../hooks/command-hook-service', () => ({
|
||||
CommandHookService: {
|
||||
getHooks: jest.fn().mockReturnValue([]),
|
||||
getSecrets: jest.fn().mockReturnValue([]),
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('../../../input', () => ({
|
||||
__esModule: true,
|
||||
default: {},
|
||||
}));
|
||||
|
||||
jest.mock('../../../github', () => ({
|
||||
__esModule: true,
|
||||
default: {
|
||||
githubInputEnabled: false,
|
||||
},
|
||||
}));
|
||||
|
||||
describe('TaskParameterSerializer', () => {
|
||||
describe('ToEnvVarFormat', () => {
|
||||
it('converts camelCase to UPPER_SNAKE_CASE', () => {
|
||||
expect(TaskParameterSerializer.ToEnvVarFormat('targetPlatform')).toBe('TARGET_PLATFORM');
|
||||
});
|
||||
|
||||
it('converts single word to uppercase', () => {
|
||||
expect(TaskParameterSerializer.ToEnvVarFormat('version')).toBe('VERSION');
|
||||
});
|
||||
|
||||
it('preserves already-uppercase strings', () => {
|
||||
expect(TaskParameterSerializer.ToEnvVarFormat('AWS_REGION')).toBe('AWS_REGION');
|
||||
});
|
||||
|
||||
it('handles multi-word camelCase', () => {
|
||||
expect(TaskParameterSerializer.ToEnvVarFormat('buildPlatformTarget')).toBe('BUILD_PLATFORM_TARGET');
|
||||
});
|
||||
|
||||
it('handles string starting with uppercase', () => {
|
||||
expect(TaskParameterSerializer.ToEnvVarFormat('BuildGuid')).toBe('BUILD_GUID');
|
||||
});
|
||||
});
|
||||
|
||||
describe('UndoEnvVarFormat', () => {
|
||||
it('converts UPPER_SNAKE_CASE back to camelCase', () => {
|
||||
expect(TaskParameterSerializer.UndoEnvVarFormat('TARGET_PLATFORM')).toBe('targetPlatform');
|
||||
});
|
||||
|
||||
it('handles single word', () => {
|
||||
expect(TaskParameterSerializer.UndoEnvVarFormat('VERSION')).toBe('version');
|
||||
});
|
||||
|
||||
it('handles multiple underscores', () => {
|
||||
expect(TaskParameterSerializer.UndoEnvVarFormat('BUILD_PLATFORM_TARGET')).toBe('buildPlatformTarget');
|
||||
});
|
||||
});
|
||||
|
||||
describe('round-trip conversion', () => {
|
||||
it('ToEnvVarFormat -> UndoEnvVarFormat returns original for simple camelCase', () => {
|
||||
const original = 'targetPlatform';
|
||||
const envVar = TaskParameterSerializer.ToEnvVarFormat(original);
|
||||
const roundTrip = TaskParameterSerializer.UndoEnvVarFormat(envVar);
|
||||
expect(roundTrip).toBe(original);
|
||||
});
|
||||
|
||||
it('round-trips multi-word keys', () => {
|
||||
const original = 'cacheKey';
|
||||
const envVar = TaskParameterSerializer.ToEnvVarFormat(original);
|
||||
const roundTrip = TaskParameterSerializer.UndoEnvVarFormat(envVar);
|
||||
expect(roundTrip).toBe(original);
|
||||
});
|
||||
});
|
||||
|
||||
describe('uniqBy', () => {
|
||||
it('removes duplicates by key function', () => {
|
||||
const items = [
|
||||
{ name: 'A', value: '1' },
|
||||
{ name: 'B', value: '2' },
|
||||
{ name: 'A', value: '3' },
|
||||
];
|
||||
const result = TaskParameterSerializer.uniqBy(items, (x) => x.name);
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result[0].value).toBe('1');
|
||||
expect(result[1].value).toBe('2');
|
||||
});
|
||||
|
||||
it('returns all items when no duplicates', () => {
|
||||
const items = [
|
||||
{ name: 'A', value: '1' },
|
||||
{ name: 'B', value: '2' },
|
||||
{ name: 'C', value: '3' },
|
||||
];
|
||||
const result = TaskParameterSerializer.uniqBy(items, (x) => x.name);
|
||||
expect(result).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('handles empty array', () => {
|
||||
const result = TaskParameterSerializer.uniqBy([], (x) => x.name);
|
||||
expect(result).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('keeps first occurrence when duplicates exist', () => {
|
||||
const items = [
|
||||
{ name: 'KEY', value: 'first' },
|
||||
{ name: 'KEY', value: 'second' },
|
||||
{ name: 'KEY', value: 'third' },
|
||||
];
|
||||
const result = TaskParameterSerializer.uniqBy(items, (x) => x.name);
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].value).toBe('first');
|
||||
});
|
||||
});
|
||||
|
||||
describe('blockedParameterNames', () => {
|
||||
it('contains expected blocked names', () => {
|
||||
expect(TaskParameterSerializer.blockedParameterNames.has('0')).toBe(true);
|
||||
expect(TaskParameterSerializer.blockedParameterNames.has('length')).toBe(true);
|
||||
expect(TaskParameterSerializer.blockedParameterNames.has('prototype')).toBe(true);
|
||||
expect(TaskParameterSerializer.blockedParameterNames.has('')).toBe(true);
|
||||
expect(TaskParameterSerializer.blockedParameterNames.has('unityVersion')).toBe(true);
|
||||
expect(TaskParameterSerializer.blockedParameterNames.has('CUSTOM_JOB')).toBe(true);
|
||||
});
|
||||
|
||||
it('does not block valid parameter names', () => {
|
||||
expect(TaskParameterSerializer.blockedParameterNames.has('targetPlatform')).toBe(false);
|
||||
expect(TaskParameterSerializer.blockedParameterNames.has('buildGuid')).toBe(false);
|
||||
expect(TaskParameterSerializer.blockedParameterNames.has('cacheKey')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('readDefaultSecrets', () => {
|
||||
it('returns an array', () => {
|
||||
const secrets = TaskParameterSerializer.readDefaultSecrets();
|
||||
expect(Array.isArray(secrets)).toBe(true);
|
||||
});
|
||||
|
||||
it('includes secrets from environment when present', () => {
|
||||
const originalSerial = process.env.UNITY_SERIAL;
|
||||
process.env.UNITY_SERIAL = 'test-serial';
|
||||
|
||||
const secrets = TaskParameterSerializer.readDefaultSecrets();
|
||||
const serialSecret = secrets.find((s) => s.ParameterKey === 'UNITY_SERIAL');
|
||||
expect(serialSecret).toBeDefined();
|
||||
expect(serialSecret?.ParameterValue).toBe('test-serial');
|
||||
|
||||
if (originalSerial !== undefined) {
|
||||
process.env.UNITY_SERIAL = originalSerial;
|
||||
} else {
|
||||
delete process.env.UNITY_SERIAL;
|
||||
}
|
||||
});
|
||||
|
||||
it('excludes secrets not in environment', () => {
|
||||
const originalSerial = process.env.UNITY_SERIAL;
|
||||
delete process.env.UNITY_SERIAL;
|
||||
|
||||
const secrets = TaskParameterSerializer.readDefaultSecrets();
|
||||
const serialSecret = secrets.find((s) => s.ParameterKey === 'UNITY_SERIAL');
|
||||
expect(serialSecret).toBeUndefined();
|
||||
|
||||
if (originalSerial !== undefined) {
|
||||
process.env.UNITY_SERIAL = originalSerial;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user