Repository navigation
fix(gitlab): PLTF-3689 claim webhook rows and support hook repair - #692
Conversation
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
…ns cannot double-install Two runs of the GitLab webhook installer could read the same rows and each create a hook on the same project or group (#623). The installer now claims its rows: it locks them with SKIP LOCKED, stamps its run id, and makes every later write conditional on still holding the claim. A run that stalls past the 10-minute lease and loses its rows writes nothing and creates no hook. Each row keeps one uuid and secret across attempts, written before the create, so a hook whose create response was lost still authenticates and the next run adopts it instead of adding a second. sync/repair_gitlab_hooks.py records a reinstall request for a project or group. The installer's next run deletes every hook with our URL on the resource and creates one, then records the request it served, so a repair requested during an install is not lost. Co-authored-by: openhands <openhands@all-hands.dev>
1deb845 to
21ff3d9
Compare
…ards (mutation review) Add tests for SKIP LOCKED, claim order and limit, the admin-check delete under a lost claim, a reinstall whose hook delete fails, repair touching only the request counter, and the hook listing page size.
Mutation reviewMethod: 23 hand-written mutants applied one at a time to the PR's source, running Controls (the PR's central claims)
Nine of ten controls died on the original suite. What makes it work: GitLab is the only fake, the database is real, and the lost-claim tests take the claim over mid-run ( Survivors
C1: SKIP LOCKEDWithout async def test_a_row_locked_by_another_run_is_skipped_not_waited_on(
store, add_row, async_session_maker
):
locked = await add_row('locked')
free = await add_row('free')
async with async_session_maker() as session, session.begin():
await session.execute(
select(GitlabWebhook).where(GitlabWebhook.id == locked).with_for_update()
)
claimed = await asyncio.wait_for(store.claim_rows(uuid4(), CLAIM_LEASE), 5)
assert [row.id for row in claimed] == [free]Verified: passes on the branch, fails with the mutant (the claim waits on the lock and M3: reinstall after a failed deleteIf async def test_reinstall_stops_when_the_old_hooks_cannot_be_deleted(
store, gitlab, add_row, get_row
):
row_id = await add_row(webhook_exists=True, reinstall_requested_gen=1)
old = gitlab.add_hook('project-1')
gitlab.delete_status = WebhookStatus.RATE_LIMITED
await run_job()
assert gitlab.hooks['project-1'] == [old]
assert (await get_row(row_id)).reinstall_done_gen == 0Verified: passes on the branch, fails with the mutant. M2: admin-check delete under a lost claim
M1: repair touches only the request counterThe repair tests start from rows where async def test_repair_request_changes_only_the_request_counter(store, add_row, get_row):
row_id = await add_row(
webhook_exists=True, webhook_uuid='uuid-1', webhook_secret='secret-1'
)
before = await get_row(row_id)
assert await request_repair(GitLabResourceType.PROJECT, 'project-1')
after = await get_row(row_id)
assert after.reinstall_requested_gen == before.reinstall_requested_gen + 1
unchanged = ('webhook_exists', 'webhook_uuid', 'webhook_secret', 'claim_run_id')
assert {f: getattr(after, f) for f in unchanged} == {
f: getattr(before, f) for f in unchanged
}
assert after.reinstall_done_gen == before.reinstall_done_genVerified: passes on the branch, fails with the mutant. M4 / M5: claim order and limitWithout async def test_rows_are_claimed_least_recently_synced_first(store, add_row):
newest = await add_row('newest', last_synced=datetime(2026, 3, 1))
oldest = await add_row('oldest', last_synced=datetime(2026, 1, 1))
middle = await add_row('middle', last_synced=datetime(2026, 2, 1))
claimed = await store.claim_rows(uuid4(), CLAIM_LEASE, limit=2)
assert [row.id for row in claimed] == [oldest, middle]
assert newest not in {row.id for row in claimed}Verified: passes on the branch, fails with each mutant. M13: hook listing page sizeGitLab returns 20 hooks per page by default. Without Equivalent mutants (no test added)
Not a test gap
This comment was generated by an AI assistant on behalf of the user. |
…reinstall Co-authored-by: openhands <openhands@all-hands.dev>
|
Addressed the one open item from the mutation review in 1164483: The equivalent mutants (M6, M8, M10) are left as is: M6's explicit Ran This comment was created by an AI agent (OpenHands) on behalf of the user. |
aivong-openhands
left a comment
There was a problem hiding this comment.
Taste: acceptable. The claim/ownership design is sound. One blocking CI failure remains.
Material finding
Lint python fails on the current head (116448393, CI run 37706845123): mypy reports integrations/gitlab/gitlab_service.py:414: Need type annotation for "hook_ids" [var-annotated]. It was introduced by the pagination commit. See the inline comment for the fix.
Checked and found sound
claim_rowsusesFOR UPDATE SKIP LOCKEDwithORDER BY last_syncedand aLIMIT. Claim columns are set in the same transaction as the select.- All writes, deletes and the release go through
claim_run_id-conditional statements, so a run that lost its claim changes nothing. - The uuid and secret are written before any create call and reused, so a retry can adopt a hook whose create response was lost.
- The installer never writes
reinstall_requested_gen. It records the generation it served, so a repair requested mid-run is not lost. Failed deletes or creates leavereinstall_done_genbehind, so the next run retries. delete_webhooks_with_urllists every page before deleting. This is compatible with theLinkheader returned by_make_request.- No remaining callers of the removed
filter_rows,update_last_syncedorVerifyWebhookStatushelper methods. Other callers ofverify_webhook_conditionsandinstall_webhook_on_resourceare unaffected because the new parameters have defaults. - Tests use a real database and a fake GitLab, take over claims mid-run, and assert on stored state.
Merge gate
Hold the merge until Lint python is green and Python Tests (which runs the Postgres claim tests the author could not run locally) passes on the head commit. Python Tests was still running when this review was written.
[RISK ASSESSMENT]
Verdict: Changes needed: fix the mypy error. Posted as a COMMENT because GitHub does not allow requesting changes on one's own PR; otherwise I would approve.
Key insight: putting the ownership check and stored credentials at the write boundary makes retries safe, which is a better fix than trying to prevent overlapping runs.
Improve this review? If feedback seems incorrect or irrelevant, update the repository's
.agents/skills/custom-codereview-guide.md(with the/codereviewtrigger), then re-request review. The reviewer reads the guide from the PR head.Resolve with AI? Install the iterate skill and run
/iterate.Was this review helpful? React with 👍 or 👎.
This review was generated by an AI agent (OpenHands) on behalf of the user.
Co-authored-by: openhands <openhands@all-hands.dev>
Local validation — overlapping installer runs, lost creates and repairThe bugs: without this PR, the GitLab webhook installer reads rows without claiming them, so two overlapping runs can each install a hook on the same project or group (#623). Each run also generates fresh credentials and writes them only after GitLab answers the create, so when that answer is lost, the next run adopts a hook whose credentials the row never recorded, and GitLab's deliveries to it are rejected. How the test triggers them: the real
|
|
🚀 Released in 1.73.0. |
HUMAN:
AGENT:
Why
Overlapping installer runs can select the same webhook rows and create duplicate hooks. Each run now claims its rows and checks ownership before creating a hook. Keeping credentials across attempts lets a retry adopt a hook created before a lost response.
Summary
python -m sync.repair_gitlab_hooks --project <id>or--group <id>to request repair through generation counters.Issue Number
Refs #623.
How to Test
Video/Screenshots
N/A. No UI change.
Type
Notes
Builds on #681, which adds the claim columns and repair counters.
The repair command records a request for the installer to replace matching hooks. A process stalled after its final ownership check can still create a duplicate hook.
Enterprise server image for this PR: