From 598682b7aa11ca3031a97c7ee7a0e08dbaeaa9f5 Mon Sep 17 00:00:00 2001 From: Ian Maia Date: Wed, 28 Aug 2024 19:00:10 +0200 Subject: [PATCH] Add `publish_release` action --- .../actions/common/publish_release_action.rb | 63 +++++++++++++++++++ .../wpmreleasetoolkit/helper/github_helper.rb | 27 +++++++- spec/publish_release_spec.rb | 62 ++++++++++++++++++ 3 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 lib/fastlane/plugin/wpmreleasetoolkit/actions/common/publish_release_action.rb create mode 100644 spec/publish_release_spec.rb diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/publish_release_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/publish_release_action.rb new file mode 100644 index 000000000..6ed0572c6 --- /dev/null +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/publish_release_action.rb @@ -0,0 +1,63 @@ +require 'fastlane/action' +require_relative '../../helper/github_helper' + +module Fastlane + module Actions + class PublishReleaseAction < Action + def self.run(params) + repository = params[:repository] + name = params[:name] + prerelease = params[:prerelease] + + github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) + + url = github_helper.publish_release( + repository: repository, + name: name, + prerelease: prerelease + ) + UI.success("Successfully published GitHub Release #{name}. You can see it at '#{url}'") + url + end + + def self.description + 'Publishes an existing GitHub Release still in draft mode' + end + + def self.authors + ['Automattic'] + end + + def self.return_value + 'The URL of the published GitHub Release' + end + + def self.details + 'Publishes an existing GitHub Release still in draft mode' + end + + def self.available_options + [ + FastlaneCore::ConfigItem.new(key: :repository, + description: 'The slug (`/`) of the GitHub repository we want to create the release on', + optional: false, + type: String), + FastlaneCore::ConfigItem.new(key: :name, + description: 'The name of the release', + optional: false, + type: String), + FastlaneCore::ConfigItem.new(key: :prerelease, + description: 'True if this is a pre-release', + optional: true, + default_value: false, + type: Boolean), + Fastlane::Helper::GithubHelper.github_token_config_item, + ] + end + + def self.is_supported?(platform) + true + end + end + end +end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb index c52e6c9e3..1fbebbdf0 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb @@ -186,7 +186,7 @@ def generate_release_notes(repository:, tag_name:, previous_tag:, target_commiti res.body end - # Returns the URL of the GitHub release pointing at a given tag + # Returns the URL of the published GitHub release pointing at a given tag # @param [String] repository The repository to create the GitHub release on. Typically a repo slug (/). # @param [String] tag_name The name of the git tag to get the associated release of # @@ -198,6 +198,31 @@ def get_release_url(repository:, tag_name:) nil end + # Publishes an existing GitHub Release still in draft mode. Note that this will search only within the first page of releases, as returned by the GitHub API. + # + # @param [String] repository The repository name, including the organization (e.g. `wordpress-mobile/wordpress-ios`) + # @param [String] name The name of the release to publish. + # @param [Boolean] prerelease Indicates if this should be created as a pre-release (i.e. for alpha/beta) + # + # @return [String] URL of the corresponding GitHub Release + # + def publish_release(repository:, name:, prerelease: false) + releases = client.releases(repository) + release = releases.find { |r| r.name == name } + + UI.user_error!("No release found with version #{name}") unless release + + client.update_release( + release.url, + { + draft: false, + prerelease: prerelease + } + ) + + release.html_url + end + # Downloads a file from the given GitHub tag # # @param [String] repository The repository name (including the organization) diff --git a/spec/publish_release_spec.rb b/spec/publish_release_spec.rb new file mode 100644 index 000000000..5e71d3462 --- /dev/null +++ b/spec/publish_release_spec.rb @@ -0,0 +1,62 @@ +require 'spec_helper' + +describe Fastlane::Actions::PublishReleaseAction do + let(:test_token) { 'ghp_fake_token' } + let(:test_repo) { 'repo-test/project-test' } + let(:test_name) { '1.0.0' } + let(:github_helper) { double(Fastlane::Helper::GithubHelper) } + + before do + allow(Fastlane::Helper::GithubHelper).to receive(:new).and_return(github_helper) + end + + context 'when providing valid parameters' do + it 'publishes the release and returns the URL' do + expect(github_helper).to receive(:publish_release).with( + repository: test_repo, + name: test_name, + prerelease: false + ).and_return('https://github.com/repo-test/project-test/releases/tag/1.0.0') + + result = run_described_fastlane_action( + github_token: test_token, + repository: test_repo, + name: test_name + ) + + expect(result).to eq('https://github.com/repo-test/project-test/releases/tag/1.0.0') + end + + it 'publishes a prerelease when specified' do + expect(github_helper).to receive(:publish_release).with( + repository: test_repo, + name: test_name, + prerelease: true + ).and_return('https://github.com/repo-test/project-test/releases/tag/1.0.0-beta') + + result = run_described_fastlane_action( + github_token: test_token, + repository: test_repo, + name: test_name, + prerelease: true + ) + + expect(result).to eq('https://github.com/repo-test/project-test/releases/tag/1.0.0-beta') + end + end + + context 'when successful' do + it 'prints a success message' do + allow(github_helper).to receive(:publish_release).and_return('https://github.com/repo-test/project-test/releases/tag/1.0.0') + + allow(Fastlane::UI).to receive(:success).with(anything).at_least(:once) + expect(Fastlane::UI).to receive(:success).with("Successfully published GitHub Release 1.0.0. You can see it at 'https://github.com/repo-test/project-test/releases/tag/1.0.0'") + + run_described_fastlane_action( + github_token: test_token, + repository: test_repo, + name: test_name + ) + end + end +end