Class: Fastlane::Actions::CiChangelogAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



170
171
172
# File 'lib/fastlane/plugin/ci_changelog/actions/ci_changelog_action.rb', line 170

def self.authors
  ["icyleaf"]
end

.available_optionsObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/fastlane/plugin/ci_changelog/actions/ci_changelog_action.rb', line 174

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :silent,
                            env_name: "CICL_SILENT",
                         description: "Hide all information of print table",
                            optional: true,
                       default_value: false,
                           is_string: false),
    FastlaneCore::ConfigItem.new(key: :jenkins_user,
                            env_name: "CICL_CHANGELOG_JENKINS_USER",
                         description: "the user of jenkins if enabled security",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :jenkins_token,
                            env_name: "CICL_CHANGELOG_JENKINS_TOKEN",
                         description: "the token or password of jenkins if enabled security",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :gitlab_api_url,
                            env_name: "CICL_CHANGELOG_GITLAB_API_URL",
                         description: "the api url of gitlab",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :gitlab_private_token,
                            env_name: "CICL_CHANGELOG_GITLAB_PRIVATE_TOKEN",
                         description: "the private token of gitlab",
                            optional: true,
                                type: String)
  ]
end

.descriptionObject



162
163
164
# File 'lib/fastlane/plugin/ci_changelog/actions/ci_changelog_action.rb', line 162

def self.description
  "Automate generate changelog between previous build failed and the latest commit of scm in CI."
end

.detailsObject



166
167
168
# File 'lib/fastlane/plugin/ci_changelog/actions/ci_changelog_action.rb', line 166

def self.details
  "availabled with jenkins, gitlab ci, more support is comming soon."
end

.fetch_gitlab_changelog!Object



129
130
131
132
133
134
135
# File 'lib/fastlane/plugin/ci_changelog/actions/ci_changelog_action.rb', line 129

def self.fetch_gitlab_changelog!
  endpoint = @params[:gitlab_api_url] || ENV['CI_API_V4_URL']
  private_token = @params[:gitlab_private_token]
  commits = Helper::CiChangelogHelper.dump_gitlab_commits(endpoint, private_token)

  Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_CHANGELOG, commits.to_json)
end

.fetch_gitlab_env!Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/fastlane/plugin/ci_changelog/actions/ci_changelog_action.rb', line 137

def self.fetch_gitlab_env!
  build_url =
    if ENV['CI_JOB_URL']
      # Gitlab >= 11.1, Runner 0.5
      ENV['CI_JOB_URL']
    elsif ENV['CI_PROJECT_URL']
      # Gitlab >= 8.10, Runner 0.5
      "#{ENV['CI_PROJECT_URL']}/-/jobs/#{ENV['CI_BUILD_ID']}"
    end

  Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_CI, CICLType::GITLAB_CI)
  Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_BRANCH, ENV['CI_BUILD_REF_NAME'])
  Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_COMMIT, ENV['CI_BUILD_REF'])
  Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_PROJECT_URL, build_url) if build_url
end

.fetch_jenkins_changelog!Object



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

def self.fetch_jenkins_changelog!
  changelog = []

  build_branch = ENV['GIT_BRANCH']
  build_number = ENV['BUILD_NUMBER'].to_i

  loop do
    build_url = "#{ENV['JOB_URL']}#{build_number}/api/json"
    UI.verbose("Fetching changelog #{build_url}")
    UI.verbose("- Branch #{build_branch}")
    begin
      res =
        if Helper::CiChangelogHelper.determine_jenkins_basic_auth?
          HTTP.basic_auth(user: @params.fetch(:jenkins_user), pass: @params.fetch(:jenkins_token))
              .get(build_url)
        else
          HTTP.get(build_url)
        end

      if res.code == 200
        build_status, data = Helper::CiChangelogHelper.dump_jenkins_commits(res.body, build_branch)
        UI.verbose("- Status #{build_status}")
        UI.verbose("- Changelog #{data}")

        changelog.concat(data) unless build_status

        break if build_status == true
      end

      build_number -= 1
      break if build_number <= 0
    rescue JSON::ParserError => e
      UI.verbose(e.message)
      build_number -= 1
      break if build_number <= 0
    rescue HTTP::Error => e
      # NOTE: break out of loop if build setted keep max builds count
      UI.verbose(e.message)
      UI.verbose(e.backtrace.join("\n"))
      break
    end
  end

  Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_CHANGELOG, changelog.to_json)
