|
6 | 6 | <p>Works in python and PySpark.</p>
|
7 | 7 | <br>
|
8 | 8 | <h3>How to use:</h3>
|
9 |
| - <p>You can clone the repo <a target="_blank" href="https://github.com/anthonypernia/pre-commit-python-action-example.git">https://github.com/anthonypernia/pre-commit-python-action-example.git</a> or just download the files that you need and use in your repo.</p> |
| 9 | + <p>You can clone the repo <a target="_blank" href="https://github.com/anthonypernia/pre-commit-python-action-example.git">https://github.com/anthonypernia/pre-commit-python-action-example.git</a> or just download the files that you need and use in your repo.</p> |
10 | 10 | <p>Install the necessary libraries:</p>
|
11 | 11 | <ul>
|
12 | 12 | <li>pre-commit</li>
|
|
20 | 20 | <p>Or just install the requirements.txt that is in this repo using: <code>pip install -r requirements.txt </code></p>
|
21 | 21 | <h2>Pre-commit</h2>
|
22 | 22 | <p>Pre-commit is a tool that we use to identify issues in our code, such as type errors, syntax errors, functions without documentation, etc </p>
|
23 |
| - <p>To learn more about Pre-commit you can check the <a target="_blank" href="https://pre-commit.com/#intro">Documentation</a> </p> |
| 23 | + <p>To learn more about Pre-commit you can check the <a target="_blank" href="https://pre-commit.com/#intro">Documentation</a> </p> |
24 | 24 | <p>To execute <code>pre-commit</code> use the following command:</p>
|
25 | 25 | <pre><code>pre-commit run --all-files</code></pre>
|
26 | 26 | <p>And then, the library starts checking the code with all the hooks that we are using in the <code>pre-commit-config</code> file. The first time it will take about 2 minutes </p>
|
|
44 | 44 | <img src="https://raw.githubusercontent.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/main/assets/commit-example.png"></img>
|
45 | 45 | <p>In case it passes all the tests, the commit will be executed, otherwise it will not</p>
|
46 | 46 | <p>You can copy my pre-commit configuration:</p>
|
47 |
| - <p>Pre-commit file: <code>.pre-commit-config.yaml</code> <a target="_blank" href="https://github.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/blob/main/.pre-commit-config.yaml">Link</a></p> |
| 47 | + <p>Pre-commit file: <code>.pre-commit-config.yaml</code> <a target="_blank" href="https://github.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/blob/main/.pre-commit-config.yaml">Link</a></p> |
48 | 48 | <h2>Github Action using Pre-Commit, PyTest and Coverage</h2>
|
49 | 49 | <p>Github actions is the way that we have to apply CICD or run customized workflows</p>
|
50 |
| - <p>You can take a look at the <a target="_blank" href="https://docs.github.com/en/actions">Documentation</a></p> |
| 50 | + <p>You can take a look at the <a target="_blank" href="https://docs.github.com/en/actions">Documentation</a></p> |
51 | 51 | <p>The funniest thing is that pre-commit can be added to a workflow as an automated task, we can add unit testing, check the coverage, etc. All this task can be executed after push our code or create a pull request.</p>
|
52 | 52 | <h4>There are some examples with customized workflows:</h4>
|
53 | 53 | <ul>
|
54 |
| - <li><p><a target="_blank" href="https://github.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/blob/main/action-to-use/pre-commit-and-merge.yml">pre-commit-and-merge.yml</a>, This workflow will merge all the push that pass the pre-commit tests, from the develop branch, into the master branch</p></li> |
55 |
| - <li><p><a target="_blank" href="https://github.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/blob/main/action-to-use/pre-commit-on-pr.yml">pre-commit-on-pr.yml</a>, This workflow will execute pre-commit on all pull requests to main branch and will show the result in the PR</p></li> |
56 |
| - <li><p><a target="_blank" href="https://github.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/blob/main/action-to-use/pre-commit-on-push-and-pr.yml">pre-commit-on-push-and-pr.yml</a>, Same as before , but in this case it will execute test on PR to main branch and all push to develop |
| 54 | + <li><p><a target="_blank" href="https://github.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/blob/main/action-to-use/pre-commit-and-merge.yml">pre-commit-and-merge.yml</a>, This workflow will merge all the push that pass the pre-commit tests, from the develop branch, into the master branch</p></li> |
| 55 | + <li><p><a target="_blank" href="https://github.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/blob/main/action-to-use/pre-commit-on-pr.yml">pre-commit-on-pr.yml</a>, This workflow will execute pre-commit on all pull requests to main branch and will show the result in the PR</p></li> |
| 56 | + <li><p><a target="_blank" href="https://github.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/blob/main/action-to-use/pre-commit-on-push-and-pr.yml">pre-commit-on-push-and-pr.yml</a>, Same as before , but in this case it will execute test on PR to main branch and all push to develop |
57 | 57 | </p></li>
|
58 |
| - <li><p><a target="_blank" href="https://github.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/blob/main/action-to-use/pre-commit-and-pytest.yml">pre-commit-and-pytest.yml.yml</a>, In this case, in addition to running the pre-commit tests, this workflow will run the pytest suite.</p></li> |
59 |
| - <li><p><a target="_blank" href="https://github.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/blob/main/action-to-use/pre-commit-pytest-coverage-codecov.yml">pre-commit-pytest-coverage-codecov.yml</a>, This workflow is the most complete. it will execute the following actions: |
| 58 | + <li><p><a target="_blank" href="https://github.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/blob/main/action-to-use/pre-commit-and-pytest.yml">pre-commit-and-pytest.yml.yml</a>, In this case, in addition to running the pre-commit tests, this workflow will run the pytest suite.</p></li> |
| 59 | + <li><p><a target="_blank" href="https://github.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/blob/main/action-to-use/pre-commit-pytest-coverage-codecov.yml">pre-commit-pytest-coverage-codecov.yml</a>, This workflow is the most complete. it will execute the following actions: |
60 | 60 | <ul>
|
61 | 61 | <li>run pre-commit test</li>
|
62 | 62 | <li>run pytest suite</li>
|
63 | 63 | <li>check the coverage and upload to Codecov</li>
|
64 | 64 | <li>it will fail if coverage is not ok</li>
|
65 | 65 | </ul>
|
66 |
| - You can find more info in <a target="_blank" href="https://app.codecov.io/gh">Codecov</a></p></li> |
| 66 | + You can find more info in <a target="_blank" href="https://app.codecov.io/gh">Codecov</a></p></li> |
67 | 67 | </ul>
|
68 | 68 | <h3>Best Option: pre-commit-pytest-coverage-codecov</h3>
|
69 | 69 | <p>The one that I consider the best is the last one because it checks the coverage, execute the pytest suite, and executes the pre-commit so that at the end you can see the coverage graphs in the PR</p>
|
70 |
| - <p>You need to create an account in <a target="_blank" href="https://app.codecov.io/gh">Codecov</a>, give it permission to scan repositories, get the codecov token , and store in secrets as CODECOV_TOKEN</p> |
| 70 | + <p>You need to create an account in <a target="_blank" href="https://app.codecov.io/gh">Codecov</a>, give it permission to scan repositories, get the codecov token , and store in secrets as CODECOV_TOKEN</p> |
71 | 71 | <p>If you want to configure coverage to approve, you need to create a file called <code>codecov.yml</code></p>
|
72 |
| - <p>You can check the file that I use in <a target="_blank" href="https://github.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/blob/main/codecov.yml" ></a></p> |
| 72 | + <p>You can check the file that I use in <a target="_blank" href="https://github.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/blob/main/codecov.yml" ></a></p> |
73 | 73 | <p>Here is an example of a PR using that workflow, in that case, we have some errors in pre-commit and we need to solve it</p>
|
74 | 74 | <img src="https://raw.githubusercontent.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/main/assets/pre-commit-fail-github.png"></img>
|
75 | 75 | <p>When the problem is solved, when you push your code again, the tests will be updated, in that case, we need to update the test to complete the coverage</p>
|
|
79 | 79 | <img src="https://raw.githubusercontent.com/anthonypernia/setup-python-environment-with-testing-and-github-actions/main/assets/pr-ok.png"></img>
|
80 | 80 | <br>
|
81 | 81 | The documentation that I used to create this repository is:
|
82 |
| - <p><a target="_blank" href="https://pre-commit.com/#install">https://pre-commit.com/#install</a></p> |
83 |
| - <p><a target="_blank" href="https://pre-commit.com/#usage">https://pre-commit.com/#usage</a></p> |
84 |
| - <p><a target="_blank" href="https://composed.blog/python/pre-commit">https://composed.blog/python/pre-commit</a></p> |
| 82 | + <p><a target="_blank" href="https://pre-commit.com/#install">https://pre-commit.com/#install</a></p> |
| 83 | + <p><a target="_blank" href="https://pre-commit.com/#usage">https://pre-commit.com/#usage</a></p> |
| 84 | + <p><a target="_blank" href="https://composed.blog/python/pre-commit">https://composed.blog/python/pre-commit</a></p> |
85 | 85 | <p><a
|
86 |
| - target="_blank" href="https://towardsdatascience.com/pre-commit-hooks-you-must-know-ff247f5feb7e">https://towardsdatascience.com/pre-commit-hooks-you-must-know-ff247f5feb7e</a> |
| 86 | + target="_blank" href="https://towardsdatascience.com/pre-commit-hooks-you-must-know-ff247f5feb7e">https://towardsdatascience.com/pre-commit-hooks-you-must-know-ff247f5feb7e</a> |
87 | 87 | </p>
|
88 | 88 | <p><a
|
89 |
| - target="_blank" href="https://verdantfox.com/blog/view/how-to-use-git-pre-commit-hooks-the-hard-way-and-the-easy-way">https://verdantfox.com/blog/view/how-to-use-git-pre-commit-hooks-the-hard-way-and-the-easy-way</a> |
| 89 | + target="_blank" href="https://verdantfox.com/blog/view/how-to-use-git-pre-commit-hooks-the-hard-way-and-the-easy-way">https://verdantfox.com/blog/view/how-to-use-git-pre-commit-hooks-the-hard-way-and-the-easy-way</a> |
90 | 90 | </p>
|
91 | 91 | <p>Some hooks examples:</p>
|
92 |
| - <p><a target="_blank" href="https://github.com/pre-commit/pre-commit-hooks">https://github.com/pre-commit/pre-commit-hooks</a></p> |
| 92 | + <p><a target="_blank" href="https://github.com/pre-commit/pre-commit-hooks">https://github.com/pre-commit/pre-commit-hooks</a></p> |
93 | 93 | <p>Information about Pytest:</p>
|
94 |
| - <p><a target="_blank" href="https://docs.pytest.org/en/6.2.x/customize.html">https://docs.pytest.org/en/6.2.x/customize.html</a></p> |
| 94 | + <p><a target="_blank" href="https://docs.pytest.org/en/6.2.x/customize.html">https://docs.pytest.org/en/6.2.x/customize.html</a></p> |
95 | 95 | <p>Codecov Documentation:</p>
|
96 |
| - <p><a target="_blank" href="https://docs.codecov.com/docs">https://docs.codecov.com/docs</a></p> |
| 96 | + <p><a target="_blank" href="https://docs.codecov.com/docs">https://docs.codecov.com/docs</a></p> |
97 | 97 | </div>
|
0 commit comments