123456789_123456789_123456789_123456789_123456789_

Module: Octokit::Client::Contents

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/octokit/client/contents.rb

Overview

Methods for the Repo Contents API

Instance Method Summary

Instance Method Details

#add_content(*args)

Alias for #create_contents.

[ GitHub ]

  
# File 'lib/octokit/client/contents.rb', line 87

alias add_content create_contents

#add_contents(*args)

Alias for #create_contents.

[ GitHub ]

  
# File 'lib/octokit/client/contents.rb', line 88

alias add_contents create_contents

#content(repo, options = {})

Alias for #contents.

[ GitHub ]

  
# File 'lib/octokit/client/contents.rb', line 40

alias content contents

#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:

[ GitHub ]

  
# 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_content(*args)

Alias for #create_contents.

[ GitHub ]

  
# File 'lib/octokit/client/contents.rb', line 86

alias create_content create_contents

#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:

[ GitHub ]

  
# 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_content(repo, path, message, sha, options = {})

Alias for #delete_contents.

[ GitHub ]

  
# File 'lib/octokit/client/contents.rb', line 142

alias delete_content delete_contents

#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:

[ GitHub ]

  
# 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:

[ GitHub ]

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

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

#remove_content(repo, path, message, sha, options = {})

Alias for #delete_contents.

[ GitHub ]

  
# File 'lib/octokit/client/contents.rb', line 143

alias remove_content delete_contents

#remove_contents(repo, path, message, sha, options = {})

Alias for #delete_contents.

[ GitHub ]

  
# File 'lib/octokit/client/contents.rb', line 144

alias remove_contents delete_contents

#update_content(*args)

Alias for #update_contents.

[ GitHub ]

  
# File 'lib/octokit/client/contents.rb', line 119

alias update_content update_contents

#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:

[ GitHub ]

  
# 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