Class: Fastlane::Actions::UpdateJenkinsBuildAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



145
146
147
# File 'lib/fastlane/plugin/update_jenkins_build/actions/update_jenkins_build_action.rb', line 145

def self.authors
  ["icyleaf"]
end

.available_optionsObject



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
# File 'lib/fastlane/plugin/update_jenkins_build/actions/update_jenkins_build_action.rb', line 105

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :description,
                            env_name: "UPDATE_JENKINS_BUILD_DESCRIPTION",
                         description: "the description of current build",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :url,
                            env_name: "UPDATE_JENKINS_BUILD_URL",
                         description: "the url of jenkins",
                       default_value: ENV['JENKINS_URL'],
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :project,
                            env_name: "UPDATE_JENKINS_BUILD_PROJECT",
                         description: "the name of project(job)",
                       default_value: ENV["JOB_NAME"],
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :build_number,
                            env_name: "UPDATE_JENKINS_BUILD_BUILD_NUMBER",
                         description: "the build number of project(job)",
                       default_value: ENV["BUILD_NUMBER"],
                            optional: true,
                                type: Integer),
    FastlaneCore::ConfigItem.new(key: :user,
                            env_name: "UPDATE_JENKINS_BUILD_USER",
                         description: "the user of jenkins if enabled security",
                       default_value: ENV["CICL_CHANGELOG_JENKINS_USER"],
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :password,
                            env_name: "UPDATE_JENKINS_BUILD_PASSWORD",
                         description: "the password of jenkins if enabled security",
                       default_value: ENV["CICL_CHANGELOG_JENKINS_TOKEN"],
                            optional: true,
                                type: String),
  ]
end

.base_uriObject



64
65
66
67
68
# File 'lib/fastlane/plugin/update_jenkins_build/actions/update_jenkins_build_action.rb', line 64

def self.base_uri
  uri = URI(@url)
  uri.path = "/job/#{@project}/#{@build_number}"
  uri.to_s
end

.descriptionObject



97
98
99
# File 'lib/fastlane/plugin/update_jenkins_build/actions/update_jenkins_build_action.rb', line 97

def self.description
  "Update build's description of jenkins"
end

.detailsObject



101
102
103
# File 'lib/fastlane/plugin/update_jenkins_build/actions/update_jenkins_build_action.rb', line 101

def self.details
  "MUST disable CSRF in Security configure"
end

.example_codeObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fastlane/plugin/update_jenkins_build/actions/update_jenkins_build_action.rb', line 78

def self.example_code
  [
    'update_jenkins_build(
      description: "AdHoc v1.0 (1.0.1)"
    )',
    'update_jenkins_build(
      url: "http://127.0.0.1:8080/", # specify specific jenkins url
      project: "foobar", # specify specific project name
      build_number: 75, # specify specific build number
      description: "AdHoc v1.0 (1.0.1)",
      user: "admin",
      password: "123"
    )',
    'result = update_jenkins_build(
      description: "AdHoc v1.0 (1.0.1)"
    )'
  ]
end

.format_description(descripion) ⇒ Object



60
61
62
# File 'lib/fastlane/plugin/update_jenkins_build/actions/update_jenkins_build_action.rb', line 60

def self.format_description(descripion)
  descripion.gsub('\n', "\n")
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/fastlane/plugin/update_jenkins_build/actions/update_jenkins_build_action.rb', line 149

def self.is_supported?(platform)
  true
end

.return_typeObject



74
75
76
# File 'lib/fastlane/plugin/update_jenkins_build/actions/update_jenkins_build_action.rb', line 74

def self.return_type
  :array
end

.return_valueObject



70
71
72
# File 'lib/fastlane/plugin/update_jenkins_build/actions/update_jenkins_build_action.rb', line 70

def self.return_value
  "[ture/false, response_body]"
end

.run(params) ⇒ Object



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
# File 'lib/fastlane/plugin/update_jenkins_build/actions/update_jenkins_build_action.rb', line 10

def self.run(params)
  @user = params[:user]
  @password = params[:password]
  @url = params[:url]
  @project = params[:project]
  @build_number = params[:build_number]
  @description = format_description(params[:description])

  url = "#{base_uri}/submitDescription"
  res = if @user || @password
          HTTP.basic_auth(user: @user, pass: @password)
              .post(url, form: {description: @description})
        else
          HTTP.post(url, form: {
            "description" => @description,
            "Jenkins-Crumb" => "234234234234234234" # random value
          })
        end

  # Submit form succesed it will 302 to the build url.
  result = res.code == 302 ? 'success' : "#{res.code} fail"

  jenkins_version = Helper::UpdateJenkinsBuildHelper.jenkins_version(@url, @user, @password)
  params = {
    title: "Summary for update_jenkins_build #{UpdateJenkinsBuild::VERSION}".green,
    rows: {
      jenkins_version: jenkins_version,
      result: result,
      url: "#{base_uri}/editDescription",
      auth: @user ? true : false,
      description: @description,
    }
  }

  puts Terminal::Table.new(params)

  if res.code != 302
    UI.error "Detect `update_jenkins_build` ran fail."
    if jenkins_version && Gem::Version.new(jenkins_version) > Gem::Version.new('2.221')
      UI.error "Because Jenkins v#{jenkins_version} removed 'disable CSRF Protection' option since 2.222,"
      UI.error "You need append '-Dhudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true' into Jenkins startup argument."
      UI.error "Please check https://github.com/icyleaf/fastlane-plugin-update_jenkins_build/issues/2"
    else
      UI.error "Please 'disable CSRF Protection' in 'Global Security Settings' page from Jenkins."
    end
  end

  [result, res.body]
end