Skip to content

Instantly share code, notes, and snippets.

@mariia-cherniuk
Last active September 23, 2020 18:34
Show Gist options
  • Save mariia-cherniuk/df774d4f8f7a25d91c882d0011af7d49 to your computer and use it in GitHub Desktop.
Save mariia-cherniuk/df774d4f8f7a25d91c882d0011af7d49 to your computer and use it in GitHub Desktop.
module Fastlane
module Actions
class CheckoutBranchForJiraIssueAction < Action
def self.run(params)
key = params[:key].split('/').last
branch_name = self.branch_name(key)
branch = "#{key}_#{branch_name}"
sh "git fetch"
ref_count = sh "git show-ref refs/remotes/origin/#{branch} | wc -l"
if ref_count.include? "1"
puts "Branch exists, checking out..."
sh "git checkout #{branch} && git pull"
return
end
puts "Creating branch..."
sh "git checkout develop && git pull"
sh "git checkout -b #{branch}"
sh "git push --set-upstream origin #{branch}"
end
def self.branch_name(key)
options = FastlaneCore::Configuration.create(JiraQueryAction.available_options, { jql_query: "key = #{key}" })
ticket = JiraQueryAction.run(options).first
sanitized = ticket.summary.gsub(/[^0-9A-Za-z\-]/, '_').downcase
return sanitized
end
def self.available_options
[
FastlaneCore::ConfigItem.new(key: :key,
optional: false,
description: "JIRA ticket ID/KEY")
]
end
def self.is_supported?(platform)
true
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment