File: contents.rb

package info (click to toggle)
ruby-octokit 10.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,092 kB
  • sloc: ruby: 13,339; sh: 99; makefile: 7; javascript: 3
file content (165 lines) | stat: -rw-r--r-- 7,709 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# frozen_string_literal: true

module Octokit
  class Client
    # Methods for the Repo Contents API
    #
    # @see https://developer.github.com/v3/repos/contents/
    module Contents
      # Receive the default Readme for a repository
      #
      # @param repo [Integer, String, Repository, Hash] A GitHub repository
      # @option options [String] :ref name of the Commit/Branch/Tag. Defaults to “master”.
      # @return [Sawyer::Resource] The detail of the readme
      # @see https://developer.github.com/v3/repos/contents/#get-the-readme
      # @example Get the readme file for a repo
      #   Octokit.readme("octokit/octokit.rb")
      # @example Get the readme file for a particular branch of the repo
      #   Octokit.readme("octokit/octokit.rb", :query => {:ref => 'some-other-branch'})
      def readme(repo, options = {})
        get "#{Repository.path repo}/readme", options
      end

      # Receive a listing of a repository folder or the contents of a file
      #
      # @param repo [Integer, String, Repository, Hash] A GitHub repository
      # @option options [String] :path A folder or file path
      # @option options [String] :ref name of the Commit/Branch/Tag. Defaults to “master”.
      # @return [Sawyer::Resource] The contents of a file or list of the files in the folder
      # @see https://developer.github.com/v3/repos/contents/#get-contents
      # @example List the contents of lib/octokit.rb
      #   Octokit.contents("octokit/octokit.rb", :path => 'lib/octokit.rb')
      # @example 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'})
      def contents(repo, options = {})
        options = options.dup
        repo_path = options.delete :path
        url = "#{Repository.path repo}/contents/#{repo_path}"
        get url, options
      end
      alias content contents

      # Add content to a repository
      #
      # @overload create_contents(repo, path, message, content = nil, options = {})
      #   @param repo [Integer, String, Repository, Hash] A GitHub repository
      #   @param path [String] A path for the new content
      #   @param message [String] A commit message for adding the content
      #   @param optional content [String] The content for the file
      #   @option options [String] :branch The branch on which to add the content
      #   @option options [String] :file Path or Ruby File object for content
      # @return [Sawyer::Resource] The contents and commit info for the addition
      # @see https://developer.github.com/v3/repos/contents/#create-a-file
      # @example Add content at lib/octokit.rb
      #   Octokit.create_contents("octokit/octokit.rb",
      #                    "lib/octokit.rb",
      #                    "Adding content",
      #                    "File content",
      #                    :branch => "my-new-feature")
      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
      alias create_content create_contents
      alias add_content create_contents
      alias add_contents create_contents

      # Update content in a repository
      #
      # @overload update_contents(repo, path, message, sha, content = nil, options = {})
      #   @param repo [Integer, String, Repository, Hash] A GitHub repository
      #   @param path [String] A path for the content to update
      #   @param message [String] A commit message for updating the content
      #   @param sha [String] The _blob sha_ of the content to update
      #   @param content [String] The content for the file
      #   @option options [String] :branch The branch on which to update the content
      #   @option options [String] :file Path or Ruby File object for content
      # @return [Sawyer::Resource] The contents and commit info for the update
      # @see https://developer.github.com/v3/repos/contents/#update-a-file
      # @example Update content at lib/octokit.rb
      #   Octokit.update_contents("octokit/octokit.rb",
      #                    "lib/octokit.rb",
      #                    "Updating content",
      #                    "7eb95f97e1a0636015df3837478d3f15184a5f49",
      #                    "File content",
      #                    :branch => "my-new-feature")
      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
      alias update_content update_contents

      # Delete content in a repository
      #
      # @param repo [Integer, String, Repository, Hash] A GitHub repository
      # @param path [String] A path for the content to delete
      # @param message [String] A commit message for deleting the content
      # @param sha [String] The _blob sha_ of the content to delete
      # @option options [String] :branch The branch on which to delete the content
      # @return [Sawyer::Resource] The commit info for the delete
      # @see https://developer.github.com/v3/repos/contents/#delete-a-file
      # @example Delete content at lib/octokit.rb
      #   Octokit.delete_contents("octokit/octokit.rb",
      #                    "lib/octokit.rb",
      #                    "Deleting content",
      #                    "7eb95f97e1a0636015df3837478d3f15184a5f49",
      #                    :branch => "my-new-feature")
      def delete_contents(repo, path, message, sha, options = {})
        options[:message] = message
        options[:sha] = sha
        url = "#{Repository.path repo}/contents/#{path}"
        delete url, options
      end
      alias delete_content delete_contents
      alias remove_content delete_contents
      alias remove_contents delete_contents

      # This method will provide a URL to download a tarball or zipball archive for a repository.
      #
      # @param repo [Integer, String, Repository, Hash] A GitHub repository.
      # @option options format [String] Either tarball (default) or zipball.
      # @option options [String] :ref Optional valid Git reference, defaults to master.
      # @return [String] Location of the download
      # @see https://developer.github.com/v3/repos/contents/#get-archive-link
      # @example Get archive link for octokit/octokit.rb
      #   Octokit.archive_link("octokit/octokit.rb")
      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
    end
  end
end