All files / src publish.ts

0% Statements 0/27
0% Branches 0/10
0% Functions 0/2
0% Lines 0/27

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90                                                                                                                                                                                   
import type { PublishContext } from 'semantic-release';
import type { PluginConfig } from './types';
import { isPrerelease } from './utils/semrel';
import resolveConfig from './config';
import { Forgejo } from './services/forgejo';
import { stat } from 'fs/promises';
import { Stats } from 'fs';
import { RELEASE_NAME } from './constants';
import { rfetch } from '@ribbon-studios/js-utils';
import SemanticReleaseError from '@semantic-release/error';
 
/**
 * Called by semantic-release during the publish step.
 * Responsible for publishing the release.
 */
export async function publish(pluginConfig: PluginConfig, context: PublishContext) {
  const {
    cwd,
    branch,
    nextRelease: { name, gitTag, notes },
    logger,
  } = context;
  const { forgejoToken, forgejoUrl, slug, assets, draftRelease } = resolveConfig(pluginConfig, context);
 
  const release: Forgejo.CreateReleaseOption = {
    tag_name: gitTag,
    target_commitish: branch.name,
    name: name,
    body: notes,
    prerelease: isPrerelease(branch),
    hide_archive_links: true,
  };
 
  const client = new Forgejo({
    url: forgejoUrl,
    token: forgejoToken,
  });
 
  try {
    const { id: releaseId, html_url: draftUrl } = await client.release.create(slug, {
      ...release,
      draft: true,
    });
 
    if (assets.length > 0) {
      await Promise.all(
        assets.map(async (path) => {
          const asset = Forgejo.asset(cwd, path);
 
          let file: Stats;
 
          try {
            file = await stat(asset.path);
          } catch {
            logger.error('The asset %s cannot be read, and will be ignored.', asset.path);
            return;
          }
 
          if (!file || !file.isFile()) {
            logger.error('The asset %s is not a file, and will be ignored.', asset.path);
            return;
          }
 
          const { browser_download_url: downloadUrl } = await client.release.assets.create(slug, releaseId, asset);
          logger.log('Published file %s', downloadUrl);
        })
      );
    }
 
    if (draftRelease) {
      logger.log('Created GitHub draft release: %s', draftUrl);
      return { url: draftUrl, name: RELEASE_NAME, id: releaseId };
    }
 
    const { html_url: url } = await client.release.update(slug, releaseId, {
      draft: false,
    });
 
    logger.log('Published Forgejo release: %s', url);
 
    return { url, name: RELEASE_NAME, id: releaseId };
  } catch (error) {
    if (rfetch.is.error(error)) {
      throw new SemanticReleaseError('An error occured while creating releases / assets', 'ECODE', error.content);
    }
 
    throw error;
  }
}