Skip to content

Commit

Permalink
Merge pull request #17 from EmergeTools/telkins/prepopulate-git
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-e authored Mar 8, 2024
2 parents f46182e + 5f23185 commit 32cbc6f
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.8.0

- Automatically populate various Git fields if they are not provided (`sha`, `base_sha`, `branch`, `pr_number`, `repo_name`).

## 0.7.0

- Renamed the `build_type` field to `tag`.
20 changes: 9 additions & 11 deletions lib/fastlane/plugin/emerge/actions/emerge_action.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'fastlane/action'
require 'fastlane_core/print_table'
require_relative '../helper/emerge_helper'
require_relative '../helper/git'
require_relative '../helper/github'
require 'pathname'
require 'tmpdir'
require 'json'
Expand All @@ -16,11 +18,12 @@ def self.run(params)
if file_path.nil?
file_path = Dir.glob("#{lane_context[SharedValues::SCAN_DERIVED_DATA_PATH]}/Build/Products/Debug-iphonesimulator/*.app").first
end
pr_number = params[:pr_number]
branch = params[:branch]
sha = params[:sha] || params[:build_id]
base_sha = params[:base_sha] || params[:base_build_id]
repo_name = params[:repo_name]
git_params = Helper::EmergeHelper.make_git_params
pr_number = params[:pr_number] || git_params.pr_number
branch = params[:branch] || git_params.branch
sha = params[:sha] || params[:build_id] || git_params.sha
base_sha = params[:base_sha] || params[:base_build_id] || git_params.base_sha
repo_name = params[:repo_name] || git_params.repo_name
gitlab_project_id = params[:gitlab_project_id]
tag = params[:tag]
order_file_version = params[:order_file_version]
Expand Down Expand Up @@ -172,11 +175,10 @@ def self.authors
end

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

def self.details
# Optional:
""
end

Expand Down Expand Up @@ -246,10 +248,6 @@ def self.available_options
end

def self.is_supported?(platform)
# Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
# See: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
#
# [:ios, :mac, :android].include?(platform)
platform == :ios
end
end
Expand Down
38 changes: 36 additions & 2 deletions lib/fastlane/plugin/emerge/helper/emerge_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,58 @@
module Fastlane
UI = FastlaneCore::UI unless Fastlane.const_defined?("UI")

class GitResult
attr_accessor :sha, :base_sha, :branch, :pr_number, :repo_name

def initialize(sha:, base_sha:, branch:, pr_number: nil, repo_name: nil)
@pr_number = pr_number
@sha = sha
@base_sha = base_sha
@branch = branch
@repo_name = repo_name
end
end

module Helper
class EmergeHelper
def self.perform_upload(upload_url, upload_id, file_path)
UI.message("Starting upload")
response = Faraday.put(upload_url) do |req|
req.headers['Content-Type'] = 'application/zip'
req.headers['Content-Length'] = "#{File.size(file_path)}"
req.headers['Content-Length'] = File.size(file_path).to_s
req.body = Faraday::UploadIO.new(file_path, 'application/zip')
end
case response.status
when 200
UI.success("Your app is processing, you can find the results at https://emergetools.com/build/#{upload_id}")
UI.success("🎉 Your app is processing, you can find the results at https://emergetools.com/build/#{upload_id}")
return upload_id
else
UI.error("Upload failed")
end
return nil
end

def self.make_git_params
git_result = if Helper::Github.is_supported_github_event?
UI.message("Fetching Git info from Github event")
GitResult.new(
sha: Helper::Github.sha,
base_sha: Helper::Github.base_sha,
branch: Helper::Github.branch,
pr_number: Helper::Github.pr_number,
repo_name: Helper::Github.repo_name
)
else
UI.message("Fetching Git info from system Git")
GitResult.new(
sha: Helper::Git.sha,
base_sha: Helper::Git.base_sha,
branch: Helper::Git.branch
)
end
UI.message("Got git result #{git_result.inspect}")
git_result
end
end
end
end
66 changes: 66 additions & 0 deletions lib/fastlane/plugin/emerge/helper/git.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'fastlane_core/print_table'
require 'open3'

