Module: Octokit::Client::Contents

Included in:
Octokit::Client
Defined in:
lib/octokit/client/contents.rb

Overview

Methods for the Repo Contents API

See Also:

Instance Method Summary (collapse)

Instance Method Details

This method will provide a URL to download a tarball or zipball archive for a repository.

Examples:

Get archive link for octokit/octokit.rb

Octokit.archive_link("octokit/octokit.rb")

Options Hash (options):

  • format (String)

    Either tarball (default) or zipball.

  • :ref (String)

    Optional valid Git reference, defaults to master.

See Also:



152
153
154
155
156
157
158
159
# File 'lib/octokit/client/contents.rb', line 152

def archive_link(repo, options={})
  repo_ref = options.delete :ref
  format = (options.delete :format) || 'tarball'
  url = "#{Repository.path repo}/#{format}/#{repo_ref}"
  request :head, url, options

  last_response.headers['Location']
end

- (Sawyer::Resource) contents(repo, options = {}) Also known as: content

Receive a listing of a repository folder or the contents of a file

Examples:

List the contents of lib/octokit.rb

Octokit.contents("octokit/octokit.rb", :path => 'lib/octokit.rb')

Options Hash (options):

  • :path (String)

    A folder or file path

  • :ref (String)

    name of the Commit/Branch/Tag. Defaults to “master”.

See Also:



32
33
34
35
36
# File 'lib/octokit/client/contents.rb', line 32

def contents(repo, options={})
  repo_path = options.delete :path
  url = "#{Repository.path repo}/contents/#{repo_path}"
  get url, options
end

- (Sawyer::Resource) create_contents(repo, path, message, content = nil, options = {}) Also known as: create_content, add_content, add_contents

Add content to a repository

Examples:

Add content at lib/octokit.rb

Octokit.create_contents("octokit/octokit.rb",
                 "lib/octokit.rb",
                 "Adding content",
                 "File content",
                 :branch => "my-new-feature")

Options Hash (options):

  • :branch (String)

    The branch on which to add the content

  • :file (String)

    Path or Ruby File object for content

Raises:

  • (ArgumentError)

See Also:



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
# File 'lib/octokit/client/contents.rb', line 56

def create_contents(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  repo    = args.shift
  path    = args.shift
  message = args.shift
  content = args.shift
  if content.nil? && file = options.delete(:file)
    case file
    when String
      if File.exist?(file)
        file = File.open(file, "r")
        content = file.read
        file.close
      end
    when File, Tempfile
      content = file.read
      file.close
    end
  end
  raise ArgumentError.new("content or :file option required") if content.nil?
  options[:content] = Base64.respond_to?(:strict_encode64) ?
    Base64.strict_encode64(content) :
    Base64.encode64(content).delete("\n") # Ruby 1.9.2
  options[:message] = message
  url = "#{Repository.path repo}/contents/#{path}"
  put url, options
end

- (Sawyer::Resource) delete_contents(repo, path, message, sha, options = {}) Also known as: delete_content, remove_content, remove_contents

Delete content in a repository

Examples:

Delete content at lib/octokit.rb

Octokit.delete_contents("octokit/octokit.rb",
                 "lib/octokit.rb",
                 "Deleting content",
                 "7eb95f97e1a0636015df3837478d3f15184a5f49",
                 :branch => "my-new-feature")

Options Hash (options):

  • :branch (String)

    The branch on which to delete the content

See Also:



133
134
135
136
137
138
# File 'lib/octokit/client/contents.rb', line 133

def delete_contents(repo, path, message, sha, options = {})
  options[:message] = message
  options[:sha] = sha
  url = "#{Repository.path repo}/contents/#{path}"
  delete url, options
end

- (Sawyer::Resource) readme(repo, options = {})

Receive the default Readme for a repository

Examples:

Get the readme file for a repo

Octokit.readme("octokit/octokit.rb")

Options Hash (options):

  • :ref (String)

    name of the Commit/Branch/Tag. Defaults to “master”.

See Also:



19
20
21
# File 'lib/octokit/client/contents.rb', line 19

def readme(repo, options={})
  get "#{Repository.path repo}/readme", options
end

- (Sawyer::Resource) update_contents(repo, path, message, sha, content = nil, options = {}) Also known as: update_content

Update content in a repository

Examples:

Update content at lib/octokit.rb

Octokit.update_contents("octokit/octokit.rb",
                 "lib/octokit.rb",
                 "Updating content",
                 "7eb95f97e1a0636015df3837478d3f15184a5f49",
                 "File content",
                 :branch => "my-new-feature")

Options Hash (options):

  • :branch (String)

    The branch on which to update the content

  • :file (String)

    Path or Ruby File object for content

See Also:



106
107
108
109
110
111
112
113
114
115
# File 'lib/octokit/client/contents.rb', line 106

def update_contents(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  repo    = args.shift
  path    = args.shift
  message = args.shift
  sha     = args.shift
  content = args.shift
  options.merge!(:sha => sha)
  create_contents(repo, path, message, content, options)
end