fix: DownloadReleaseAsset handles renamed repository#3392
fix: DownloadReleaseAsset handles renamed repository#3392gmlewis merged 3 commits intogoogle:masterfrom
Conversation
This fixes issue google#3081, such that `Repositories.DownloadReleaseAsset` correctly handles scenarios where a non-`nil` `followRedirectsClient` argument has been passed, but the specified repository has been renamed to a new name. Previously, in such scenarios... 1. The initial `GET /repos/<OWNER>/<REPO>/releases/assets/<ASSET ID>` responds 301 w/ a `Location: <GH API HOST>/repositories/<REPO ID>/releases/assets/<ASSET ID>`. 2. The `followRedirectsClient` issues a `GET <GH API HOST>/repositories/<REPO ID>/releases/assets/<ASSET ID>` with a `Accept: */*` header. 3. `GET <GH API HOST>/repositories/<REPO ID>/releases/assets/<ASSET ID>` responds 200 with a JSON response body (rather than 302 w/ a `Location: <MEDIA SERVER ASSET URL>` header) due to the presence of the `Accept: */*` header (and the absence of a `Accept: application/octet-stream` header)`from step 2. The following `curl` offers an example of correct redirect follows & correct `Accept: application/octet-stream`-preservation in this scenario: ``` curl \ --verbose \ --location \ --header "Accept: application/octet-stream" \ https://api.github.com/repos/mterwill/go-github-issue-demo/releases/assets/151970555 ``` Now, as per this change: 1. The initial `GET /repos/<OWNER>/<REPO>/releases/assets/<ASSET ID>` responds 301 w/ a `Location: <GH API HOST>/repositories/<REPO ID>/releases/assets/<ASSET ID>`. 2. The `followRedirectsClient` issues a `GET <GH API HOST>/repositories/<REPO ID>/releases/assets/<ASSET ID>` with a `Accept: application/octet-stream` header [as required by the GH API](https://docs.github.com/en/enterprise-cloud@latest/rest/releases/assets?apiVersion=2022-11-28) 3. `GET <GH API HOST>/repositories/<REPO ID>/releases/assets/<ASSET ID>` responds 302 w/ a `Location: <MEDIA SERVER ASSET URL>`. 4. The `followRedirectsClient` follows the redirects to `GET <MEDIA SERVER ASSET URL>` w/ a `Accept: application/octet-stream` header, and `GET <MEDIA SERVER ASSET URL>` responds 200 w/ the asset contents. Signed-off-by: Mike Ball <mikedball@gmail.com>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #3392 +/- ##
==========================================
- Coverage 97.72% 92.21% -5.51%
==========================================
Files 153 173 +20
Lines 13390 14770 +1380
==========================================
+ Hits 13085 13620 +535
- Misses 215 1060 +845
Partials 90 90 ☔ View full report in Codecov by Sentry. |
This improves `RepositoriesService.DownloadReleaseAsset` documentation to be a bit more specific and accurate, as per: google#3081 (comment) Signed-off-by: Mike Ball <mikedball@gmail.com>
gmlewis
left a comment
There was a problem hiding this comment.
LGTM.
Awaiting second LGTM+Approval from any other contributor to this repo before merging.
|
Thank you, @tomfeigin ! |
Per request of @deiga (integrations#2514 (comment)), this adds tests for previewHeaderInjectorTransport.RoundTrip logic around application/octest-stream header handling. See google/go-github#3392 for context. Signed-off-by: Mike Ball <mikedball@gmail.com>
Per request of @deiga (integrations#2514 (comment)), this adds tests for previewHeaderInjectorTransport.RoundTrip logic around application/octest-stream header handling. See google/go-github#3392 for context. Signed-off-by: Mike Ball <mikedball@gmail.com>
Per request of @deiga (integrations#2514 (comment)), this adds tests for previewHeaderInjectorTransport.RoundTrip logic around application/octest-stream header handling. See google/go-github#3392 for context. Signed-off-by: Mike Ball <mikedball@gmail.com>
Per request of @deiga (integrations#2514 (comment)), this adds tests for previewHeaderInjectorTransport.RoundTrip logic around application/octest-stream header handling. See google/go-github#3392 for context. Signed-off-by: Mike Ball <mikedball@gmail.com>
Per request of @deiga (integrations#2514 (comment)), this adds tests for previewHeaderInjectorTransport.RoundTrip logic around application/octest-stream header handling. See google/go-github#3392 for context. Signed-off-by: Mike Ball <mikedball@gmail.com>
Per request of @deiga (integrations#2514 (comment)), this adds tests for previewHeaderInjectorTransport.RoundTrip logic around application/octest-stream header handling. See google/go-github#3392 for context. Signed-off-by: Mike Ball <mikedball@gmail.com>
Per request of @deiga (integrations#2514 (comment)), this adds tests for previewHeaderInjectorTransport.RoundTrip logic around application/octest-stream header handling. See google/go-github#3392 for context. Signed-off-by: Mike Ball <mikedball@gmail.com>
Per request of @deiga (integrations#2514 (comment)), this adds tests for previewHeaderInjectorTransport.RoundTrip logic around application/octest-stream header handling. See google/go-github#3392 for context. Signed-off-by: Mike Ball <mikedball@gmail.com>
Per request of @deiga (integrations#2514 (comment)), this adds tests for previewHeaderInjectorTransport.RoundTrip logic around application/octest-stream header handling. See google/go-github#3392 for context. Signed-off-by: Mike Ball <mikedball@gmail.com>
* feat: add github_release_asset data source This addresses issue #2513 and adds support for a `github_release_asset` data source. Example of passing acceptance tests: ``` GITHUB_ORGANIZATION=mterwill \ GITHUB_OWNER=mterwill \ TF_ACC=1 \ go test -v ./... -run ^TestAccGithubReleaseAssetDataSource ? github.com/integrations/terraform-provider-github/v6 [no test files] === RUN TestAccGithubReleaseAssetDataSource === RUN TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID === RUN TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_anonymous_account provider_utils.go:51: GITHUB_TOKEN environment variable should be empty provider_utils.go:74: Skipping TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_anonymous_account which requires anonymous mode === RUN TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_individual_account === RUN TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_organization_account --- PASS: TestAccGithubReleaseAssetDataSource (11.65s) --- PASS: TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID (11.65s) --- SKIP: TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_anonymous_account (0.00s) --- PASS: TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_individual_account (8.90s) --- PASS: TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_organization_account (2.75s) PASS ok github.com/integrations/terraform-provider-github/v6/github 12.434s ``` Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(config): test previewHeaderInjectorTransport.RoundTrip Per request of @deiga (#2514 (comment)), this adds tests for previewHeaderInjectorTransport.RoundTrip logic around application/octest-stream header handling. See google/go-github#3392 for context. Signed-off-by: Mike Ball <mikedball@gmail.com> * chore: github_release_asset data source tests use new test patterns Per code review feedback from @deiga (#2514 (comment)), this updates the github_release_asset data source tests to use the new testing patterns implemented in #2986 This was tested via: ``` $ TF_ACC=1 \ go test -v ./... -run ^TestAccGithubReleaseAssetDataSource ? github.com/integrations/terraform-provider-github/v6 [no test files] === RUN TestAccGithubReleaseAssetDataSource === RUN TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID --- PASS: TestAccGithubReleaseAssetDataSource (5.91s) --- PASS: TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID (5.91s) PASS ok github.com/integrations/terraform-provider-github/v6/github 6.788s ``` Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset): use if err := ...; err != nil pattern Per code review feedback (#2514 (comment)) from @stevehipwell, this tweaks the `data_source_github_release_asset` to use the `if err := ...; err != nil` pattern towards the goal of improving readability. Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset): improve terseness Use idiomatic `:=` pattern over `var`-pre-defined variables, per code review feedback from @stevehipwell: #2514 (comment) Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset_test): inline ComposeTestCheckFunc This inlines `resource.ComposeTestCheckFunc` to improve readability (rather than define it as a variable), per code review feedback from @stevehipwell: #2514 (comment) Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset): source config from acc_test.go Per code review feedback from @stevehipwell, this simplifies test configuration: - source test configuration from `testAccConf`, as done elsewhere - remove the ability to override test configuration via `GH_TEST_*` env vars; this is arguably premature optimization and can be added to `acc_test.go` if it's needed in the future Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset): remove unnecessary TestCase.Providers Per code review feedback from @deiga, this removes unnecessary Providers configuration from the data_source_github_release_asset tests. #2514 (comment) Signed-off-by: Mike Ball <mikedball@gmail.com> * fix(data_source_github_release_asset): use composite ID To protect against the possibility that release asset IDs are not globally unique across GitHub, this configures the use of a 3-part composite ID when tracking `data_source_github_release_asset` instances in TF state. This addresses code review feedback from @deiga: #2514 (comment) Signed-off-by: Mike Ball <mikedball@gmail.com> * fix(data_source_github_release_asset): avoid client mutation Per code review feedback from @stevehipwell (#2514 (comment)), this avoids the use of `client.Repositories.DownloadReleaseAsset` out of concern the function modifies the GitHub client state which could cause unexpected behaviour in Terraform. Signed-off-by: Mike Ball <mikedball@gmail.com> * fix(data_source_github_release_asset): properly set updated_at This fixes a copy/paste bug; updated_at is now correctly set on the data source. Signed-off-by: Mike Ball <mikedball@gmail.com> * feat(data_source_github_release_asset): make download configurable Per code review feedback from @stevehipwell (#2514 (review)), this makes the release asset download optional, controlled by a `download_body` attribute, and off by default. Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset): use `download_file`/`file` attributes Replace `download_body` argument and `body` attributes w/ `download_file`/`file`, respectively, towards the goal of improving the data source interface. This addresses code review feedback from @stevehipwell: #2514 (comment) Signed-off-by: Mike Ball <mikedball@gmail.com> * fix(data_source_github_release_asset): base64 encode `file` Per code review feedback from @stevehipwell (#2514 (comment)), this ensures `file` is base64-encoded. Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset): adjust file contents attributes Per code review feedback from @stevehipwell, this changes `download_file`/`file` to `download_file_contents`/`file_contents`, respectively: #2514 (comment) Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset): improve acc test This improves the `data_source_github_release_asset` acceptance tests to be more TF-focused via use of the `base64decode` function, per code review request of @stevehipwell: #2514 (comment) Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset_test): appease linting This commits the result of `gofumpt`, as per lint failures in: https://github.com/integrations/terraform-provider-github/actions/runs/21232219897/job/61621068985?pr=2514 Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(config_test): appease lint errors This appeases lint warnings in `github/config_test.go`: https://github.com/integrations/terraform-provider-github/actions/runs/21232219897/job/61621068985?pr=2514 Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(config): address staticcheck errors This addresses the following staticcheck error (seen in https://github.com/integrations/terraform-provider-github/actions/runs/21232219897/job/61621068985?pr=2514): ``` Error: github/config.go:196:13: QF1001: could apply De Morgan's law (staticcheck) ``` Signed-off-by: Mike Ball <mikedball@gmail.com> * fix(config_test): use canonical HTTP header names in expectedHeaders Go's http.Header canonicalizes header names (e.g., X-GitHub-Api-Version becomes X-Github-Api-Version). The test's unexpected header check was failing because it compared canonical names against non-canonical keys in the expectedHeaders map. Signed-off-by: Mike Ball <mikedball@gmail.com> --------- Signed-off-by: Mike Ball <mikedball@gmail.com>
* feat: add github_release_asset data source This addresses issue integrations#2513 and adds support for a `github_release_asset` data source. Example of passing acceptance tests: ``` GITHUB_ORGANIZATION=mterwill \ GITHUB_OWNER=mterwill \ TF_ACC=1 \ go test -v ./... -run ^TestAccGithubReleaseAssetDataSource ? github.com/integrations/terraform-provider-github/v6 [no test files] === RUN TestAccGithubReleaseAssetDataSource === RUN TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID === RUN TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_anonymous_account provider_utils.go:51: GITHUB_TOKEN environment variable should be empty provider_utils.go:74: Skipping TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_anonymous_account which requires anonymous mode === RUN TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_individual_account === RUN TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_organization_account --- PASS: TestAccGithubReleaseAssetDataSource (11.65s) --- PASS: TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID (11.65s) --- SKIP: TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_anonymous_account (0.00s) --- PASS: TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_individual_account (8.90s) --- PASS: TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_organization_account (2.75s) PASS ok github.com/integrations/terraform-provider-github/v6/github 12.434s ``` Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(config): test previewHeaderInjectorTransport.RoundTrip Per request of @deiga (integrations#2514 (comment)), this adds tests for previewHeaderInjectorTransport.RoundTrip logic around application/octest-stream header handling. See google/go-github#3392 for context. Signed-off-by: Mike Ball <mikedball@gmail.com> * chore: github_release_asset data source tests use new test patterns Per code review feedback from @deiga (integrations#2514 (comment)), this updates the github_release_asset data source tests to use the new testing patterns implemented in integrations#2986 This was tested via: ``` $ TF_ACC=1 \ go test -v ./... -run ^TestAccGithubReleaseAssetDataSource ? github.com/integrations/terraform-provider-github/v6 [no test files] === RUN TestAccGithubReleaseAssetDataSource === RUN TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID --- PASS: TestAccGithubReleaseAssetDataSource (5.91s) --- PASS: TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID (5.91s) PASS ok github.com/integrations/terraform-provider-github/v6/github 6.788s ``` Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset): use if err := ...; err != nil pattern Per code review feedback (integrations#2514 (comment)) from @stevehipwell, this tweaks the `data_source_github_release_asset` to use the `if err := ...; err != nil` pattern towards the goal of improving readability. Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset): improve terseness Use idiomatic `:=` pattern over `var`-pre-defined variables, per code review feedback from @stevehipwell: integrations#2514 (comment) Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset_test): inline ComposeTestCheckFunc This inlines `resource.ComposeTestCheckFunc` to improve readability (rather than define it as a variable), per code review feedback from @stevehipwell: integrations#2514 (comment) Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset): source config from acc_test.go Per code review feedback from @stevehipwell, this simplifies test configuration: - source test configuration from `testAccConf`, as done elsewhere - remove the ability to override test configuration via `GH_TEST_*` env vars; this is arguably premature optimization and can be added to `acc_test.go` if it's needed in the future Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset): remove unnecessary TestCase.Providers Per code review feedback from @deiga, this removes unnecessary Providers configuration from the data_source_github_release_asset tests. integrations#2514 (comment) Signed-off-by: Mike Ball <mikedball@gmail.com> * fix(data_source_github_release_asset): use composite ID To protect against the possibility that release asset IDs are not globally unique across GitHub, this configures the use of a 3-part composite ID when tracking `data_source_github_release_asset` instances in TF state. This addresses code review feedback from @deiga: integrations#2514 (comment) Signed-off-by: Mike Ball <mikedball@gmail.com> * fix(data_source_github_release_asset): avoid client mutation Per code review feedback from @stevehipwell (integrations#2514 (comment)), this avoids the use of `client.Repositories.DownloadReleaseAsset` out of concern the function modifies the GitHub client state which could cause unexpected behaviour in Terraform. Signed-off-by: Mike Ball <mikedball@gmail.com> * fix(data_source_github_release_asset): properly set updated_at This fixes a copy/paste bug; updated_at is now correctly set on the data source. Signed-off-by: Mike Ball <mikedball@gmail.com> * feat(data_source_github_release_asset): make download configurable Per code review feedback from @stevehipwell (integrations#2514 (review)), this makes the release asset download optional, controlled by a `download_body` attribute, and off by default. Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset): use `download_file`/`file` attributes Replace `download_body` argument and `body` attributes w/ `download_file`/`file`, respectively, towards the goal of improving the data source interface. This addresses code review feedback from @stevehipwell: integrations#2514 (comment) Signed-off-by: Mike Ball <mikedball@gmail.com> * fix(data_source_github_release_asset): base64 encode `file` Per code review feedback from @stevehipwell (integrations#2514 (comment)), this ensures `file` is base64-encoded. Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset): adjust file contents attributes Per code review feedback from @stevehipwell, this changes `download_file`/`file` to `download_file_contents`/`file_contents`, respectively: integrations#2514 (comment) Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset): improve acc test This improves the `data_source_github_release_asset` acceptance tests to be more TF-focused via use of the `base64decode` function, per code review request of @stevehipwell: integrations#2514 (comment) Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(data_source_github_release_asset_test): appease linting This commits the result of `gofumpt`, as per lint failures in: https://github.com/integrations/terraform-provider-github/actions/runs/21232219897/job/61621068985?pr=2514 Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(config_test): appease lint errors This appeases lint warnings in `github/config_test.go`: https://github.com/integrations/terraform-provider-github/actions/runs/21232219897/job/61621068985?pr=2514 Signed-off-by: Mike Ball <mikedball@gmail.com> * chore(config): address staticcheck errors This addresses the following staticcheck error (seen in https://github.com/integrations/terraform-provider-github/actions/runs/21232219897/job/61621068985?pr=2514): ``` Error: github/config.go:196:13: QF1001: could apply De Morgan's law (staticcheck) ``` Signed-off-by: Mike Ball <mikedball@gmail.com> * fix(config_test): use canonical HTTP header names in expectedHeaders Go's http.Header canonicalizes header names (e.g., X-GitHub-Api-Version becomes X-Github-Api-Version). The test's unexpected header check was failing because it compared canonical names against non-canonical keys in the expectedHeaders map. Signed-off-by: Mike Ball <mikedball@gmail.com> --------- Signed-off-by: Mike Ball <mikedball@gmail.com>
This fixes issue #3081, such that
Repositories.DownloadReleaseAssetcorrectly handles scenarios where a non-nilfollowRedirectsClientargument has been passed, and the specified repository has been renamed to a new name.Previously, in such scenarios...
GET /repos/<OWNER>/<REPO>/releases/assets/<ASSET ID>responds 301 w/ aLocation: <GH API HOST>/repositories/<REPO ID>/releases/assets/<ASSET ID>.followRedirectsClientissues aGET <GH API HOST>/repositories/<REPO ID>/releases/assets/<ASSET ID>with aAccept: */*header.GET <GH API HOST>/repositories/<REPO ID>/releases/assets/<ASSET ID>responds 200 with a JSON response body (rather than 302 w/ aLocation: <MEDIA SERVER ASSET URL>header) due to the presence of theAccept: */*header (and the absence of aAccept: application/octet-streamheader) from step 2.The following
curloffers an example of correct redirect follows & correctAccept: application/octet-stream-preservation in this scenario:Now, as per this change:
GET /repos/<OWNER>/<REPO>/releases/assets/<ASSET ID>responds 301 w/ aLocation: <GH API HOST>/repositories/<REPO ID>/releases/assets/<ASSET ID>.followRedirectsClientissues aGET <GH API HOST>/repositories/<REPO ID>/releases/assets/<ASSET ID>with aAccept: application/octet-streamheader as required by the GH API.GET <GH API HOST>/repositories/<REPO ID>/releases/assets/<ASSET ID>responds 302 w/ aLocation: <MEDIA SERVER ASSET URL>.followRedirectsClientfollows the redirects toGET <MEDIA SERVER ASSET URL>w/ aAccept: application/octet-streamheader, andGET <MEDIA SERVER ASSET URL>responds 200 w/ the asset contents.