end

.fetch_jenkins_env!Object



119
120
121
122
123
124
125
126
127
# File 'lib/fastlane/plugin/ci_changelog/actions/ci_changelog_action.rb', line 119

def self.fetch_jenkins_env!
  branch = ENV['GIT_BRANCH'] || ENV['SVN_BRANCH']
  branch = branch.split('/')[1..-1].join('/') if branch.include?('/') # fix origin/xxxx

  Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_CI, CICLType::JENKINS)
  Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_BRANCH, branch)
  Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_COMMIT, ENV['GIT_COMMIT'])
  Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_PROJECT_URL, ENV['JOB_URL'])
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/fastlane/plugin/ci_changelog/actions/ci_changelog_action.rb', line 205

def self.is_supported?(platform)
  true
end

.outputObject



153
154
155
156
157
158
159
160
# File 'lib/fastlane/plugin/ci_changelog/actions/ci_changelog_action.rb', line 153

def self.output
  [
    [SharedValues::CICL_CI.to_s, 'the name of CI'],
    [SharedValues::CICL_BRANCH.to_s, 'the name of CVS branch'],
    [SharedValues::CICL_COMMIT.to_s, 'the last hash of CVS commit'],
    [SharedValues::CICL_CHANGELOG.to_s, 'the json formatted changelog of CI (datetime, message, author and email)']
  ]
end


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
# File 'lib/fastlane/plugin/ci_changelog/actions/ci_changelog_action.rb', line 46

def self.print_table!
  data = Actions.lane_context[SharedValues::CICL_CHANGELOG]
  changelog =
    if !data.to_s.empty? && data != '[]'
      JSON.parse(data).each_with_object([]) do |commit, obj|
        obj << commit.collect { |k, v| "#{k}: #{v}" }.join("\n")
      end.join("\n\n")
    else
      'Not found changelog'
    end

  params = {
    title: "Summary for ci_changelog #{CiChangelog::VERSION}".green,
    rows: {
      ci: Actions.lane_context[SharedValues::CICL_CI],
      project_url: Actions.lane_context[SharedValues::CICL_PROJECT_URL],
      branch: Actions.lane_context[SharedValues::CICL_BRANCH],
      commit: Actions.lane_context[SharedValues::CICL_COMMIT],
      changelog: changelog
    }
  }

  puts ""
  puts Terminal::Table.new(params)
  puts ""
end

.run(params) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fastlane/plugin/ci_changelog/actions/ci_changelog_action.rb', line 22

def self.run(params)
  return UI.message('No detect CI environment') unless FastlaneCore::Helper.is_ci?

  @params = params
  if Helper::CiChangelogHelper.jenkins?
    UI.message('detected: jenkins')
    Helper::CiChangelogHelper.determine_jenkins_options!(params)
    fetch_jenkins_changelog!
    fetch_jenkins_env!
  elsif Helper::CiChangelogHelper.gitlab?
    UI.message('detected: gitlab ci')
    Helper::CiChangelogHelper.determine_gitlab_options!(params)
    fetch_gitlab_changelog!
    fetch_gitlab_env!
  else
    Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_CI, CICLType::UNKNOWN)
    UI.message('Sorry, No found CI variable, maybe not support yet, available is Jenkins/Gitlab CI')
  end

  print_table! unless params[:silent]

  Actions.lane_context[SharedValues::CICL_CHANGELOG]
end