go-github icon indicating copy to clipboard operation
go-github copied to clipboard

feat: Add UploadReleaseAssetFromRelease convenience helper

Open rockygeekz opened this issue 3 weeks ago • 5 comments

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 UploadReleaseAssetFromRelease in repos_releases.go
  • Reuses the existing UploadReleaseAsset logic (addOptions, NewUploadRequest, client.Do)
  • Trims the {?name,label} template from upload_url before uploading
  • Includes code comments, documentation URL, and the //meta:operation directive 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!

rockygeekz avatar Dec 04 '25 05:12 rockygeekz

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.

codecov[bot] avatar Dec 04 '25 12:12 codecov[bot]

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?

klaernie avatar Dec 04 '25 13:12 klaernie

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.

rockygeekz avatar Dec 04 '25 16:12 rockygeekz

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.

gmlewis avatar Dec 04 '25 16:12 gmlewis

Got it. I’ll use the existing helpers (like testBadOptions) to improve coverage. Working on it.

rockygeekz avatar Dec 04 '25 16:12 rockygeekz