d
WE ARE EXPERTS IN TECHNOLOGY

Let’s Work Together

n

StatusNeo

Automating PR Merges with GitHub Webhooks and AWS


In today’s fast-paced development environments, maintaining a deployable state is crucial. Continuous Integration/Continuous Deployment (CI/CD) practices help teams ensure their code is always in a ready-to-deploy state. One powerful way to achieve this is by automating the testing and merging of pull requests (PRs) using GitHub webhooks and AWS. This blog will guide you through setting up such a system, making it easy and foolproof.


Understanding Git Pre-Merge Web-hooks

In the world of software development, keeping the main branch of your codebase clean and deployable is like maintaining a well-tuned engine in a car. Just as you’d want to avoid sudden breakdowns on the road, you want to avoid unexpected issues in your code when it’s time to deploy. This is where Git pre-merge webhooks come into play.

What Are Git Pre-Merge Webhooks?

Imagine you’re working on a project with your team, and you’ve just written a fantastic new feature. You’re ready to merge your changes into the main branch. But wait – what if there’s a hidden bug in your code? What if merging your changes breaks the build, causing chaos for everyone else?

Git pre-merge webhooks act like gatekeepers. They run specific scripts before the merge request (also known as a pull request or PR) is completed. Think of these hooks as automated quality inspectors who make sure everything is in order before allowing the merge to happen.

Why Are They Important?

  1. Maintaining Code Integrity: By running scripts that check your code for errors, style issues, or failed tests, pre-merge webhooks help ensure that only high-quality, bug-free code makes it into the main branch. This is vital for maintaining a stable and reliable codebase.
  2. Ensuring a Deployable State: In a fast-paced development environment, it’s crucial that your main branch is always ready to be deployed. Pre-merge webhooks help by preventing problematic code from being merged, thus keeping the main branch in a deployable state at all times.

Practical Benefits

Let’s say your team uses automated tests to verify that new code doesn’t introduce bugs. With a pre-merge webhook, you can set up a script that runs these tests automatically every time someone tries to merge a PR. If the tests pass, the merge goes through. If not, the merge is blocked, and the developer is notified to fix the issues.

This process not only saves time but also reduces the risk of human error. Developers don’t have to remember to run tests manually before merging – the webhook does it for them, consistently and reliably.

An Underrated Technology

Despite their clear benefits, Git pre-merge webhooks don’t get the attention they deserve. A quick search online reveals that there’s surprisingly little content available about them. This could be because:

  • Lack of Awareness: Many developers and teams might not be aware of the power and utility of pre-merge webhooks. They might rely on other methods to maintain code quality, not realizing how much easier and effective webhooks can be.
  • Complexity: Setting up pre-merge webhooks can seem daunting, especially for teams that are new to automation and CI/CD practices. Without straightforward guides and resources, it’s easy to overlook this powerful tool.

By shining a light on Git pre-merge webhooks, we can help more teams discover their potential. Whether you’re a solo developer or part of a large team, implementing pre-merge webhooks can significantly enhance your development workflow, making your codebase more robust and your life a whole lot easier.


This explanation aims to make the concept of Git pre-merge webhooks more relatable and highlights their practical importance and the current lack of content available about them.

Automating the Testing Process for PRs Using Github Webhooks

To automate the testing process for PRs and merge them only if all tests pass, we can use GitHub webhooks and AWS services. Here’s an overview of how this can be achieved.

Architecture Overview

The architecture involves several components working together to automate the testing and merging process:

  1. GitHub: Where the code and PRs reside.
  2. Amazon API Gateway: Receives the webhook events from GitHub.
  3. AWS Lambda: Processes the events and triggers the test execution.
  4. AWS Systems Manager (SSM): Executes commands on EC2 instances.
  5. Amazon EC2: Where the tests are run.

