All files / src verify-conditions.ts

64.28% Statements 9/14
50% Branches 5/10
100% Functions 1/1
64.28% Lines 9/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50                          1x 1x     1x         1x         1x       1x         1x   1x             1x        
import type { VerifyConditionsContext } from 'semantic-release';
import type { PluginConfig } from './types';
import SemanticReleaseError from '@semantic-release/error';
import AggregateError from 'aggregate-error';
import { empty } from './utils/guards';
import { Forgejo } from './services/forgejo';
import { join } from 'path/posix';
import resolveConfig from './config';
 
/**
 * Called by semantic-release during the verification step
 */
export async function verifyConditions(pluginConfig: PluginConfig, context: VerifyConditionsContext): Promise<void> {
  const errors: SemanticReleaseError[] = [];
  const { forgejoToken, forgejoUrl, slug } = resolveConfig(pluginConfig, context);
 
  // Check Forgejo token
  Iif (empty(forgejoToken)) {
    errors.push(new SemanticReleaseError('Empty FORGEJO_TOKEN', 'ECONFIG'));
  }
 
  // Check Forgejo url
  Iif (empty(forgejoUrl)) {
    errors.push(new SemanticReleaseError('Empty FORGEJO_URL', 'ECONFIG'));
  }
 
  // Check Forgejo repository
  Iif (empty(slug)) {
    errors.push(new SemanticReleaseError('Empty FORGEJO_REPOSITORY', 'ECONFIG'));
  }
 
  const forgejo = new Forgejo({
    url: forgejoUrl,
    token: forgejoToken,
  });
 
  const info = await forgejo.repositories.get(slug);
 
  Iif (!info.has_releases) {
    errors.push(
      new SemanticReleaseError(`Please enable releases at "${join(forgejoUrl, slug, '/settings/units')}"`, 'ECONFIG')
    );
  }
 
  // Throw any errors we accumulated during the validation
  Iif (errors.length > 0) {
    throw new AggregateError(errors);
  }
}