Module: Octokit::Client::Contents

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

Overview

Methods for the Repo Contents API

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")

Parameters:

  • repo (Integer, String, Repository, Hash)

    A GitHub repository.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • format (String)

    Either tarball (default) or zipball.

  • :ref (String)

    Optional valid Git reference, defaults to master.

Returns:

  • (String)

    Location of the download

See Also:



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

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

  response = client_without_redirects.head(url, options)
  response.headers['Location']
end

#contents(repo, options = {}) ⇒ Sawyer::Resource 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')

Parameters:

  • repo (Integer, String, Repository, Hash)

    A GitHub repository

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :path (String)

    A folder or file path

  • :ref (String)

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

Returns:

  • (Sawyer::Resource)

    The contents of a file or list of the files in the folder

See Also:



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

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

#create_contents(repo, path, message, content = nil, options = {}) ⇒ Sawyer::Resource 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")

Parameters:

  • repo (Integer, String, Repository, Hash)

    A GitHub repository

  • path (String)

    A path for the new content

  • message (String)

    A commit message for adding the content

  • optional

    content [String] The content for the file

Options Hash (options):

  • :branch (String)

    The branch on which to add the content

  • :file (String)

    Path or Ruby File object for content

Returns:

  • (Sawyer::Resource)

    The contents and commit info for the addition

Raises:

  • (ArgumentError)

See Also:



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

def create_contents(*args)
  args    = args.map { |item| item && item.dup }
  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

#delete_contents(repo, path, message, sha, options = {}) ⇒ Sawyer::Resource 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")

Parameters:

  • repo (Integer, String, Repository, Hash)

    A GitHub repository

  • path (String)

    A path for the content to delete

  • message (String)

    A commit message for deleting the content

  • sha (String)

    The _blob sha_ of the content to delete

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :branch (String)

    The branch on which to delete the content

Returns:

  • (Sawyer::Resource)

    The commit info for the delete

See Also:



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

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

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

Receive the default Readme for a repository

Examples:

Get the readme file for a repo

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

Parameters:

  • repo (Integer, String, Repository, Hash)

    A GitHub repository

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :ref (String)

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

Returns:

  • (Sawyer::Resource)

    The detail of the readme

See Also:



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

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

#update_contents(repo, path, message, sha, content = nil, options = {}) ⇒ Sawyer::Resource 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")

Parameters:

  • repo (Integer, String, Repository, Hash)

    A GitHub repository

  • path (String)

    A path for the content to update

  • message (String)

    A commit message for updating the content

  • sha (String)

    The _blob sha_ of the content to update

  • content (String) (defaults to: nil)

    The content for the file

Options Hash (options):

  • :branch (String)

    The branch on which to update the content

  • :file (String)

    Path or Ruby File object for content

Returns:

  • (Sawyer::Resource)

    The contents and commit info for the update

See Also:



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

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