Module: GitWand::GitHub::API::Commands::File
- Included in:
- GitWand::GitHub::API::Client
- Defined in:
- lib/git_wand/github/api/commands/file.rb
Instance Method Summary collapse
- #create_file(owner:, repo:, path:, message:, content:, branch:) ⇒ Object
-
#delete_file(owner:, repo:, path:, message:, branch:) ⇒ Object
Delete a file This method deletes a file in a repository.
-
#get_file(owner:, repo:, path:, ref:) ⇒ Object
Get contents This method returns the contents of a file or directory in a repository.
-
#update_file(owner:, repo:, path:, message:, content:, branch:) ⇒ Object
Update a file PUT /repos/:owner/:repo/contents/:path | Name | Type | Description | |——|——|————-| | path | string | Required.
Instance Method Details
#create_file(owner:, repo:, path:, message:, content:, branch:) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/git_wand/github/api/commands/file.rb', line 58 def create_file(owner:, repo:, path:, message:, content:, branch:) parameters = { message: , content: Base64::encode64(content), branch: branch, } response = put(resource: "repos/#{owner}/#{repo}/contents/#{path}", parameters: parameters) result = Result.new result.success = response[:status][:code] == "201" result.body = response[:body] result end |
#delete_file(owner:, repo:, path:, message:, branch:) ⇒ Object
Delete a file This method deletes a file in a repository
DELETE /repos/:owner/:repo/contents/:path
| Name | Type | Description | |——|——|————-| | path | string | Required. The content path. | | message | string | Required. The commit message. | | sha | string | Required. The blob SHA of the file being replaced. | | branch | string | The branch name. Default: the repository’s default branch (usually master) |
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/git_wand/github/api/commands/file.rb', line 82 def delete_file(owner:, repo:, path:, message:, branch:) result = get_file(owner: owner, repo: repo, path: path, ref: branch) # TODO: handle errors while retrieving the resource sha = result.body["sha"] parameters = { message: , sha: sha, branch: branch, } response = delete(resource: "repos/#{owner}/#{repo}/contents/#{path}", parameters: parameters) result = Result.new result.success = response[:status][:code] == "200" result.body = response[:body] result end |
#get_file(owner:, repo:, path:, ref:) ⇒ Object
Get contents This method returns the contents of a file or directory in a repository.
GET /repos/:owner/:repo/contents/:path
| Name | Type | Description | |——|——|————-| | path | string | The content path. | | ref | string | The name of the commit/branch/tag. Default: the repository’s default branch (usually master) | b8b0a4b3d8dfa9a571353a5c29a9963110d32856
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/git_wand/github/api/commands/file.rb', line 20 def get_file(owner:, repo:, path:, ref:) parameters = { ref: ref } response = get(resource: "repos/#{owner}/#{repo}/contents/#{path}", query_parameters: parameters) result = Result.new result.success = response[:status][:code] == "200" result.body = response[:body] result.resource = Resource::File.build_from_api_result(result) result end |
#update_file(owner:, repo:, path:, message:, content:, branch:) ⇒ Object
Update a file PUT /repos/:owner/:repo/contents/:path | Name | Type | Description | |——|——|————-| | path | string | Required. The content path. | | message | string | Required. The commit message. | | content | string | Required. The updated file content, Base64 encoded. | | sha | string | Required. The blob SHA of the file being replaced. | | branch | string | The branch name. Default: the repository’s default branch (usually master) |
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/git_wand/github/api/commands/file.rb', line 41 def update_file(owner:, repo:, path:, message:, content:, branch:) result = get_file(owner: owner, repo: repo, path: path, ref: branch) # TODO: handle errors while retrieving the resource sha = result.body["sha"] parameters = { message: , content: Base64::encode64(content), sha: sha, branch: branch, } response = put(resource: "repos/#{owner}/#{repo}/contents/#{path}", parameters: parameters) result = Result.new result.success = response[:status][:code] == "200" result.body = response[:body] result end |