feat: Add UploadReleaseAssetFromRelease convenience helper
This PR adds a new convenience method:
UploadReleaseAssetFromRelease(ctx context.Context, release *RepositoryRelease, opts *UploadOptions, file *os.File)
Why?
GitHub's REST API returns the upload_url directly inside the RepositoryRelease object.
Currently, uploading a release asset requires callers to manually specify:
- owner
- repo
- release ID
- AND to configure a separate Upload API endpoint (especially for GHES)
This helper solves that by allowing users to upload an asset directly using the
release.UploadURL returned from the API.
What this PR does
- Adds a new method
UploadReleaseAssetFromReleaseinrepos_releases.go - Reuses the existing UploadReleaseAsset logic (addOptions, NewUploadRequest, client.Do)
- Trims the
{?name,label}template fromupload_urlbefore uploading - Includes code comments, documentation URL, and the
//meta:operationdirective so the codegen pipeline works correctly
Tests and validation
- All tests passed (
script/test.sh) - Code formatted (
script/fmt.sh) - Generated files updated (
script/generate.sh) - Lint passed (
script/lint.sh) - No changes needed in
openapi_operations.yaml
Reference
Fixes #3849
Thanks!
Codecov Report
:x: Patch coverage is 93.33333% with 2 lines in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 92.36%. Comparing base (e279f70) to head (501de8f).
:warning: Report is 6 commits behind head on master.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| github/repos_releases.go | 93.33% | 1 Missing and 1 partial :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## master #3851 +/- ##
==========================================
- Coverage 92.46% 92.36% -0.11%
==========================================
Files 199 199
Lines 14240 14338 +98
==========================================
+ Hits 13167 13243 +76
- Misses 884 895 +11
- Partials 189 200 +11
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
I wonder if "rendering" the template URL by stripping the curly braces is the best approach - should GitHub change their URL and move these variables into e.g. path components it would instantly break. Should one maybe add the dependency for a uritemplate module?
Thanks for the feedback! I’ve added the tests and updated the URL handling as discussed. I’ll now work on improving the patch coverage by adding tests for the remaining branches so Codecov passes.
Regarding the URI-template approach, I’m open to implementing that as well if the maintainers prefer it.
Thanks also for the note about accepting a more flexible input than *os.File. I’ll update the helper to match the NewUploadRequest API (io.Reader + size) and adjust the tests accordingly.
If there are any other suggestions or preferred directions, I’m happy to update the PR. I’ll push the next set of changes shortly.
Also, please see if you can improve the code coverage stats by taking advantage of some of the test helper functions you see being used in other tests like testBadOptions for example.
Got it. I’ll use the existing helpers (like testBadOptions) to improve coverage. Working on it.