Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/cool-places-dig.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---

---



40 changes: 19 additions & 21 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

title: ""
labels: ""
assignees: ""
---

**Describe the bug**
A clear and concise description of what the bug is.
**Describe the bug** A clear and concise description of what the bug is.

**To Reproduce** Steps to reproduce the behavior:

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.
**Expected behavior** A clear and concise description of what you expected to
happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.
**Screenshots** If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]

**Additional context**
Add any other context about the problem here.
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]

**Additional context** Add any other context about the problem here.
23 changes: 11 additions & 12 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''

title: ""
labels: ""
assignees: ""
---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
**Is your feature request related to a problem? Please describe.** A clear and
concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.
**Describe the solution you'd like** A clear and concise description of what you
want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.
**Describe alternatives you've considered** A clear and concise description of
any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
**Additional context** Add any other context or screenshots about the feature
request here.
3 changes: 2 additions & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Choose the right checklist for the change that you're making:

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Tests added
- [ ] Documentation added
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<div align="center">

# Danger

</div>

<div align="center">

A monorepo for the core libraries of Danger client.

</div>

<hr />

## Packages

- [@tapsioss/danger (core)](https://github.com/Tap30/danger/blob/main/packages/core/README.md)

## Contributing

Read the
[contributing guide](https://github.com/Tap30/danger/blob/main/CONTRIBUTING.md)
to learn about our development process, how to propose bug fixes and
improvements, and how to build and test your changes.

## License

This project is licensed under the terms of the
[MIT license](https://github.com/Tap30/danger/blob/main/LICENSE).
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@repo/root",
"version": "0.0.0",
"description": "danger client monorepo",
"description": "A monorepo for the core libraries of Danger client.",
"type": "module",
"packageManager": "pnpm@9.15.0",
"scripts": {
Expand Down Expand Up @@ -65,7 +65,13 @@
"output": []
}
},
"keywords": [],
"keywords": [
"ci",
"cd",
"review-tools",
"danger-plugin",
"danger-js"
],
"license": "MIT",
"devDependencies": {
"@changesets/changelog-github": "^0.5.1",
Expand Down
116 changes: 68 additions & 48 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
An extensible framework for integrating [Danger.js](https://danger.systems/js/)
declaratively.

</div\>
</div>

<hr /\>
<hr />

## Installation

Expand All @@ -20,40 +20,43 @@ pnpm add @tapsioss/danger
## Features

- **Plugin-based Architecture**: Add custom validation logic using plugins.
- **Centralized Data Store**: Share arbitrary data across plugins during a Danger run.
- **Streamlined Dangerfile Creation**: Simplify your `dangerfile.ts` with a client-based approach.
- **Centralized Data Store**: Share arbitrary data across plugins during a
Danger run.
- **Streamlined Dangerfile Creation**: Simplify your `dangerfile.ts` with a
client-based approach.

### Client Fields

| Field Name | Type | Description |
| :---------------- | :------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------- |
| `gitlab` | [`GitLabDSL`](https://www.google.com/search?q=%5Bhttps://github.com/danger/danger-js/blob/main/source/dsl/GitLabDSL.ts%5D\(https://github.com/danger/danger-js/blob/main/source/dsl/GitLabDSL.ts\)) | GitLab-specific information for the current merge request. |
| `github` | [`GitHubDSL`](https://www.google.com/search?q=%5Bhttps://github.com/danger/danger-js/blob/main/source/dsl/GitHubDSL.ts%5D\(https://github.com/danger/danger-js/blob/main/source/dsl/GitHubDSL.ts\)) | GitHub-specific information for the current pull request. |
| `git` | [`GitDSL`](https://www.google.com/search?q=%5Bhttps://github.com/danger/danger-js/blob/main/source/dsl/GitDSL.ts%5D\(https://github.com/danger/danger-js/blob/main/source/dsl/GitDSL.ts\)) | Git-related information such as modified, created, and deleted files. |
| `results` | [`DangerResults`](https://www.google.com/search?q=%5Bhttps://github.com/danger/danger-js/blob/main/source/dsl/DangerResults.ts%5D\(https://github.com/danger/danger-js/blob/main/source/dsl/DangerResults.ts\)) | The shared Danger results object used to store warnings, fails, messages, etc. |
| `data` | `Record<PropertyKey, unknown>` | Arbitrary data storage shared across plugins for this Danger run. |
| `modifiedFiles` | `string[]` | List of files that have been modified in the current merge request. |
| `createdFiles` | `string[]` | List of files that have been newly created in the current merge request. |
| `deletedFiles` | `string[]` | List of files that have been deleted in the current merge request. |
| Field Name | Type | Description |
| :-------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------- |
| `gitlab` | [`GitLabDSL`](<https://www.google.com/search?q=%5Bhttps://github.com/danger/danger-js/blob/main/source/dsl/GitLabDSL.ts%5D(https://github.com/danger/danger-js/blob/main/source/dsl/GitLabDSL.ts)>) | GitLab-specific information for the current merge request. |
| `github` | [`GitHubDSL`](<https://www.google.com/search?q=%5Bhttps://github.com/danger/danger-js/blob/main/source/dsl/GitHubDSL.ts%5D(https://github.com/danger/danger-js/blob/main/source/dsl/GitHubDSL.ts)>) | GitHub-specific information for the current pull request. |
| `git` | [`GitDSL`](<https://www.google.com/search?q=%5Bhttps://github.com/danger/danger-js/blob/main/source/dsl/GitDSL.ts%5D(https://github.com/danger/danger-js/blob/main/source/dsl/GitDSL.ts)>) | Git-related information such as modified, created, and deleted files. |
| `results` | [`DangerResults`](<https://www.google.com/search?q=%5Bhttps://github.com/danger/danger-js/blob/main/source/dsl/DangerResults.ts%5D(https://github.com/danger/danger-js/blob/main/source/dsl/DangerResults.ts)>) | The shared Danger results object used to store warnings, fails, messages, etc. |
| `data` | `Record<PropertyKey, unknown>` | Arbitrary data storage shared across plugins for this Danger run. |
| `modifiedFiles` | `string[]` | List of files that have been modified in the current merge request. |
| `createdFiles` | `string[]` | List of files that have been newly created in the current merge request. |
| `deletedFiles` | `string[]` | List of files that have been deleted in the current merge request. |

### Client Methods

| Method Name | Type | Description |
| :---------- | :--------------------------------------------------------------------------- | :--------------------------------------------------------- |
| `warn` | `(msg: string, file?: string, line?: number) => void` | Function to mark a warning in the Danger output. |
| `fail` | `(msg: string, file?: string, line?: number) => void` | Function to mark a failure in the Danger output. |
| `message` | `(msg: string, file?: string, line?: number) => void` | Function to add a general message to the Danger report. |
| `markdown` | `(msg: string, file?: string, line?: number) => void` | Function to add markdown output to the Danger report. |
| `use` | `<Option>(plugin: PluginRuntime<Option>, options?: Option) => this` | Registers a plugin to the DangerClient. |
| `analyze` | `() => Promise<DangerResults>` | Analyzes the merge request by executing all registered plugins. |
| Method Name | Type | Description |
| :---------- | :------------------------------------------------------------------ | :-------------------------------------------------------------- |
| `warn` | `(msg: string, file?: string, line?: number) => void` | Function to mark a warning in the Danger output. |
| `fail` | `(msg: string, file?: string, line?: number) => void` | Function to mark a failure in the Danger output. |
| `message` | `(msg: string, file?: string, line?: number) => void` | Function to add a general message to the Danger report. |
| `markdown` | `(msg: string, file?: string, line?: number) => void` | Function to add markdown output to the Danger report. |
| `use` | `<Option>(plugin: PluginRuntime<Option>, options?: Option) => this` | Registers a plugin to the DangerClient. |
| `analyze` | `() => Promise<DangerResults>` | Analyzes the merge request by executing all registered plugins. |

### Registering Plugins

You can add plugins using the `use` method on the `DangerClient` instance.

#### Defining Custom Plugins

The `createPlugin` helper ensures a consistent structure and typing for your Danger plugins.
The `createPlugin` helper ensures a consistent structure and typing for your
Danger plugins.

```typescript
import { createPlugin } from "@tapsioss/danger";
Expand All @@ -65,23 +68,30 @@ import { createPlugin } from "@tapsioss/danger";
* // Registering the plugin:
* // dangerClient.use(mergeRequestSizePlugin, { size: 100 });
*/
export const mergeRequestSizePlugin = createPlugin<{ size?: number }>((client, options) => {
const threshold = options?.size || 200;
const changedFiles = [...client.git.modified_files, ...client.git.created_files];
if (changedFiles.length > threshold) {
client.warn(
`The merge request is too big (${changedFiles.length} files changed). Consider breaking it into multiple merge requests.`,
);
}
});
export const mergeRequestSizePlugin = createPlugin<{ size?: number }>(
(client, options) => {
const threshold = options?.size || 200;
const changedFiles = [
...client.git.modified_files,
...client.git.created_files,
];
if (changedFiles.length > threshold) {
client.warn(
`The merge request is too big (${changedFiles.length} files changed). Consider breaking it into multiple merge requests.`,
);
}
},
);
```

> [!TIP]
> Use the `createPlugin` helper for strongly typed plugins and `PluginRuntime` type (exported from `@tapsioss/danger`) for inline plugin definitions if you prefer.
> [!TIP] Use the `createPlugin` helper for strongly typed plugins and
> `PluginRuntime` type (exported from `@tapsioss/danger`) for inline plugin
> definitions if you prefer.

### Analysis

After adding plugins to the client, you can run the plugins sequentially using the `analyze` method.
After adding plugins to the client, you can run the plugins sequentially using
the `analyze` method.

```ts
const dangerClient = new DangerClient(danger);
Expand All @@ -105,15 +115,15 @@ if (results.fails.length > 0) {

### Install `danger`

You need to import some variables and functions from the `danger` package to pass
them to our `DangerClient` class.
You need to import some variables and functions from the `danger` package to
pass them to our `DangerClient` class.

```bash
pnpm add danger
```

> [!WARNING]
> Use Danger.js version `11.3.1` if your Node.js version is less than `16`.
> [!WARNING] Use Danger.js version `11.3.1` if your Node.js version is less than
> `16`.

### Create the `dangerfile.ts`

Expand Down Expand Up @@ -155,30 +165,40 @@ import { DangerClient } from "@tapsioss/danger";
dangerClient.markdown(
"The following issues must be resolved before this merge request can be merged:",
);
results.fails.forEach((f) => dangerClient.markdown(`- ${f.message}`));
results.fails.forEach(f => dangerClient.markdown(`- ${f.message}`));
dangerClient.markdown("---");
}
})();
```

## Setting Up the CI

Danger needs specific environment variables in your CI/CD pipeline to work properly.
Danger needs specific environment variables in your CI/CD pipeline to work
properly.

### GitHub

- `DANGER_GITHUB_API_TOKEN`: Create a GitHub Personal Access Token with appropriate repository permissions (e.g., `repo` scope for private repositories) and save it as `DANGER_GITHUB_API_TOKEN` in your repository secrets.
- `DANGER_GITHUB_API_TOKEN`: Create a GitHub Personal Access Token with
appropriate repository permissions (e.g., `repo` scope for private
repositories) and save it as `DANGER_GITHUB_API_TOKEN` in your repository
secrets.

### GitLab

Danger requires the following variables in your CI to work properly:

- `DANGER_GITLAB_API_TOKEN`: Create a GitLab Personal Access Token with `api` scope and `Developer` role (or higher) and save it as `DANGER_GITLAB_API_TOKEN` in your repository/group CI/CD variables.
- `DANGER_GITLAB_HOST`: The host of your GitLab instance, for example: `https://gitlab.com` or `https://your-on-premise-gitlab.com`.
- `DANGER_GITLAB_API_BASE_URL`: The base URL of GitLab APIs, for example: `https://gitlab.com/api/v4` or `https://your-on-premise-gitlab.com/api/v4`.

Some other [predefined CI/CD variables](https://docs.gitlab.com/ci/variables/predefined_variables/)
are also required for setting up Danger in CI. Ensure these are available to your Danger job:
- `DANGER_GITLAB_API_TOKEN`: Create a GitLab Personal Access Token with `api`
scope and `Developer` role (or higher) and save it as
`DANGER_GITLAB_API_TOKEN` in your repository/group CI/CD variables.
- `DANGER_GITLAB_HOST`: The host of your GitLab instance, for example:
`https://gitlab.com` or `https://your-on-premise-gitlab.com`.
- `DANGER_GITLAB_API_BASE_URL`: The base URL of GitLab APIs, for example:
`https://gitlab.com/api/v4` or `https://your-on-premise-gitlab.com/api/v4`.

Some other
[predefined CI/CD variables](https://docs.gitlab.com/ci/variables/predefined_variables/)
are also required for setting up Danger in CI. Ensure these are available to
your Danger job:

- `CI_MERGE_REQUEST_IID`
- `CI_PROJECT_PATH`
Expand Down
13 changes: 12 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"type": "module",
"files": [
"./dist",
"./README.md",
"./LICENSE"
],
"scripts": {
"clear": "shx rm -rf dist",
"prebuild": "pnpm run clear",
Expand All @@ -30,7 +35,13 @@
]
}
},
"keywords": [],
"keywords": [
"ci",
"cd",
"review-tools",
"danger-plugin",
"danger-js"
],
"license": "MIT",
"peerDependencies": {
"danger": "^13.0.4"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/create-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ describe("createPlugin", () => {
// Assert that the function returned by createPlugin is strictly the same
// as the function that was passed in. This covers the 'return pluginFn;' line.
expect(resultPlugin).toBe(mockPluginFn);
expect(typeof resultPlugin).toBeInstanceOf("function"); // Ensures it's still a function
expect(resultPlugin).toBeInstanceOf(Function);
});
});