Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[action]Adds action for obtaining remote repo default branch (#18845)
* Adds action for obtaining remote repo default branch * Correct example name * Adds unit test * Completed tests * Uses nil return and some cleanup for remote work * Optional remote name * Cleanup and test updates * Removed default from name * Small syntax cleanup Co-authored-by: Josh Holtz <josh@rokkincat.com>
- Loading branch information
1 parent
0a25f2f
commit da1fd40
Showing
3 changed files
with
157 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module Fastlane | ||
module Actions | ||
class GitRemoteBranchAction < Action | ||
def self.run(params) | ||
Actions.git_remote_branch_name(params[:remote_name]) | ||
end | ||
|
||
##################################################### | ||
# @!group Documentation | ||
##################################################### | ||
|
||
def self.description | ||
"Returns the name of the current git remote default branch" | ||
end | ||
|
||
def self.details | ||
"If no default remote branch could be found, this action will return nil. This is a wrapper for the internal action Actions.git_default_remote_branch_name" | ||
end | ||
|
||
def self.available_options | ||
[ | ||
FastlaneCore::ConfigItem.new(key: :remote_name, | ||
env_name: "FL_REMOTE_REPOSITORY_NAME", | ||
description: "The remote repository to check", | ||
optional: true) | ||
] | ||
end | ||
|
||
def self.output | ||
[] | ||
end | ||
|
||
def self.authors | ||
["SeanMcNeil"] | ||
end | ||
|
||
def self.is_supported?(platform) | ||
true | ||
end | ||
|
||
def self.example_code | ||
[ | ||
'git_remote_branch # Query git for first available remote name', | ||
'git_remote_branch(remote_name:"upstream") # Provide a remote name' | ||
] | ||
end | ||
|
||
def self.return_type | ||
:string | ||
end | ||
|
||
def self.category | ||
:source_control | ||
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
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,83 @@ | ||
describe Fastlane do | ||
describe Fastlane::FastFile do | ||
directory = "fl_spec_default_remote_branch" | ||
|
||
describe "Git Remote Branch Action" do | ||
it "generates the correct git command for retrieving default branch from remote" do | ||
result = Fastlane::FastFile.new.parse("lane :test do | ||
git_remote_branch | ||
end").runner.execute(:test) | ||
|
||
expect(result).to eq("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'") | ||
end | ||
end | ||
|
||
describe "Git Remote Branch Action with optional remote name" do | ||
it "generates the correct git command for retrieving default branch using provided remote name" do | ||
result = Fastlane::FastFile.new.parse("lane :test do | ||
git_remote_branch(remote_name:'upstream') | ||
end").runner.execute(:test) | ||
|
||
expect(result).to eq("git remote show upstream | grep 'HEAD branch' | sed 's/.*: //'") | ||
end | ||
end | ||
|
||
context "runs the command in a directory with no git repo" do | ||
it "Confirms that no default remote is found" do | ||
test_directory_path = Dir.mktmpdir(directory) | ||
|
||
Dir.chdir(test_directory_path) do | ||
expect(Fastlane::Actions).to receive(:sh) | ||
.with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) | ||
|
||
result = Fastlane::FastFile.new.parse("lane :test do | ||
git_remote_branch | ||
end").runner.execute(:test) | ||
|
||
expect(result).to be_nil | ||
end | ||
end | ||
end | ||
|
||
context "runs the command in a directory with no remote git repo" do | ||
it "Confirms that no default remote is found" do | ||
test_directory_path = Dir.mktmpdir(directory) | ||
|
||
Dir.chdir(test_directory_path) do | ||
`git init` | ||
|
||
File.write('test_file', <<-TESTFILE) | ||
'Hello' | ||
TESTFILE | ||
`git add .` | ||
`git commit --message "Test file"` | ||
|
||
expect(Fastlane::Actions).to receive(:sh) | ||
.with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) | ||
|
||
result = Fastlane::FastFile.new.parse("lane :test do | ||
git_remote_branch | ||
end").runner.execute(:test) | ||
|
||
expect(result).to be_nil | ||
end | ||
end | ||
end | ||
|
||
context "runs the command with a remote git repo" do | ||
it "Confirms that a default remote is found" do | ||
allow(Fastlane::Actions).to receive(:sh) | ||
.with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) | ||
.and_return("main") | ||
allow(Fastlane::Actions).to receive(:git_branch).and_return(nil) | ||
|
||
result = Fastlane::FastFile.new.parse("lane :test do | ||
git_remote_branch | ||
end").runner.execute(:test) | ||
|
||
expect(result).to eq("main") | ||
end | ||
end | ||
|
||
end | ||
end |