Class: Pully::TestHelpers::Branch

Inherits:
Object
  • Object
show all
Defined in:
lib/pully.rb

Defined Under Namespace

Modules: Error

Instance Method Summary collapse

Constructor Details

#initialize(user:, pass:, repo_selector:, clone_url:) ⇒ Branch

repo_selector is like ‘my_user/repo’



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/pully.rb', line 92

def initialize(user:, pass:, repo_selector:, clone_url:)
  @user = user
  @pass = pass
  @repo_selector = repo_selector
  @clone_url = clone_url

  #Setup the local git client
  ##############################################################
  Git.configure do |config|
    config.git_ssh = "./spec/assets/git_ssh"
  end
  ##############################################################

  clone_repo

  #Setup Octocat client
  ##############################################################
  @gh_client = Octokit::Client.new(:login => @user, :password => @pass)
  begin
    @gh_client.user #throw exception if auth is bad
  rescue Octokit::Unauthorized
    raise Error::BadLogin
  end

  begin
    @repo = @gh_client.repo(repo_selector)
  rescue ArgumentError
    raise Error::BadRepoSelector
  rescue Octokit::NotFound
    raise Error::NoSuchRepository
  end
  ##############################################################
end

Instance Method Details

#clone_repoObject

Will clone down repo and set @gh_client to the new repo



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/pully.rb', line 127

def clone_repo
  #Create a temp path
  temp_file = Tempfile.new('pully')
  @path = temp_file.path
  temp_file.close
  temp_file.unlink

  #Clone repo
  begin
    @git_client = Git.clone(@clone_url, 'pully', :path => @path)
    @git_client.config("user.name", "pully-test-account")
    @git_client.config("user.email", "[email protected]")
  rescue Git::GitExecuteError => e
    raise Error::NoSuchCloneURL if e.message =~ /fatal: repository.*does not exist/
    raise "Unknown git execute error: #{e}"
  end
end

#commit_new_random_file(branch_name) ⇒ Object

Create, commit, and push to github



179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/pully.rb', line 179

def commit_new_random_file(branch_name)
  #Create a new file
  Dir.chdir "#{@path}/pully" do
    File.write "#{branch_name}.#{SecureRandom.hex}", branch_name
  end

  #Commit
  @git_client.add(:all => true)
  @git_client.commit_all("New branch from Pully Test Suite #{SecureRandom.hex}")
  local_sha = @git_client.object("HEAD").sha
  @git_client.push("origin", branch_name)

  return local_sha
end

#create_branch(new_branch_name) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/pully.rb', line 145

def create_branch(new_branch_name)
  #Checkout what ever real master is
  @git_client.branch(master_branch).checkout

  #Create a new branch
  @git_client.branch(new_branch_name)
  @git_client.branch(new_branch_name).checkout
end

#delete_branch(branch_name) ⇒ Object



154
155
156
# File 'lib/pully.rb', line 154

def delete_branch(branch_name)
  @git_client.push("origin", ":#{branch_name}")
end

#latest_message(branch_name) ⇒ Object



169
170
171
172
# File 'lib/pully.rb', line 169

def latest_message branch_name
  @git_client.checkout(branch_name)
  @git_client.object("HEAD").message
end

#latest_sha(branch_name) ⇒ Object



164
165
166
167
# File 'lib/pully.rb', line 164

def latest_sha branch_name
  @git_client.checkout(branch_name)
  @git_client.object("HEAD").sha
end

#list_branchesObject



158
159
160
161
162
# File 'lib/pully.rb', line 158

def list_branches
  #Re-pull repo from github
  clone_repo
  @git_client.branches.remote.map{|e| e.name}
end

#master_branchObject



174
175
176
# File 'lib/pully.rb', line 174

def master_branch
  @repo.default_branch
end