← 输入框|github.com|刷新
Skip to content

fix(hvcs-github): skip upload when release asset already exists - #1511

Open
ychampion wants to merge 3 commits into
python-semantic-release:masterfrom
ychampion:fix/github-existing-release-asset-729
Open

ychampion wants to merge 3 commits into
python-semantic-release:masterfrom
ychampion:fix/github-existing-release-asset-729

Conversation

@ychampion

Copy link
Copy Markdown

Purpose

Prevent semantic-release publish (and GitHub release asset uploads) from failing when an asset with the same filename has already been uploaded to the target GitHub release.

Rationale

When semantic-release publish runs on a branch where the latest version tag and release assets have already been published (for example, after semantic-release version reports that no new release will be made), GitHub's POST /repos/{owner}/{repo}/releases/{release_id}/assets endpoint responds with 422 Unprocessable Entity and a structured validation payload containing errors[*].code == "already_exists". Because Github.session registers a raise_for_status() response hook, self.session.post(...) raised HTTPError and Github.upload_dists() failed with AssetUploadError.

To handle already-published assets safely without masking genuine upload failures:

  1. Added _is_asset_already_exists_error(err: HTTPError) -> bool in src/semantic_release/hvcs/github.py to match HTTP 422 responses whose JSON errors list contains an entry with "code": "already_exists".
  2. Updated Github.upload_release_asset() to catch HTTPError around the upload POST, log a warning (Asset '<name>' already exists on release <id>, skipping upload), and return False when _is_asset_already_exists_error() matches, while re-raising HTTPError for all other errors.
  3. Updated Github.upload_dists() to increment n_succeeded only when self.upload_release_asset() returns True, allowing any remaining new distribution files in dist_glob to still be uploaded.

Skipping (rather than deleting and re-uploading) avoids overwriting artifacts built from the original tagged commit and remains compatible with GitHub's immutable releases setting. Narrowly matching errors[*].code == "already_exists" ensures other 422 errors—such as "Cannot upload assets to an immutable release" (#1478), other validation failures, or non-JSON 422 responses—still raise HTTPError / AssetUploadError and exit non-zero.

How did you test?

  • End-to-End tests (tests/e2e/cmd_publish/test_publish.py):
    • test_publish_succeeds_when_github_asset_already_exists: verifies semantic-release publish --tag latest logs a warning for an asset returning HTTP 422 with code == "already_exists", continues uploading remaining distribution files, and exits with code 0.
    • test_publish_fails_on_github_non_duplicate_422: verifies semantic-release publish --tag latest still fails with exit code 1 when GitHub returns HTTP 422 without already_exists (e.g., an immutable release response).
  • Unit tests (tests/unit/semantic_release/hvcs/test_github.py):
    • test_upload_release_asset_skips_when_already_exists: verifies Github.upload_release_asset() returns False and emits a warning log when GitHub returns 422 with already_exists.
    • test_upload_release_asset_fails_on_other_422_errors: verifies Github.upload_release_asset() re-raises HTTPError across immutable-release JSON, other validation error codes, non-dict JSON, and plain-text 422 responses.
    • test_upload_dists_when_release_id_found: verifies Github.upload_dists() returns the count of newly uploaded assets when upload_release_asset() returns [False, True] or [False, False].

How to Verify

  1. Check out the first commit (test(cmd-publish)) and run:
    pytest tests/e2e/cmd_publish/test_publish.py -k already_exists
    Observe that test_publish_succeeds_when_github_asset_already_exists fails with exit code 1 (AssetUploadError ... (HTTP 422)).
  2. Check out the full branch and run:
    ruff format --check .
    ruff check --unsafe-fixes .
    mypy .
    pytest -m unit tests/unit/semantic_release/hvcs/test_github.py
    pytest tests/e2e/cmd_publish/test_publish.py

PR Completion Checklist

  • Reviewed & followed the Contributor Guidelines

  • Changes Implemented & Validation pipeline succeeds

  • Commits follow the Conventional Commits standard
    and are separated into the proper commit type and scope (recommended order: test, build, feat/fix, docs)

  • Appropriate Unit tests added/updated

  • Appropriate End-to-End tests added/updated

  • Appropriate Documentation added/updated and syntax validated for sphinx build (see Contributor Guidelines)

When GitHub returns HTTP 422 with an already_exists error code on asset
upload, log a warning and return False instead of raising HTTPError so
publish skips existing assets while still failing on other 422 errors.

Resolves: python-semantic-release#729

This branch has not been deployed

No deployments
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.

Semantic-released error on already released assets

1 participant