-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from EmergeTools/telkins/prepopulate-git
- Loading branch information
Showing
6 changed files
with
193 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |