Class: Fastlane::Actions::JiraCommentUpdateAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/jira_update/actions/jira_comment_update_action.rb

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



141
142
143
# File 'lib/fastlane/plugin/jira_update/actions/jira_comment_update_action.rb', line 141

def self.authors
  ["Flop Butylkin"]
end

.available_optionsObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/fastlane/plugin/jira_update/actions/jira_comment_update_action.rb', line 94

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") 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 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,
                                 env_name: "FL_JIRA_TICKET",
                                 description: "Jira ticket id",
                                 type: String,
                                 optional: true,
                                 default_value: nil),
    FastlaneCore::ConfigItem.new(key: :comment_search,
                                 description: "Jira comment text to find",
                                 type: String,
                                 optional: true,
                                 default_value: nil),
    FastlaneCore::ConfigItem.new(key: :comment_text,
                                 description: "Jira comment text to add",
                                 type: String,
                                 default_value: "comment"),
    FastlaneCore::ConfigItem.new(key: :fail_if_not_found,
                                 description: "Fail if Issue or comment not found",
                                 type: Boolean,
                                 default_value: false),
    FastlaneCore::ConfigItem.new(key: :update_comment,
                                 description: "Update comment if found otherwise will delete and add new comment",
                                 type: Boolean,
                                 default_value: false)
  ]
end

.categoryObject



164
165
166
# File 'lib/fastlane/plugin/jira_update/actions/jira_comment_update_action.rb', line 164

def self.category
  :misc
end

.descriptionObject



78
79
80
# File 'lib/fastlane/plugin/jira_update/actions/jira_comment_update_action.rb', line 78

def self.description
  "Jira ticket comment replace action"
end

.detailsObject



82
83
84
# File 'lib/fastlane/plugin/jira_update/actions/jira_comment_update_action.rb', line 82

def self.details
  "Replace Jira ticket comment"
end

.example_codeObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/fastlane/plugin/jira_update/actions/jira_comment_update_action.rb', line 149

def self.example_code
  [
    '
    gym
    slack(message: "Done")
    jira_comment_update(
      ticket: "APP-132",
      search: "Test comment",
      comment: "Test comment updated",
      update_comment: true
    )
    '
  ]
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/fastlane/plugin/jira_update/actions/jira_comment_update_action.rb', line 145

def self.is_supported?(platform)
  true
end

.return_typeObject



90
91
92
# File 'lib/fastlane/plugin/jira_update/actions/jira_comment_update_action.rb', line 90

def self.return_type
  :hash
end

.return_valueObject



86
87
88
# File 'lib/fastlane/plugin/jira_update/actions/jira_comment_update_action.rb', line 86

def self.return_value
  "Hash where keys are Jira ticket IDs and values results of the action"
end

.run(params) ⇒ Object



4
5
6
7
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fastlane/plugin/jira_update/actions/jira_comment_update_action.rb', line 4

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

  client = JIRA::Client.new(
    username:     params[:username],
    password:     params[:password],
    site:         params[:url],
    context_path: '',
    auth_type:    :basic
  )

  ticket = params[:ticket]
  search = params[:comment_search]
  username = params[:username]
  comment = params[:comment_text]
  update_comment = params[:update_comment] || false
  fail_if_not_found = params[:fail_if_not_found] || false


  UI.message("JIRA ticket to update: '#{ticket}'")


  begin
    issue = client.Issue.find(ticket)
    
    UI.message("JIRA ticket: '#{issue.id}'")

    if search
      # search comment
      UI.message("Searching for comment: '#{search}'")
      comment_obj = issue.comments.find { |comment| 
        comment.body.include?(search) && comment.author['displayName'] == username
      }

      UI.error!("Comment not found") if fail_if_not_found && !comment_obj
      UI.message("Comment found: '#{comment_obj.body}'") if comment_obj

      if comment_obj && update_comment
        comment_obj.save({ 'body' => comment })
        UI.message("Comment updated: '#{comment_obj.body}'")
      elsif comment_obj
        comment_obj.delete
        UI.message("Comment deleted: '#{comment_obj.body}'")
      end
      
      if !update_comment
        new_comment = issue.comments.build
        new_comment.save({ 'body' => comment })
        UI.message("Comment added: '#{new_comment.body}'")
      end

    else 
      UI.message("No search provided, will add new comment")
      new_comment = issue.comments.build
      new_comment.save({ 'body' => comment })
      UI.message("Comment added: '#{new_comment.body}'")
    end

  rescue StandardError => e
    UI.important("Failed to fetch issue #{ticket}: #{e.message}")
    UI.error!("Failed to fetch issue #{ticket}: #{e.message}") if fail_if_not_found
    result = "Ticket not found"
  end

  UI.success("JIRA ticket updated: '#{issue.id}'")

  return issue
end