Class: Worochi::Agent::Github
- Inherits:
-
Worochi::Agent
- Object
- Worochi::Agent
- Worochi::Agent::Github
- Defined in:
- lib/worochi/agent/github.rb,
lib/worochi/helper/github_helper.rb
Overview
The Worochi::Agent for GitHub API. This wraps around the ‘octokit` gem.
Defined Under Namespace
Modules: Helper
Instance Attribute Summary
Attributes inherited from Worochi::Agent
Instance Method Summary collapse
-
#delete(path) ⇒ Object
Deletion is not supported by GitHub, so raises Error.
-
#init_client ⇒ Octokit::Client
Initializes Octokit client.
-
#list(path = nil, sha = nil) ⇒ Array<Hash>
Returns a list of files and subdirectories at the remote path specified by ‘options`.
-
#push_all(items) ⇒ String
Pushes a list of Item to GitHub.
-
#push_item(item) ⇒ nil
Pushes a single Item to GitHub.
-
#repos(opts = { push: false, details: false, orgs: true }) ⇒ Array<String>, Array<Hash>
Returns a list of repositories for the remote branch specified by ‘options`.
Methods inherited from Worochi::Agent
#files, #files_and_folders, #folders, #initialize, #name, new, #push, #push_items, #remove, #set_dir, #set_options, #type
Constructor Details
This class inherits a constructor from Worochi::Agent
Instance Method Details
#delete(path) ⇒ Object
Deletion is not supported by GitHub, so raises Error.
93 94 95 |
# File 'lib/worochi/agent/github.rb', line 93 def delete(path) raise Error, 'Cannot delete from GitHub' end |
#init_client ⇒ Octokit::Client
Initializes Octokit client. Refer to octokit.rb documentation.
13 14 15 16 |
# File 'lib/worochi/agent/github.rb', line 13 def init_client @client = Octokit::Client.new(login: 'me', oauth_token: [:token]) @client end |
#list(path = nil, sha = nil) ⇒ Array<Hash>
Returns a list of files and subdirectories at the remote path specified by ‘options`.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/worochi/agent/github.rb', line 48 def list(path=nil, sha=nil) remote_path = list_path(path).sub(/^\//, '').sub(/\/$/, '') result = @client.tree(repo, sha || source_branch, recursive: true).tree result.sort! do |x, y| x.path.split('/').size <=> y.path.split('/').size end # Filters for folders containing the specified path result.reject! { |elem| !elem.path.match(remote_path + '($|\/.+)') } raise Error, 'Invalid GitHub path specified' if result.empty? # Filters out lower levels result.reject! do |elem| filename = elem.path.split('/').last File.join(remote_path, filename).sub(/^\//, '') != elem.path end result.map do |elem| { name: elem.path.split('/').last, path: elem.path, type: elem.type == 'tree' ? 'folder' : 'file', sha: elem.sha } end end |
#push_all(items) ⇒ String
Pushes a list of Item to GitHub.
23 24 25 26 27 28 29 30 |
# File 'lib/worochi/agent/github.rb', line 23 def push_all(items) source_sha = source_branch items.each { |item| source_sha = tree_append(source_sha, item) } commit = @client.create_commit(repo, [:commit_msg], source_sha, target_branch) @client.update_ref(repo, "heads/#{options[:target]}", commit.sha) commit.sha end |
#push_item(item) ⇒ nil
37 38 39 40 |
# File 'lib/worochi/agent/github.rb', line 37 def push_item(item) Worochi::Log.warn 'push_item should not be used for GitHub' push_all([item]) end |
#repos(opts = { push: false, details: false, orgs: true }) ⇒ Array<String>, Array<Hash>
83 84 85 86 87 88 89 90 |
# File 'lib/worochi/agent/github.rb', line 83 def repos(opts={ push: false, details: false, orgs: true }) repos = @client.repositories.map {|repo| parse_repo repo} @client.organizations.each do |org| repos += @client.organization_repositories(org.login).map {|repo| parse_repo repo} end repos.reject! {|repo| !repo[:push]} if opts[:push] repos.map { |repo| repo[:full_name] } unless opts[:details] end |