Skip to content

Commit 32cbc6f

Browse files
authored
Merge pull request #17 from EmergeTools/telkins/prepopulate-git
2 parents f46182e + 5f23185 commit 32cbc6f

File tree

6 files changed

+193
-14
lines changed

6 files changed

+193
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.8.0
4+
5+
- Automatically populate various Git fields if they are not provided (`sha`, `base_sha`, `branch`, `pr_number`, `repo_name`).
6+
37
## 0.7.0
48

59
- Renamed the `build_type` field to `tag`.

lib/fastlane/plugin/emerge/actions/emerge_action.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require 'fastlane/action'
22
require 'fastlane_core/print_table'
33
require_relative '../helper/emerge_helper'
4+
require_relative '../helper/git'
5+
require_relative '../helper/github'
46
require 'pathname'
57
require 'tmpdir'
68
require 'json'
@@ -16,11 +18,12 @@ def self.run(params)
1618
if file_path.nil?
1719
file_path = Dir.glob("#{lane_context[SharedValues::SCAN_DERIVED_DATA_PATH]}/Build/Products/Debug-iphonesimulator/*.app").first
1820
end
19-
pr_number = params[:pr_number]
20-
branch = params[:branch]
21-
sha = params[:sha] || params[:build_id]
22-
base_sha = params[:base_sha] || params[:base_build_id]
23-
repo_name = params[:repo_name]
21+
git_params = Helper::EmergeHelper.make_git_params
22+
pr_number = params[:pr_number] || git_params.pr_number
23+
branch = params[:branch] || git_params.branch
24+
sha = params[:sha] || params[:build_id] || git_params.sha
25+
base_sha = params[:base_sha] || params[:base_build_id] || git_params.base_sha
26+
repo_name = params[:repo_name] || git_params.repo_name
2427
gitlab_project_id = params[:gitlab_project_id]
2528
tag = params[:tag]
2629
order_file_version = params[:order_file_version]
@@ -172,11 +175,10 @@ def self.authors
172175
end
173176

174177
def self.return_value
175-
# If your method provides a return value, you can describe here what it does
178+
"If successful, returns the upload id of the generated build"
176179
end
177180

178181
def self.details
179-
# Optional:
180182
""
181183
end
182184

@@ -246,10 +248,6 @@ def self.available_options
246248
end
247249

248250
def self.is_supported?(platform)
249-
# Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
250-
# See: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
251-
#
252-
# [:ios, :mac, :android].include?(platform)
253251
platform == :ios
254252
end
255253
end

lib/fastlane/plugin/emerge/helper/emerge_helper.rb

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,58 @@
44
module Fastlane
55
UI = FastlaneCore::UI unless Fastlane.const_defined?("UI")
66

