Skip to content

fix(report): raise FossologyApiError when report ID cannot be parsed#178

Open
Valyrian-Code wants to merge 1 commit into
fossology:mainfrom
Valyrian-Code:fix/generate-report-parse-id
Open

fix(report): raise FossologyApiError when report ID cannot be parsed#178
Valyrian-Code wants to merge 1 commit into
fossology:mainfrom
Valyrian-Code:fix/generate-report-parse-id

Conversation

@Valyrian-Code
Copy link
Copy Markdown
Contributor

@Valyrian-Code Valyrian-Code commented May 18, 2026

Closes #176.

The regex [0-9]*$ (zero-or-more) always matches even when the response message has no trailing digits, so re.search returned an empty-string match and the caller got "" with no error. Switched to [0-9]+$ and raise FossologyApiError when 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 as FossologyApiError rather than leaking raw exceptions.

The return value is coerced to int to match the docstring :rtype: int and the download_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.

Copilot AI review requested due to automatic review settings May 18, 2026 16:39
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 FossologyApiError when 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.

Comment thread fossology/report.py Outdated
@Valyrian-Code
Copy link
Copy Markdown
Contributor Author

Valyrian-Code commented May 21, 2026

Hi @deveaud-m ,

I’ve provided a minimal patch for this PR. Could you please review it when you have a chance?

@Valyrian-Code
Copy link
Copy Markdown
Contributor Author

Valyrian-Code commented May 22, 2026

Hi @deveaud-m

Heads up: the failing Integration Tests job here isn't caused by this PR. The pytest run itself completes successfully (192 passed, 2 skipped, 5 xfailed, 2 xpassed); the failure is in the "upload codecoverage results only if we are on the repository fossology/fossology-python" step that runs afterwards:

poetry run codecov -t 
codecov: error: argument --token/-t: expected one argument
##[error]Process completed with exit code 2.

The step's -t ${{ secrets.CODECOV_TOKEN }} substitutes to an empty value on fork PRs (secrets aren't passed to fork workflows), and the codecov CLI then exits non-zero, which fails the whole job. The step's intent appears to be "skip on forks" per its name, but the current conditional doesn't actually prevent it from running.

Happy to leave this for you to handle on the workflow side — just flagging so you don't have to dig.

Comment thread fossology/report.py
@Valyrian-Code Valyrian-Code force-pushed the fix/generate-report-parse-id branch from 8b2d46a to 512114d Compare May 22, 2026 22:18
@Valyrian-Code
Copy link
Copy Markdown
Contributor Author

Valyrian-Code commented May 22, 2026

Hi @deveaud-m (Thanks for the review)

Updates

  • Added try/except handling for:

    • ValueError
    • KeyError
    • TypeError
  • Added 2 new tests for:

    • Malformed JSON body
    • Missing "message" key

Verification

  • Rebased on the latest main
  • Verified all 11 report tests pass locally using the Fossology 4.4.0 container

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread fossology/report.py Outdated
Comment on lines +64 to +70
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)
Comment thread fossology/report.py Outdated
Comment on lines +69 to +71
)
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>
@Valyrian-Code Valyrian-Code force-pushed the fix/generate-report-parse-id branch from 512114d to b78db0c Compare May 23, 2026 09:55
@Valyrian-Code
Copy link
Copy Markdown
Contributor Author

Valyrian-Code commented May 23, 2026

Addressed both review points:

  1. Moved re.search inside the try block so cases where message is not a string (triggering a TypeError) are now handled by the existing except (ValueError, KeyError, TypeError) path and correctly re-raised as FossologyApiError.
  2. Cast the returned report ID to int to align with the documented return type (:rtype: int) and the download_report(report_id: int, ...) signature.

Also added two regression tests:

  • test_generate_report_non_string_message — validates the new TypeError handling path.
  • test_generate_report_returns_int — ensures the successful path returns an int rather than a str.

Verified all five execution paths locally against a FOSSology container, along with a separate offline pure unit-test run.

@deveaud-m
Copy link
Copy Markdown
Collaborator

deveaud-m commented May 23, 2026

@Valyrian-Code please fix according to copilot's review comments

@Valyrian-Code
Copy link
Copy Markdown
Contributor Author

Hi @deveaud-m

Both points addressed in b78db0c. Could you re-approve CI on this branch when you get a chance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

generate_report silently returns empty string when report ID cannot be parsed

3 participants