Skip to content

croodycoder/PR_Automation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Webhook-based PR Test Automation for Django Project

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.


Features

  • Automatically runs tests and coverage for PRs.
  • Posts coverage and test reports as comments on PRs.
  • Publishes reports on GitHub Pages.

Setup and Installation

Step 1: Fork and Clone the Repository

  1. Fork this repository.
  2. Clone it
    git clone https://github.com/<YOUR_USERNAME>/pr-automation.git
    cd pr-automation

Step 2: Set Up Codespaces

  1. Open GitHub and navigate to your repository.
  2. Click the "Code" button and select "Open with Codespaces".
  3. 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.
  4. Copy your codepsaces url to be used in step 4.

Step 3: Install Dependencies

  1. Navigate to the root directory of your project.
  2. Install the required dependencies from the requirements.txt file by running:
    pip install -r requirements.txt

Step 4: Set Up Webhook in Django Repo

  • 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.app that 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.

Step 5: Create a Personal Access Token (PAT)

  • 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.

Step 6: Set Up Environment Variables

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

Step 7: Set Up GitHub Pages Repo

  • Create a new public repo say test_repo for github pages
  • In the test_repo repository, create a new branch named gh-pages.

Step 8:Run the Flask Application

  • Run flask server and make port public
  • The app will be accessible on the specified port after making it public (e.g., 8000).

Step 9: Generate a Pull Request

  • In your Django repository, create a pull request.

Step 10: Verify the Results

  • 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

License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages