Skip to content

feat(users): backfill destination email via a second PATCH when handle differs - #645

Merged
michael-richey merged 3 commits into
mainfrom
michael.richey/v2-user-email-backfill
Jul 27, 2026
Merged

feat(users): backfill destination email via a second PATCH when handle differs#645
michael-richey merged 3 commits into
mainfrom
michael.richey/v2-user-email-backfill

Conversation

@michael-richey

Copy link
Copy Markdown
Collaborator

Summary

v2 user creation (POST /api/v2/users) derives the destination handle from the create-time email and does not accept handle directly. When a source user's handle isn't itself their email address, this leaves no way to make the destination handle match the source handle.

This PR adds a two-call create path for that case:

  1. POST /api/v2/users with the source handle passed as the create-time email, so the destination derives a handle matching the source handle.
  2. PATCH /api/v2/users/{id} to set the real source email.

When the source handle already equals the source email, create is unchanged (a single POST).

Behavior

  • Handle differs from email → POST (email=handle) then PATCH (email=real email).
  • Handle equals email, or handle/email missing → single POST, no PATCH.
  • If the email-restoring PATCH fails, the created user (with the handle as a placeholder email) is persisted to state so the next run's diff routes it through the update path instead of re-attempting a create that now 409s on the already-taken handle.

Test plan

  • POST uses handle as email, PATCH restores the real email
  • No second call when handle already equals email
  • PATCH failure persists partial state and still raises
  • Existing v2 create/update payload and handle-mapping-key tests unaffected
  • Full unit suite (tox -e py311)

@michael-richey
michael-richey requested a review from Copilot July 27, 2026 19:23
@michael-richey
michael-richey marked this pull request as ready for review July 27, 2026 19:23
@michael-richey
michael-richey requested a review from a team as a code owner July 27, 2026 19:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a two-step v2 user creation flow to preserve the source handle when it differs from the source email, by creating the user with email=handle and then PATCHing the real email afterward.

Changes:

  • Extend Users.create_resource() to optionally do a follow-up PATCH to restore the real email when handle != email.
  • Persist partially-created user state when the email-restoring PATCH fails to avoid re-creates that would 409 on the already-taken handle.
  • Add unit tests covering the two-call flow, the no-op case when handle == email, and persistence on PATCH failure.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
datadog_sync/model/users.py Implements conditional “email backfill” logic via a second PATCH and persists state on PATCH failure.
tests/unit/test_users.py Adds tests validating POST/PATCH behavior, no-backfill behavior, and failure-state persistence.

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

Comment thread datadog_sync/model/users.py Outdated
michael-richey and others added 3 commits July 27, 2026 15:44
…e differs

v2 user creation derives the destination handle from the create-time email,
so when a source user's handle isn't itself their email, create with the
handle as the email (matching the destination handle to the source handle),
then PATCH the real email in a second call. Falls back to a single create
call when handle and email already match.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@michael-richey
michael-richey force-pushed the michael.richey/v2-user-email-backfill branch from 8a9da73 to 085808a Compare July 27, 2026 19:51
@michael-richey
michael-richey merged commit 800b571 into main Jul 27, 2026
11 checks passed
@michael-richey
michael-richey deleted the michael.richey/v2-user-email-backfill branch July 27, 2026 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants