diff --git a/CHANGELOG.md b/CHANGELOG.md index a5597d5cf..7aa211785 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ _None_ ### New Features -_None_ +- `get_prs_between_tags` now accepts a `fail_on_error:` parameter. [#772] ### Bug Fixes diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_between_tags.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_between_tags.rb index f396dc8d7..e68104c01 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_between_tags.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/get_prs_between_tags.rb @@ -25,6 +25,8 @@ def self.run(params) config_file_path: config_file_path ) rescue StandardError => e + raise if params[:fail_on_error] + error_msg = "❌ Error computing the list of PRs since #{previous_tag || 'last release'}: `#{e.message}`" UI.important(error_msg) error_msg # Use error message as GitHub Release body to help us be aware of what went wrong. @@ -102,6 +104,15 @@ def self.available_options 'See https://docs.github.com/en/repositories/releasing-projects-on-github/automatically-generated-release-notes#configuration-options', optional: true, type: String), + FastlaneCore::ConfigItem.new(key: :fail_on_error, + description: 'Whether to fail the lane if the changelog cannot be computed. ' \ + 'When `false` (the default), the error message is returned as the changelog itself, ' \ + 'so that it ends up visible in the GitHub Release body. ' \ + 'Set this to `true` if the caller publishes the release only after this action succeeds, ' \ + 'and would rather stop than publish a release whose notes are an error message', + optional: true, + default_value: false, + type: Boolean), Fastlane::Helper::GithubHelper.github_token_config_item, ] end diff --git a/spec/get_prs_between_tags_spec.rb b/spec/get_prs_between_tags_spec.rb index 1136d2639..0c63fdb2f 100644 --- a/spec/get_prs_between_tags_spec.rb +++ b/spec/get_prs_between_tags_spec.rb @@ -107,8 +107,7 @@ def test_with_params(target_commitish: nil, previous_tag: nil, configuration_fil end describe 'error handling' do - def test_with_params(target_commitish: nil, previous_tag: nil, configuration_file_path: nil, error_msg: 'API Failure') - # Arrange + def stub_failing_api_call(target_commitish: nil, previous_tag: nil, configuration_file_path: nil, error_msg: 'API Failure') allow(client).to receive(:post).with( "repos/#{test_repo}/releases/generate-notes", config_file_path: configuration_file_path, @@ -116,6 +115,16 @@ def test_with_params(target_commitish: nil, previous_tag: nil, configuration_fil tag_name: test_tag_name, target_commitish: target_commitish || test_head_ref ).and_raise(StandardError, error_msg) + end + + def test_with_params(target_commitish: nil, previous_tag: nil, configuration_file_path: nil, error_msg: 'API Failure') + # Arrange + stub_failing_api_call( + target_commitish: target_commitish, + previous_tag: previous_tag, + configuration_file_path: configuration_file_path, + error_msg: error_msg + ) # Act result = run_described_fastlane_action( @@ -154,5 +163,42 @@ def test_with_params(target_commitish: nil, previous_tag: nil, configuration_fil error_msg: '400 - Invalid previous_tag parameter' ) end + + context 'when `fail_on_error` is enabled' do + it 'raises instead of returning the error message as the changelog' do + # Arrange + stub_failing_api_call(previous_tag: '12.2', error_msg: 'API Failure') + + # Act & Assert + expect do + run_described_fastlane_action( + github_token: test_token, + repository: test_repo, + tag_name: test_tag_name, + previous_tag: '12.2', + fail_on_error: true + ) + end.to raise_error(StandardError, 'API Failure') + end + end + + context 'when `fail_on_error` is explicitly disabled' do + it 'returns the error message as the changelog, like the default does' do + # Arrange + stub_failing_api_call(previous_tag: '12.2', error_msg: 'API Failure') + + # Act + result = run_described_fastlane_action( + github_token: test_token, + repository: test_repo, + tag_name: test_tag_name, + previous_tag: '12.2', + fail_on_error: false + ) + + # Assert + expect(result).to eq('❌ Error computing the list of PRs since 12.2: `API Failure`') + end + end end end