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’



86
87
88
89
90
91
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
# File 'lib/pully.rb', line 86

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/pully.rb', line 121

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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/pully.rb', line 173

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



139
140
141
142
143
144
145
146
# File 'lib/pully.rb', line 139

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



148
149
150
# File 'lib/pully.rb', line 148

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

#latest_message(branch_name) ⇒ Object



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

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

#latest_sha(branch_name) ⇒ Object



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

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

#list_branchesObject



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

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

#master_branchObject



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

def master_branch
  @repo.default_branch
end