Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup contributing copy and streamline repo styling #762

Merged
merged 10 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
22 changes: 21 additions & 1 deletion .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,27 @@ plugins:
ref: v1.0.4

lint:
# enabled linters inherited from github.com/trunk-io/configs plugin
files:
- name: plugin.yaml
filenames: [plugin.yaml]
definitions:
- name: definition-checker
description: Checks plugin.yaml files for repo best practices
files: [plugin.yaml]
runtime: node
extra_packages:
- ts-node
- yaml
commands:
- name: check
run: ts-node ${workspace}/repo-tools/definition-checker/check.ts ${target}
batch: true
output: regex
parse_regex: "((?P<path>.*) \\[(?P<severity>.*)\\]: (?P<message>.*) \\((?P<code>.*)\\))"
success_codes: [0]
enabled:
# enabled linters inherited from github.com/trunk-io/configs plugin
- definition-checker
disabled:
- pylint # pylint diagnostics are too strict
- semgrep
Expand Down
159 changes: 84 additions & 75 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
# Contribution

Thanks for contributing to trunk's default plugins! Read on to learn more.
Thanks for contributing to Trunk's default plugins! Read on to learn more.

- [Prerequisites](#prerequisites)
- [Overview](#overview)
- [Release process](#releases)
- [Adding new linters](#linters)
- [Adding new actions](#actions)
- [Adding new tools](#tools)
- [Adding new actions](#actions)
- [Release process](#releases)
- [Guidelines](#guidelines)
- [Docs](https://docs.trunk.io)

## Prerequisites

1. Please [install the trunk CLI](https://docs.trunk.io/check/usage#install-the-cli)
2. Run `trunk check` in this repo to quickstart git-hooks and codegen

## Overview

We use this repository to provide our users with default linters, actions, and tools. Trunk
We use this repository to provide our users with default linters, tools, and actions. Trunk
automatically adds the following to users' trunk.yaml:

```yaml
Expand All @@ -32,43 +38,24 @@ plugins:
local: </path/to/repo/root>
```

Adding a plugin source lets users run `trunk check enable` or `trunk actions enable` with linters
and actions defined in that plugin. For more information, see our
Adding a plugin source lets users run `trunk check enable`, `trunk tools enable`, or
`trunk actions enable` with definitions in that plugin. For more information, see our
[docs](https://docs.trunk.io/docs/plugins).

**Please review our [Testing Guide](tests/README.md) for info on writing and running tests.**

If you have questions, please [stop by our community Slack](https://slack.trunk.io/), and if you
have a feature request, you can [file it here](https://features.trunk.io/).

## Releases

`trunk-io/plugins` is released on a fairly regular cadence that is independent of PRs. Users will
pick up these configuration changes by running `trunk upgrade` to automatically update their plugin
version to the latest release.

```yaml
plugins:
sources:
- id: trunk
uri: https://github.com/trunk-io/plugins
ref: v1.2.5 # will change to the latest release on next `trunk upgrade`
```

We recommend only setting the above `ref` field to be our released tags, but if you need a linter or
action that hasn't been released yet, you can set `ref` to be a git SHA. Do **not** set `ref` to be
a branch name, or `HEAD`, as users will observe buggy behavior.

Note that the ref and the cli version in the trunk.yaml must be compatible. This is managed by
`required_trunk_version`, as specified in [`plugin.yaml`](plugin.yaml). Users will not be able to
load a plugin source until they have upgraded to a compliant CLI version.

## Linters

To add a new linter:

1. Run `trunk check` to start up Trunk in the background.
2. Create a directory inside `linters/` with the name of your new linter.
3. Inside this new directory, create the following structure. Most of these files will be
automatically created for you:
2. Run `mkdir linters/<my-linter>` to start. This should autopopulate with a sample

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have to run this before check finishes? I think this is the part I missed 😆

Copy link
Collaborator Author

@TylerJang27 TylerJang27 May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really trunk check here is just a shorthand for launching our daemon, which listens for file changes in order to run Trunk Actions. trunk check should finish almost immediately if you're starting from a fresh checkout.

Running mkdir after that (>=1-2 seconds after the trunk check command starts) should be sufficient. But if you ran something like code linters/vale/plugin.yaml, the script won't autogen since it only checks empty directories :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have trunk daemon start that just stats the daemon.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer having trunk check here in the documentation just to normalize standard usage

[plugin.yaml](./repo-tools/linter-test-helper/linter_sample_plugin.yaml) and
[test file](./repo-tools/linter-test-helper/linter_sample.test.ts). If necessary, add them
yourself:

```text
linters/
Expand All @@ -78,25 +65,57 @@ To add a new linter:
│ README.md (optional)
│ my-config.json (optional)
└─test_data/
└─basic.in.py (with appropriate extension)
└─basic.in.foo (with appropriate extension)
```

4. Add your linter definition to `plugin.yaml` (consult the docs for [custom linters] and [custom
3. Add your linter definition to `plugin.yaml` (consult the docs for [custom linters] and [custom
parsers] to understand how it should be defined). Most linters in this repository are defined as
tools as well, so that they can be easily run manually from the command line.
5. Making sure the plugin in [`.trunk/trunk.yaml`](.trunk/trunk.yaml) is pointing to your local
repository, run `trunk check enable <my-linter>` to enable your linter, and run `trunk check` to
verify that the configuration is valid and that you get desired diagnostics. Running
`trunk check --verbose` can help provide greater insights when debugging.
6. Add a few simple test cases to `my_linter.test.ts` to exercise your linter and generate
snapshots. Refer to [Testing Guidelines](tests/README.md) for more information on writing and
running tests.
7. Run `trunk check` to lint your changes.
8. Open a PR!
4. Run `trunk check enable <my-linter>` to enable your linter, and run `trunk check` to verify that
the configuration is valid and that you get desired diagnostics. Running `trunk check --verbose`
can help provide greater insights when debugging. You may also wish to run on your test data,
i.e. `trunk check --verbose --force linters/<my-linter>/test_data/basic.in.foo`.
5. Add a few simple test cases to `my_linter.test.ts` to exercise your linter and generate
snapshots. View our [Testing Guide](tests/README.md) for more information on writing, running,
and debugging tests.
6. Revert any `.trunk/trunk.yaml` changes, and run `trunk check` to lint your changes.
7. Open a PR!

[custom linters]: https://docs.trunk.io/check/custom-linters
[custom parsers]: https://docs.trunk.io/check/custom-parsers

## Tools

If the tool you intend to add functions primarily as a linter, please follow the instruction in
[linters](#linters). If it functions more as a standalone tool, please add it in the `tools/`
directory and follow the instructions below.

To add a new tool:

1. Run `trunk check` to start up Trunk in the background.
2. Run `mkdir tools/<my-tool>` to start. This should autopopulate with a sample
[plugin.yaml](./repo-tools/tool-test-helper/tool_sample_plugin.yaml) and
[test file](./repo-tools/tool-test-helper/tool_sample.test.ts). If necessary, add them yourself:

```text
tests/
└─my-tool/
│ plugin.yaml
│ my_tool.test.ts
└─README.md (optional)
```

3. Add your tool definition to `plugin.yaml` (consult the docs for
[custom tools](https://docs.trunk.io/tools/configuration#tool-definitions) to understand how it
should be defined).
4. Run `trunk tools enable <my-tool>` to enable your tool, and run its shim(s) from
`.trunk/tools/<tool-name>`.
5. Add a `toolInstallTest` to `my_tool.test.ts` to verify your tool's installation. If neccessary,
use `toolTest` instead. Refer to the [Testing Guide](tests/README.md) for more information on
writing, running, and debugging tests.
6. Revert any `.trunk/trunk.yaml` changes, and run `trunk check` to lint your changes.
7. Open a PR!

## Actions

To add a new action:
Expand All @@ -118,54 +137,44 @@ To add a new action:
`trunk actions enable <my-action>` to enable your linter, and run `trunk run <my-action>` to
verify that the configuration is valid and that you get desired results. Running
`trunk actions history <my-action>` can help provide greater insights when debugging.
6. We have not yet defined a testing framework for plugin actions, but we are working to add one
soon! Please briefly document your action in its README.md and in your PR.
6. Please briefly document your action in its README.md as appropriate.
7. Run `trunk check` to lint your changes.
8. Open a PR!

[actions]: https://docs.trunk.io/actions
Testing for Trunk Actions in this repo is in early development, so we do not require testing for new
action definitions.

## Tools
[actions]: https://docs.trunk.io/actions

If the tool you intend to add functions primarily as a linter, please follow the instruction in
[linters](#linters). If it functions more as a standalone tool, please add it in the `tools/`
directory and follow the instructions below.
## Releases

To add a new tool:
`trunk-io/plugins` is released on every few weeks. Users will pick up these configuration changes by
running `trunk upgrade` to automatically update their plugin version to the latest release.

1. Run `trunk check` to start up Trunk in the background.
2. Create a directory inside `tools/` with the name of your new linter.
3. Inside this new directory, create the following structure. Most of these files will be
automatically created for you:
```yaml
plugins:
sources:
- id: trunk
uri: https://github.com/trunk-io/plugins
ref: v1.2.5 # will change to the latest release on next `trunk upgrade`
```

```text
tests/
└─my-tool/
│ plugin.yaml
│ my_tool.test.ts
└─README.md (optional)
```
We recommend only setting the above `ref` field to be our released tags, but if you need a
linter/tool/action that hasn't been released yet, you can set `ref` to be a git SHA. Do **not** set
`ref` to be a branch name, as plugin branches are not refreshed.

4. Add your tool definition to `plugin.yaml` (consult the docs for
[custom tools](https://docs.trunk.io/tools/configuration#tool-definitions) to understand how it
should be defined).
5. Making sure the plugin in [`.trunk/trunk.yaml`](.trunk/trunk.yaml) is pointing to your local
repository, run `trunk tools enable <my-tool>` to enable your tool, and access its shim(s) to run
it from `.trunk/tools/<tool-name>`.
6. Add a `toolInstallTest` to `my_tool.test.ts` to verify your tool's installation. If neccessary,
use `toolTest` instead. Refer to [Testing Guidelines](tests/README.md) for more information on
writing and running tests.
7. Run `trunk check` to lint your changes.
8. Open a PR!
Note that the ref and the cli version in the trunk.yaml must be compatible. This is managed by
`required_trunk_version`, as specified in [`plugin.yaml`](plugin.yaml). Users will not be able to
load a plugin source until they have upgraded to a compliant CLI version.

## Guidelines

Please follow the guidelines below when contributing:

- After defining a new linter or action, please add it to [`README.md`](README.md).
- If you run into any problems while defining new linters or actions, feel free to reach out on our
[Slack](https://slack.trunk.io/). We are continuously working to improve the process of
integrating with trunk, and all feedback is appreciated!
- If you run into any problems while defining new linters/tools/actions, feel free to reach out on
our [Slack](https://slack.trunk.io/). We are continuously working to improve the process of
integrating with Trunk, and all feedback is appreciated!

## Development

Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@
### Welcome

This repository is the official [Trunk.io](https://trunk.io/) repo containing Trunk's integrations
for linters, formatters, security tools, githooks, and default configs. By default, all trunk users
for linters, formatters, security tools, githooks, and default configs. By default, all Trunk users
import this repo as a plugin, via this snippet in `.trunk/trunk.yaml`:

```yaml
plugins:
sources:
- id: trunk
uri: https://github.com/trunk-io/plugins
ref: v1.4.4
ref: v1.5.0
```

This repo is open to contributions! See our
[contribution guidelines](https://github.com/trunk-io/plugins/blob/main/CONTRIBUTING.md) and join
our [slack community][slack] for help. **If you're adding new tools, please see our
This repo is open to contributions! See our [contribution guidelines](CONTRIBUTING.md) and join our
[slack community][slack] for help. **If you're adding new tools, please see our
[testing guide](tests/README.md) as well!**

### Supported Linters, Formatters, and Security Tools
Expand Down
3 changes: 2 additions & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"preset": "ts-jest",
"modulePaths": ["<rootDir>"],
"setupFilesAfterEnv": ["<rootDir>/tests/jest_setup.ts"],
"reporters": ["<rootDir>/tests/reporter/index.js", "default"]
"reporters": ["<rootDir>/tests/reporter/index.js", "default"],
"testPathIgnorePatterns": ["<rootDir>/node_modules/", "<rootDir>/repo-tools/"]
}
2 changes: 1 addition & 1 deletion linters/pragma-once/pragma_once.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { linterFmtTest } from "tests";

// TODO(Tyler): We will eventually need to add a couple more test cases involving failure modes and other autofixes.
// A simple formatter test to run 'pragma-once' on 'test_data/basic.in.hh'
linterFmtTest({ linterName: "pragma-once" });
1 change: 1 addition & 0 deletions linters/ruff/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ lint:
run: ruff --version

- name: ruff-nbqa
description: A Python linter for Jupyter notebooks
files: [jupyter]
commands:
- name: lint
Expand Down
1 change: 1 addition & 0 deletions linters/sort-package-json/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ lint:

definitions:
- name: sort-package-json
description: Sorts package.json keys
files: [package-json]
commands:
- name: format
Expand Down
5 changes: 3 additions & 2 deletions linters/sqlfluff/sqlfluff.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { linterCheckTest, linterFmtTest, TestCallback } from "tests";

// A simple test to run 'sqlfluff lint'
// basic_check.out.json is the result of running:
// trunk check linters/sqlfluff/test/basic_check.in.sql --force --filter=sqlfluff --output=json
linterCheckTest({ linterName: "sqlfluff", namedTestPrefixes: ["basic_check"] });
Expand All @@ -11,10 +12,10 @@ const fmtCallbacks: TestCallback = (driver) => {
const sqlfluffRegex = /- sqlfluff@(.+)\n/;
const newContents = currentContents.replace(
sqlfluffRegex,
"- sqlfluff@$1:\n commands: [lint, fix]\n"
"- sqlfluff@$1:\n commands: [lint, fix]\n",
);
driver.writeFile(trunkYamlPath, newContents);
};

// TODO(Tyler): We will eventually need to add a couple more test cases involving failure modes.
// An additional test to run 'sqlfluff fmt' with some additional test setup.
linterFmtTest({ linterName: "sqlfluff", namedTestPrefixes: ["basic_fmt"], preCheck: fmtCallbacks });
1 change: 1 addition & 0 deletions linters/trufflehog/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ lint:

# Variant of trufflehog that scans git history.
- name: trufflehog-git
description: Scan git history for secrets
files: [ALL]
download: trufflehog
known_good_version: 3.59.0
Expand Down
Loading
Loading