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’



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pully.rb', line 53

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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pully.rb', line 88

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



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/pully.rb', line 130

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

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

#create_branch(new_branch_name) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/pully.rb', line 106

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



115
116
117
# File 'lib/pully.rb', line 115

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

#list_branchesObject



119
120
121
122
123
# File 'lib/pully.rb', line 119

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

#master_branchObject



125
126
127
# File 'lib/pully.rb', line 125

def master_branch
  @repo.default_branch
end