Skip to content

Instantly share code, notes, and snippets.

@mariia-cherniuk
Last active September 23, 2020 18:41
Show Gist options
  • Save mariia-cherniuk/5556f48a7d4e0e272418713b9823ff4f to your computer and use it in GitHub Desktop.
Save mariia-cherniuk/5556f48a7d4e0e272418713b9823ff4f to your computer and use it in GitHub Desktop.
module Fastlane
module Actions
class JiraTransitionTicketsAction < Action
def self.run(params)
Actions.verify_gem!('jira-ruby')
require 'jira-ruby'
site = params[:url]
auth_type = :basic
username = params[:username]
password = params[:password]
project_key = params[:project_key]
transition_name = params[:transition]
context_path = params[:context_path]
comment_text = params[:comment]
issue_ids = self.issue_ids_from_param(params)
options = {
site: site,
auth_type: auth_type,
use_cookies: false,
context_path: context_path,
username: username,
password: password
}
client = JIRA::Client.new(options)
issue_ids.each do |issue_id|
begin
issue = client.Issue.find(issue_id)
issue_type = issue.fields["issuetype"]["name"]
UI.important("#{issue_id} is #{issue_type}")
available_transitions = client.Transition.all(:issue => issue)
transition_id = available_transitions.select { |transition| transition.name == transition_name }.map { |transition| transition.id }.first
next if transition_id.to_s.empty?
UI.important("\"#{transition_name}\" id is #{transition_id}")
transition = issue.transitions.build
transition.save!("transition" => { "id" => transition_id })
if comment_text
comment = issue.comments.build
comment.save({"body" => comment_text})
end
rescue => error
UI.user_error!(error.response.body)
raise
end
end
end
def self.issue_ids_from_param(params)
issue_ids = params[:issue_ids]
if issue_ids.nil?
issue_ids = Actions.lane_context[SharedValues::FL_JIRA_ISSUE_IDS]
end
if issue_ids.kind_of?(Array) == false || issue_ids.empty?
UI.user_error!("No issue ids or keys were supplied or the value is not an array.")
return
end
UI.message("Issues: #{issue_ids}")
return issue_ids
end
#####################################################
# @!group Documentation
#####################################################
def self.description
"Apply a JIRA transition to issues mentioned in the changelog"
end
def self.available_options
[
FastlaneCore::ConfigItem.new(key: :url,
env_name: "FL_JIRA_SITE",
description: "URL for Jira instance",
verify_block: proc do |value|
UI.user_error!("No url for Jira given, pass using `url: 'url'`") if value.to_s.length == 0
end),
FastlaneCore::ConfigItem.new(key: :username,
env_name: "FL_JIRA_USERNAME",
description: "Username for JIRA instance",
verify_block: proc do |value|
UI.user_error!("No username") if value.to_s.length == 0
end),
FastlaneCore::ConfigItem.new(key: :password,
env_name: "FL_JIRA_PASSWORD",
description: "Password for Jira",
verify_block: proc do |value|
UI.user_error!("No password") if value.to_s.length == 0
end),
FastlaneCore::ConfigItem.new(key: :project_key,
env_name: "FL_JIRA_TRANSITION_PROJECT_KEY",
description: "Project ID or key where version will be created if needed",
verify_block: proc do |value|
UI.user_error!("No Project Key specified") if value.to_s.length == 0
end),
FastlaneCore::ConfigItem.new(key: :issue_ids,
description: "Issue IDs or keys for JIRA, i.e. [\"IOS-123\", \"IOS-123\"]",
optional: false,
is_string: false),
FastlaneCore::ConfigItem.new(key: :transition,
env_name: "FL_JIRA_TRANSITION",
description: "Transition to apply to the tickets referenced in the changelog",
verify_block: proc do |value|
UI.user_error!("No transition specified") if value.to_s.length == 0
end),
FastlaneCore::ConfigItem.new(key: :comment,
env_name: "FL_JIRA_TRANSITION_COMMENT",
description: "Comment to add if the transition is applied",
optional: true,
verify_block: proc do |value|
UI.user_error!("No transition id specified") if value.to_s.length == 0
end),
FastlaneCore::ConfigItem.new(key: :context_path,
env_name: "FL_JIRA_CONTEXT_PATH",
optional: true,
description: "Context path for JIRA instance",
default_value: "")
]
end
def self.output
end
def self.return_value
end
def self.authors
["Your Name"]
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