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:



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

def archive_link(repo, options = {})
  repo_ref = ERB::Util.url_encode(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')

Lists the contents of lib /octokit.rb on a particular branch

Octokit.contents("octokit/octokit.rb", :path => 'lib/octokit.rb', :query => {:ref => 'some-other-branch'})

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:



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

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:



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

def create_contents(*args)
  args    = args.map { |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, 'content or :file option required' if content.nil?

  options[:content] = [content].pack('m0') # Base64.strict_encode64
  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:



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

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

Get the readme file for a particular branch of the repo

Octokit.readme("octokit/octokit.rb", :query => {:ref => 'some-other-branch'})

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:



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

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