Class: Bookbinder::GitHubRepository
- Inherits:
-
Object
- Object
- Bookbinder::GitHubRepository
show all
- Includes:
- ShellOut
- Defined in:
- lib/bookbinder/git_hub_repository.rb
Defined Under Namespace
Classes: RepositoryCloneError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from ShellOut
#announce_failure, #shell_out
Constructor Details
#initialize(logger: nil, full_name: nil, target_ref: nil, github_token: nil, directory: nil, local_repo_dir: nil) ⇒ GitHubRepository
Returns a new instance of GitHubRepository.
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/bookbinder/git_hub_repository.rb', line 38
def initialize(logger: nil, full_name: nil, target_ref: nil, github_token: nil, directory: nil, local_repo_dir: nil)
@logger = logger
raise 'No full_name provided ' unless full_name
@full_name = full_name
@github = GitClient.new(logger, access_token: github_token || ENV['GITHUB_API_TOKEN'])
@target_ref = target_ref
@directory = directory
@local_repo_dir = local_repo_dir
end
|
Instance Attribute Details
#copied_to ⇒ Object
Returns the value of attribute copied_to.
17
18
19
|
# File 'lib/bookbinder/git_hub_repository.rb', line 17
def copied_to
@copied_to
end
|
#full_name ⇒ Object
Returns the value of attribute full_name.
17
18
19
|
# File 'lib/bookbinder/git_hub_repository.rb', line 17
def full_name
@full_name
end
|
Class Method Details
.build_from_local(logger, section_hash, local_repo_dir, destination_dir) ⇒ Object
28
29
30
31
32
33
34
35
36
|
# File 'lib/bookbinder/git_hub_repository.rb', line 28
def self.build_from_local(logger, section_hash, local_repo_dir, destination_dir)
full_name = section_hash.fetch('repository').fetch('name')
directory = section_hash['directory']
repository = new(logger: logger, full_name: full_name, directory: directory, local_repo_dir: local_repo_dir)
repository.copy_from_local(destination_dir) if destination_dir
repository
end
|
.build_from_remote(logger, section_hash, destination_dir, target_ref, git_accessor) ⇒ Object
19
20
21
22
23
24
25
26
|
# File 'lib/bookbinder/git_hub_repository.rb', line 19
def self.build_from_remote(logger, section_hash, destination_dir, target_ref, git_accessor)
full_name = section_hash.fetch('repository', {}).fetch('name')
target_ref = target_ref || section_hash.fetch('repository', {})['ref']
directory = section_hash['directory']
repository = new(logger: logger, full_name: full_name, target_ref: target_ref, github_token: ENV['GITHUB_API_TOKEN'], directory: directory)
repository.copy_from_remote(destination_dir, git_accessor) if destination_dir
repository
end
|
Instance Method Details
#announce_skip ⇒ Object
115
116
117
|
# File 'lib/bookbinder/git_hub_repository.rb', line 115
def announce_skip
@logger.log ' skipping (not found) '.magenta + path_to_local_repo
end
|
#copied? ⇒ Boolean
98
99
100
|
# File 'lib/bookbinder/git_hub_repository.rb', line 98
def copied?
!@copied_to.nil?
end
|
#copy_from_local(destination_dir) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/bookbinder/git_hub_repository.rb', line 82
def copy_from_local(destination_dir)
if File.exist?(path_to_local_repo)
@logger.log ' copying '.yellow + path_to_local_repo
destination = File.join(destination_dir, directory)
unless destination.include?(path_to_local_repo)
FileUtils.mkdir_p(destination)
FileUtils.cp_r(File.join(path_to_local_repo, '.'), destination)
end
@copied_to = File.join(destination_dir, directory)
else
announce_skip
end
end
|
#copy_from_remote(destination_dir, git_accessor = Git) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/bookbinder/git_hub_repository.rb', line 65
def copy_from_remote(destination_dir, git_accessor = Git)
begin
@git = git_accessor.clone("[email protected]:#{full_name}", directory, path: destination_dir)
rescue => e
if e.message.include? "Permission denied (publickey)"
raise RepositoryCloneError.new "Unable to access repository #{full_name}. You do not have the correct access rights. Please either add the key to your SSH agent, or set the GIT_SSH environment variable to override default SSH key usage. For more information run: `man git`."
elsif
e.message.include? "Repository not found."
raise RepositoryCloneError.new "Could not read from repository. Please make sure you have the correct access rights and the repository #{full_name} exists."
else
raise e
end
end
@git.checkout(target_ref) unless target_ref == 'master'
@copied_to = destination_dir
end
|
#directory ⇒ Object
61
62
63
|
# File 'lib/bookbinder/git_hub_repository.rb', line 61
def directory
@directory || short_name
end
|
#get_modification_date_for(file: nil, git: nil) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/bookbinder/git_hub_repository.rb', line 119
def get_modification_date_for(file: nil, git: nil)
@git ||= git
raise "Unexpected Error: Git accessor unavailable." if @git.nil?
irrelevant_path_component = directory+'/'
repo_path = file.gsub(irrelevant_path_component, '')
begin
@git.log(1).object(repo_path).first.date
rescue Git::GitExecuteError => e
raise "This file does not exist or is not tracked by git! Cannot get last modified date for #{repo_path}."
end
end
|
#has_git_object? ⇒ Boolean
137
138
139
|
# File 'lib/bookbinder/git_hub_repository.rb', line 137
def has_git_object?
!!@git
end
|
#has_tag?(tagname) ⇒ Boolean
102
103
104
|
# File 'lib/bookbinder/git_hub_repository.rb', line 102
def has_tag?(tagname)
tags.any? { |tag| tag.name == tagname }
end
|
#head_sha ⇒ Object
57
58
59
|
# File 'lib/bookbinder/git_hub_repository.rb', line 57
def head_sha
@head_sha ||= @github.head_sha(full_name)
end
|
#path_to_local_repo ⇒ Object
133
134
135
|
# File 'lib/bookbinder/git_hub_repository.rb', line 133
def path_to_local_repo
File.join(@local_repo_dir, short_name)
end
|
#short_name ⇒ Object
53
54
55
|
# File 'lib/bookbinder/git_hub_repository.rb', line 53
def short_name
full_name.split('/')[1]
end
|
#tag_with(tagname) ⇒ Object
49
50
51
|
# File 'lib/bookbinder/git_hub_repository.rb', line 49
def tag_with(tagname)
@github.create_tag! full_name, tagname, head_sha
end
|
#update_local_copy ⇒ Object
106
107
108
109
110
111
112
113
|
# File 'lib/bookbinder/git_hub_repository.rb', line 106
def update_local_copy
if File.exist?(path_to_local_repo)
@logger.log 'Updating ' + path_to_local_repo.cyan
Kernel.system("cd #{path_to_local_repo} && git pull")
else
announce_skip
end
end
|