Debian Codegraph @

ansible_collections/community/general/CONTRIBUTING.md

raw
1 <!--
2 Copyright (c) Ansible Project
3 GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 SPDX-License-Identifier: GPL-3.0-or-later
5 -->
6
7 # Contributing
8
9 We follow [Ansible Code of Conduct](https://docs.ansible.com/projects/ansible/latest/community/code_of_conduct.html) in all our contributions and interactions within this repository.
10
11 If you are a committer, also refer to the [collection's committer guidelines](https://github.com/ansible-collections/community.general/blob/main/commit-rights.md).
12
13 ## Issue tracker
14
15 Whether you are looking for an opportunity to contribute or you found a bug and already know how to solve it, please go to the [issue tracker](https://github.com/ansible-collections/community.general/issues).
16 There you can find feature ideas to implement, reports about bugs to solve, or submit an issue to discuss your idea before implementing it which can help choose a right direction at the beginning of your work and potentially save a lot of time and effort.
17 Also somebody may already have started discussing or working on implementing the same or a similar idea,
18 so you can cooperate to create a better solution together.
19
20 * If you are interested in starting with an easy issue, look for [issues with an `easyfix` label](https://github.com/ansible-collections/community.general/labels/easyfix).
21 * Often issues that are waiting for contributors to pick up have [the `waiting_on_contributor` label](https://github.com/ansible-collections/community.general/labels/waiting_on_contributor).
22
23 ## Review pull requests
24
25 Look through currently [open pull requests](https://github.com/ansible-collections/community.general/pulls).
26
27 You can help by reviewing them. Reviews help move pull requests to merge state. Some good pull requests cannot be merged only due to a lack of reviews. And it is always worth saying that good reviews are often more valuable than pull requests themselves.
28 Note that reviewing does not only mean code review, but also offering comments on new interfaces added to existing plugins/modules, interfaces of new plugins/modules, improving language (not everyone is a native English speaker), or testing bugfixes and new features!
29
30 Also, consider taking up a valuable, reviewed, but abandoned pull request which you could politely ask the original authors to complete yourself.
31
32 ## Open pull requests
33
34 Please read our ['Contributing to collections'](https://docs.ansible.com/projects/ansible/devel/dev_guide/developing_collections_contributing.html#contributing-to-a-collection-community-general) guide.
35
36 * Try committing your changes with an informative but short commit message.
37 * Do not squash your commits and force-push to your branch if not needed. Reviews of your pull request are much easier with individual commits to comprehend the pull request history. All commits of your pull request branch will be squashed into one commit by GitHub upon merge.
38 * Do not add merge commits to your PR. The bot will complain and you will have to rebase ([instructions for rebasing](https://docs.ansible.com/projects/ansible/latest/dev_guide/developing_rebasing.html)) to remove them before your PR can be merged. To avoid that git automatically does merges during pulls, you can configure it to do rebases instead by running `git config pull.rebase true` inside the repository checkout.
39 * Make sure your PR includes a [changelog fragment](https://docs.ansible.com/projects/ansible/devel/community/collection_development_process.html#creating-a-changelog-fragment).
40 * You must not include a fragment for new modules or new plugins. Also you shouldn't include one for docs-only changes. (If you're not sure, simply don't include one, we'll tell you whether one is needed or not :) )
41 * Please always include a link to the pull request itself, and if the PR is about an issue, also a link to the issue. Also make sure the fragment ends with a period, and begins with a lower-case letter after `-`. (Again, if you don't do this, we'll add suggestions to fix it, so don't worry too much :) )
42 * Note that we format the code with `ruff format`. If your change does not match the formatters expectations, CI will fail and your PR will not get merged. See below for how to format code with antsibull-nox.
43
44 You can also read the Ansible community's [Quick-start development guide](https://docs.ansible.com/projects/ansible/devel/community/create_pr_quick_start.html).
45
46 ## Test pull requests
47
48 If you want to test a PR locally, refer to [our testing guide](https://docs.ansible.com/projects/ansible/devel/community/collection_contributors/collection_test_pr_locally.html) for instructions on how do it quickly.
49
50 If you find any inconsistencies or places in this document which can be improved, feel free to raise an issue or pull request to fix it.
51
52 ## Format code; and run sanity or unit tests locally (with antsibull-nox)
53
54 The easiest way to format the code, and to run sanity and unit tests locally is to use [antsibull-nox](https://docs.ansible.com/projects/antsibull-nox/).
55 (If you have [nox](https://nox.thea.codes/en/stable/) installed, it will automatically install antsibull-nox in a virtual environment for you.)
56
57 ### Format code
58
59 The following commands show how to run ruff format:
60
61 ```.bash
62 # Run all configured formatters:
63 nox -Re formatters
64
65 # If you notice discrepancies between your local formatter and CI, you might
66 # need to re-generate the virtual environment:
67 nox -e formatters
68 ```
69
70 ### Sanity tests
71
72 The following commands show how to run ansible-test sanity tests:
73
74 ```.bash
75 # Run basic sanity tests for all files in the collection:
76 nox -Re ansible-test-sanity-devel
77
78 # Run basic sanity tests for the given files and directories:
79 nox -Re ansible-test-sanity-devel -- plugins/modules/system/pids.py tests/integration/targets/pids/
80
81 # Run all other sanity tests for all files in the collection:
82 nox -R
83 ```
84
85 If you replace `-Re` with `-e`, respectively. If you leave `-R` away, then the virtual environments will be re-created. The `-R` re-uses them (if they already exist).
86
87 ### Unit tests
88
89 The following commands show how to run unit tests:
90
91 ```.bash
92 # Run all unit tests:
93 nox -Re ansible-test-units-devel
94
95 # Run all unit tests for one Python version (a lot faster):
96 nox -Re ansible-test-units-devel -- --python 3.13
97
98 # Run a specific unit test (for the nmcli module) for one Python version:
99 nox -Re ansible-test-units-devel -- --python 3.13 tests/unit/plugins/modules/net_tools/test_nmcli.py
100 ```
101
102 If you replace `-Re` with `-e`, then the virtual environments will be re-created. The `-R` re-uses them (if they already exist).
103
104 ## Run basic sanity, unit or integration tests locally (with ansible-test)
105
106 Instead of using antsibull-nox, you can also run sanity and unit tests with ansible-test directly.
107 This also allows you to run integration tests.
108
109 You have to check out the repository into a specific path structure to be able to run `ansible-test`. The path to the git checkout must end with `.../ansible_collections/community/general`. Please see [our testing guide](https://docs.ansible.com/projects/ansible/devel/community/collection_contributors/collection_test_pr_locally.html) for instructions on how to check out the repository into a correct path structure. The short version of these instructions is:
110
111 ```.bash
112 mkdir -p ~/dev/ansible_collections/community
113 git clone https://github.com/ansible-collections/community.general.git ~/dev/ansible_collections/community/general
114 cd ~/dev/ansible_collections/community/general
115 ```
116
117 Then you can run `ansible-test` (which is a part of [ansible-core](https://pypi.org/project/ansible-core/)) inside the checkout. The following example commands expect that you have installed Docker or Podman. Note that Podman has only been supported by more recent ansible-core releases. If you are using Docker, the following will work with Ansible 2.9+.
118
119 ### Basic sanity tests
120
121 The following commands show how to run basic sanity tests:
122
123 ```.bash
124 # Run basic sanity tests for all files in the collection:
125 ansible-test sanity --docker -v
126
127 # Run basic sanity tests for the given files and directories:
128 ansible-test sanity --docker -v plugins/modules/system/pids.py tests/integration/targets/pids/
129 ```
130
131 ### Unit tests
132
133 Note that for running unit tests, you need to install required collections in the same folder structure that `community.general` is checked out in.
134 Right now, you need to install [`community.internal_test_tools`](https://github.com/ansible-collections/community.internal_test_tools).
135 If you want to use the latest version from GitHub, you can run:
136
137 ```
138 git clone https://github.com/ansible-collections/community.internal_test_tools.git ~/dev/ansible_collections/community/internal_test_tools
139 ```
140
141 The following commands show how to run unit tests:
142
143 ```.bash
144 # Run all unit tests:
145 ansible-test units --docker -v
146
147 # Run all unit tests for one Python version (a lot faster):
148 ansible-test units --docker -v --python 3.8
149
150 # Run a specific unit test (for the nmcli module) for one Python version:
151 ansible-test units --docker -v --python 3.8 tests/unit/plugins/modules/net_tools/test_nmcli.py
152 ```
153
154 ### Integration tests
155
156 Note that for running integration tests, you need to install required collections in the same folder structure that `community.general` is checked out in.
157 Right now, depending on the test, you need to install [`ansible.posix`](https://github.com/ansible-collections/ansible.posix), [`community.crypto`](https://github.com/ansible-collections/community.crypto), and [`community.docker`](https://github.com/ansible-collections/community.docker):
158 If you want to use the latest versions from GitHub, you can run:
159
160 ```
161 mkdir -p ~/dev/ansible_collections/ansible
162 git clone https://github.com/ansible-collections/ansible.posix.git ~/dev/ansible_collections/ansible/posix
163 git clone https://github.com/ansible-collections/community.crypto.git ~/dev/ansible_collections/community/crypto
164 git clone https://github.com/ansible-collections/community.docker.git ~/dev/ansible_collections/community/docker
165 ```
166
167 The following commands show how to run integration tests:
168
169 #### In Docker
170
171 Integration tests on Docker have the following parameters:
172
173 - `image_name` (required): The name of the Docker image. To get the list of supported Docker images, run
174 `ansible-test integration --help` and look for _target docker images_.
175 - `test_name` (optional): The name of the integration test.
176 For modules, this equals the short name of the module; for example, `pacman` in case of `community.general.pacman`.
177 For plugins, the plugin type is added before the plugin's short name, for example `callback_yaml` for the `community.general.yaml` callback.
178
179 ```.bash
180 # Test all plugins/modules on fedora
181 ansible-test integration -v --docker fedora
182
183 # Template
184 ansible-test integration -v --docker image_name test_name
185
186 # Example community.general.ini_file module on fedora Docker image:
187 ansible-test integration -v --docker fedora ini_file
188 ```
189
190 #### Without isolation
191
192 ```.bash
193 # Run integration tests for the flattened lookup **without any isolation**:
194 ansible-test integration -v lookup_flattened
195 ```
196
197 If you are unsure about the integration test target name for a module or plugin, you can take a look in `tests/integration/targets/`. Tests for plugins have the plugin type prepended.
198
199 ## Devcontainer
200
201 Since community.general 12.2.0, the project repository supports [devcontainers](https://containers.dev/). In short, it is a standard mechanism to
202 create a container that is then used during the development cycle. Many tools are pre-installed in the container and will be already available
203 to you as a developer. A number of different IDEs support that configuration, the most prominent ones being VSCode and PyCharm.
204
205 See the files under [.devcontainer](.devcontainer) for details on what is deployed inside that container.
206
207 Beware of:
208
209 - By default, the devcontainer installs the latest version of `ansible-core`.
210 When testing your changes locally, keep in mind that the collection must support older versions of
211 `ansible-core` and, depending on what is being tested, results may vary.
212 - Integration tests executed directly inside the devcontainer without isolation (see above) may fail if
213 they expected to be run in full fledged VMs. On the other hand, the devcontainer setup allows running
214 containers inside the container (the `docker-in-docker` feature).
215 - The devcontainer is built with a directory structure such that
216 `.../ansible_collections/community/general` contains the project repository, so `ansible-test` and
217 other standard tools should work without any additional setup
218 - By default, the devcontainer installs `pre-commit` and configures it to perform `ruff check` and
219 `ruff format` on the Python files, prior to commiting. That configuration is going to be used by
220 `git` even outside the devcontainer. To prevent errors, you have to either install `pre-commit` in
221 your computer, outside the devcontainer, or run `pre-commit uninstall` from within the devcontainer
222 before quitting it.
223
224 ## Creating new modules or plugins
225
226 Creating new modules and plugins requires a bit more work than other Pull Requests.
227
228 1. Please make sure that your new module or plugin is of interest to a larger audience. Very specialized modules or plugins that
229 can only be used by very few people should better be added to more specialized collections.
230
231 2. Please do not add more than one plugin/module in one PR, especially if it is the first plugin/module you are contributing.
232 That makes it easier for reviewers, and increases the chance that your PR will get merged. If you plan to contribute a group
233 of plugins/modules (say, more than a module and a corresponding `_info` module), please mention that in the first PR. In
234 such cases, you also have to think whether it is better to publish the group of plugins/modules in a new collection.
235
236 3. When creating a new module or plugin, please make sure that you follow various guidelines:
237
238 - Follow [development conventions](https://docs.ansible.com/projects/ansible/devel/dev_guide/developing_modules_best_practices.html);
239 - Follow [documentation standards](https://docs.ansible.com/projects/ansible/devel/dev_guide/developing_modules_documenting.html) and
240 the [Ansible style guide](https://docs.ansible.com/projects/ansible/devel/dev_guide/style_guide/index.html#style-guide);
241 - Make sure your modules and plugins are [GPL-3.0-or-later](https://www.gnu.org/licenses/gpl-3.0-standalone.html) licensed
242 (new module_utils can also be [BSD-2-clause](https://opensource.org/licenses/BSD-2-Clause) licensed);
243 - Make sure that new plugins and modules have tests (unit tests, integration tests, or both); it is preferable to have some tests
244 which run in CI.
245
246 4. Action plugins need to be accompanied by a module, even if the module file only contains documentation
247 (`DOCUMENTATION`, `EXAMPLES` and `RETURN`). The module must have the same name and directory path in `plugins/modules/`
248 than the action plugin has in `plugins/action/`.
249
250 5. Make sure to add a BOTMETA entry for your new module/plugin in `.github/BOTMETA.yml`. Search for other plugins/modules in the
251 same directory to see how entries could look. You should list all authors either as `maintainers` or under `ignore`. People
252 listed as `maintainers` will be pinged for new issues and PRs that modify the module/plugin or its tests.
253
254 When you add a new plugin/module, we expect that you perform maintainer duty for at least some time after contributing it.