module Fastlane
module Helper
module Git
def self.branch
shell_command = "git rev-parse --abbrev-ref HEAD"
UI.command(shell_command)
stdout, _, status = Open3.capture3(shell_command)
stdout.strip if status.success?
end

def self.sha
shell_command = "git rev-parse HEAD"
UI.command(shell_command)
stdout, _, status = Open3.capture3(shell_command)
stdout.strip if status.success?
end

def self.base_sha
shell_command = "git merge-base #{remote_head_branch} #{branch}"
UI.command(shell_command)
stdout, _, status = Open3.capture3(shell_command)
return nil if stdout.strip.empty? || !status.success?
current_sha = sha
stdout.strip == current_sha ? nil : stdout.strip
end

def self.primary_remote
remote = remote()
return nil if remote.nil?
remote.include?("origin") ? "origin" : remote.first
end

def self.remote_head_branch(remote = primary_remote)
return nil if remote.nil?
shell_command = "git remote show #{remote}"
UI.command(shell_command)
stdout, _, status = Open3.capture3(shell_command)
return nil if stdout.nil? || !status.success?
stdout
.split("\n")
.map(&:strip)
.find { |line| line.start_with?("HEAD branch: ") }
&.split(' ')
&.last
end

def self.remote_url(remote = primary_remote)
return nil if remote.nil?
shell_command = "git config --get remote.#{remote}.url"
UI.command(shell_command)
stdout, _, status = Open3.capture3(shell_command)
stdout if status.success?
end

def self.remote
shell_command = "git remote"
UI.command(shell_command)
stdout, _, status = Open3.capture3(shell_command)
stdout.split("\n") if status.success?
end
end
end
end
77 changes: 77 additions & 0 deletions lib/fastlane/plugin/emerge/helper/github.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require 'json'
require 'fastlane_core/print_table'
require_relative 'git'

module Fastlane
module Helper
module Github
GITHUB_EVENT_PR = "pull_request".freeze
GITHUB_EVENT_PUSH = "push".freeze

def self.event_name
ENV['GITHUB_EVENT_NAME']
end

def self.is_supported_github_event?
UI.message("GitHub event name: #{event_name}")
is_pull_request? || is_push?
end

def self.is_pull_request?
event_name == GITHUB_EVENT_PR
end

def self.is_push?
event_name == GITHUB_EVENT_PUSH
end

def self.sha
if is_push?
ENV['GITHUB_SHA']
elsif is_pull_request?
github_event_data.dig(:pull_request, :head, :sha)
end
end

def self.base_sha
if is_pull_request?
github_event_data.dig(:pull_request, :base, :sha)
end
end

def self.pr_number
is_pull_request? ? github_event_data.dig(:number) : nil
end

def self.branch
is_pull_request? ? github_event_data.dig(:pull_request, :head, :ref) : Git.branch
end

def self.repo_owner
github_event_data.dig(:repository, :owner, :login)
end

def self.repo_name
github_event_data.dig(:repository, :full_name)
end

private_class_method

def self.github_event_data
github_event_path = ENV['GITHUB_EVENT_PATH']
UI.error!("GITHUB_EVENT_PATH is not set") if github_event_path.nil?

unless File.exist?(github_event_path)
UI.error!("File #{github_event_path} doesn't exist")
end

file_content = File.read(github_event_path)
file_json = JSON.parse(file_content, symbolize_names: true)
if ENV['DEBUG']
UI.message("Parsed GitHub event data: #{file_json.inspect}")
end
file_json
end
end
end
end
2 changes: 1 addition & 1 deletion lib/fastlane/plugin/emerge/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Fastlane
module Emerge
VERSION = "0.7.0"
VERSION = "0.8.0"
end
end

0 comments on commit 32cbc6f

Please sign in to comment.