diff --git a/lib/init-action.js b/lib/init-action.js index 164428e29..e4ea32ec5 100644 --- a/lib/init-action.js +++ b/lib/init-action.js @@ -87199,20 +87199,25 @@ var Result = class _Result { this._ok = _ok; this.value = value; } - static ok(value) { + /** A success result. */ + static success(value) { return new _Result(true, value); } - static error(error3) { - return new _Result(false, error3); + /** A failure result. */ + static failure(value) { + return new _Result(false, value); } - isOk() { + /** Whether this result represents a success. */ + isSuccess() { return this._ok; } - isError() { + /** Whether this result represents a failure. */ + isFailure() { return !this._ok; } + /** Get the value if this is a success, or return the default value if this is a failure. */ orElse(defaultValue) { - return this.isOk() ? this.value : defaultValue; + return this.isSuccess() ? this.value : defaultValue; } }; @@ -92686,7 +92691,7 @@ async function run(startedAt) { repositoryProperties: repositoryProperties.orElse({}), logger }); - if (repositoryProperties.isError()) { + if (repositoryProperties.isFailure()) { addDiagnostic( config, // Arbitrarily choose the first language. We could also choose all languages, but that @@ -92991,23 +92996,23 @@ async function loadRepositoryProperties(repositoryNwo, gitHubVersion, features, logger.debug( "Skipping loading repository properties because the repository is owned by a user and therefore cannot have repository properties." ); - return Result.ok({}); + return Result.success({}); } if (!await features.getValue("use_repository_properties" /* UseRepositoryProperties */)) { logger.debug( "Skipping loading repository properties because the UseRepositoryProperties feature flag is disabled." ); - return Result.ok({}); + return Result.success({}); } try { - return Result.ok( + return Result.success( await loadPropertiesFromApi(gitHubVersion, logger, repositoryNwo) ); } catch (error3) { logger.warning( `Failed to load repository properties: ${getErrorMessage(error3)}` ); - return Result.error(error3); + return Result.failure(error3); } } function getTrapCachingEnabled() { diff --git a/src/init-action.ts b/src/init-action.ts index d57a1441d..64d94959d 100644 --- a/src/init-action.ts +++ b/src/init-action.ts @@ -373,7 +373,7 @@ async function run(startedAt: Date) { logger, }); - if (repositoryProperties.isError()) { + if (repositoryProperties.isFailure()) { addDiagnostic( config, // Arbitrarily choose the first language. We could also choose all languages, but that @@ -810,25 +810,25 @@ async function loadRepositoryProperties( "Skipping loading repository properties because the repository is owned by a user and " + "therefore cannot have repository properties.", ); - return Result.ok({}); + return Result.success({}); } if (!(await features.getValue(Feature.UseRepositoryProperties))) { logger.debug( "Skipping loading repository properties because the UseRepositoryProperties feature flag is disabled.", ); - return Result.ok({}); + return Result.success({}); } try { - return Result.ok( + return Result.success( await loadPropertiesFromApi(gitHubVersion, logger, repositoryNwo), ); } catch (error) { logger.warning( `Failed to load repository properties: ${getErrorMessage(error)}`, ); - return Result.error(error); + return Result.failure(error); } } diff --git a/src/util.ts b/src/util.ts index 2e2bd0511..2298acab3 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1293,32 +1293,42 @@ export function joinAtMost( return array.join(separator); } +/** A success result. */ type Success = Result; +/** A failure result. */ type Failure = Result; +/** + * A simple result type representing either a success or a failure. + */ export class Result { private constructor( private readonly _ok: boolean, public readonly value: T | E, ) {} - static ok(value: T): Success { + /** A success result. */ + static success(value: T): Success { return new Result(true, value) as Success; } - static error(error: E): Failure { - return new Result(false, error) as Failure; + /** A failure result. */ + static failure(value: E): Failure { + return new Result(false, value) as Failure; } - isOk(): this is Success { + /** Whether this result represents a success. */ + isSuccess(): this is Success { return this._ok; } - isError(): this is Failure { + /** Whether this result represents a failure. */ + isFailure(): this is Failure { return !this._ok; } + /** Get the value if this is a success, or return the default value if this is a failure. */ orElse(defaultValue: T): T { - return this.isOk() ? this.value : defaultValue; + return this.isSuccess() ? this.value : defaultValue; } }