Class: Fastlane::Actions::JiraAction

Inherits:
Fastlane::Action show all
Defined in:
fastlane/lib/fastlane/actions/jira.rb

Constant Summary

Constants inherited from Fastlane::Action

Fastlane::Action::AVAILABLE_CATEGORIES, Fastlane::Action::RETURN_TYPES

Documentation collapse

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, author, deprecated_notes, details, lane_context, method_missing, other_action, sample_return_value, shell_out_should_use_bundle_exec?, step_text

Class Method Details

.authorsObject



124
125
126
# File 'fastlane/lib/fastlane/actions/jira.rb', line 124

def self.authors
  ["iAmChrisTruman", "crazymanish"]
end

.available_optionsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'fastlane/lib/fastlane/actions/jira.rb', line 60

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: :context_path,
                                env_name: "FL_JIRA_CONTEXT_PATH",
                                description: "Appends to the url (ex: \"/jira\")",
                                optional: true,
                                default_value: ""),
    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 or API token for Jira",
                                 sensitive: true,
                                 verify_block: proc do |value|
                                   UI.user_error!("No password") if value.to_s.length == 0
                                 end),
    FastlaneCore::ConfigItem.new(key: :ticket_id,
                                 env_name: "FL_JIRA_TICKET_ID",
                                 description: "Ticket ID for Jira, i.e. IOS-123",
                                 verify_block: proc do |value|
                                   UI.user_error!("No Ticket specified") if value.to_s.length == 0
                                 end),
    FastlaneCore::ConfigItem.new(key: :comment_text,
                                 env_name: "FL_JIRA_COMMENT_TEXT",
                                 description: "Text to add to the ticket as a comment",
                                 verify_block: proc do |value|
                                   UI.user_error!("No comment specified") if value.to_s.length == 0
                                 end),
    FastlaneCore::ConfigItem.new(key: :fail_on_error,
                                 env_name: "FL_JIRA_FAIL_ON_ERROR",
                                 description: "Should an error adding the Jira comment cause a failure?",
                                 type: Boolean,
                                 optional: true,
                                 default_value: true) # Default value is true for 'Backward compatibility'
  ]
end

.categoryObject



157
158
159
# File 'fastlane/lib/fastlane/actions/jira.rb', line 157

def self.category
  :misc
end

.descriptionObject



56
57
58
# File 'fastlane/lib/fastlane/actions/jira.rb', line 56

def self.description
  "Leave a comment on a Jira ticket"
end

.example_codeObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'fastlane/lib/fastlane/actions/jira.rb', line 132

def self.example_code
  [
    'jira(
      url: "https://bugs.yourdomain.com",
      username: "Your username",
      password: "Your password or API token",
      ticket_id: "IOS-123",
      comment_text: "Text to post as a comment"
    )', # How to get API token: https://developer.atlassian.com/cloud/jira/platform/basic-auth-for-rest-apis/#get-an-api-token
    'jira(
      url: "https://yourserverdomain.com",
      context_path: "/jira",
      username: "Your username",
      password: "Your password or API token",
      ticket_id: "IOS-123",
      comment_text: "Text to post as a comment"
    )',
    'jira(
      ticket_id: "IOS-123",
      comment_text: "Text to post as a comment",
      fail_on_error: false
    )'
  ]
end

.is_supported?(platform) ⇒ Boolean

Returns:



128
129
130
# File 'fastlane/lib/fastlane/actions/jira.rb', line 128

def self.is_supported?(platform)
  true
end

.outputObject



107
108
109
110
111
# File 'fastlane/lib/fastlane/actions/jira.rb', line 107

def self.output
  [
    ['JIRA_JSON', 'The whole Jira API JSON object']
  ]
end

.return_typeObject



120
121
122
# File 'fastlane/lib/fastlane/actions/jira.rb', line 120

def self.return_type
  :hash
end

.return_valueObject



113
114
115
116
117
118
# File 'fastlane/lib/fastlane/actions/jira.rb', line 113

def self.return_value
  [
    "A hash containing all relevant information of the Jira comment",
    "Access Jira comment 'id', 'author', 'body', and more"
  ].join("\n")
end

.run(params) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'fastlane/lib/fastlane/actions/jira.rb', line 8

def self.run(params)
  Actions.verify_gem!('jira-ruby')
  require 'jira-ruby'

  site         = params[:url]
  auth_type    = :basic
  context_path = params[:context_path]
  username     = params[:username]
  password     = params[:password]
  ticket_id    = params[:ticket_id]
  comment_text = params[:comment_text]

  options = {
              site: site,
              context_path: context_path,
              auth_type: auth_type,
              username: username,
              password: password
            }

  begin
    client = JIRA::Client.new(options)
    issue = client.Issue.find(ticket_id)
    comment = issue.comments.build
    comment.save({ 'body' => comment_text })

    # An exact representation of the JSON returned from the JIRA API
    # https://github.com/sumoheavy/jira-ruby/blob/master/lib/jira/base.rb#L67
    json_response = comment.attrs
    raise 'Failed to add a comment on Jira ticket' if json_response.nil?

    Actions.lane_context[SharedValues::JIRA_JSON] = json_response
    UI.success('Successfully added a comment on Jira ticket')
    return json_response
  rescue => exception
    message = "Received exception when adding a Jira comment: #{exception}"
    if params[:fail_on_error]
      UI.user_error!(message)
    else
      UI.error(message)
    end
  end
end