fastlane-plugin-ci_changelog 0.5.7 → 0.6.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9187cae7e24e9b529f3370beceed5b22f3e07d5c640ec3f957df1eae21518810
4
- data.tar.gz: fbf76c9d336fda77f48fb7f23314c9e0c2ec388037b804e2afa8c61c6f920ea6
3
+ metadata.gz: 6da83bb912083e4b737cb6ac066502236b26ea5176816f85579ec7b5d13e258a
4
+ data.tar.gz: cbbaeaafb329e368573e61d65d7774401e5916498a40cb18e26f5d0bb021cf84
5
5
  SHA512:
6
- metadata.gz: a92bf0405fea2b9b0f087a141ff0bf10c105c5030629668c1619fd2389ce1412e27908fa4eee499e415e89643f84b97c2eaadcad70c02ded5b32b7b9d0465d25
7
- data.tar.gz: c0a8e9e7ecc5a717ec09ac2862289fe55975f3af74af1faf4e4c25aa08506f779d53df6a65ad2cd197018ba814fdb388475cff320bd7e14c8634d3010057e727
6
+ metadata.gz: 185224c17abe1a12c670e09c112b1f0a07038a91493829e691ab893d5e6ee8d807c542ecbf5d7030284a6825d0898d223b164bdd9b8867ae55ad94a83148a236
7
+ data.tar.gz: 4d9b8a2eef3d4ba592284bcc2b4e75f305d8c4216a391569fc5ed0c3cd811c2a2e95d3b7c76901a995d9e9a32f7774d786dd0a5473f9a4edddadd34ec8a609e7
data/README.md CHANGED
@@ -53,7 +53,7 @@ Loading documentation for ci_changelog:
53
53
  | jenkins_user | the user of jenkins if enabled security | CICL_CHANGELOG_JENKINS_USER | |
54
54
  | jenkins_token | the token or password of jenkins if enabled | CICL_CHANGELOG_JENKINS_TOKEN | |
55
55
  | | security | | |
56
- | gitlab_url | the url of gitlab | CICL_CHANGELOG_GITLAB_URL | |
56
+ | gitlab_api_url | the api url of gitlab | CICL_CHANGELOG_GITLAB_API_URL | |
57
57
  | gitlab_private_token | the private token of gitlab | CICL_CHANGELOG_GITLAB_PRIVATE_TOKEN | |
58
58
  +----------------------+----------------------------------------------+-------------------------------------+---------+
59
59
 
@@ -130,63 +130,27 @@ module Fastlane
130
130
  end
131
131
 
132
132
  def self.fetch_gitlab_changelog!
133
- commits = []
134
- loop_count = 1
135
- fetch_correct_changelog = false
136
-
137
- build_number = ENV['CI_BUILD_ID'].to_i
138
- loop do
139
- build_url = "#{@params[:gitlab_url]}/api/v3/projects/#{ENV['CI_PROJECT_ID']}/builds/#{build_number}"
140
- UI.verbose("Fetching changelog #{build_url}")
141
-
142
- begin
143
- res = HTTP.headers('PRIVATE-TOKEN' => @params[:gitlab_private_token]).get(build_url)
144
- if res.code == 200
145
- build_status, data = Helper::CiChangelogHelper.dump_gitlab_commits(res.body)
146
- UI.verbose("- Status #{build_status}")
147
- UI.verbose("- Changelog #{data}")
148
-
149
- if build_status == true
150
- commits = data if commits.empty?
151
- fetch_correct_changelog = true
152
- else
153
- commits = commits.empty? ? data : commits.concat(data)
154
- loop_count += 1
155
- end
156
- end
157
- build_number -= 1
158
-
159
- break if fetch_correct_changelog || build_number <= 0
160
- rescue JSON::ParserError => e
161
- UI.verbose(e.message)
162
- build_number -= 1
163
- break if fetch_correct_changelog || build_number <= 0
164
- rescue HTTP::Error => e
165
- UI.verbose(e.message)
166
- UI.verbose(e.backtrace.join("\n"))
167
- break
168
- end
169
- end
133
+ endpoint = @params[:gitlab_api_url] || ENV['CI_API_V4_URL']
134
+ private_token = @params[:gitlab_private_token]
135
+ commits = Helper::CiChangelogHelper.dump_gitlab_commits(endpoint, private_token)
170
136
 
171
137
  Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_CHANGELOG, commits.to_json)
172
138
  end
173
139
 
174
140
  def self.fetch_gitlab_env!
175
141
  build_url =
