Introduction: The Role of a Changeset Management API

Welcome back! In the previous lesson, you learned how to set up a FastAPI backend for a Code Review Assistant. You created your first endpoint and learned how to store code changes (called changesets) in a database. Now, we will build on that foundation by creating an API to manage these changesets.

A changeset is a group of code changes, usually submitted together for review. In a code review assistant, managing changesets means being able to list them, view their details, and perform actions like scanning or reviewing them. The Changeset Management API is the part of your backend that lets users and other systems interact with these changesets.

By the end of this lesson, you will know how to:

  • List changesets with optional filters.
  • Retrieve detailed information about a specific changeset.
  • Add helpful endpoints for health checks and triggering scans.

Let’s get started!

Quick Recall: FastAPI Endpoints and Models

Before we dive in, let’s quickly remind ourselves how FastAPI endpoints and models work. In the last lesson, you learned how to:

  • Define a FastAPI app using FastAPI().
  • Create models (like Changeset) to represent your data.
  • Set up endpoints using decorators like @app.post() or @app.get().

For example, here’s a simple FastAPI endpoint that returns a list of items:

This is the basic pattern we will use for our changeset management endpoints. Now, let’s see how to list changesets.

Listing Changesets with Filtering and Limiting

The first step in managing changesets is being able to list them. Often, you want to filter the list (for example, by status) or limit how many results you get.

Let’s build this step by step.

Step 1: Define the Endpoint

We start by creating a GET endpoint at /api/changesets. This endpoint will return a list of changesets.

Step 2: Add Filtering and Limiting

We want to allow users to filter changesets by status (like "pending" or "reviewed") and limit the number of results. We do this by adding query parameters to our endpoint.

  • status: Optional[str] = None means the user can provide a status to filter by, but it’s not required.
  • limit: int = 10 sets a default limit of 10 results.
Step 3: Query the Database

Let’s assume we have a database session and a Changeset model. We use these to get the data.

Explanation:

  • We use Depends(get_session) to get a database session.
  • We build a query for Changeset objects.
  • If a status is provided, we filter by it.
  • We order the results by creation date (newest first) and limit the number of results.
  • We return a list of dictionaries with the changeset details.

Example Output:

Now you know how to list changesets with optional filtering and limiting.

Getting Detailed Information for a Changeset

Sometimes, you need more information about a specific changeset, such as the files it changed and any review comments. Let’s build an endpoint for that.

Step 1: Define the Endpoint

We want to get details for a specific changeset by its ID. We use a path parameter for this.

Step 2: Fetch the Changeset

We query the database for the changeset with the given ID.

  • If the changeset is not found, we return a 404 error.
Step 3: Get Related Files

Each changeset may have several files associated with it. We fetch these as well.

Step 4: Add Review Information

If the changeset has been reviewed, we can include review comments for each file. In this example, we generate dummy reviews.

Step 5: Return the Full Details

Finally, we return all the information in a single response.

Example Output:

This endpoint gives you all the details you need about a changeset, including its files and reviews.

Other Helpful Endpoints: Health Check and Scan Trigger

Besides managing changesets, it’s useful to have endpoints for checking if your service is running and for triggering scans of your code repository.

Health Check Endpoint

A health check endpoint lets you confirm that your API is up and running.

Example Output:

Scan Trigger Endpoint

You might want to trigger a scan of your repository to find new changesets.

Example Output:

These endpoints help you monitor and interact with your code review assistant beyond just managing changesets.

Summary And What’s Next

In this lesson, you learned how to build a Changeset Management API using FastAPI. You saw how to:

  • List changesets with optional filters and limits.
  • Retrieve detailed information about a specific changeset, including its files and reviews.
  • Add helpful endpoints for health checks and triggering repository scans.

These features are essential for any code review assistant, making it easy to manage and review code changes. Take a moment to review the code examples and explanations above. In the next section, you’ll get hands-on practice implementing and using these endpoints yourself. Good luck, and keep up the great work!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal