This project automates the process of running tests, generating coverage reports, and posting the results to a GitHub pull request (PR) using a Flask application. When a PR is opened or updated, it triggers tests in a Django repository, generates HTML reports, and posts the results back to the PR in the form of comments with links to the reports.
- Automatically runs tests and coverage for PRs.
- Posts coverage and test reports as comments on PRs.
- Publishes reports on GitHub Pages.
- Fork this repository.
- Clone it
git clone https://github.com/<YOUR_USERNAME>/pr-automation.git cd pr-automation
- Open GitHub and navigate to your repository.
- Click the "Code" button and select "Open with Codespaces".
- Follow the prompts to create a new Codespace environment. Once the environment is set up, you'll be able to access the project directly from within Codespaces.
- Copy your codepsaces url to be used in step 4.
- Navigate to the root directory of your project.
- Install the required dependencies from the
requirements.txtfile by running:pip install -r requirements.txt
- Go to the Settings of your Django_Repo on which PR is to be raised on GitHub.
- Navigate to Webhooks and click Add webhook.
- In the Payload URL, use the URL copied in step 2 generated by your Codespaces and add
-8000.appthat should look like for
e.g.,https://<Random_Name>-8000.app.github.dev/webhook). - Set Content type to application/json.
- Under Let me select individual events, select Pull requests.
- Uncheck Pushes and save the webhook.
- Go to Profile settings > Developer settings > Personal access tokens on GitHub.
- Generate a new token with all permissions (e.g., repo, workflow).
- Save the token as it will not be shown again.
In the root directory of your repository, create a .env file with the following content:
MY_GITHUB_TOKEN=<YOUR_PAT>
TESTS_REPO=https://github.com/username/tests-repo
DJANGO_REPO=https://github.com/username/django-repo
COVERAGE_BASE_URL=https://username.github.io/tests-repo
COMMENT_BASE_URL=https://api.github.com/repos/username/repository/pulls
DJANGO_PROJECT_NAME=your_django_project_name
- Create a new public repo say test_repo for github pages
- In the test_repo repository, create a new branch named gh-pages.
- Run flask server and make port public
- The app will be accessible on the specified port after making it public (e.g., 8000).
- In your Django repository, create a pull request.
- The webhook will trigger the Flask app, which will:
- Clone the Django repository.
- It will automatically run the tests with coverage and pytest.
- It will generate HTML reports (index.html for coverage, report.html for test results).
- It will push the reports along with css assets folder to the gh-pages branch in the test_repo.
- It will post a comment on the PR with links to the generated reports and the assets.
- In case, if your django repo test cases did not run successfully so it will not create any report or comment
- Click on the links and it will redirect you on reports
This project is licensed under the MIT License.