Skip to content

feat: Add sort order to cohorts endpoint#38149

Open
brianjbuck-wgu wants to merge 3 commits intoopenedx:masterfrom
WGU-Open-edX:feature/38119-cohorts-sort-order
Open

feat: Add sort order to cohorts endpoint#38149
brianjbuck-wgu wants to merge 3 commits intoopenedx:masterfrom
WGU-Open-edX:feature/38119-cohorts-sort-order

Conversation

@brianjbuck-wgu
Copy link
Contributor

Description

Adds alphabetical ordering to the GET /api/cohorts/v1/courses/{course_id}/cohorts/ endpoint. Previously, the response order was non-deterministic (database insertion order). Cohorts are now returned in ascending alphabetical order by name by default, with an optional ordering query parameter to control sort direction.

Changes:

  • get_course_cohorts() now accepts an ordering parameter ('asc' or 'desc', default 'asc') and applies ORDER BY name to the queryset. All callers benefit from deterministic ordering.
  • CohortHandler.GET reads the ?ordering=asc|desc query parameter, validates it, and passes it through. Returns HTTP 400 for invalid values.

User roles impacted: Developers and Operators consuming the cohorts API. No UI changes.

Supporting information

Testing instructions

  1. Create a course with several cohorts whose names are out of alphabetical order.
  2. GET /api/cohorts/v1/courses/{course_id}/cohorts/ — verify cohorts are returned A→Z.
  3. GET /api/cohorts/v1/courses/{course_id}/cohorts/?ordering=desc — verify cohorts are returned Z→A.
  4. GET /api/cohorts/v1/courses/{course_id}/cohorts/?ordering=invalid — verify HTTP 400 is returned.

Deadline

None

Other information

  • No database migrations required.
  • This is a non-breaking change. Existing callers without the ordering param will now receive cohorts in ascending alphabetical order, which is an improvement over the previous non-deterministic ordering. No response schema changes.
  • A v2 endpoint was considered but deemed unnecessary since this is purely an ordering improvement with no schema changes.

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Mar 10, 2026
@openedx-webhooks
Copy link

openedx-webhooks commented Mar 10, 2026

Thanks for the pull request, @brianjbuck-wgu!

This repository is currently maintained by @openedx/wg-maintenance-openedx-platform.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds deterministic alphabetical sorting to the cohorts list endpoint (GET /api/cohorts/v1/courses/{course_id}/cohorts/) by default, and introduces an ordering=asc|desc query parameter to control sort direction.

Changes:

  • Adds ordering query param handling in CohortHandler.GET, returning HTTP 400 for invalid values.
  • Extends cohorts.get_course_cohorts() with an ordering argument and applies ORDER BY name.
  • Adds API tests covering default ascending order, descending order, and invalid ordering behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
openedx/core/djangoapps/course_groups/views.py Parses/validates ordering query param and passes it to cohort retrieval.
openedx/core/djangoapps/course_groups/cohorts.py Adds ordering parameter and orders cohort queryset by name.
openedx/core/djangoapps/course_groups/tests/test_api_views.py Adds tests validating ordering behavior and invalid input handling.
Comments suppressed due to low confidence (1)

openedx/core/djangoapps/course_groups/cohorts.py:337

  • get_course_cohorts accepts both course and course_id, but the docstring’s Arguments section doesn’t mention course_id. Please document course_id (and clarify the XOR requirement) to keep the signature and docs consistent.
def get_course_cohorts(course=None, course_id=None, assignment_type=None, ordering='asc'):
    """
    Get a list of all the cohorts in the given course. This will include auto cohorts,
    regardless of whether or not the auto cohorts include any users.

    Arguments:
        course: the course for which cohorts should be returned
        assignment_type: cohort assignment type
        ordering: sort direction for results by name ('asc' or 'desc'), defaults to 'asc'

    Returns:
        A list of CourseUserGroup objects. Empty if there are no cohorts. Does
        not check whether the course is cohorted.
    """
    assert bool(course) ^ bool(course_id), "course or course_id required"
    # Migrate cohort settings for this course

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

@brianjbuck-wgu brianjbuck-wgu marked this pull request as ready for review March 11, 2026 16:58
@mphilbrick211 mphilbrick211 added the mao-onboarding Reviewing this will help onboard devs from an Axim mission-aligned organization (MAO). label Mar 11, 2026
@mphilbrick211 mphilbrick211 moved this from Needs Triage to Ready for Review in Contributions Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mao-onboarding Reviewing this will help onboard devs from an Axim mission-aligned organization (MAO). open-source-contribution PR author is not from Axim or 2U

Projects

Status: Ready for Review

Development

Successfully merging this pull request may close these issues.

4 participants