Class: Fastlane::Actions::RedmineFileUploadAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



96
97
98
# File 'lib/fastlane/plugin/redmine_file_upload/actions/redmine_file_upload_action.rb', line 96

def self.authors
  ["Mattia Salvetti"]
end

.available_optionsObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fastlane/plugin/redmine_file_upload/actions/redmine_file_upload_action.rb', line 117

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :redmine_host,
                            env_name: "REDMINE_HOST",
                         description: "Redmine host where upload file. e.g. ",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :redmine_username,
                            env_name: "REDMINE_USERNAME",
                         description: "Redmine username (optional). An API key can be provided instead",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :redmine_password,
                            env_name: "REDMINE_PASSWORD",
                         description: "Redmine password (optional). An API key can be provided instead",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :redmine_api_key,
                            env_name: "REDMINE_API_KEY",
                         description: "Redmine API key (optional). username and password can be provided instead",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :redmine_use_ssl,
                                env_name: "REDMINE_USE_SSL",
                             description: "Redmine USE_SSL key (optional). it use use plain http instead of https",
                                optional: true,
                                    type: String),  
    FastlaneCore::ConfigItem.new(key: :file_path,
                            env_name: "FILE_PATH",
                         description: "Local path of file to upload to redmine",
                            optional: false,
                                type: String)
  ]
end

.descriptionObject



92
93
94
# File 'lib/fastlane/plugin/redmine_file_upload/actions/redmine_file_upload_action.rb', line 92

def self.description
  "A fastlane plugin to upload file contents to Redmine"
end

.detailsObject



111
112
113
114
115
# File 'lib/fastlane/plugin/redmine_file_upload/actions/redmine_file_upload_action.rb', line 111

def self.details
  # Optional:
  "This plugin uses Redmine REST API to attach a generic file and release a token to use for attachment binding to any Redmine entity. It makes a http request to a redmine host POST /uploads.json
  See APIs documentations at http://www.redmine.org/projects/redmine/wiki/Rest_api"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
155
156
157
158
# File 'lib/fastlane/plugin/redmine_file_upload/actions/redmine_file_upload_action.rb', line 152

def self.is_supported?(platform)
  # Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
  # See: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
  #
  # [:ios, :mac, :android].include?(platform)
  true
end

.outputObject



100
101
102
103
104
105
# File 'lib/fastlane/plugin/redmine_file_upload/actions/redmine_file_upload_action.rb', line 100

def self.output
  [
    ['REDMINE_UPLOAD_FILE_TOKEN', 'Token release as response of redmine POST /uploads.json'],
    ['REDMINE_UPLOAD_FILE_NAME', 'Uploading file name']
  ]
end

.return_valueObject



107
108
109
# File 'lib/fastlane/plugin/redmine_file_upload/actions/redmine_file_upload_action.rb', line 107

def self.return_value
  "Returns a token released from redmine http POST to /uploads.json."
end

.run(params) ⇒ Object



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fastlane/plugin/redmine_file_upload/actions/redmine_file_upload_action.rb', line 11

def self.run(params)
  require 'net/http'
  require 'net/http/uploadprogress'
  require 'uri'
  require 'json'

  # getting parameters
  file_path = params[:file_path]
  version_number =  ''
  project_name =  ''

  if ENV.key?('BUILD_CODE') && !ENV['BUILD_CODE'].empty? && !ENV['BUILD_CODE'].nil?
    version_number =  "V" + ENV['BUILD_CODE'] + "_"
  end
  if ENV.key?('PROJECT_NAME')
    project_name = ENV['PROJECT_NAME']
  end
  current_date = Time.new.strftime('%d_%m_%Y')
  new_file_path = File.dirname(file_path) + "/#{project_name}App_" + version_number + current_date+File.extname(file_path) unless file_path.nil?
  File.rename(file_path,new_file_path);
  file_name = File.basename(new_file_path) unless new_file_path.nil?
  file_path  = new_file_path
  


  Actions.lane_context[SharedValues::REDMINE_UPLOAD_FILE_NAME] = file_name

  redmine_url = params[:redmine_host]
  api_key = params[:redmine_api_key]
  redmine_use_sslhost = "true"
  username = params[:redmine_username]
  password = params[:redmine_password]

  unless redmine_use_sslhost.nil?
    redmine_use_sslhost = params[:redmine_use_ssl]
  end

  upload_content_uri = URI.parse(redmine_url + '/uploads.json'+'?filename='+file_name)
  UI.message("Start file upload \"#{file_name}\" to Redmine API #{upload_content_uri}")

  token = nil
  response_upload_content = nil
  File.open(file_path, 'rb') do |io|
    # Create the HTTP objects
    http_upload_content = Net::HTTP.new(upload_content_uri.host, upload_content_uri.port)

    if redmine_use_sslhost == "true"
      http_upload_content.use_ssl = true
      http_upload_content.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end

    request_upload_content = Net::HTTP::Post.new(upload_content_uri.request_uri)
    request_upload_content["Content-Type"] = "application/octet-stream"
    unless api_key.nil?
      request_upload_content["X-Redmine-API-Key"] = api_key.to_s
    end
    unless username.nil? || password.nil?
      request_upload_content.basic_auth(username, password)
    end

    request_upload_content.content_length = io.size
    request_upload_content.body_stream = io
    # print upload progress
    Net::HTTP::UploadProgress.new(request_upload_content) do |progress|
       printf("\rUploading \"#{file_name}\"...  #{100 * progress.upload_size / io.size}%%")
    end
    # Send the request
    response_upload_content = http_upload_content.request(request_upload_content)
    printf("\n")
  end
  case response_upload_content
  when Net::HTTPSuccess
    # get token from upload content response
    token = JSON.parse(response_upload_content.body)['upload']['token']
    UI.success("Content uploaded! File token released: #{token}")
    Actions.lane_context[SharedValues::REDMINE_UPLOAD_FILE_TOKEN] = token
  else
    UI.error(response_upload_content.to_s)
  end
end