Skip to content

Commit 0ca3037

Browse files
committed
overhaul docs
- inputs and outputs are in a dedicated page - README and docs landing page auto-sync with MD substitutions - permissions are noted where needed (per feature) - adds pr-review-caveats doc - add examples recipes (resolves #195) - demo folder moved to docs/examples and corresponding CI adjusted
1 parent 7b9c90d commit 0ca3037

20 files changed

+875
-196
lines changed

.github/workflows/cpp-linter.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ name: cpp-linter
22

33
on:
44
push:
5-
paths-ignore: "docs/**"
5+
paths:
6+
- "!**"
7+
- "docs/examples/demo/*"
68
pull_request:
7-
paths-ignore: "docs/**"
9+
paths:
10+
- "!**"
11+
- "docs/examples/demo/*"
12+
813

914
jobs:
1015
cpp-linter:

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ repos:
66
- id: end-of-file-fixer
77
- id: check-added-large-files
88
- id: check-yaml
9+
# special mkdocs config to include inline icons fails (see `pymdownx.emoji` in mkdocs.yml)
10+
args: ['--unsafe'] # use `--unsafe` to workaround yaml loading
911
- id: requirements-txt-fixer

README.md

Lines changed: 48 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
<!--intro-start-->
1+
[file-annotations]: https://cpp-linter.github.io/cpp-linter-action/inputs-outputs/#file-annotations
2+
[thread-comments]: https://cpp-linter.github.io/cpp-linter-action/inputs-outputs/#thread-comments
3+
[step-summary]: https://cpp-linter.github.io/cpp-linter-action/inputs-outputs/#step-summary
4+
[tidy-review]: https://cpp-linter.github.io/cpp-linter-action/inputs-outputs/#tidy-review
5+
[format-review]: https://cpp-linter.github.io/cpp-linter-action/inputs-outputs/#format-review
6+
7+
[io-doc]: https://cpp-linter.github.io/cpp-linter-action/inputs-outputs
8+
[recipes-doc]: https://cpp-linter.github.io/cpp-linter-action/examples
9+
10+
[format-annotations-preview]: https://raw.githubusercontent.com/cpp-linter/cpp-linter-action/main/docs/images/annotations-clang-format.png
11+
[tidy-annotations-preview]: https://raw.githubusercontent.com/cpp-linter/cpp-linter-action/main/docs/images/annotations-clang-tidy.png
12+
[thread-comment-preview]: https://raw.githubusercontent.com/cpp-linter/cpp-linter-action/main/docs/images/comment.png
13+
[step-summary-preview]: https://raw.githubusercontent.com/cpp-linter/cpp-linter-action/main/docs/images/step-summary.png
14+
[tidy-review-preview]: https://raw.githubusercontent.com/cpp-linter/cpp-linter-action/main/docs/images/tidy-review.png
15+
[format-review-preview]: https://raw.githubusercontent.com/cpp-linter/cpp-linter-action/main/docs/images/format-review.png
16+
[format-suggestion-preview]: https://raw.githubusercontent.com/cpp-linter/cpp-linter-action/main/docs/images/format-suggestion.png
17+
18+
<!--README-start-->
219

320
# C/C++ Linter Action <sub><sup>| clang-format & clang-tidy</sup></sub>
421

@@ -8,7 +25,11 @@
825
[![MkDocs Deploy](https://github.com/cpp-linter/cpp-linter-action/actions/workflows/mkdocs-deploy.yml/badge.svg)](https://github.com/cpp-linter/cpp-linter-action/actions/workflows/mkdocs-deploy.yml)
926
![GitHub](https://img.shields.io/github/license/cpp-linter/cpp-linter-action?label=license&logo=github)
1027

11-
A Github Action for linting C/C++ code integrating clang-tidy and clang-format to collect feedback provided in the form of [`file-annotations`](#file-annotations), [`thread-comments`](#thread-comments), workflow [`step-summary`](#step-summary), and Pull Request reviews (with [`tidy-review`](#tidy-review) or [`format-review`](#format-review)).
28+
A Github Action for linting C/C++ code integrating clang-tidy and clang-format
29+
to collect feedback provided in the form of
30+
[`file-annotations`][file-annotations], [`thread-comments`][thread-comments],
31+
workflow [`step-summary`][step-summary], and Pull Request reviews (with
32+
[`tidy-review`][tidy-review] or [`format-review`][format-review]).
1233

1334
> [!WARNING]
1435
> We only support Linux runners using a Debian based Linux OS (like Ubuntu and many others).
@@ -41,218 +62,69 @@ Create a new GitHub Actions workflow in your project, e.g. at [.github/workflows
4162
The content of the file should be in the following format.
4263

4364
```yaml
44-
name: cpp-linter
45-
46-
on: pull_request
47-
48-
jobs:
49-
cpp-linter:
50-
runs-on: ubuntu-latest
5165
steps:
52-
- uses: actions/checkout@v3
66+
- uses: actions/checkout@v4
5367
- uses: cpp-linter/cpp-linter-action@v2
5468
id: linter
5569
env:
5670
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5771
with:
58-
style: file
59-
# The following value will only update a single comment
60-
# in a pull request's thread. Set it to false to disable the comment.
61-
# Set it to true to post a new comment (and delete the old comment).
72+
style: 'file' # Use .clang-format config file
73+
tidy-checks: '' # Use .clang-tidy config file
74+
# only 'update' a single comment in a pull request's thread.
6275
thread-comments: ${{ github.event_name == 'pull_request' && 'update' }}
63-
6476
- name: Fail fast?!
6577
if: steps.linter.outputs.checks-failed > 0
66-
run: echo "Some files failed the linting checks!"
67-
# for actual deployment
68-
# run: exit 1
78+
run: exit 1
6979
```
7080
71-
### Optional Inputs
72-
73-
#### `style`
74-
75-
- **Description**: The style rules to use.
76-
- Set this to 'file' to have clang-format use the closest relative .clang-format file.
77-
- Set this to a blank string (`''`) to disable the use of clang-format entirely.
78-
- Any code style supported by the specified version of clang-format.
79-
- Default: 'llvm'
80-
81-
#### `extensions`
82-
83-
- **Description**: The file extensions to run the action against. This is a comma-separated string.
84-
- Default: 'c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx'
85-
86-
#### `tidy-checks`
87-
88-
- **Description**: Comma-separated list of globs with optional `-` prefix. Globs are processed in order of appearance in the list. Globs without `-` prefix add checks with matching names to the set, globs with the `-` prefix remove checks with matching names from the set of enabled checks. This option's value is appended to the value of the 'Checks' option in a .clang-tidy file (if any).
89-
- It is possible to disable clang-tidy entirely by setting this option to `'-*'`.
90-
- It is also possible to rely solely on a .clang-tidy config file by specifying this option as a blank string (`''`).
91-
- Default: 'boost-\*,bugprone-\*,performance-\*,readability-\*,portability-\*,modernize-\*,clang-analyzer-\*,cppcoreguidelines-\*'
92-
93-
#### `repo-root`
94-
95-
- **Description**: The relative path to the repository root directory. This path is relative to the path designated as the runner's GITHUB_WORKSPACE environment variable.
96-
- Default: '.'
97-
98-
#### `version`
99-
100-
- **Description**: The desired version of the [clang-tools](https://github.com/cpp-linter/clang-tools-pip) to use. Accepted options are strings which can be 17, 16, 15, 14, 13, 12, 11, 10, 9, 8 or 7.
101-
- Set this option to a blank string (`''`) to use the platform's default installed version.
102-
- This value can also be a path to where the clang tools are installed (if using a custom install location).
103-
- Default: '12'
104-
105-
#### `verbosity`
106-
107-
- **Description**: This controls the action's verbosity in the workflow's logs. Supported options are `info` or `debug`. This option does not affect the verbosity of resulting thread comments or file annotations.
108-
- The verbosity can also be engaged by enabling debug logs when [re-running jobs or workflows](https://docs.github.com/en/actions/managing-workflow-runs/re-running-workflows-and-jobs).
109-
- Default: 'info'
110-
111-
#### `lines-changed-only`
112-
113-
- **Description**: This controls what part of the files are analyzed. The following values are accepted:
114-
- false: All lines in a file are analyzed.
115-
- true: Only lines in the diff that contain additions are analyzed.
116-
- diff: All lines in the diff are analyzed (including unchanged lines but not subtractions).
117-
- Default: false.
118-
119-
#### `files-changed-only`
120-
121-
- **Description**: Set this option to false to analyze any source files in the repo. This is automatically enabled if lines-changed-only is enabled.
122-
- Default: true
123-
- NOTE: The `GITHUB_TOKEN` should be supplied when running on a private repository with this option enabled, otherwise the runner does not not have the privilege to list changed files for an event. See [Authenticating with the GITHUB_TOKEN](https://docs.github.com/en/actions/reference/authentication-in-a-workflow)
124-
125-
#### `ignore`
126-
127-
- **Description**: Set this option with string of path(s) to ignore.
128-
- In the case of multiple paths, you can use a pipe character (`|`)
129-
to separate the multiple paths. Multiple lines are forbidden as an input to this option; it must be a single string.
130-
- This can also have files, but the file's relative path has to be specified
131-
as well.
132-
- There is no need to use `./` for each entry; a blank string (`''`) represents
133-
the repo-root path (specified by the `repo-root` input option).
134-
- Submodules are automatically ignored. Hidden directories (beginning with a `.`) are also ignored automatically.
135-
- Prefix a path with a bang (`!`) to make it explicitly _not_ ignored. The order of
136-
multiple paths does _not_ take precedence. The `!` prefix can be applied to
137-
a submodule's path (if desired) but not hidden directories.
138-
- Glob patterns are not supported here. All asterisk characters (`*`) are literal.
139-
- Default: '.github'
140-
141-
#### `thread-comments`
142-
143-
- **Description**: Set this option to true to enable the use of thread comments as feedback. Set this to 'update' to update an existing comment if one exists; the value 'true' will always delete an old comment and post a new one if necessary.
144-
- To use thread comments, the `GITHUB_TOKEN` (provided by Github to each repository) must be declared as an environment
145-
variable. See [Authenticating with the GITHUB_TOKEN](https://docs.github.com/en/actions/reference/authentication-in-a-workflow)
146-
- Default: false
147-
- NOTE: If run on a private repository, then this feature is disabled because the GitHub REST API behaves differently for thread comments on a private repository.
81+
For all explanations of our available input parameters and output variables, see our
82+
[Inputs and Outputs document][io-doc].
14883
149-
#### `no-lgtm`
150-
151-
- **Description**: Set this option to true or false to enable or disable the use of a thread comment or pull request review that basically says 'Looks Good To Me' (when all checks pass).
152-
- See [`thread-comments`](#thread-comments), [`tidy-review`](#tidy-review), and [`format-review`](#format-review) options for further details.
153-
- Default: true (meaning no LGTM comment used)
154-
155-
#### `step-summary`
156-
157-
- **Description**: Set this option to true to append content as part of workflow's job summary.
158-
- See implementation details in GitHub's documentation about
159-
[Adding a job summary](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary).
160-
This option is independent of the [`thread-comments`](#thread-comments) option, rather this option uses the same content that the [`thread-comments`](#thread-comments) option would use.
161-
- Note: The [`no-lgtm`](#no-lgtm) option is _not_ applied to step summaries.
162-
- Default: false
163-
164-
#### `file-annotations`
165-
166-
- **Description**: Set this option to false to disable the use of file annotations as feedback.
167-
- Default: true
168-
169-
#### `database`
170-
171-
- **Description**: The directory containing compilation database (like compile_commands.json) file.
172-
- Default: ''
173-
174-
#### `extra-args`
175-
176-
- **Description**: A string of extra arguments passed to clang-tidy for use as compiler arguments (like `-std=c++14 -Wall`).
177-
- Default: ''
178-
179-
#### `tidy-review`
180-
181-
**Beta feature** 🚧
182-
183-
- **Description**: Set this option to true to enable Pull Request reviews from clang-tidy.
184-
- To use Pull Request reviews, the `GITHUB_TOKEN` (provided by Github to each repository) must be declared as an environment
185-
variable. See [Authenticating with the GITHUB_TOKEN](https://docs.github.com/en/actions/reference/authentication-in-a-workflow)
186-
- See also [the PR review feature caveats](https://cpp-linter.github.io/cpp-linter/pr_review_caveats.html)
187-
- Note: The [`no-lgtm`](#no-lgtm) option is applicable to Pull Request reviews.
188-
- Default: false
189-
190-
#### `format-review`
191-
192-
- **Description**: Set this option to true to enable Pull Request reviews from clang-format.
193-
- To use Pull Request reviews, the `GITHUB_TOKEN` (provided by Github to each repository) must be declared as an environment
194-
variable. See [Authenticating with the GITHUB_TOKEN](https://docs.github.com/en/actions/reference/authentication-in-a-workflow)
195-
- See also [the PR review feature caveats](https://cpp-linter.github.io/cpp-linter/pr_review_caveats.html)
196-
- Note: The [`no-lgtm`](#no-lgtm) option is applicable to Pull Request reviews.
197-
- Default: false
198-
199-
### Outputs
200-
201-
This action creates 3 output variables. Even if the linting checks fail for source files this action will still pass, but users' CI workflows can use this action's outputs to exit the workflow early if that is desired.
202-
203-
#### `checks-failed`
204-
205-
The total number of concerns raised by both clang-format and clang-tidy.
206-
207-
#### `clang-tidy-checks-failed`
208-
209-
The total number of concerns raised by clang-tidy only.
210-
211-
#### `clang-format-checks-failed`
212-
213-
The total number of concerns raised by clang-format only.
84+
See also our [example recipes][recipes-doc].
21485
21586
## Example
21687
217-
<!--intro-end-->
218-
21988
### Annotations
22089
221-
Using [`file-annotations`](#file-annotations):
90+
Using [`file-annotations`][file-annotations]:
22291

22392
#### clang-format annotations
22493

225-
![clang-format annotations](https://raw.githubusercontent.com/cpp-linter/cpp-linter-action/main/docs/images/annotations-clang-format.png)
94+
![clang-format annotations][format-annotations-preview]
22695

22796
#### clang-tidy annotations
22897

229-
![clang-tidy annotations](https://raw.githubusercontent.com/cpp-linter/cpp-linter-action/main/docs/images/annotations-clang-tidy.png)
98+
![clang-tidy annotations][tidy-annotations-preview]
23099

231100
### Thread Comment
232101

233-
Using [`thread-comments`](#thread-comments):
102+
Using [`thread-comments`][thread-comments]:
234103

235-
![sample comment](https://raw.githubusercontent.com/cpp-linter/cpp-linter-action/main/docs/images/comment.png)
104+
![sample thread-comment][thread-comment-preview]
236105

237106
### Step Summary
238107

239-
Using [`step-summary`](#step-summary):
108+
Using [`step-summary`][step-summary]:
240109

241-
![step summary](https://raw.githubusercontent.com/cpp-linter/cpp-linter-action/main/docs/images/step-summary.png)
110+
![step summary][step-summary-preview]
242111

243112
### Pull Request Review
244113

245-
Using only clang-tidy ([`tidy-review`](#tidy-review)):
114+
#### Only clang-tidy
115+
116+
Using [`tidy-review`][tidy-review]:
117+
118+
![sample tidy-review][tidy-review-preview]
246119

247-
![sample tidy-review](https://raw.githubusercontent.com/cpp-linter/cpp-linter-action/main/docs/images/tidy-review.png)
120+
#### Only clang-format
248121

249-
Using only clang-format ([`format-review`](#format-review)):
122+
Using [`format-review`][format-review]:
250123

251-
![sample format-review](https://raw.githubusercontent.com/cpp-linter/cpp-linter-action/main/docs/images/format-review.png)
124+
![sample format-review][format-review-preview]
252125

253-
![sample tidy-review](https://raw.githubusercontent.com/cpp-linter/cpp-linter-action/main/docs/images/format-suggestion.png)
126+
![sample format-suggestion][format-suggestion-preview]
254127

255-
<!--footer-start-->
256128

257129
## Add C/C++ Linter Action badge in README
258130

@@ -274,4 +146,4 @@ To provide feedback (requesting a feature or reporting a bug) please post to [is
274146

275147
The scripts and documentation in this project are released under the [MIT License](https://github.com/cpp-linter/cpp-linter-action/blob/main/LICENSE)
276148

277-
<!--footer-end-->
149+
<!--README-end-->

docs/badge_hook.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""A mkdocs hook that injects an HTML syntax used to generate badges at build time."""
2+
import re
3+
from re import Match
4+
from mkdocs.config.defaults import MkDocsConfig
5+
from mkdocs.structure.files import Files
6+
from mkdocs.structure.pages import Page
7+
8+
9+
def on_page_markdown(markdown: str, *, page: Page, config: MkDocsConfig, files: Files):
10+
# Replace callback
11+
def replace(match: Match):
12+
badge_type, args = match.groups()
13+
args = args.strip()
14+
if badge_type == "version":
15+
return _badge_for_version(args, page, files)
16+
elif badge_type == "flag":
17+
return _badge_for_flags(args, page, files)
18+
elif badge_type == "permission":
19+
return _badge_for_permissions(args, page, files)
20+
elif badge_type == "default":
21+
return _badge_for_default(args, page, files)
22+
23+
# Otherwise, raise an error
24+
raise RuntimeError(f"Unknown badge type: {badge_type}")
25+
26+
# Find and replace all external asset URLs in current page
27+
return re.sub(r"<!-- md:(\w+) (.*) -->", replace, markdown, flags=re.I | re.M)
28+
29+
30+
# -----------------------------------------------------------------------------
31+
# Helper functions
32+
33+
def _badge_for_flags(arg, page: Page, files: Files):
34+
if arg == "experimental":
35+
return _badge_for_experimental(page, files)
36+
raise ValueError(f"Unsupported badge flag: {arg}")
37+
38+
# Create badge
39+
def _badge(icon: str, text: str = ""):
40+
return "".join(
41+
[
42+
'<span class="mdx-badge">',
43+
*([f'<span class="mdx-badge__icon">{icon}</span>'] if icon else []),
44+
*([f'<span class="mdx-badge__text">{text}</span>'] if text else []),
45+
"</span>",
46+
]
47+
)
48+
49+
50+
# Create badge for version
51+
def _badge_for_version(text: str, page: Page, files: Files):
52+
icon = "material-tag-outline"
53+
href = f"https://github.com/cpp-linter/cpp-linter-action/releases/v{text}"
54+
return _badge(
55+
icon=f'[:{icon}:]({href} "required version")',
56+
text=f'[{text}]({href} "required version")',
57+
)
58+
59+
60+
# Create badge for default value
61+
def _badge_for_default(text: str, page: Page, files: Files):
62+
return _badge(icon="Default", text=f"`#!yaml {text}`")
63+
64+
65+
# Create badge for required value flag
66+
def _badge_for_permissions(args: str, page: Page, files: Files):
67+
match_permission = re.match(r"([^#]+)(.*)", args)
68+
if match_permission is None:
69+
raise ValueError(f"failed to parse permissions from {args}")
70+
permission, link = match_permission.groups()[:2]
71+
permission = permission.strip()
72+
link = "permissions.md" + link
73+
icon = "material-lock"
74+
return _badge(
75+
icon=f'[:{icon}:]({link} "required permissions")',
76+
text=f'[`#!yaml {permission}`]({link} "required permission")',
77+
)
78+
79+
80+
# Create badge for experimental flag
81+
def _badge_for_experimental(page: Page, files: Files):
82+
icon = "material-flask-outline"
83+
return _badge(icon=f":{icon}:{{ .mdx-badge--heart }}", text="experimental")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)