Class: Worochi::Agent::Github

Inherits:
Worochi::Agent show all
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

#options

Instance Method Summary collapse

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.

Raises:



93
94
95
# File 'lib/worochi/agent/github.rb', line 93

def delete(path)
  raise Error, 'Cannot delete from GitHub'
end

#init_clientOctokit::Client

Initializes Octokit client. Refer to octokit.rb documentation.

Returns:

  • (Octokit::Client)


13
14
15
16
# File 'lib/worochi/agent/github.rb', line 13

def init_client
  @client = Octokit::Client.new(login: 'me', oauth_token: options[:token])
  @client
end

#list(path = nil, sha = nil) ⇒ Array<Hash>

Returns a list of files and subdirectories at the remote path specified by ‘options`.

Parameters:

  • path (String) (defaults to: nil)

    path to list instead of the current directory

  • sha (String) (defaults to: nil)

    list a different branch than the ‘:source`

Returns:

  • (Array<Hash>)

    list of files and subdirectories

Raises:



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.

Parameters:

  • items (Array<Item>)

Returns:

  • (String)

    Commit SHA1 hash

See Also:



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, options[:commit_msg], source_sha,
                                 target_branch)
  @client.update_ref(repo, "heads/#{options[:target]}", commit.sha)
  commit.sha
end

#push_item(item) ⇒ nil

Pushes a single Item to GitHub. This means making a new commit for each file. Not recommended and should just use #push_all instead.

Parameters:

Returns:

  • (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>

Returns a list of repositories for the remote branch specified by ‘options`. If `opts` is `true`, then only repos with push access are returned. If `opts` is `true`, returns hashes containing more information about each repo.

Parameters:

  • opts (Hash) (defaults to: { push: false, details: false, orgs: true })

Returns:

  • (Array<String>, Array<Hash>)

    a list of repositories



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.).map {|repo| parse_repo repo}
  end
  repos.reject! {|repo| !repo[:push]} if opts[:push]
  repos.map { |repo| repo[:full_name] } unless opts[:details]
end