|
20 | 20 | push_to_remote_repository, |
21 | 21 | add_merge_request_labels, |
22 | 22 | add_blocking_merge_request_comment, |
| 23 | + retry_pipeline_job, |
| 24 | + get_failed_pipeline_jobs_from_merge_request, |
23 | 25 | ) |
24 | 26 | from test_utils import mock_git_repo_basepath |
25 | 27 |
|
@@ -295,3 +297,115 @@ async def test_create_merge_request_checklist(): |
295 | 297 | ) |
296 | 298 |
|
297 | 299 | assert result == f"Successfully created checklist for merge request {merge_request_url}" |
| 300 | + |
| 301 | + |
| 302 | +@pytest.mark.asyncio |
| 303 | +async def test_retry_pipeline_job(): |
| 304 | + project_url = "https://gitlab.com/redhat/rhel/rpms/bash" |
| 305 | + job_id = 12345678 |
| 306 | + |
| 307 | + flexmock(GitlabService).should_receive("get_project_from_url").with_args( |
| 308 | + url=project_url |
| 309 | + ).and_return( |
| 310 | + flexmock( |
| 311 | + gitlab_repo=flexmock( |
| 312 | + jobs=flexmock().should_receive("get").with_args(job_id).and_return( |
| 313 | + flexmock(id=job_id, status="pending").should_receive("retry").once().mock() |
| 314 | + ).mock() |
| 315 | + ) |
| 316 | + ) |
| 317 | + ) |
| 318 | + |
| 319 | + result = await retry_pipeline_job(project_url=project_url, job_id=job_id) |
| 320 | + |
| 321 | + assert result == f"Successfully retried job {job_id}. Status: pending" |
| 322 | + |
| 323 | + |
| 324 | +@pytest.mark.asyncio |
| 325 | +async def test_retry_pipeline_job_invalid_project(): |
| 326 | + project_url = "https://gitlab.com/nonexistent/project" |
| 327 | + job_id = 12345678 |
| 328 | + |
| 329 | + flexmock(GitlabService).should_receive("get_project_from_url").with_args( |
| 330 | + url=project_url |
| 331 | + ).and_raise(Exception("Project not found")) |
| 332 | + |
| 333 | + with pytest.raises(Exception) as exc_info: |
| 334 | + await retry_pipeline_job(project_url=project_url, job_id=job_id) |
| 335 | + |
| 336 | + assert "Failed to retry job" in str(exc_info.value) |
| 337 | + |
| 338 | + |
| 339 | +@pytest.mark.asyncio |
| 340 | +async def test_get_failed_pipeline_jobs_from_merge_request(): |
| 341 | + merge_request_url = "https://gitlab.com/redhat/centos-stream/rpms/bash/-/merge_requests/123" |
| 342 | + pipeline_id = 789 |
| 343 | + |
| 344 | + flexmock(GitlabService).should_receive("get_project_from_url").with_args( |
| 345 | + url="https://gitlab.com/redhat/centos-stream/rpms/bash" |
| 346 | + ).and_return( |
| 347 | + flexmock().should_receive("get_pr").with_args(123).and_return( |
| 348 | + flexmock( |
| 349 | + _raw_pr=flexmock(head_pipeline={"id": pipeline_id}), |
| 350 | + target_project=flexmock( |
| 351 | + namespace="redhat/centos-stream/rpms", |
| 352 | + repo="bash", |
| 353 | + gitlab_repo=flexmock( |
| 354 | + pipelines=flexmock().should_receive("get").with_args(pipeline_id).and_return( |
| 355 | + flexmock( |
| 356 | + jobs=flexmock().should_receive("list").with_args(get_all=True).and_return([ |
| 357 | + flexmock(id=11111, name="check-tickets", status="failed", stage="build", artifacts_file={"filename": "debug.log"}), |
| 358 | + flexmock(id=22222, name="build_rpm", status="failed", stage="test", artifacts_file=None), |
| 359 | + flexmock(id=33333, name="trigger_tests", status="success", stage="test", artifacts_file=None), |
| 360 | + ]).mock() |
| 361 | + ) |
| 362 | + ).mock() |
| 363 | + ) |
| 364 | + ), |
| 365 | + ) |
| 366 | + ).mock() |
| 367 | + ) |
| 368 | + |
| 369 | + result = await get_failed_pipeline_jobs_from_merge_request(merge_request_url=merge_request_url) |
| 370 | + |
| 371 | + assert len(result) == 2 |
| 372 | + assert result[0].id == "11111" |
| 373 | + assert result[0].name == "check-tickets" |
| 374 | + assert result[0].status == "failed" |
| 375 | + assert result[0].stage == "build" |
| 376 | + assert "/-/jobs/11111" in result[0].url |
| 377 | + assert result[0].artifacts_url == "https://gitlab.com/redhat/centos-stream/rpms/bash/-/jobs/11111/artifacts/browse" |
| 378 | + |
| 379 | + assert result[1].id == "22222" |
| 380 | + assert result[1].name == "build_rpm" |
| 381 | + assert result[1].status == "failed" |
| 382 | + assert result[1].stage == "test" |
| 383 | + assert "/-/jobs/22222" in result[1].url |
| 384 | + assert result[1].artifacts_url == "" |
| 385 | + |
| 386 | + |
| 387 | +@pytest.mark.asyncio |
| 388 | +async def test_get_failed_pipeline_jobs_from_merge_request_no_pipelines(): |
| 389 | + merge_request_url = "https://gitlab.com/redhat/centos-stream/rpms/bash/-/merge_requests/123" |
| 390 | + |
| 391 | + flexmock(GitlabService).should_receive("get_project_from_url").with_args( |
| 392 | + url="https://gitlab.com/redhat/centos-stream/rpms/bash" |
| 393 | + ).and_return( |
| 394 | + flexmock().should_receive("get_pr").with_args(123).and_return( |
| 395 | + flexmock(_raw_pr=flexmock(head_pipeline=None)) |
| 396 | + ).mock() |
| 397 | + ) |
| 398 | + |
| 399 | + result = await get_failed_pipeline_jobs_from_merge_request(merge_request_url=merge_request_url) |
| 400 | + |
| 401 | + assert len(result) == 0 |
| 402 | + |
| 403 | + |
| 404 | +@pytest.mark.asyncio |
| 405 | +async def test_get_failed_pipeline_jobs_from_merge_request_invalid_url(): |
| 406 | + merge_request_url = "https://github.com/user/repo/pull/123" |
| 407 | + |
| 408 | + with pytest.raises(Exception) as exc_info: |
| 409 | + await get_failed_pipeline_jobs_from_merge_request(merge_request_url=merge_request_url) |
| 410 | + |
| 411 | + assert "Could not parse merge request URL" in str(exc_info.value) |
0 commit comments