176
- if ENV['CI_PROJECT_URL']
177
- "#{ENV['CI_PROJECT_URL']}/builds/#{ENV['CI_BUILD_ID']}"
178
- else
179
- uri = URI.parse(ENV['CI_BUILD_REPO'])
180
- ci_project_namespace = ENV['CI_PROJECT_DIR'].split('/')[-2..-1].join('/')
181
- url_port = uri.port == 80 ? '' : ":#{uri.port}"
182
-
183
- "#{uri.scheme}://#{uri.host}#{url_port}/#{ci_project_namespace}/builds/#{ENV['CI_BUILD_ID']}"
142
+ if ENV['CI_JOB_URL']
143
+ # Gitlab >= 11.1, Runner 0.5
144
+ ENV['CI_JOB_URL']
145
+ elsif ENV['CI_PROJECT_URL']
146
+ # Gitlab >= 8.10, Runner 0.5
147
+ "#{ENV['CI_PROJECT_URL']}/-/jobs/#{ENV['CI_BUILD_ID']}"
184
148
  end
185
149
 
186
150
  Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_CI, CICLType::GITLAB_CI)
187
151
  Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_BRANCH, ENV['CI_BUILD_REF_NAME'])
188
152
  Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_COMMIT, ENV['CI_BUILD_REF'])
189
- Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_PROJECT_URL, build_url)
153
+ Helper::CiChangelogHelper.store_sharedvalue(SharedValues::CICL_PROJECT_URL, build_url) if build_url
190
154
  end
191
155
 
192
156
  def self.output
@@ -228,9 +192,9 @@ module Fastlane
228
192
  description: "the token or password of jenkins if enabled security",
229
193
  optional: true,
230
194
  type: String),
