Skip to content

Commit 69fd085

Browse files
committed
Fix GitHub release asset lookup
1 parent 93464af commit 69fd085

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ _None_
1414

1515
### Bug Fixes
1616

17-
_None_
17+
- `upload_github_release_assets`: make release lookup more resilient by falling back to GitHub's direct release-by-tag lookup when the releases list does not include the requested release.
1818

1919
### Internal Changes
2020

lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def create_release(repository:, version:, description:, assets:, prerelease:, is
200200
# @raise [Fastlane::UI::Error] UI.user_error! if the release does not exist.
201201
#
202202
def get_release(repository:, version:)
203-
release = client.releases(repository).find { |candidate| candidate.tag_name == version }
203+
release = find_release(repository: repository, version: version)
204204
return release unless release.nil?
205205

206206
UI.user_error!("Could not find GitHub Release for tag #{version} in #{repository}")
@@ -244,6 +244,22 @@ def upload_release_assets(repository:, version:, assets:, replace_existing: true
244244
release.html_url
245245
end
246246

247+
def find_release(repository:, version:)
248+
release = client.releases(repository).find { |candidate| candidate.tag_name == version }
249+
return release unless release.nil?
250+
251+
release_for_tag(repository: repository, version: version)
252+
end
253+
254+
def release_for_tag(repository:, version:)
255+
client.release_for_tag(repository, version)
256+
rescue Octokit::NotFound
257+
nil
258+
end
259+
260+
private :find_release
261+
private :release_for_tag
262+
247263
# Use the GitHub API to generate release notes based on the list of PRs between current tag and previous tag.
248264
# @note This API uses the `.github/release.yml` config file to classify the PRs by category in the generated list according to PR labels.
249265
#

spec/github_helper_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,12 +640,14 @@ def create_release(is_draft:, assets: [], name: nil)
640640

641641
before do
642642
allow(Octokit::Client).to receive(:new).and_return(client)
643+
allow(client).to receive(:release_for_tag).with(test_repo, test_version).and_return(release)
643644
allow(client).to receive(:releases).with(test_repo).and_return([release])
644645
allow(client).to receive(:release_assets).with(release_url).and_return(existing_assets)
645646
allow(client).to receive_messages(upload_asset: uploaded_asset, delete_release_asset: true)
646647
end
647648

648649
it 'fails clearly if the release does not exist' do
650+
allow(client).to receive(:release_for_tag).with(test_repo, test_version).and_raise(Octokit::NotFound)
649651
allow(client).to receive(:releases).with(test_repo).and_return([])
650652

651653
with_tmp_file(named: 'test-app.zip') do |file_path|
@@ -702,6 +704,7 @@ def create_release(is_draft:, assets: [], name: nil)
702704
draft_release = sawyer_resource_stub(url: release_url, html_url: release_html_url, tag_name: test_version, draft: true)
703705
other_release = sawyer_resource_stub(url: 'https://api.github.com/repos/repo-test/project-test/releases/456', html_url: 'https://github.com/repo-test/project-test/releases/tag/0.9.0', tag_name: '0.9.0')
704706

707+
allow(client).to receive(:release_for_tag).with(test_repo, test_version).and_raise(Octokit::NotFound)
705708
allow(client).to receive(:releases).with(test_repo).and_return([other_release, draft_release])
706709
allow(client).to receive(:release_assets).with(release_url).and_return([])
707710

@@ -714,6 +717,34 @@ def create_release(is_draft:, assets: [], name: nil)
714717
end
715718
end
716719

720+
it 'uses the release list without calling the direct release-by-tag lookup' do
721+
draft_release = sawyer_resource_stub(url: 'draft-api-url', html_url: 'draft-html-url', tag_name: test_version, draft: true)
722+
723+
allow(client).to receive(:releases).with(test_repo).and_return([draft_release])
724+
expect(client).not_to receive(:release_for_tag)
725+
allow(client).to receive(:release_assets).with(draft_release.url).and_return([])
726+
727+
with_tmp_file(named: 'test-app.zip') do |file_path|
728+
expect(client).to receive(:upload_asset).with(draft_release.url, file_path, { content_type: 'application/octet-stream' })
729+
730+
result = upload_release_assets(assets: [file_path])
731+
732+
expect(result).to eq(draft_release.html_url)
733+
end
734+
end
735+
736+
it 'falls back to the direct release-by-tag lookup when the release list misses' do
737+
with_tmp_file(named: 'test-app.zip') do |file_path|
738+
allow(client).to receive(:releases).with(test_repo).and_return([])
739+
allow(client).to receive(:release_for_tag).with(test_repo, test_version).and_return(release)
740+
expect(client).to receive(:upload_asset).with(release_url, file_path, { content_type: 'application/octet-stream' })
741+
742+
result = upload_release_assets(assets: [file_path])
743+
744+
expect(result).to eq(release_html_url)
745+
end
746+
end
747+
717748
it 'uploads one asset to the existing release' do
718749
with_tmp_file(named: 'test-app.zip') do |file_path|
719750
expect(client).to receive(:upload_asset).with(release_url, file_path, { content_type: 'application/octet-stream' })

0 commit comments

Comments
 (0)