Module: GithubApi

Defined in:
lib/github_api.rb

Overview

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 - ''

Class Method Summary collapse

Class Method Details

.BranchCommitDiff(base_branch, derived_branch) ⇒ Object

Returns commits in order of newest to oldest



146
147
148
149
150
151
152
# 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) ⇒ Object



19
20
21
22
23
24
25
26
27
# 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) ⇒ Object



40
41
42
43
# File 'lib/github_api.rb', line 40

def GithubApi.CheckoutLocal branch
  puts "Checking out local branch: #{branch}..."
  `git checkout #{branch}`
end

.CheckoutNewBranch(branch) ⇒ Object



14
15
16
17
# 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) ⇒ Object

we do NOT want to switch to parent folder but stay in current repo dir when we exit this method



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# 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) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# 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) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# 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) ⇒ Object



9
10
11
12
# 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) ⇒ Object



96
97
98
# File 'lib/github_api.rb', line 96

def GithubApi.DeleteLocalBranch branch
  `git branch -D #{branch}`
end

.DeleteRemoteBranch(remote, branch) ⇒ Object



100
101
102
103
# 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) ⇒ Object



29
30
31
32
# 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) ⇒ Object



68
69
70
71
72
73
# 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) ⇒ Object

Returns commits in order of newest to oldest



155
156
157
158
159
160
# 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) ⇒ Object



59
60
61
62
# 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

.HaveLocalChangesObject



92
93
94
# File 'lib/github_api.rb', line 92

def GithubApi.HaveLocalChanges
  `git status -s`
end

.InsertCredsInRemote(remote_name) ⇒ Object



75
76
77
78
79
# 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) ⇒ Object



81
82
83
84
# 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) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# 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) ⇒ Object



105
106
107
# File 'lib/github_api.rb', line 105

def GithubApi.PullWithRebase remote, branch
  `git pull --rebase #{@repo_url} #{@branch}`
end

.PushBranch(remote, branch) ⇒ Object



86
87
88
89
90
# 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) ⇒ Object



34
35
36
37
38
# 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) ⇒ Object

Reverts commits from commit_hashes, expected order is newest to oldest



46
47
48
49
50
51
52
# 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

.SetPushDefaultSimpleObject



226
227
228
# File 'lib/github_api.rb', line 226

def GithubApi.SetPushDefaultSimple
  `git config --global push.default simple`
end

.ShowCommitInfoLocal(commit_hash) ⇒ Object



64
65
66
# 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) ⇒ Object



54
55
56
57
# 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