Class: Fastlane::Actions::RedmineUploadAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



71
72
73
# File 'lib/fastlane/plugin/redmine_upload/actions/redmine_upload_action.rb', line 71

def self.authors
  ["Mattia Salvetti"]
end

.available_optionsObject



92
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
# File 'lib/fastlane/plugin/redmine_upload/actions/redmine_upload_action.rb', line 92

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: :file_path,
                            env_name: "FILE_PATH",
                         description: "Local path of file to upload to redmine",
                            optional: false,
                                type: String)
  ]
end

.descriptionObject



67
68
69
# File 'lib/fastlane/plugin/redmine_upload/actions/redmine_upload_action.rb', line 67

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

.detailsObject



86
87
88
89
90
# File 'lib/fastlane/plugin/redmine_upload/actions/redmine_upload_action.rb', line 86

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)


122
123
124
125
126
127
128
# File 'lib/fastlane/plugin/redmine_upload/actions/redmine_upload_action.rb', line 122

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



75
76
77
78
79
80
# File 'lib/fastlane/plugin/redmine_upload/actions/redmine_upload_action.rb', line 75

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



82
83
84
# File 'lib/fastlane/plugin/redmine_upload/actions/redmine_upload_action.rb', line 82

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
# File 'lib/fastlane/plugin/redmine_upload/actions/redmine_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]
  file_name = File.basename(file_path) unless file_path.nil?

  Actions.lane_context[SharedValues::REDMINE_UPLOAD_FILE_NAME] = file_name

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

  upload_content_uri = URI.parse(redmine_url + '/uploads.json')
  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)
    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.value)
    end
end