Class: Fastlane::Helper::TestappioHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/testappio/helper/testappio_helper.rb

Class Method Summary collapse

Class Method Details

.call_ta_cli(command) ⇒ Object

Run the given command. Streams stdout to UI.message; routes stderr through handle_error on non-zero exit. Masks –api_token in verbose command echo.

Uses Open3.popen3 with the command array form (no shell interpretation), so shell metacharacters in api_token / release_notes / file paths are passed literally to ta-cli without need for manual escaping.



93
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
# File 'lib/fastlane/plugin/testappio/helper/testappio_helper.rb', line 93

def self.call_ta_cli(command)
  UI.message("Starting ta-cli...")
  if FastlaneCore::Globals.verbose?
    UI.verbose("ta-cli command:\n\n")
    UI.command(redact_command(command).to_s)
    UI.verbose("\n\n")
  end

  out = []
  error = []
  out_thread = nil
  err_thread = nil
  Open3.popen3(*command) do |stdin, stdout, stderr, wait_thr|
    stdin.close

    # Read stdout and stderr concurrently to avoid deadlock when ta-cli
    # fills one buffer before we finish draining the other.
    out_thread = Thread.new do
      while (line = stdout.gets)
        out << line
        UI.message(line.strip) unless line.strip.empty?
      end
    end

    err_thread = Thread.new do
      while (line = stderr.gets)
        error << line.strip
      end
    end

    out_thread.join
    err_thread.join

    exit_status = wait_thr.value
    handle_error(error) unless exit_status.success?
  end
  out.join
ensure
  # If popen3's block raised before threads joined, make sure no readers
  # are left holding pipe FDs.
  out_thread&.kill if out_thread&.alive?
  err_thread&.kill if err_thread&.alive?
end

.check_ta_cliObject

Check if ‘ta_cli` exists, install it if not



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fastlane/plugin/testappio/helper/testappio_helper.rb', line 10

def self.check_ta_cli
  unless system('which ta-cli > /dev/null 2>&1')
    UI.error("ta-cli not found, installing")

    unless system('curl -Ls https://github.com/testappio/cli/releases/latest/download/install | bash')
      UI.error("Error installing ta-cli")
      return false
    end
  end

  return true
end

.check_update(self_update) ⇒ Object

Check if there is an update available for ‘ta_cli`, and install it if necessary



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
# File 'lib/fastlane/plugin/testappio/helper/testappio_helper.rb', line 24

def self.check_update(self_update)
  version_output = `ta-cli version`
  UI.verbose(version_output)

  if version_output.include?("breaking changes")
    if self_update
      UI.message("Updating ta-cli due to breaking changes...")
    else
      # UI.user_error! raises FastlaneError; method never returns past this point.
      UI.user_error!("Update necessary due to breaking changes. Please update ta-cli manually or set self_update to true")
    end
  elsif version_output.include?("New version available")
    if self_update
      UI.message("Updating ta-cli...")
    else
      UI.important("New version available, but skipping update as self_update is set to false.")
      return true
    end
  else
    UI.success("ta-cli is already up-to-date")
    return true
  end

  update_command = "curl -Ls https://github.com/testappio/cli/releases/latest/download/install | bash"
  update_output, = Open3.capture3(update_command)
  UI.verbose(update_output)

  if update_output.include?("ta-cli successfully installed") || update_output.include?("ta-cli has been updated")
    version_output = `ta-cli version`
    UI.verbose(version_output)

    if version_output.include?("Update necessary due to breaking changes") || version_output.include?("New version available")
      UI.user_error!("Error updating ta-cli: the version is still outdated")
    else
      UI.success("ta-cli has been updated successfully")
      return true
    end
  else
    UI.user_error!("Error updating ta-cli: #{update_output}")
  end
end

.handle_error(errors) ⇒ Object

Handle errors from ta-cli. Raises user_error only when stderr contains an /Error/-matching line. Non-error stderr (warnings, info) is logged via UI.verbose. The raised message includes the actual error lines so the user has context (auth failure, provisioning issue, network error, etc.) without re-running verbose.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fastlane/plugin/testappio/helper/testappio_helper.rb', line 70

def self.handle_error(errors)
  errors.each do |error|
    next unless error

    if error.include?('Error')
      UI.error(error.to_s)
    else
      UI.verbose(error.to_s)
    end
  end

  error_lines = errors.compact.select { |e| e.include?('Error') }
  return if error_lines.empty?

  UI.user_error!("Error while calling ta-cli: #{error_lines.join(' | ')}")
end