Skip to content

Commit

Permalink
Validate branch with prompt (#20)
Browse files Browse the repository at this point in the history
* Validate current branch with a user prompt instead of automatically with a parameter

* Fix tests

* Update sample fastfile
  • Loading branch information
tonidero authored Aug 16, 2022
1 parent 2377a38 commit 078609b
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 34 deletions.
1 change: 0 additions & 1 deletion fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ lane :sample_bump_version_update_changelog_create_pr_action do |options|
github_pr_token: 'github-api-token', # This can also be obtained from ENV GITHUB_PULL_REQUEST_API_TOKEN
github_token: 'github-token',
github_rate_limit: 0,
branch: 'main',
editor: 'vim'
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module Fastlane
module Actions
class BumpVersionUpdateChangelogCreatePrAction < Action
def self.run(params)
branch = params[:branch]
repo_name = params[:repo_name]
github_pr_token = params[:github_pr_token]
github_token = params[:github_token]
Expand All @@ -20,6 +19,11 @@ def self.run(params)
changelog_path = params[:changelog_path]
editor = params[:editor]

current_branch = Actions.git_branch
unless UI.confirm("Current branch is #{current_branch}. Are you sure this is the base branch for your bump?")
UI.user_error!("Cancelled during branch confirmation")
end

UI.important("Current version is #{version_number}")

# Ask for new version number
Expand All @@ -29,7 +33,7 @@ def self.run(params)

new_branch_name = "release/#{new_version_number}"

Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump(branch, new_branch_name, github_pr_token)
Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump(new_branch_name, github_pr_token)

generated_contents = Helper::VersioningHelper.auto_generate_changelog(repo_name, github_token, rate_limit_sleep)
Helper::RevenuecatInternalHelper.edit_changelog(generated_contents, changelog_latest_path, editor)
Expand Down Expand Up @@ -102,11 +106,6 @@ def self.available_options
optional: true,
default_value: 0,
type: Integer),
FastlaneCore::ConfigItem.new(key: :branch,
description: "Allows to execute the action from the given branch",
optional: true,
default_value: "main",
type: String),
FastlaneCore::ConfigItem.new(key: :editor,
env_name: "RC_INTERNAL_FASTLANE_EDITOR",
description: "Allows to override editor to be used when editting the changelog",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def self.run(params)
new_branch_name = "bump/#{next_version_snapshot}"
label = 'next_release'

Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump(nil, new_branch_name, github_pr_token)
Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump(new_branch_name, github_pr_token)

Helper::RevenuecatInternalHelper.create_new_branch_and_checkout(new_branch_name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,14 @@ def self.create_pr_to_main(title, body, repo_name, head_branch, github_pr_token,
)
end

def self.validate_local_config_status_for_bump(current_branch, new_branch, github_pr_token)
def self.validate_local_config_status_for_bump(new_branch, github_pr_token)
# Ensure GitHub API token is set
if github_pr_token.nil? || github_pr_token.empty?
UI.error("A github_pr_token parameter or an environment variable GITHUB_PULL_REQUEST_API_TOKEN is required to create a pull request")
UI.error("Please make a fastlane/.env file from the fastlane/.env.SAMPLE template")
UI.user_error!("Could not find value for GITHUB_PULL_REQUEST_API_TOKEN")
end
ensure_new_branch_local_remote(new_branch)
Actions::EnsureGitBranchAction.run(branch: current_branch) unless current_branch.nil?
Actions::EnsureGitStatusCleanAction.run({})
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
let(:mock_repo_name) { 'mock-repo-name' }
let(:mock_changelog_latest_path) { './fake-changelog-latest-path/CHANGELOG.latest.md' }
let(:mock_changelog_path) { './fake-changelog-path/CHANGELOG.md' }
let(:branch) { 'main' }
let(:editor) { 'vim' }
let(:auto_generated_changelog) { 'mock-auto-generated-changelog' }
let(:edited_changelog) { 'mock-edited-changelog' }
Expand All @@ -16,9 +15,10 @@

it 'calls all the appropriate methods with appropriate parameters' do
allow(FastlaneCore::UI).to receive(:input).with('New version number: ').and_return(new_version)
allow(FastlaneCore::UI).to receive(:confirm).with(anything).and_return(true)
allow(File).to receive(:read).with(mock_changelog_latest_path).and_return(edited_changelog)
expect(Fastlane::Helper::RevenuecatInternalHelper).to receive(:validate_local_config_status_for_bump)
.with(branch, 'release/1.13.0', mock_github_pr_token)
.with('release/1.13.0', mock_github_pr_token)
.once
expect(Fastlane::Helper::VersioningHelper).to receive(:auto_generate_changelog)
.with(mock_repo_name, mock_github_token, 3)
Expand Down Expand Up @@ -53,15 +53,32 @@
github_pr_token: mock_github_pr_token,
github_token: mock_github_token,
github_rate_limit: 3,
branch: branch,
editor: editor
)
end

it 'fails if selected no during prompt validating current branch' do
allow(FastlaneCore::UI).to receive(:confirm).with(anything).and_return(false)
expect do
Fastlane::Actions::BumpVersionUpdateChangelogCreatePrAction.run(
current_version: current_version,
changelog_latest_path: mock_changelog_latest_path,
changelog_path: mock_changelog_path,
files_to_update: ['./test_file.sh', './test_file2.rb'],
files_to_update_without_prerelease_modifiers: ['./test_file3.kt', './test_file4.swift'],
repo_name: mock_repo_name,
github_pr_token: mock_github_pr_token,
github_token: mock_github_token,
github_rate_limit: 3,
editor: editor
)
end.to raise_exception(StandardError)
end
end

describe '#available_options' do
it 'has correct number of options' do
expect(Fastlane::Actions::BumpVersionUpdateChangelogCreatePrAction.available_options.size).to eq(12)
expect(Fastlane::Actions::BumpVersionUpdateChangelogCreatePrAction.available_options.size).to eq(11)
end
end
end
2 changes: 1 addition & 1 deletion spec/actions/create_next_snapshot_version_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

it 'calls all the appropriate methods with appropriate parameters' do
expect(Fastlane::Helper::RevenuecatInternalHelper).to receive(:validate_local_config_status_for_bump)
.with(nil, "bump/#{next_version}", github_pr_token)
.with("bump/#{next_version}", github_pr_token)
.once
expect(Fastlane::Helper::RevenuecatInternalHelper).to receive(:calculate_next_snapshot_version)
.with(current_version)
Expand Down
25 changes: 7 additions & 18 deletions spec/helper/revenuecat_internal_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,26 +202,25 @@
before(:each) do
allow(Fastlane::Actions).to receive(:sh).with('git', 'branch', '--list', 'new-branch').and_return('')
allow(Fastlane::Actions).to receive(:sh).with('git', 'ls-remote', '--heads', 'origin', 'new-branch').and_return('')
allow(Fastlane::Actions::EnsureGitBranchAction).to receive(:run).with(branch: 'fake-branch')
allow(Fastlane::Actions::EnsureGitStatusCleanAction).to receive(:run)
end

it 'fails if github_pr_token is nil' do
expect do
Fastlane::Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump('fake-branch', 'new-branch', nil)
Fastlane::Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump('new-branch', nil)
end.to raise_exception(StandardError)
end

it 'fails if github_pr_token is empty' do
expect do
Fastlane::Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump('fake-branch', 'new-branch', '')
Fastlane::Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump('new-branch', '')
end.to raise_exception(StandardError)
end

it 'fails if new branch exists locally' do
expect(Fastlane::Actions).to receive(:sh).with('git', 'branch', '--list', 'new-branch').and_return('new-branch').once
expect do
Fastlane::Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump('fake-branch', 'new-branch', 'fake-github-pr-token')
Fastlane::Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump('new-branch', 'fake-github-pr-token')
end.to raise_exception(StandardError)
end

Expand All @@ -230,35 +229,25 @@
.with('git', 'ls-remote', '--heads', 'origin', 'new-branch')
.and_return('59f7273ae446cef04eb402b9708f0772389c59c4 refs/heads/new-branch')
expect do
Fastlane::Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump('fake-branch', 'new-branch', 'fake-github-pr-token')
Fastlane::Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump('new-branch', 'fake-github-pr-token')
end.to raise_exception(StandardError)
end

it 'works if git returns ssh warning' do
expect(Fastlane::Actions).to receive(:sh)
.with('git', 'ls-remote', '--heads', 'origin', 'new-branch')
.and_return("Warning: Permanently added the ECDSA host key for IP address 'xxx.xxx.xxx.xxx' to the list of known hosts.")
Fastlane::Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump('fake-branch', 'new-branch', 'fake-github-pr-token')
Fastlane::Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump('new-branch', 'fake-github-pr-token')
end

it 'ensures new branch does not exist remotely' do
expect(Fastlane::Actions).to receive(:sh).with('git', 'ls-remote', '--heads', 'origin', 'new-branch').once
Fastlane::Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump('fake-branch', 'new-branch', 'fake-github-pr-token')
end

it 'ensures repo is in specified branch' do
expect(Fastlane::Actions::EnsureGitBranchAction).to receive(:run).with(branch: 'fake-branch').once
Fastlane::Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump('fake-branch', 'new-branch', 'fake-github-pr-token')
end

it 'does not check repo is in specific branch if none passed' do
expect(Fastlane::Actions::EnsureGitBranchAction).not_to receive(:run)
Fastlane::Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump(nil, 'new-branch', 'fake-github-pr-token')
Fastlane::Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump('new-branch', 'fake-github-pr-token')
end

it 'ensures repo is in a clean state' do
expect(Fastlane::Actions::EnsureGitStatusCleanAction).to receive(:run).with({}).once
Fastlane::Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump('fake-branch', 'new-branch', 'fake-github-pr-token')
Fastlane::Helper::RevenuecatInternalHelper.validate_local_config_status_for_bump('new-branch', 'fake-github-pr-token')
end
end

Expand Down

0 comments on commit 078609b

Please sign in to comment.