class Fastlane::Actions::SetJiraFixVersionAction

Public Class Methods

account_manager(params) click to toggle source
# File lib/fastlane/plugin/set_jira_fix_version/actions/set_jira_fix_version_action.rb, line 31
def self.account_manager(params)
  require 'credentials_manager'

  username = params[:username]
  password = params[:password]

  # gets password via interactive shell if needed
  return CredentialsManager::AccountManager.new(
    user: username,
    password: password,
    prefix: "set_jira_fix_version",
    note: "set_jira_fix_version fastlane action"
  )
end
authors() click to toggle source
# File lib/fastlane/plugin/set_jira_fix_version/actions/set_jira_fix_version_action.rb, line 160
def self.authors
  ["yuriy-tolstoguzov", "vadimux"]
end
available_options() click to toggle source
# File lib/fastlane/plugin/set_jira_fix_version/actions/set_jira_fix_version_action.rb, line 120
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",
                                 optional: true,
                                 description: "Username for JIRA"),
    FastlaneCore::ConfigItem.new(key: :password,
                                 env_name: "FL_JIRA_PASSWORD",
                                 description: "Password for JIRA",
                                 optional: true,
                                 sensitive: true),
    FastlaneCore::ConfigItem.new(key: :auth_type,
                                 description: "Authorization type to use. Currently supported: :basic or :cookie",
                                 default_value: :basic),
    FastlaneCore::ConfigItem.new(key: :use_cookies,
                                 description: "Whether JIRA client should use cookies",
                                 default_value: false),
    FastlaneCore::ConfigItem.new(key: :issue_ids,
                                 description: "Issue IDs or keys for JIRA, i.e. [\"IOS-123\", \"IOS-123\"]",
                                 optional: true,
                                 is_string: false),
    FastlaneCore::ConfigItem.new(key: :version_name,
                                 env_name: "FL_JIRA_VERSION_NAME",
                                 description: "Version name that will be set as fix version to specified issues.\nIf version does not exist, it will be created",
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :project_key,
                                 env_name: "FL_JIRA_PROJECT_ID",
                                 description: "Project ID or key where version will be created if needed",
                                 is_string: true,
                                 optional: true)
  ]
end
category() click to toggle source
# File lib/fastlane/plugin/set_jira_fix_version/actions/set_jira_fix_version_action.rb, line 176
def self.category
  :misc
end
description() click to toggle source

@!group Documentation

# File lib/fastlane/plugin/set_jira_fix_version/actions/set_jira_fix_version_action.rb, line 107
def self.description
  "Adds fix version to specified JIRA issues. Creates version if needed"
end
details() click to toggle source
# File lib/fastlane/plugin/set_jira_fix_version/actions/set_jira_fix_version_action.rb, line 111
def self.details
  [
    "This action allows to easily add fix version to specified JIRA issues. It supports basic and cookie authorization.",
    "If JIRA account password won't be specified in parameters (recommended) it will ask it at first launch and save in a macOS keychain.",
    "If JIRA version with specified name is not created, this action will create it. The action can take issue IDs from lane context",
    "(FL_JIRA_ISSUE_IDS) and many other JIRA related parameters from environment variables."
  ].join("\n")
end
example_code() click to toggle source
# File lib/fastlane/plugin/set_jira_fix_version/actions/set_jira_fix_version_action.rb, line 164
def self.example_code
  [
    'set_jira_fix_version(
      url: "https://jira.yourdomain.com",
      username: "Your username",
      issue_ids: ["IOS-1000", "IOS-1001"],
      version_name: "1.0.0 (123)",
      project_key: "IOS"
    )'
  ]
end
is_supported?(platform) click to toggle source
# File lib/fastlane/plugin/set_jira_fix_version/actions/set_jira_fix_version_action.rb, line 180
def self.is_supported?(platform)
  true
end
issue_ids_from_param(params) click to toggle source
# File lib/fastlane/plugin/set_jira_fix_version/actions/set_jira_fix_version_action.rb, line 46
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
run(params) click to toggle source
# File lib/fastlane/plugin/set_jira_fix_version/actions/set_jira_fix_version_action.rb, line 4
def self.run(params)
  Actions.verify_gem!('jira-ruby')
  require 'jira-ruby'

  site         = params[:url]
  auth_type    = params[:auth_type]
  use_cookies  = params[:use_cookies]
  project_id   = params[:project_key]
  version_name = params[:version_name]
  issue_ids    = self.issue_ids_from_param(params)

  account_manager = self.account_manager(params)
  username = account_manager.user
  password = account_manager.password

  options = {
    site: site,
    auth_type: auth_type,
    use_cookies: use_cookies,
    username: username,
    password: password
  }
  client = JIRA::Client.new(options)

  self.save_version(client, version_name, project_id, issue_ids)
end
save_version(client, version_name, project_key, issue_ids) click to toggle source
# File lib/fastlane/plugin/set_jira_fix_version/actions/set_jira_fix_version_action.rb, line 62
def self.save_version(client, version_name, project_key, issue_ids)
  # create new version if needed
  begin
    project = client.Project.find(project_key)
  rescue => error
    UI.error("JIRA API call failed. Check if JIRA is available and correct credentials for user with proper permissions are provided!")
    UI.user_error!(error.response)
    return
  end

  is_version_created = false
  project.versions.each do |version|
    if version.name == version_name
      is_version_created = true
      break
    end
  end

  # if the version does not exist then create this JIRA version
  if is_version_created == false
    version = project.versions.build
    create_version_parameters = { "name" => version_name, "projectId" => project.id }
    version.save(create_version_parameters)
    UI.message("#{version_name} version is created.")
  else
    UI.message("#{version_name} version already exists and will be used as a fix version.")
  end

  # update issues with fix version
  issue_ids.each do |issue_id|
    begin
      issue = client.Issue.find(issue_id)
    rescue
      UI.error("JIRA issue with #{issue_id} is not found or specified user don't have permissions to see it. It won't be updated!")
    end
    add_new_version_parameters = { 'update' => { 'fixVersions' => [{ 'add' => { 'name' => version_name } }] } }
    issue.save(add_new_version_parameters)
    UI.message("#{issue_id} is updated with fix version #{version_name}")
  end
end