Skip to content

Commit

Permalink
Add publish_release action
Browse files Browse the repository at this point in the history
  • Loading branch information
iangmaia committed Aug 28, 2024
1 parent e8f74c4 commit 598682b
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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 (`<org>/<repo>`) 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
27 changes: 26 additions & 1 deletion lib/fastlane/plugin/wpmreleasetoolkit/helper/github_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<org>/<repo>).
# @param [String] tag_name The name of the git tag to get the associated release of
#
Expand All @@ -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)
Expand Down
62 changes: 62 additions & 0 deletions spec/publish_release_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 598682b

Please sign in to comment.