0.69.0 • Published 5 months ago

@code-pushup/ci v0.69.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@code-pushup/ci

npm downloads dependencies

šŸ”ŽšŸ”¬ Quality metrics for your software project. šŸ“‰šŸ”

  1. āš™ļø Configure what you want to track using your favourite tools.
  2. šŸ¤– Integrate it in your CI.
  3. 🌈 Visualize reports in a beautiful dashboard.

This package exports provider-agnostic core logic for running Code PushUp in CI pipelines. It serves as the base for the following provider integrations:

GitHub Actionscode-pushup/github-action
GitLab CI/CDcode-pushup/gitlab-pipelines-template

Setup

npm install @code-pushup/ci
yarn add @code-pushup/ci
pnpm add @code-pushup/ci

Usage

The runInCI function implements the full CI flow:

import { runInCI } from '@code-pushup/ci';

const result = await runInCI(
  {
    /* Git refs */
  },
  {
    /* Provider API client */
  },
  {
    /* Options */
  },
);

Parameters

Git refs

For each CI run, you must pass in the commit SHA and Git ref (e.g. main) of what was pushed. These values can be detected from the CI environment, the details depend on which provider is being used.

If only the head is supplied, then Code PushUp will collect a new report and optionally upload it to portal (depending on your Code PushUp config). If triggered by a pull request, then specify the base ref as well. This will additionally compare reports from both source and target branches and post a comment to the PR.

PropertyRequiredTypeDescription
headyesstring \| { ref: string, sha: string }Current branch/commit
basenostring \| { ref: string, sha: string }Branch targeted by PR

Provider API client

The PR flow requires interacting with the Git provider's API to post a comparison comment. Wrap these requests in functions and pass them in as an object which configures the provider.

PropertyRequiredTypeDescription
createCommentyes(body: string) => Promise<Comment>Posts a new comment to PR
updateCommentyes(id: number, body: string) => Promise<Comment>Updates existing PR comment
listCommentsyes() => Promise<Comment[]>Fetches all comments from PR
maxCommentCharsyesnumberCharacter limit for comment body
downloadReportArtifactno(project?: string) => Promise<string \| null>Fetches previous (root/project) report.json for base branch and returns path, used as cache to speed up comparison

A Comment object has the following required properties:

PropertyTypeDescription
idnumberComment ID
bodystringContent of comment as Markdown string
urlstringWeb link to comment in PR

Options

Optionally, you can override default options for further customization:

PropertyTypeDefaultDescription
monorepoboolean \| MonorepoToolfalseEnables monorepo mode
parallelboolean \| numberfalseEnables parallel execution in monorepo mode
projectsstring[] \| nullnullCustom projects configuration for monorepo mode
taskstring'code-pushup'Name of command to run Code PushUp per project in monorepo mode
nxProjectsFilterstring \| string[]'--with-target={task}'Arguments passed to nx show projects, only relevant for Nx in monorepo mode ^2
directorystringprocess.cwd()Directory in which Code PushUp CLI should run
configstring \| nullnull ^1Path to config file (--config option)
silentbooleanfalseHides logs from CLI commands (erros will be printed)
binstring'npx --no-install code-pushup'Command for executing Code PushUp CLI
detectNewIssuesbooleantrueToggles if new issues should be detected and returned in newIssues property
loggerLoggerconsoleLogger for reporting progress and encountered problems
skipCommentbooleanfalseToggles if comparison comment is posted to PR

^1: By default, the code-pushup.config file is autodetected as described in @code-pushup/cli docs.

^2: The {task} pattern is replaced with the task value, so the default behaviour is to list projects using npx nx show projects --with-target=code-pushup --json. The nxProjectsFilter options gives Nx users the flexibility to filter projects in alternative ways supported by the Nx CLI (e.g. --affected, --projects, --exclude, --type) - refer to options in Nx docs for details.

The Logger object has the following required properties:

PropertyTypeDescription
error(message: string) => voidPrints error log
warn(message: string) => voidPrints warning log
info(message: string) => voidPrints info log
debug(message: string) => voidPrints debug log

Standalone mode

By default, it is assumed that Code PushUp is set up to run on the whole repo with one command (standalone mode). If you want to run Code PushUp on multiple projects separately, you should enable monorepo mode.

Standalone result

In standalone mode, the resolved object will include paths to report files (JSON and Markdown formats), as well as diff files, comment ID and new issues in case of PR comparisons.

const result = await runInCI(refs, api);

if (result.mode === 'standalone') {
  const {
    // output files, can be uploaded as job artifact
    files: { current, comparison },
    // ID of created/updated PR comment
    commentId,
    // array of source code issues, can be used to annotate changed files in PR
    newIssues,
  } = result;
}

Monorepo mode

For monorepo setups, Code PushUp reports can be collected and compared individually per project. All project comparisons are then combined into a single PR comment.

Use the monorepo option to activate monorepo mode:

await runInCI(refs, api, {
  monorepo: true,
});

The runInCI function will try to detect which monorepo tool you're using from the file system. The following tools are supported out of the box:

If you're using one of these tools, you can also skip auto-detection by setting monorepo option to 'nx', 'turbo', 'yarn', 'pnpm' or 'npm'.

If none of these tools are detected, then the fallback is to run Code PushUp in all folders which have a package.json file. If that's not what you want, then you can also configure folder patterns using the projects option (supports globs):

await runInCI(refs, api, {
  monorepo: true,
  projects: ['frontend', 'backend/*'],
});

Based on which monorepo tool is used, Code PushUp CLI commands will be executed using a package.json script, Nx target, Turbo task, or binary executable (as fallback). By default, these are expected to be called code-pushup, but you can override the name using the task option:

await runInCI(refs, api, {
  monorepo: 'nx',
  task: 'analyze', // custom Nx target
});

Parallel tasks

By default, tasks are run sequentially for each project in the monorepo. The parallel option enables parallel execution for tools which support it (Nx, Turborepo, PNPM, Yarn 2+).

await runInCI(refs, api, {
  monorepo: true,
  parallel: true,
});

The maximum number of concurrent tasks can be set by passing in a number instead of a boolean:

await runInCI(refs, api, {
  monorepo: true,
  parallel: 3,
});

Monorepo result

In monorepo mode, the resolved object includes the merged diff at the top-level, as well as a list of projects. Each project has its own report files and issues.

const result = await runInCI(refs, api);

if (result.mode === 'monorepo') {
  const {
    // array of objects with result for each project
    projects,
    // ID of created/updated PR comment
    commentId,
    // merged report-diff.md used in PR comment, can also be uploaded as job artifact
    files: { comparison },
  } = result;

  for (const project of projects) {
    const {
      // detected project name (from package.json, project.json or folder name)
      name,
      // output files, can be uploaded as job artifacts
      files: { current, comparison },
      // array of source code issues, can be used to annotate changed files in PR
      newIssues,
    } = project;
  }
}
0.63.0

8 months ago

0.62.0

8 months ago

0.60.2

8 months ago

0.65.0

8 months ago

0.64.1

8 months ago

0.64.0

8 months ago

0.61.0

8 months ago

0.60.1

8 months ago

0.60.0

8 months ago

0.59.0

8 months ago

0.57.0

10 months ago

0.58.0

8 months ago

0.55.0

11 months ago

0.56.0

11 months ago

0.54.0

12 months ago

0.67.0

6 months ago

0.66.1

7 months ago

0.65.2

7 months ago

0.66.0

7 months ago

0.65.1

7 months ago

0.64.2

8 months ago

0.69.0

5 months ago

0.68.0

5 months ago

0.66.2

7 months ago

0.65.3

7 months ago

0.53.1

1 year ago

0.53.0

1 year ago

0.52.0

1 year ago