231
- FastlaneCore::ConfigItem.new(key: :gitlab_url,
232
- env_name: "CICL_CHANGELOG_GITLAB_URL",
233
- description: "the url of gitlab",
195
+ FastlaneCore::ConfigItem.new(key: :gitlab_api_url,
196
+ env_name: "CICL_CHANGELOG_GITLAB_API_URL",
197
+ description: "the api url of gitlab",
234
198
  optional: true,
235
199
  type: String),
236
200
  FastlaneCore::ConfigItem.new(key: :gitlab_private_token,
@@ -3,10 +3,6 @@ require 'json'
3
3
  module Fastlane
4
4
  module Helper
5
5
  class CiChangelogHelper
6
- def self.show_message
7
- UI.message("Hello from the ci_changelog plugin helper!")
8
- end
9
-
10
6
  def self.git_commits(last_success_commit)
11
7
  git_logs = `git log --pretty="format:%s - %cn [%ci]" #{last_success_commit}..HEAD`.strip.gsub(' +0800', '')
12
8
  git_logs.split("\n")
@@ -35,34 +31,118 @@ module Fastlane
35
31
  [result, commits]
36
32
  end
37
33
 
38
- def self.dump_gitlab_commits(body)
34
+ def self.dump_gitlab_commits(endpoint, private_token)
35
+ project_id = ENV['CI_PROJECT_ID']
36
+ from, to = fetch_gitlab_compare_commit(endpoint, private_token, project_id)
37
+ return [] unless from && to
38
+
39
+ fetch_gitlab_commits(endpoint, private_token, project_id, from, to)
40
+ end
41
+
42
+ def self.fetch_gitlab_compare_commit(endpoint, private_token, project_id)
43
+ job_name = ENV['CI_JOB_NAME']
44
+
45
+ from_commit = nil
46
+ to_commit = nil
47
+
48
+ jobs_url = "#{endpoint}/projects/#{project_id}/jobs"
49
+ UI.verbose("Fetching jobs url #{jobs_url}")
50
+ res = res = HTTP.follow
51
+ .headers('PRIVATE-TOKEN' => private_token)
52
+ .get(jobs_url)
53
+
54
+ jobs = res.parse.select { |job| job['name'] == job_name }
55
+
56
+ commits = []
57
+ jobs.each_with_index do |job, i|
58
+ commit = job['pipeline']['sha']
59
+ case job['status']
60
+ when 'running'
61
+ to_commit = commit
62
+ when 'success'
63
+ if to_commit.nil?
64
+ to_commit = commit
65
+ elsif to_commit && from_commit.nil?
66
+ from_commit = commit
67
+ end
68
+ end
69
+ end
70
+
71
+ [from_commit, to_commit]
72
+ end
73
+
74
+ def self.fetch_gitlab_commit(body)
75
+ job_name = ENV['CI_JOB_NAME']
39
76
  json = JSON.parse(body)
40
- commit = {
41
- id: json['commit']['id'],
42
- date: json['commit']['created_at'],
43
- title: json['commit']['title'].strip,
44
- message: json['commit']['message'].strip,
45
- author: json['commit']['author_name'].strip,
46
- email: json['commit']['author_email'].strip
47
- }
77
+ commit = json['pipeline']['sha']
48
78
 
49
- if json['status'] == 'success'
50
- [true, [commit]]
79
+ if json['name'] != job_name
80
+ UI.verbose("No match job name: #{job_name} != #{json['name']}")
81
+ [false, commit]
82
+ elsif json['status'] == 'success'
83
+ [true, commit]
51
84
  else
52
- [false, [commit]]
85
+ [false, commit]
86
+ end
87
+ end
88
+
89
+ def self.fetch_gitlab_commits(endpoint, private_token, project_id, from, to)
90
+ compare_url = "#{endpoint}/projects/#{project_id}/repository/compare"
91
+ params = {
92
+ from: from,
93
+ to: to
94
+ }
95
+
96
+ UI.verbose("Fetching commit compare url #{compare_url} with params: #{params}")
97
+ res = HTTP.follow
98
+ .headers('PRIVATE-TOKEN' => private_token)
99
+ .get(compare_url, params: params)
100
+
101
+ commits = []
102
+ if res.code == 200
103
+ res.parse['commits'].each do |commit|
104
+ commits << {
105
+ id: commit['id'],
106
+ date: commit['created_at'],
107
+ title: commit['title'].strip,
108
+ message: commit['title'].strip,
109
+ author: commit['author_name'].strip,
110
+ email: commit['author_email'].strip
111
+ }
112
+ end
53
113
  end
114
+
115
+ commits
54
116
  end
55
117
 
118
+ # def self.dump_gitlab_commits(body)
119
+ # json = JSON.parse(body)
120
+ # json['commits'].each_with_object([]) do |commit, obj|
121
+ # commit = {
122
+ # id: commit['id'],
123
+ # date: commit['created_at'],
124
+ # title: commit['title'].strip,
125
+ # message: commit['title'].strip,
126
+ # author: commit['author_name'].strip,
127
+ # email: commit['author_email'].strip
128
+ # }
129
+
130
+ # obj << commit
131
+ # end
132
+ # end
133
+
56
134
  def self.determine_gitlab_options!(options)
57
- %w(gitlab_url gitlab_private_token).each do |key|
58
- UI.user_error!("Missing #{key} param or empty value.") unless options.fetch(key.to_sym) && !options[key.to_sym].empty?
135
+ return if options[:gitlab_api_url].to_s.empty? && !ENV['CI_API_V4_URL'].to_s.empty?
136
+
137
+ %w(gitlab_api_url gitlab_private_token).each do |key|
138
+ UI.user_error!("Missing #{key} param or it is an empty value.") unless options.fetch(key.to_sym) && !options[key.to_sym].empty?
59
139
  end
60
140
  end
61
141
 
62
142
  def self.determine_jenkins_options!(options)
63
143
  if determine_jenkins_basic_auth?
64
144
  %w(jenkins_user jenkins_token).each do |key|
65
- UI.user_error!("Missing #{key} param or empty value.") unless options.fetch(key.to_sym) && !options[key.to_sym].empty?
145
+ UI.user_error!("Missing #{key} param or it is an empty value.") unless options.fetch(key.to_sym) && !options[key.to_sym].empty?
66
146
  end
67
147
  end
68
148
  end
@@ -1,5 +1,5 @@
1
1
  module Fastlane
2
2
  module CiChangelog
3
- VERSION = '0.5.7'.freeze
3
+ VERSION = '0.6.0.beta1'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastlane-plugin-ci_changelog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.7
4
+ version: 0.6.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - icyleaf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-05 00:00:00.000000000 Z
11
+ date: 2020-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -135,11 +135,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
135
135
  version: '0'
136
136
  required_rubygems_version: !ruby/object:Gem::Requirement
137
137
  requirements:
138
- - - ">="
138
+ - - ">"
139
139
  - !ruby/object:Gem::Version
140
- version: '0'
140
+ version: 1.3.1
141
141
  requirements: []
142
- rubygems_version: 3.0.3
142
+ rubygems_version: 3.0.6
143
143
  signing_key:
144
144
  specification_version: 4
145
145
  summary: Automate generate changelog between previous build failed and the latest