@code-pushup/ci 0.52.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +222 -0
- package/index.js +1708 -0
- package/package.json +28 -0
- package/src/index.d.ts +2 -0
- package/src/lib/cli/commands/collect.d.ts +3 -0
- package/src/lib/cli/commands/compare.d.ts +9 -0
- package/src/lib/cli/commands/merge-diffs.d.ts +3 -0
- package/src/lib/cli/commands/print-config.d.ts +2 -0
- package/src/lib/cli/context.d.ts +6 -0
- package/src/lib/cli/index.d.ts +6 -0
- package/src/lib/cli/persist.d.ts +21 -0
- package/src/lib/comment.d.ts +2 -0
- package/src/lib/constants.d.ts +2 -0
- package/src/lib/git.d.ts +23 -0
- package/src/lib/issues.d.ts +16 -0
- package/src/lib/models.d.ts +102 -0
- package/src/lib/monorepo/detect-tool.d.ts +2 -0
- package/src/lib/monorepo/handlers/index.d.ts +3 -0
- package/src/lib/monorepo/handlers/npm.d.ts +2 -0
- package/src/lib/monorepo/handlers/nx.d.ts +2 -0
- package/src/lib/monorepo/handlers/pnpm.d.ts +2 -0
- package/src/lib/monorepo/handlers/turbo.d.ts +2 -0
- package/src/lib/monorepo/handlers/yarn.d.ts +2 -0
- package/src/lib/monorepo/index.d.ts +2 -0
- package/src/lib/monorepo/list-projects.d.ts +3 -0
- package/src/lib/monorepo/packages.d.ts +17 -0
- package/src/lib/monorepo/tools.d.ts +18 -0
- package/src/lib/run.d.ts +11 -0
package/README.md
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
# @code-pushup/ci
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@code-pushup/ci)
|
|
4
|
+
[](https://npmtrends.com/@code-pushup/ci)
|
|
5
|
+
[](https://www.npmjs.com/package/@code-pushup/ci?activeTab=dependencies)
|
|
6
|
+
|
|
7
|
+
🔎🔬 **Quality metrics for your software project.** 📉🔍
|
|
8
|
+
|
|
9
|
+
1. ⚙️ **Configure what you want to track using your favourite tools.**
|
|
10
|
+
2. 🤖 **Integrate it in your CI.**
|
|
11
|
+
3. 🌈 **Visualize reports in a beautiful dashboard.**
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
This package exports **provider-agnostic core logic for running Code PushUp in CI pipelines**. It serves as the base for the following provider integrations:
|
|
16
|
+
|
|
17
|
+
| | |
|
|
18
|
+
| :------------- | :-------------------------------------------------------------------------------- |
|
|
19
|
+
| GitHub Actions | [`code-pushup/github-action`](https://github.com/marketplace/actions/code-pushup) |
|
|
20
|
+
| GitLab CI/CD | _coming soon_ |
|
|
21
|
+
|
|
22
|
+
## Setup
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
npm install @code-pushup/ci
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
yarn add @code-pushup/ci
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
pnpm add @code-pushup/ci
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
The `runInCI` function implements the full CI flow:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { runInCI } from '@code-pushup/ci';
|
|
42
|
+
|
|
43
|
+
const result = await runInCI(
|
|
44
|
+
{
|
|
45
|
+
/* Git refs */
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
/* Provider API client */
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
/* Options */
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Parameters
|
|
57
|
+
|
|
58
|
+
### Git refs
|
|
59
|
+
|
|
60
|
+
For each CI run, you must pass in the commit SHA and Git ref (e.g. `main`) of what was pushed.
|
|
61
|
+
These values can be detected from the CI environment, the details depend on which provider is being used.
|
|
62
|
+
|
|
63
|
+
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).
|
|
64
|
+
If triggered by a pull request, then specify the `base` ref as well.
|
|
65
|
+
This will additionally compare reports from both source and target branches and post a comment to the PR.
|
|
66
|
+
|
|
67
|
+
| Property | Required | Type | Description |
|
|
68
|
+
| :------- | :------: | :----------------------------- | :-------------------- |
|
|
69
|
+
| `head` | yes | `{ ref: string, sha: string }` | Current branch/commit |
|
|
70
|
+
| `base` | no | `{ ref: string, sha: string }` | Branch targeted by PR |
|
|
71
|
+
|
|
72
|
+
### Provider API client
|
|
73
|
+
|
|
74
|
+
The PR flow requires interacting with the Git provider's API to post a comparison comment.
|
|
75
|
+
Wrap these requests in functions and pass them in as an object which configures the provider.
|
|
76
|
+
|
|
77
|
+
| Property | Required | Type | Description |
|
|
78
|
+
| :----------------------- | :------: | :----------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------- |
|
|
79
|
+
| `createComment` | yes | `(body: string) => Promise<Comment>` | Posts a new comment to PR |
|
|
80
|
+
| `updateComment` | yes | `(id: number, body: string) => Promise<Comment>` | Updates existing PR comment |
|
|
81
|
+
| `listComments` | yes | `() => Promise<Comment[]>` | Fetches all comments from PR |
|
|
82
|
+
| `maxCommentChars` | yes | `number` | Character limit for comment body |
|
|
83
|
+
| `downloadReportArtifact` | no | `() => Promise<string \| null>` | Fetches previous report for base branch (returns path to downloaded `report.json`), used as cache to speed up comparison |
|
|
84
|
+
|
|
85
|
+
A `Comment` object has the following required properties:
|
|
86
|
+
|
|
87
|
+
| Property | Type | Description |
|
|
88
|
+
| :------- | :------- | :------------------------------------ |
|
|
89
|
+
| `id` | `number` | Comment ID |
|
|
90
|
+
| `body` | `string` | Content of comment as Markdown string |
|
|
91
|
+
| `url` | `string` | Web link to comment in PR |
|
|
92
|
+
|
|
93
|
+
### Options
|
|
94
|
+
|
|
95
|
+
Optionally, you can override default options for further customization:
|
|
96
|
+
|
|
97
|
+
| Property | Type | Default | Description |
|
|
98
|
+
| :---------------- | :------------------------ | :------------------------------- | :-------------------------------------------------------------------------------- |
|
|
99
|
+
| `monorepo` | `boolean \| MonorepoTool` | `false` | Enables [monorepo mode](#monorepo-mode) |
|
|
100
|
+
| `projects` | `string[] \| null` | `null` | Custom projects configuration for [monorepo mode](#monorepo-mode) |
|
|
101
|
+
| `task` | `string` | `'code-pushup'` | Name of command to run Code PushUp per project in [monorepo mode](#monorepo-mode) |
|
|
102
|
+
| `directory` | `string` | `process.cwd()` | Directory in which Code PushUp CLI should run |
|
|
103
|
+
| `config` | `string \| null` | `null` [^1] | Path to config file (`--config` option) |
|
|
104
|
+
| `silent` | `boolean` | `false` | Toggles if logs from CLI commands are printed |
|
|
105
|
+
| `bin` | `string` | `'npx --no-install code-pushup'` | Command for executing Code PushUp CLI |
|
|
106
|
+
| `detectNewIssues` | `boolean` | `true` | Toggles if new issues should be detected and returned in `newIssues` property |
|
|
107
|
+
| `logger` | `Logger` | `console` | Logger for reporting progress and encountered problems |
|
|
108
|
+
|
|
109
|
+
[^1]: By default, the `code-pushup.config` file is autodetected as described in [`@code-pushup/cli` docs](../cli/README.md#configuration).
|
|
110
|
+
|
|
111
|
+
The `Logger` object has the following required properties:
|
|
112
|
+
|
|
113
|
+
| Property | Type | Description |
|
|
114
|
+
| :------- | :-------------------------- | :----------------- |
|
|
115
|
+
| `error` | `(message: string) => void` | Prints error log |
|
|
116
|
+
| `warn` | `(message: string) => void` | Prints warning log |
|
|
117
|
+
| `info` | `(message: string) => void` | Prints info log |
|
|
118
|
+
| `debug` | `(message: string) => void` | Prints debug log |
|
|
119
|
+
|
|
120
|
+
## Standalone mode
|
|
121
|
+
|
|
122
|
+
By default, it is assumed that Code PushUp is set up to run on the whole repo with one command (_standalone mode_).
|
|
123
|
+
If you want to run Code PushUp on multiple projects separately, you should enable [_monorepo mode_](#monorepo-mode).
|
|
124
|
+
|
|
125
|
+
### Standalone result
|
|
126
|
+
|
|
127
|
+
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.
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
const result = await runInCI(refs, api);
|
|
131
|
+
|
|
132
|
+
if (result.mode === 'standalone') {
|
|
133
|
+
const {
|
|
134
|
+
// output files, can be uploaded as job artifact
|
|
135
|
+
artifacts: { report, diff },
|
|
136
|
+
// ID of created/updated PR comment
|
|
137
|
+
commentId,
|
|
138
|
+
// array of source code issues, can be used to annotate changed files in PR
|
|
139
|
+
newIssues,
|
|
140
|
+
} = result;
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Monorepo mode
|
|
145
|
+
|
|
146
|
+
For monorepo setups, Code PushUp reports can be collected and compared
|
|
147
|
+
individually per project. All project comparisons are then combined into a
|
|
148
|
+
single PR comment.
|
|
149
|
+
|
|
150
|
+
Use the `monorepo` option to activate monorepo mode:
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
await runInCI(refs, api, {
|
|
154
|
+
monorepo: true,
|
|
155
|
+
});
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
The `runInCI` function will try to detect which monorepo tool you're using from the file system.
|
|
159
|
+
The following tools are supported out of the box:
|
|
160
|
+
|
|
161
|
+
- [Nx](https://nx.dev/)
|
|
162
|
+
- [Turborepo](https://turbo.build/)
|
|
163
|
+
- [Yarn workspaces](https://classic.yarnpkg.com/lang/en/docs/workspaces/)
|
|
164
|
+
- [pnpm workspace](https://pnpm.io/workspaces)
|
|
165
|
+
- [npm workspaces](https://docs.npmjs.com/cli/using-npm/workspaces)
|
|
166
|
+
|
|
167
|
+
If you're using one of these tools, you can also skip auto-detection by setting
|
|
168
|
+
`monorepo` option to `'nx'`, `'turbo'`, `'yarn'`, `'pnpm'` or `'npm'`.
|
|
169
|
+
|
|
170
|
+
If none of these tools are detected, then the fallback is to run Code PushUp in
|
|
171
|
+
all folders which have a `package.json` file. If that's not what you want, then
|
|
172
|
+
you can also configure folder patterns using the `projects` option (supports globs):
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
await runInCI(refs, api, {
|
|
176
|
+
monorepo: true,
|
|
177
|
+
projects: ['frontend', 'backend/*'],
|
|
178
|
+
});
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Based on which monorepo tool is used, Code PushUp CLI commands will be executed
|
|
182
|
+
using a `package.json` script, Nx target, Turbo task, or binary executable (as
|
|
183
|
+
fallback). By default, these are expected to be called `code-pushup`, but you
|
|
184
|
+
can override the name using the `task` option:
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
await runInCI(refs, api, {
|
|
188
|
+
monorepo: 'nx',
|
|
189
|
+
task: 'analyze', // custom Nx target
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Monorepo result
|
|
194
|
+
|
|
195
|
+
In monorepo mode, the resolved object includes the merged diff at the top-level, as well as a list of projects.
|
|
196
|
+
Each project has its own report files and issues.
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
const result = await runInCI(refs, api);
|
|
200
|
+
|
|
201
|
+
if (result.mode === 'monorepo') {
|
|
202
|
+
const {
|
|
203
|
+
// array of objects with result for each project
|
|
204
|
+
projects,
|
|
205
|
+
// ID of created/updated PR comment
|
|
206
|
+
commentId,
|
|
207
|
+
// merged report-diff.md used in PR comment, can also be uploaded as job artifact
|
|
208
|
+
diffArtifact,
|
|
209
|
+
} = result;
|
|
210
|
+
|
|
211
|
+
for (const project of projects) {
|
|
212
|
+
const {
|
|
213
|
+
// detected project name (from package.json, project.json or folder name)
|
|
214
|
+
name,
|
|
215
|
+
// output files, can be uploaded as job artifacts
|
|
216
|
+
artifacts: { report, diff },
|
|
217
|
+
// array of source code issues, can be used to annotate changed files in PR
|
|
218
|
+
newIssues,
|
|
219
|
+
} = project;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
```
|