fix(report): raise FossologyApiError when report ID cannot be parsed#178
fix(report): raise FossologyApiError when report ID cannot be parsed#178Valyrian-Code wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves generate_report error handling by making report ID parsing fail clearly when the FOSSology API response message does not end with a numeric ID.
Changes:
- Requires at least one trailing digit when extracting the report ID.
- Raises
FossologyApiErrorwhen parsing fails. - Adds a regression test for an unparseable report-generation response message.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
fossology/report.py |
Updates report ID parsing and adds explicit parse-failure handling. |
tests/test_report.py |
Adds regression coverage for a 201 report response without a parseable report ID. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Hi @deveaud-m , I’ve provided a minimal patch for this PR. Could you please review it when you have a chance? |
|
Hi @deveaud-m Heads up: the failing Integration Tests job here isn't caused by this PR. The pytest run itself completes successfully ( The step's Happy to leave this for you to handle on the workflow side — just flagging so you don't have to dig. |
8b2d46a to
512114d
Compare
|
Hi @deveaud-m (Thanks for the review) Updates
Verification
|
| match = re.search(r"[0-9]+$", message) | ||
| if match is None: | ||
| description = ( | ||
| f"Report generation for upload {upload.uploadname} succeeded " | ||
| f"but report ID could not be parsed from response message: {message!r}" | ||
| ) | ||
| raise FossologyApiError(description, response) |
| ) | ||
| raise FossologyApiError(description, response) | ||
| return match[0] |
The regex `[0-9]*$` (zero-or-more) always matches — even on a message with no trailing digits — so `re.search` returned an empty-string match and the caller got `""` with no error. Switch to `[0-9]+$` and raise `FossologyApiError` when no ID can be parsed. Wrap the parse block (JSON decode, message lookup, regex search) in `try/except (ValueError, KeyError, TypeError)` so malformed JSON, missing keys, and non-string `message` values surface as `FossologyApiError` consistently rather than leaking raw exceptions. Coerce the returned report ID to `int` to match the docstring `:rtype: int` and the `download_report(report_id: int, ...)` parameter type. Tests cover the five paths: int return on happy path, missing trailing digits, malformed JSON body, missing "message" key, non-string "message" value. Closes fossology#176 Signed-off-by: RAJVEER42 <irajveer.bishnoi2310@gmail.com>
512114d to
b78db0c
Compare
|
Addressed both review points:
Also added two regression tests:
Verified all five execution paths locally against a FOSSology container, along with a separate offline pure unit-test run. |
|
@Valyrian-Code please fix according to copilot's review comments |
|
Hi @deveaud-m Both points addressed in b78db0c. Could you re-approve CI on this branch when you get a chance? |
Closes #176.
The regex
[0-9]*$(zero-or-more) always matches even when the response message has no trailing digits, sore.searchreturned an empty-string match and the caller got""with no error. Switched to[0-9]+$and raiseFossologyApiErrorwhen no ID can be parsed.Per review, the parse block (JSON decode, message lookup, regex search) is wrapped in
try/except (ValueError, KeyError, TypeError)so malformed JSON, a missing"message"key, or a non-string"message"value all surface asFossologyApiErrorrather than leaking raw exceptions.The return value is coerced to
intto match the docstring:rtype: intand thedownload_report(report_id: int, ...)parameter type.Tests cover five paths: int return on happy path, missing trailing digits, malformed JSON body, missing
"message"key, non-string"message"value.