module GithubApi
Provides API accessors for operations over github repos This module has several methods that interface with Git and github Unless otherwise returned specifically with a status,, commands that don't fail return an empty string - ''
end¶ ↑
Public Class Methods
BranchCommitDiff(base_branch, derived_branch)
click to toggle source
Returns commits in order of newest to oldest
# File lib/github_api.rb, line 146 def GithubApi.BranchCommitDiff base_branch, derived_branch puts "Getting commit diff from #{base_branch} to #{derived_branch}" commit_diff_raw = `git log #{base_branch}..#{derived_branch}` puts commit_diff_raw return GithubApi.GetCommitHashesFromLog commit_diff_raw end
CheckoutExistingBranch(branch)
click to toggle source
# File lib/github_api.rb, line 19 def GithubApi.CheckoutExistingBranch branch puts "Checking out existing branch #{branch}..." `git checkout #{branch}` # check if checkout succeeded actual_branch = `git rev-parse --abbrev-ref HEAD` return actual_branch.chomp! == branch end
CheckoutLocal(branch)
click to toggle source
# File lib/github_api.rb, line 40 def GithubApi.CheckoutLocal branch puts "Checking out local branch: #{branch}..." `git checkout #{branch}` end
CheckoutNewBranch(branch)
click to toggle source
# File lib/github_api.rb, line 14 def GithubApi.CheckoutNewBranch branch puts "Checking out new branch #{branch}..." `git checkout -b #{branch}` end
CheckoutRepoAfresh(repo_url, branch)
click to toggle source
we do NOT want to switch to parent folder but stay in current repo dir when we exit this method
# File lib/github_api.rb, line 164 def GithubApi.CheckoutRepoAfresh repo_url, branch repo = GithubApi.ProjectNameFromRepo repo_url return false if repo == GlobalConstants::EMPTY # clear repo folder if it already exists if File.directory? repo puts 'Repository already exists! Cleaning...' FileUtils.rm_rf repo end #repo_url = GithubApi.InsertCredsInUrl repo_url # clone to local puts 'Cloning repo to local...' begin # also tests for valid repo, this will cout if cmd fails, no need for additional message cmd_out = system "git clone #{repo_url}" return false if cmd_out.to_s == 'false' rescue puts "Clone repo for #{repo_url} failed" puts $! return false end # checkout requested branch if it's not the default branch checked out when cloned Dir.chdir repo puts "Checking out requested branch: #{branch}" `git fetch` cmd_out = GithubApi.CheckoutExistingBranch branch return cmd_out end
CommitAllLocalAndPush(comment)
click to toggle source
# File lib/github_api.rb, line 109 def GithubApi.CommitAllLocalAndPush comment `git add .` status = `git commit -m "#{comment}"` return false if status != GlobalConstants::EMPTY #todo: ensure push defaults are set up status = `git push` return status != GlobalConstants::EMPTY end
CommitChanges(comment)
click to toggle source
# File lib/github_api.rb, line 122 def GithubApi.CommitChanges comment git_status = GithubApi.HaveLocalChanges if git_status != GlobalConstants::EMPTY puts 'Going to add changes to git index...' #gotcha: line breaks need to be in double-quotes val = git_status.split("\n") val.each { |x| puts "#{x}" value = x.split(' M ').last || x.split('?? ').last if (/.csproj/.match(value) || /packages.config/.match(value) || /.semver/.match(value)) status = `git add #{value}` if status != GlobalConstants::EMPTY return false end end } end puts 'Going to commit changes...' status = `git commit -m "#{comment}"` return status != GlobalConstants::EMPTY end
CreateNewBranch(new_branch, branch)
click to toggle source
# File lib/github_api.rb, line 9 def GithubApi.CreateNewBranch new_branch, branch puts "Creating new branch #{new_branch} from #{branch}..." `git branch #{new_branch} #{branch}` end
DeleteLocalBranch(branch)
click to toggle source
# File lib/github_api.rb, line 96 def GithubApi.DeleteLocalBranch branch `git branch -D #{branch}` end
DeleteRemoteBranch(remote, branch)
click to toggle source
# File lib/github_api.rb, line 100 def GithubApi.DeleteRemoteBranch remote, branch status = GithubApi.DoesBranchExist remote, branch `git push #{remote} :#{branch}` if status.chomp! == GlobalConstants::EMPTY end
DoesBranchExist(remote, branch)
click to toggle source
# File lib/github_api.rb, line 29 def GithubApi.DoesBranchExist remote, branch puts "Checking if branch #{branch} existing at #{remote}..." `git ls-remote --heads #{remote} #{branch}` end
ForcePushBranch(remote, branch)
click to toggle source
# File lib/github_api.rb, line 68 def GithubApi.ForcePushBranch remote, branch # use url substituted with un/pwd #remote_url = GithubApi.InsertCredsInRemote remote puts "Force Pushing #{branch} to #{remote}..." `git push #{remote} #{branch} -f` end
GetCommitHashesFromLog(git_log)
click to toggle source
Returns commits in order of newest to oldest
# File lib/github_api.rb, line 155 def GithubApi.GetCommitHashesFromLog git_log matches = git_log.scan /^commit [a-zA-Z0-9]*$/ commit_len = 'commit '.length commit_hashes = matches.map { |v| v[commit_len, v.length-1] } return commit_hashes end
GetRecentCommitHash(branch)
click to toggle source
# File lib/github_api.rb, line 59 def GithubApi.GetRecentCommitHash branch git_log_raw = `git log -1 #{branch}` return GithubApi.GetCommitHashesFromLog(git_log_raw).first end
HaveLocalChanges()
click to toggle source
# File lib/github_api.rb, line 92 def GithubApi.HaveLocalChanges `git status -s` end
InsertCredsInRemote(remote_name)
click to toggle source
# File lib/github_api.rb, line 75 def GithubApi.InsertCredsInRemote remote_name url = `git config --get remote.#{remote_name}.url` url = GithubApi.InsertCredsInUrl(url) if !url.include? '@' url end
InsertCredsInUrl(url)
click to toggle source
# File lib/github_api.rb, line 81 def GithubApi.InsertCredsInUrl url url = url.sub('http://', "http://#{ENV['un']}:#{ENV['pwd']}@") url end
ProjectNameFromRepo(repo_url)
click to toggle source
# File lib/github_api.rb, line 198 def GithubApi.ProjectNameFromRepo repo_url puts "Repo Url provided: #{repo_url}. Parsing..." repo = GlobalConstants::EMPTY begin uri = Addressable::URI.parse repo_url rescue puts $! puts "repo_url: #{repo_url} parse failed" return repo end if uri.nil? puts 'Invalid repo_url provided' return repo end directory = Pathname.new(uri.path).basename if directory.nil? puts 'No directory provided in repo_url' return repo end repo = directory.to_s.gsub uri.extname, repo puts "Repository name parsed: #{repo}" repo end
PullWithRebase(remote, branch)
click to toggle source
# File lib/github_api.rb, line 105 def GithubApi.PullWithRebase remote, branch `git pull --rebase #{@repo_url} #{@branch}` end
PushBranch(remote, branch)
click to toggle source
# File lib/github_api.rb, line 86 def GithubApi.PushBranch remote, branch #remote_url = GithubApi.InsertCredsInRemote remote puts "Pushing #{branch} to #{remote}..." `git push #{remote} #{branch}` end
RebaseLocal(branch)
click to toggle source
# File lib/github_api.rb, line 34 def GithubApi.RebaseLocal branch puts "Rebasing #{branch} with checked out branch..." `git stash` `git rebase #{branch}` end
RevertLocal(branch, commit_hashes)
click to toggle source
Reverts commits from commit_hashes, expected order is newest to oldest
# File lib/github_api.rb, line 46 def GithubApi.RevertLocal branch, commit_hashes puts "Reverting commits on local branch: #{branch}..." `git checkout #{branch}` recent_hash = commit_hashes[0] past_hash = commit_hashes[-1] `git --no-edit revert #{past_hash}^..#{recent_hash}` end
SetPushDefaultSimple()
click to toggle source
# File lib/github_api.rb, line 226 def GithubApi.SetPushDefaultSimple `git config --global push.default simple` end
ShowCommitInfoLocal(commit_hash)
click to toggle source
# File lib/github_api.rb, line 64 def GithubApi.ShowCommitInfoLocal commit_hash `git show --name-only #{commit_hash}` end
TagLocal(commit_hash, tag_name, message)
click to toggle source
# File lib/github_api.rb, line 54 def GithubApi.TagLocal commit_hash, tag_name, message puts "Tagging commit hash: #{commit_hash} with #{tag_name}..." `git tag -a #{tag_name} #{commit_hash} -m #{message}` end