Class: Fastlane::Actions::RedmineFilePostAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



78
79
80
# File 'lib/fastlane/plugin/redmine_file_upload/actions/redmine_file_post.rb', line 78

def self.authors
  ["Mattia Salvetti"]
end

.available_optionsObject



91
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
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
# File 'lib/fastlane/plugin/redmine_file_upload/actions/redmine_file_post.rb', line 91

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: :redmine_project,
                            env_name: "REDMINE_PROJECT",
                         description: "Project of redmine",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :file_token,
                            env_name: "FILE_TOKEN",
                         description: "Token of file previously released",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :file_name,
                            env_name: "FILE_NAME",
                         description: "FIle name",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :file_version,
                            env_name: "FILE_VERSION",
                         description: "Version of file",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :file_description,
                            env_name: "FILE_DESCRIPTION",
                         description: "Description of file to upload",
                            optional: true,
                                type: String)

  ]
end

.descriptionObject



74
75
76
# File 'lib/fastlane/plugin/redmine_file_upload/actions/redmine_file_post.rb', line 74

def self.description
  "Uploads a file in a Redmine Files section of a given Redmine project"
end

.detailsObject



86
87
88
89
# File 'lib/fastlane/plugin/redmine_file_upload/actions/redmine_file_post.rb', line 86

def self.details
  # Optional:
  "Uploads a file in a Redmine host under files section of specified Redmine project."
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
151
152
153
# File 'lib/fastlane/plugin/redmine_file_upload/actions/redmine_file_post.rb', line 147

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

.return_valueObject



82
83
84
# File 'lib/fastlane/plugin/redmine_file_upload/actions/redmine_file_post.rb', line 82

def self.return_value
  # If your method provides a return value, you can describe here what it does
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/redmine_file_upload/actions/redmine_file_post.rb', line 4

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

  redmine_url = params[:redmine_host]
  api_key = params[:redmine_api_key]
  redmine_use_sslhost = "true"
  username = params[:redmine_username]
  password = params[:redmine_password]
  project = params[:redmine_project]
  token =  params[:file_token]
  file_name = params[:file_name]
  file_version = params[:file_version]
  file_description = params[:file_description]

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

  upload_file_uri = URI.parse(redmine_url + "/projects/#{project}/files.json")
  # prepare request with token previously got from upload
  
  json_content = {
    "file" => {
      "token" => token
    }
  }

  if file_version != nil
    json_content["file"]["version_id"] = file_version
  end
  if file_name != nil
    json_content["file"]["filename"] = file_name
  end
  if file_description != nil
    json_content["file"]["description"] = file_description
  end
      
  file_body = JSON.pretty_generate(json_content)
  UI.message("File post with content #{file_body}")

  # Create the HTTP objects
  http_file_post = Net::HTTP.new(upload_file_uri.host, upload_file_uri.port)
  if redmine_use_sslhost == "true"
    http_file_post.use_ssl = true
    http_file_post.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  request_file = Net::HTTP::Post.new(upload_file_uri.request_uri)

  request_file["Content-Type"] = "application/json"
  unless api_key.nil?
    request_file["X-Redmine-API-Key"] = api_key.to_s
  end
  unless username.nil? || password.nil?
    request_file.basic_auth(username, password)
  end

  request_file.body = file_body
    # Send the request
  request_file = http_file_post.request(request_file)

  case request_file
  when Net::HTTPSuccess
    UI.success("File uploaded successfully")
  else
    UI.error(request_file.value)
  end
end