7+
class GitResult
8+
attr_accessor :sha, :base_sha, :branch, :pr_number, :repo_name
9+
10+
def initialize(sha:, base_sha:, branch:, pr_number: nil, repo_name: nil)
11+
@pr_number = pr_number
12+
@sha = sha
13+
@base_sha = base_sha
14+
@branch = branch
15+
@repo_name = repo_name
16+
end
17+
end
18+
719
module Helper
820
class EmergeHelper
921
def self.perform_upload(upload_url, upload_id, file_path)
1022
UI.message("Starting upload")
1123
response = Faraday.put(upload_url) do |req|
1224
req.headers['Content-Type'] = 'application/zip'
13-
req.headers['Content-Length'] = "#{File.size(file_path)}"
25+
req.headers['Content-Length'] = File.size(file_path).to_s
1426
req.body = Faraday::UploadIO.new(file_path, 'application/zip')
1527
end
1628
case response.status
1729
when 200
18-
UI.success("Your app is processing, you can find the results at https://emergetools.com/build/#{upload_id}")
30+
UI.success("🎉 Your app is processing, you can find the results at https://emergetools.com/build/#{upload_id}")
1931
return upload_id
2032
else
2133
UI.error("Upload failed")
2234
end
2335
return nil
2436
end
37+
38+
def self.make_git_params
39+
git_result = if Helper::Github.is_supported_github_event?
40+
UI.message("Fetching Git info from Github event")
41+
GitResult.new(
42+
sha: Helper::Github.sha,
43+
base_sha: Helper::Github.base_sha,
44+
branch: Helper::Github.branch,
45+
pr_number: Helper::Github.pr_number,
46+
repo_name: Helper::Github.repo_name
47+
)
48+
else
49+
UI.message("Fetching Git info from system Git")
50+
GitResult.new(
51+
sha: Helper::Git.sha,
52+
base_sha: Helper::Git.base_sha,
53+
branch: Helper::Git.branch
54+
)
55+
end
56+
UI.message("Got git result #{git_result.inspect}")
57+
git_result
58+
end
2559
end
2660
end
2761
end
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require 'fastlane_core/print_table'
2+
require 'open3'
3+
4+
module Fastlane
5+
module Helper
6+
module Git
7+
def self.branch
8+
shell_command = "git rev-parse --abbrev-ref HEAD"
9+
UI.command(shell_command)
10+
stdout, _, status = Open3.capture3(shell_command)
11+
stdout.strip if status.success?
12+
end
13+
14+
def self.sha
15+
shell_command = "git rev-parse HEAD"
16+
UI.command(shell_command)
17+
stdout, _, status = Open3.capture3(shell_command)
18+
stdout.strip if status.success?
19+
end
20+
21+
def self.base_sha
22+
shell_command = "git merge-base #{remote_head_branch} #{branch}"
23+
UI.command(shell_command)
24+
stdout, _, status = Open3.capture3(shell_command)
25+
return nil if stdout.strip.empty? || !status.success?
26+
current_sha = sha
27+
stdout.strip == current_sha ? nil : stdout.strip
28+
end
29+
30+
def self.primary_remote
31+
remote = remote()
32+
return nil if remote.nil?
33+
remote.include?("origin") ? "origin" : remote.first
34+
end
35+
36+
def self.remote_head_branch(remote = primary_remote)
37+
return nil if remote.nil?
38+
shell_command = "git remote show #{remote}"
39+
UI.command(shell_command)
40+
stdout, _, status = Open3.capture3(shell_command)
41+
return nil if stdout.nil? || !status.success?
42+
stdout
43+
.split("\n")
44+
.map(&:strip)
45+
.find { |line| line.start_with?("HEAD branch: ") }
46+
&.split(' ')
47+
&.last
48+
end
49+
50+
def self.remote_url(remote = primary_remote)
51+
return nil if remote.nil?
52+
shell_command = "git config --get remote.#{remote}.url"
53+
UI.command(shell_command)
54+
stdout, _, status = Open3.capture3(shell_command)
55+
stdout if status.success?
56+
end
57+
58+
def self.remote
59+
shell_command = "git remote"
60+
UI.command(shell_command)
61+
stdout, _, status = Open3.capture3(shell_command)
62+
stdout.split("\n") if status.success?
63+
end
64+
end
65+
end
66+
end
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
require 'json'
2+
require 'fastlane_core/print_table'
3+
require_relative 'git'
4+
5+
module Fastlane
6+
module Helper
7+
module Github
8+
GITHUB_EVENT_PR = "pull_request".freeze
9+
GITHUB_EVENT_PUSH = "push".freeze
10+
11+
def self.event_name
12+
ENV['GITHUB_EVENT_NAME']
13+
end
14+
15+
def self.is_supported_github_event?
16+
UI.message("GitHub event name: #{event_name}")
17+
is_pull_request? || is_push?
18+
end
19+
20+
def self.is_pull_request?
21+
event_name == GITHUB_EVENT_PR
22+
end
23+
24+
def self.is_push?
25+
event_name == GITHUB_EVENT_PUSH
26+
end
27+
28+
def self.sha
29+
if is_push?
30+
ENV['GITHUB_SHA']
31+
elsif is_pull_request?
32+
github_event_data.dig(:pull_request, :head, :sha)
33+
end
34+
end
35+
36+
def self.base_sha
37+
if is_pull_request?
38+
github_event_data.dig(:pull_request, :base, :sha)
39+
end
40+
end
41+
42+
def self.pr_number
43+
is_pull_request? ? github_event_data.dig(:number) : nil
44+
end
45+
46+
def self.branch
47+
is_pull_request? ? github_event_data.dig(:pull_request, :head, :ref) : Git.branch
48+
end
49+
50+
def self.repo_owner
51+
github_event_data.dig(:repository, :owner, :login)
52+
end
53+
54+
def self.repo_name
55+
github_event_data.dig(:repository, :full_name)
56+
end
57+
58+
private_class_method
59+
60+
def self.github_event_data
61+
github_event_path = ENV['GITHUB_EVENT_PATH']
62+
UI.error!("GITHUB_EVENT_PATH is not set") if github_event_path.nil?
63+
64+
unless File.exist?(github_event_path)
65+
UI.error!("File #{github_event_path} doesn't exist")
66+
end
67+
68+
file_content = File.read(github_event_path)
69+
file_json = JSON.parse(file_content, symbolize_names: true)
70+
if ENV['DEBUG']
71+
UI.message("Parsed GitHub event data: #{file_json.inspect}")
72+
end
73+
file_json
74+
end
75+
end
76+
end
77+
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Fastlane
22
module Emerge
3-
VERSION = "0.7.0"
3+
VERSION = "0.8.0"
44
end
55
end

0 commit comments

Comments
 (0)