Class: Fastlane::Helper::CiChangelogHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/ci_changelog/helper/ci_changelog_helper.rb

Class Method Summary collapse

Class Method Details

.determine_gitlab_options!(options) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/fastlane/plugin/ci_changelog/helper/ci_changelog_helper.rb', line 118

def self.determine_gitlab_options!(options)
  return if options[:gitlab_api_url].to_s.empty? && !ENV['CI_API_V4_URL'].to_s.empty?

  %w(gitlab_api_url gitlab_private_token).each do |key|
    UI.user_error!("Missing #{key} param or it is an empty value.") unless options.fetch(key.to_sym) && !options[key.to_sym].empty?
  end
end

.determine_jenkins_basic_auth?Boolean

Returns:



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/fastlane/plugin/ci_changelog/helper/ci_changelog_helper.rb', line 134

def self.determine_jenkins_basic_auth?
  res = HTTP.get("#{ENV['JENKINS_URL']}/api/json")
  if res.code == 200 && res.headers[:content_type].include?('json')
    return false
  else
    return true
  end
# rescue RestClient::Forbidden
rescue Addressable::URI::InvalidURIError
  return false
rescue Exception
  return true
end

.determine_jenkins_options!(options) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/fastlane/plugin/ci_changelog/helper/ci_changelog_helper.rb', line 126

def self.determine_jenkins_options!(options)
  if determine_jenkins_basic_auth?
    %w(jenkins_user jenkins_token).each do |key|
      UI.user_error!("Missing #{key} param or it is an empty value.") unless options.fetch(key.to_sym) && !options[key.to_sym].empty?
    end
  end
end

.dump_gitlab_commits(endpoint, private_token) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/fastlane/plugin/ci_changelog/helper/ci_changelog_helper.rb', line 34

def self.dump_gitlab_commits(endpoint, private_token)
  project_id = ENV['CI_PROJECT_ID']
  from, to = fetch_gitlab_compare_commit(endpoint, private_token, project_id)
  return [] unless from && to

  fetch_gitlab_commits(endpoint, private_token, project_id, from, to)
end

.dump_jenkins_commits(body, branch) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fastlane/plugin/ci_changelog/helper/ci_changelog_helper.rb', line 11

def self.dump_jenkins_commits(body, branch)
  json = JSON.parse(body)
  UI.verbose("- API Result: #{json['result']}")
  result = json['building'] ? false : (json['result'] == 'SUCCESS')

  # return if previous build do not equal to current build branch.
  return [result, []] unless jenkins_use_same_branch?(json, branch)

  # TODO: It must use reverse_each to correct the changelog
  commits = json['changeSet']['items'].each_with_object([]) do |item, obj|
    obj.push({
      id: item['commitId'],
      date: item['date'],
      title: item['msg'].strip,
      message: item['comment'].strip,
      author: item['author']['fullName'].strip,
      email: item['authorEmail'].strip
    })
  end

  [result, commits]
end

.fetch_gitlab_commit(body) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fastlane/plugin/ci_changelog/helper/ci_changelog_helper.rb', line 74

def self.fetch_gitlab_commit(body)
  job_name = ENV['CI_JOB_NAME']
  json = JSON.parse(body)
  commit = json['pipeline']['sha']

  if json['name'] != job_name
    UI.verbose("No match job name: #{job_name} != #{json['name']}")
    [false, commit]
  elsif json['status'] == 'success'
    [true, commit]
  else
    [false, commit]
  end
end

.fetch_gitlab_commits(endpoint, private_token, project_id, from, to) ⇒ Object



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
# File 'lib/fastlane/plugin/ci_changelog/helper/ci_changelog_helper.rb', line 89

def self.fetch_gitlab_commits(endpoint, private_token, project_id, from, to)
  compare_url = "#{endpoint}/projects/#{project_id}/repository/compare"
  params = {
    from: from,
    to: to
  }

  UI.verbose("Fetching commit compare url #{compare_url} with params: #{params}")
  res = HTTP.follow
            .headers('PRIVATE-TOKEN' => private_token)
            .get(compare_url, params: params)

  commits = []
  if res.code == 200
    res.parse['commits'].each do |commit|
      commits << {
        id: commit['id'],
        date: commit['created_at'],
        title: commit['title'].strip,
        message: commit['title'].strip,
        author: commit['author_name'].strip,
        email: commit['author_email'].strip
      }
    end
  end

  commits
end

.fetch_gitlab_compare_commit(endpoint, private_token, project_id) ⇒ Object



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/ci_changelog/helper/ci_changelog_helper.rb', line 42

def self.fetch_gitlab_compare_commit(endpoint, private_token, project_id)
  job_name = ENV['CI_JOB_NAME']

  from_commit = nil
  to_commit = nil

  jobs_url = "#{endpoint}/projects/#{project_id}/jobs"
  UI.verbose("Fetching jobs url #{jobs_url}")
  res = res = HTTP.follow
                   .headers('PRIVATE-TOKEN' => private_token)
                   .get(jobs_url)

  jobs = res.parse.select { |job| job['name'] == job_name }

  commits = []
  jobs.each_with_index do |job, i|
    commit = job['pipeline']['sha']
    case job['status']
    when 'running'
      to_commit = commit
    when 'success'
      if to_commit.nil?
        to_commit = commit
      elsif to_commit && from_commit.nil?
        from_commit = commit
      end
    end
  end

  [from_commit, to_commit]
end

.git_commits(last_success_commit) ⇒ Object



6
7
8
9
# File 'lib/fastlane/plugin/ci_changelog/helper/ci_changelog_helper.rb', line 6

def self.git_commits(last_success_commit)
  git_logs = `git log --pretty="format:%s - %cn [%ci]" #{last_success_commit}..HEAD`.strip.gsub(' +0800', '')
  git_logs.split("\n")
end

.gitlab?Boolean

Returns:



176
177
178
# File 'lib/fastlane/plugin/ci_changelog/helper/ci_changelog_helper.rb', line 176

def self.gitlab?
  ENV.key?('GITLAB_CI')
end

.jenkins?Boolean

Returns:



168
169
170
171
172
173
174
# File 'lib/fastlane/plugin/ci_changelog/helper/ci_changelog_helper.rb', line 168

def self.jenkins?
  %w(JENKINS_URL JENKINS_HOME).each do |current|
    return true if ENV.key?(current)
  end

  return false
end

.jenkins_use_same_branch?(json, name) ⇒ Boolean

Returns:



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/fastlane/plugin/ci_changelog/helper/ci_changelog_helper.rb', line 148

def self.jenkins_use_same_branch?(json, name)
  same_branch = false
  json['actions'].each do |item|
    if revision = item['lastBuiltRevision']
      revision['branch'].each do |branch|
        same_branch = true if branch['name'].to_s.end_with?(name)
      end
    end
  end

  same_branch
end

.store_sharedvalue(key, value) ⇒ Object



161
162
163
164
165
166
# File 'lib/fastlane/plugin/ci_changelog/helper/ci_changelog_helper.rb', line 161

def self.store_sharedvalue(key, value)
  Actions.lane_context[key] = value
  ENV[key.to_s] = value

  value
end

.travis?Boolean

Returns:



180
181
182
# File 'lib/fastlane/plugin/ci_changelog/helper/ci_changelog_helper.rb', line 180

def self.travis?
  ENV.key?('TRAVIS')
end