Class: BulkOps::GithubAccess
- Inherits:
-
Object
- Object
- BulkOps::GithubAccess
- Defined in:
- lib/bulk_ops/github_access.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
- .add_contents(name, file_name, contents, message: false) ⇒ Object
- .add_file(name, file_path, file_name = nil, message: false) ⇒ Object
- .auth_token(user_id = nil) ⇒ Object
- .auth_url(user) ⇒ Object
- .client_id ⇒ Object
- .client_secret ⇒ Object
- .create_branch!(name) ⇒ Object
- .list_branch_names(user) ⇒ Object
- .list_branches ⇒ Object
- .load_metadata(branch:, return_headers: false) ⇒ Object
- .load_options(name, branch: nil) ⇒ Object
- .redirect_endpoint(user, ssl: false) ⇒ Object
- .set_auth_token!(code, user_id = nil) ⇒ Object
- .state(user_id = nil) ⇒ Object
- .update_options(name, options, message: false) ⇒ Object
- .username(user) ⇒ Object
- .valid_state?(astate, user_id = nil) ⇒ Boolean
- .webhook_token ⇒ Object
Instance Method Summary collapse
- #add_contents(file_name, contents, message = false) ⇒ Object
- #add_file(file_path, file_name = nil, message: nil) ⇒ Object
- #add_new_spreadsheet(file, message = false) ⇒ Object
- #can_merge? ⇒ Boolean
- #create_branch! ⇒ Object
- #create_pull_request(message: false) ⇒ Object
- #delete_branch! ⇒ Object
-
#get_file(filename) ⇒ Object
def get_metadata_row row_number @current_metadata ||= load_metadata @current_metadata[row_number - BulkOps::ROW_OFFSET] end def get_past_metadata_row commit_sha, row_number past_metadata = Base64.decode64( client.contents(repo, path: filename, ref: commit_sha) ) past_metadata[row_number - BulkOps::ROW_OFFSET] end.
- #get_file_contents(path, ref: nil) ⇒ Object
- #get_file_sha(path, ref: nil) ⇒ Object
-
#initialize(newname = "dummy", user = nil) ⇒ GithubAccess
constructor
A new instance of GithubAccess.
- #list_branch_names ⇒ Object
- #list_branches ⇒ Object
- #load_metadata(branch: nil, return_headers: false) ⇒ Object
- #load_options(branch: nil) ⇒ Object
- #log_ingest_event(log_level, row_number, event_type, message, commit_sha = nil) ⇒ Object
- #merge_pull_request(pull_id, message: false) ⇒ Object
- #repo ⇒ Object
- #spreadsheet_count(branch: nil) ⇒ Object
- #spreadsheet_path ⇒ Object
- #update_options(options, message: false) ⇒ Object
- #update_spreadsheet(file, message: false) ⇒ Object
Constructor Details
#initialize(newname = "dummy", user = nil) ⇒ GithubAccess
Returns a new instance of GithubAccess.
107 108 109 110 |
# File 'lib/bulk_ops/github_access.rb', line 107 def initialize(newname="dummy", user = nil) @name = newname.parameterize @user = user end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/bulk_ops/github_access.rb', line 8 def name @name end |
Class Method Details
.add_contents(name, file_name, contents, message: false) ⇒ Object
83 84 85 |
# File 'lib/bulk_ops/github_access.rb', line 83 def self.add_contents name, file_name, contents, message: false self.new(name).add_contents file_name, contents, end |
.add_file(name, file_path, file_name = nil, message: false) ⇒ Object
79 80 81 |
# File 'lib/bulk_ops/github_access.rb', line 79 def self.add_file name, file_path, file_name = nil, message: false self.new(name).add_file file_path, file_name, message: end |
.auth_token(user_id = nil) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/bulk_ops/github_access.rb', line 32 def self.auth_token user_id=nil user_id = current_user.id if user_id.nil? user_id = user_id.id if user_id.is_a? User user_id = user_id.to_i if user_id.is_a? String return false unless (cred = BulkOps::GithubCredential.find_by(user_id: user_id)) return cred.auth_token || false end |
.auth_url(user) ⇒ Object
10 11 12 |
# File 'lib/bulk_ops/github_access.rb', line 10 def self.auth_url user "#{Octokit.web_endpoint}login/oauth/authorize?client_id=#{client_id}&redirect_uri=#{redirect_endpoint(user)}&state=#{state(user)}&scope=repo" end |
.client_id ⇒ Object
55 56 57 |
# File 'lib/bulk_ops/github_access.rb', line 55 def self.client_id YAML.load_file("#{Rails.root.to_s}/config/github.yml")[Rails.env]["client_id"] end |
.client_secret ⇒ Object
59 60 61 |
# File 'lib/bulk_ops/github_access.rb', line 59 def self.client_secret YAML.load_file("#{Rails.root.to_s}/config/github.yml")[Rails.env]["client_secret"] end |
.create_branch!(name) ⇒ Object
75 76 77 |
# File 'lib/bulk_ops/github_access.rb', line 75 def self.create_branch! name self.new(name).create_branch! end |
.list_branch_names(user) ⇒ Object
103 104 105 |
# File 'lib/bulk_ops/github_access.rb', line 103 def self.list_branch_names user self.new.list_branch_names end |
.list_branches ⇒ Object
99 100 101 |
# File 'lib/bulk_ops/github_access.rb', line 99 def self.list_branches self.new.list_branches end |
.load_metadata(branch:, return_headers: false) ⇒ Object
91 92 93 |
# File 'lib/bulk_ops/github_access.rb', line 91 def self. branch:, return_headers: false self.new(branch). return_headers: return_headers end |
.load_options(name, branch: nil) ⇒ Object
87 88 89 |
# File 'lib/bulk_ops/github_access.rb', line 87 def self. name, branch: nil self.new(name). branch: branch end |
.redirect_endpoint(user, ssl: false) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/bulk_ops/github_access.rb', line 67 def self.redirect_endpoint user, ssl: false host = Socket.gethostname host = "localhost" if Rails.env.development? or Rails.env.test? protocol = Rails.env.production? ? "https" : "http" protocol = "https" if ssl "#{protocol}://#{host}/bulk_ops/authorize/#{User.first.id}" end |
.set_auth_token!(code, user_id = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/bulk_ops/github_access.rb', line 40 def self.set_auth_token!(code,user_id=nil) user_id = current_user.id if user_id.nil? user_id = user_id.id if user_id.is_a? User user_id = user_id.to_i if user_id.is_a? String cred = BulkOps::GithubCredential.find_by(user_id: user_id) result = HTTParty.post("https://github.com/login/oauth/access_token", body: {client_id: client_id, client_secret: client_secret, code: code, accept: "json"}.to_json, headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}) cred.update(oauth_token: result.parsed_response["access_token"]) end |
.state(user_id = nil) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/bulk_ops/github_access.rb', line 24 def self.state user_id=nil user_id = current_user.id if user_id.nil? user_id = user_id.id if user_id.is_a? User user_id = user_id.to_i if user_id.is_a? String cred = BulkOps::GithubCredential.find_by(user_id: user_id) || BulkOps::GithubCredential.create({user_id: user_id, state: SecureRandom.hex }) cred.state end |
.update_options(name, options, message: false) ⇒ Object
95 96 97 |
# File 'lib/bulk_ops/github_access.rb', line 95 def self. name, , message: false self.new(name). , message: end |
.username(user) ⇒ Object
14 15 16 17 18 |
# File 'lib/bulk_ops/github_access.rb', line 14 def self.username user return false unless (cred = BulkOps::GithubCredential.find_by(user_id: user)) return false unless (token = cred.oauth_token) Octokit::Client.new(access_token: token).user[:login] rescue false end |
.valid_state?(astate, user_id = nil) ⇒ Boolean
20 21 22 |
# File 'lib/bulk_ops/github_access.rb', line 20 def self.valid_state?(astate,user_id=nil) return (astate === state(user_id)) end |
.webhook_token ⇒ Object
63 64 65 |
# File 'lib/bulk_ops/github_access.rb', line 63 def self.webhook_token YAML.load_file("#{Rails.root.to_s}/config/github.yml")[Rails.env]["client_secret"] end |
Instance Method Details
#add_contents(file_name, contents, message = false) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/bulk_ops/github_access.rb', line 129 def add_contents file_name, contents, =false ||= "adding file #{file_name} to github branch #{name}" begin client.create_contents(repo, file_name, , contents, branch: name) rescue Octokit::UnprocessableEntity sha = get_file_sha(file_name) client.update_contents(repo, file_name, , sha, contents, branch: name) end end |
#add_file(file_path, file_name = nil, message: nil) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/bulk_ops/github_access.rb', line 121 def add_file file_path, file_name = nil, message: nil file_name ||= File.basename(file_path) # unless (file_name.downcase == "readme.md") || (file_name.downcase.include? "#{name}/") file_name = File.join(name, file_name) unless file_name.downcase.include? "#{name.downcase}/" ||= "adding file #{file_name} to github branch #{name}" client.create_contents(repo, file_name, , file: file_path, branch: name) end |
#add_new_spreadsheet(file, message = false) ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/bulk_ops/github_access.rb', line 138 def add_new_spreadsheet file, =false if file.is_a? Tempfile file.close add_file file.path, BulkOps::SPREADSHEET_FILENAME, message: elsif file.is_a?(String) && File.file?(file) add_file file, BulkOps::SPREADSHEET_FILENAME, message: elsif file.is_a? String add_contents(spreadsheet_path, BulkOps::SPREADSHEET_FILENAME, message: ) end end |
#can_merge? ⇒ Boolean
207 208 209 |
# File 'lib/bulk_ops/github_access.rb', line 207 def can_merge? return true end |
#create_branch! ⇒ Object
112 113 114 |
# File 'lib/bulk_ops/github_access.rb', line 112 def create_branch! client.create_ref repo, "heads/#{name}", current_master_commit_sha end |
#create_pull_request(message: false) ⇒ Object
197 198 199 200 201 202 203 204 205 |
# File 'lib/bulk_ops/github_access.rb', line 197 def create_pull_request message: false begin ||= "Apply operation #{name} through Hyrax browser interface" pull = client.create_pull_request(repo, "master", name, ) pull["number"] rescue Octokit::UnprocessableEntity return false end end |
#delete_branch! ⇒ Object
116 117 118 119 |
# File 'lib/bulk_ops/github_access.rb', line 116 def delete_branch! return false unless list_branch_names.include? name client.delete_branch repo, name end |
#get_file(filename) ⇒ Object
def get_metadata_row row_number
@current_metadata ||= load_metadata
@current_metadata[row_number - BulkOps::ROW_OFFSET]
end
def get_past_metadata_row commit_sha, row_number
past_metadata = Base64.decode64( client.contents(repo, path: filename, ref: commit_sha) )
past_metadata[row_number - BulkOps::ROW_OFFSET]
end
225 226 227 |
# File 'lib/bulk_ops/github_access.rb', line 225 def get_file filename client.contents(repo, path: filename, ref: name) end |
#get_file_contents(path, ref: nil) ⇒ Object
229 230 231 |
# File 'lib/bulk_ops/github_access.rb', line 229 def get_file_contents(path, ref: nil) client.blob(github_config["repo"], get_file_sha(path, ref: ref))[:content] end |
#get_file_sha(path, ref: nil) ⇒ Object
233 234 235 236 |
# File 'lib/bulk_ops/github_access.rb', line 233 def get_file_sha path, ref: nil ref ||= name client.contents(repo, path: File.dirname(path), ref: ref).select{|file| file[:name]==File.basename(path)}.first[:sha] end |
#list_branch_names ⇒ Object
153 154 155 |
# File 'lib/bulk_ops/github_access.rb', line 153 def list_branch_names list_branches.map{|branch| branch[:name]} end |
#list_branches ⇒ Object
149 150 151 |
# File 'lib/bulk_ops/github_access.rb', line 149 def list_branches client.branches(repo).select{|branch| branch[:name] != "master"} end |
#load_metadata(branch: nil, return_headers: false) ⇒ Object
187 188 189 190 |
# File 'lib/bulk_ops/github_access.rb', line 187 def branch: nil, return_headers: false branch ||= name CSV.parse(Base64.decode64(get_file_contents(spreadsheet_path, ref: branch)), {headers: true, return_headers: return_headers}) end |
#load_options(branch: nil) ⇒ Object
177 178 179 180 |
# File 'lib/bulk_ops/github_access.rb', line 177 def branch: nil branch ||= name YAML.load(Base64.decode64(get_file_contents(, ref: branch))) end |
#log_ingest_event(log_level, row_number, event_type, message, commit_sha = nil) ⇒ Object
192 193 194 195 |
# File 'lib/bulk_ops/github_access.rb', line 192 def log_ingest_event log_level, row_number, event_type, , commit_sha = nil commit_sha ||= current_branch_commit_sha #TODO WRITE THIS CODE end |
#merge_pull_request(pull_id, message: false) ⇒ Object
211 212 213 |
# File 'lib/bulk_ops/github_access.rb', line 211 def merge_pull_request pull_id, message: false client.merge_pull_request(repo, pull_id, ) end |
#repo ⇒ Object
238 239 240 |
# File 'lib/bulk_ops/github_access.rb', line 238 def repo github_config["repo"] end |
#spreadsheet_count(branch: nil) ⇒ Object
182 183 184 185 |
# File 'lib/bulk_ops/github_access.rb', line 182 def spreadsheet_count branch: nil branch ||= name Base64.decode64(get_file_contents(spreadsheet_path, ref: branch)).lines.count - 1 end |
#spreadsheet_path ⇒ Object
242 243 244 |
# File 'lib/bulk_ops/github_access.rb', line 242 def spreadsheet_path "#{name}/#{BulkOps::SPREADSHEET_FILENAME}" end |
#update_options(options, message: false) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/bulk_ops/github_access.rb', line 164 def , message: false if ['reference_column_name'] (['ignored_columns'] ||= []) << ['reference_column_name'] (['reference_identifier'] ||= []) << ['reference_column_name'] end ['ignored_columns'] = ['ignored_columns'].reject { |c| c.empty? } if ['ignored_columns'].present? ||= "updating metadata spreadsheet through hyrax browser interface." sha = get_file_sha() client.update_contents(repo, , , sha, YAML.dump(), branch: name) end |
#update_spreadsheet(file, message: false) ⇒ Object
157 158 159 160 161 162 |
# File 'lib/bulk_ops/github_access.rb', line 157 def update_spreadsheet file, message: false ||= "updating metadata spreadsheet through hyrax browser interface." sha = get_file_sha(spreadsheet_path) file = File.new(file) if file.is_a?(String) && File.exist?(file) client.update_contents(repo, spreadsheet_path, , sha, file.read, branch: name) end |