Architecture Diagram:

  1. A new PR is raised in GitHub.
  2. GitHub webhook sends an event to AWS API Gateway.
  3. API Gateway triggers an AWS Lambda function.
  4. The Lambda function executes a shell script on the EC2 instance using AWS Systems Manager (SSM).
  5. The test script runs on the EC2 instance.
  6. The test results are sent back to GitHub.
  7. If tests pass, the PR is automatically merged.

Tools and Services Used

  • GitHub: For hosting the repository and managing PRs.
  • AWS API Gateway: For receiving webhook events from GitHub.
  • AWS Lambda: For processing the webhook events.
  • AWS Systems Manager (SSM): For executing commands on EC2 instances.
  • Amazon EC2: For running the tests.

Step-by-Step Guide

Step 1: Set Up GitHub Webhook

  1. Go to your GitHub repository.
  2. Click on Settings.
  3. Select Web-hooks and click Add web-hook.
  4. Set the Payload URL to your API Gateway endpoint.
  5. Choose application/json as the Content type.
  6. Select the events you want to trigger the webhook (e.g., Pull request events).

Step 2: Create AWS API Gateway

  1. Go to the AWS Management Console and open the API Gateway service.
  2. Create a new API.
  3. Define a new resource and method (e.g., POST).
  4. Set up the integration with your Lambda function.

Step 3: Create AWS Lambda Function

  1. Go to the AWS Lambda service.
  2. Create a new Lambda function.
  3. Add the necessary permissions to the Lambda function to invoke SSM commands.
  4. Write the code to handle the web-hook event and trigger the test execution on the EC2 instance. This is a sample code written for a windows instance, you can take reference from it.
import json
import boto3
import requests

def lambda_handler(event, context):
    # Parse GitHub webhook payload
    body = json.loads(event['body'])

    # Check if the action is 'opened' or 'synchronize' (when commits are added)
    if body['action'] not in ['opened', 'synchronize']:
        return {
            'statusCode': 200,
            'body': json.dumps('No action taken')
        }

    # Get repository details
    repo_name = body['repository']['full_name']
    pr_number = body['number']

    # Send command to EC2 instance via SSM
    ssm = boto3.client('ssm')
    response = ssm.send_command(
        InstanceIds=['i-0abcdef1234567890'],  # Replace with your EC2 instance ID
        DocumentName='AWS-RunPowerShellScript',
        Parameters={'commands': ['cd path\\to\\repo', 'C:\Program Files\Git\cmd\git pull origin repo_name', 'python test_script.py']}
    )

    # Wait for command to complete
    command_id = response['Command']['CommandId']
    output = ssm.get_command_invocation(
        CommandId=command_id,
        InstanceId='i-0abcdef1234567890'  # Replace with your EC2 instance ID
    )

    # Check if tests passed
    if 'passed' in output['StandardOutputContent']:
        # Merge the PR
        github_token = 'your_github_token'
        headers = {'Authorization': f'token {github_token}'}
        merge_url = f'https://api.github.com/repos/{repo_name}/pulls/{pr_number}/merge'
        merge_response = requests.put(merge_url, headers=headers)

        return {
            'statusCode': merge_response.status_code,
            'body': json.dumps(merge_response.json())
        }

    return {
        'statusCode': 200,
        'body': json.dumps('Tests failed, PR not merged')
    }

Step 4: Set Up AWS Systems Manager (SSM)

  1. Ensure your EC2 instance has the SSM agent installed and is registered with Systems Manager.
  2. Create an SSM document(If needed) to define the test script execution.

Step 5: Run Tests on EC2 Instance

  1. The test script on your EC2 instance will run the necessary tests.
  2. Capture the test results and send them back to the Lambda function.

Step 6: Merge PR if Tests Pass

  1. Update the Lambda function to handle the test results.
  2. If tests pass, use the GitHub API to merge the PR.

Conclusion

By following the steps outlined in this guide, you can automate the testing and merging of PRs using GitHub webhooks and AWS services. This not only ensures that your main branch is always in a deployable state but also saves time and reduces the chances of human error. Happy coding!