Skip to content

Commit

Permalink
fix a problem where the fix endpoint is not usable if the MR ID has…
Browse files Browse the repository at this point in the history
…n't been added to the change object (#457)
  • Loading branch information
defreng authored May 27, 2024
1 parent f2b0c4e commit 1ea33e8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
33 changes: 19 additions & 14 deletions src/foxops/routers/changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,30 @@
router = APIRouter()


async def get_change(
async def get_change_id(
incarnation_id: Annotated[int, Path()],
revision: Annotated[int, Path(description="Change revision within the given incarnation")],
change_service: Annotated[ChangeService, Depends(get_change_service)],
) -> Change | ChangeWithMergeRequest:
) -> int:
try:
change_id = await change_service.get_change_id_by_revision(incarnation_id, revision)

match (change_type := await change_service.get_change_type(change_id)):
case DatabaseChangeType.MERGE_REQUEST:
return await change_service.get_change_with_merge_request(change_id)
case DatabaseChangeType.DIRECT:
return await change_service.get_change(change_id)
case _:
raise NotImplementedError(f"Unknown change type {change_type}")
return await change_service.get_change_id_by_revision(incarnation_id, revision)
except ChangeNotFoundError:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Change not found")


async def get_change(
change_id: Annotated[int, Depends(get_change_id)],
change_service: Annotated[ChangeService, Depends(get_change_service)],
) -> Change | ChangeWithMergeRequest:
match (change_type := await change_service.get_change_type(change_id)):
case DatabaseChangeType.MERGE_REQUEST:
return await change_service.get_change_with_merge_request(change_id)
case DatabaseChangeType.DIRECT:
return await change_service.get_change(change_id)
case _:
raise NotImplementedError(f"Unknown change type {change_type}")


class CreateChangeType(enum.Enum):
DIRECT = "direct"
MERGE_REQUEST_MANUAL = "merge_request_manual"
Expand Down Expand Up @@ -142,10 +147,10 @@ async def get_change_details(
},
)
async def fix_incomplete_change(
change: Change = Depends(get_change),
change_service: ChangeService = Depends(get_change_service),
change_id: Annotated[int, Depends(get_change_id)],
change_service: Annotated[ChangeService, Depends(get_change_service)],
):
try:
await change_service.update_incomplete_change(change.id)
await change_service.update_incomplete_change(change_id)
except CannotRepairChangeException as e:
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=str(e))
9 changes: 6 additions & 3 deletions src/foxops/services/change.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ async def get_change(self, change_id: int) -> Change:
if not change.commit_pushed:
raise IncompleteChange(
"the given change is in an incomplete state (commit_pushed=False). "
"Try calling update_incomplete_change(change_id) first."
f"Try 'POST /api/incarnations/{change.incarnation_id}/changes/{change.revision}/fix'"
)

return Change(
Expand All @@ -472,8 +472,11 @@ async def get_change_with_merge_request(self, change_id: int) -> ChangeWithMerge

if change_in_db.type != ChangeType.MERGE_REQUEST:
raise ValueError(f"Change {change_id} is not a merge request change.")
assert change_in_db.merge_request_id is not None
assert change_in_db.merge_request_branch_name is not None
if change_in_db.merge_request_id is None or change_in_db.merge_request_branch_name is None:
raise IncompleteChange(
"the given change is in an incomplete state (MR ID/Branch = null). "
f"Try 'POST /api/incarnations/{change_in_db.incarnation_id}/changes/{change_in_db.revision}/fix'"
)

status = await self._hoster.get_merge_request_status(
incarnation_repository=incarnation_in_db.incarnation_repository,
Expand Down

0 comments on commit 1ea33e8

Please sign in to comment.