-
-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for repository files create, edit and remove.
- Loading branch information
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
require 'base64' | ||
|
||
class Gitlab::Client | ||
# Defines methods related to repository files. | ||
module RepositoryFiles | ||
# Creates a new repository file. | ||
# | ||
# @example | ||
# Gitlab.create_file(42, "path", "branch", "content", "commit message") | ||
# | ||
# @param [Integer] project The ID of a project. | ||
# @param [String] full path to new file. | ||
# @param [String] the name of the branch. | ||
# @param [String] file content. | ||
# @param [String] commit message. | ||
# @return [Gitlab::ObjectifiedHash] | ||
def create_file(project, path, branch, content, commit_message) | ||
post("/projects/#{project}/repository/files", body: { | ||
file_path: path, | ||
branch_name: branch, | ||
commit_message: commit_message, | ||
}.merge(encoded_content_attributes(content))) | ||
end | ||
|
||
# Edits an existing repository file. | ||
# | ||
# @example | ||
# Gitlab.edit_file(42, "path", "branch", "content", "commit message") | ||
# | ||
# @param [Integer] project The ID of a project. | ||
# @param [String] full path to new file. | ||
# @param [String] the name of the branch. | ||
# @param [String] file content. | ||
# @param [String] commit message. | ||
# @return [Gitlab::ObjectifiedHash] | ||
def edit_file(project, path, branch, content, commit_message) | ||
put("/projects/#{project}/repository/files", body: { | ||
file_path: path, | ||
branch_name: branch, | ||
commit_message: commit_message, | ||
}.merge(encoded_content_attributes(content))) | ||
end | ||
|
||
# Removes an existing repository file. | ||
# | ||
# @example | ||
# Gitlab.remove_file(42, "path", "branch", "commit message") | ||
# | ||
# @param [Integer] project The ID of a project. | ||
# @param [String] full path to new file. | ||
# @param [String] the name of the branch. | ||
# @param [String] commit message. | ||
# @return [Gitlab::ObjectifiedHash] | ||
def remove_file(project, path, branch, commit_message) | ||
delete("/projects/#{project}/repository/files", body: { | ||
file_path: path, | ||
branch_name: branch, | ||
commit_message: commit_message, | ||
}) | ||
end | ||
|
||
private | ||
|
||
def encoded_content_attributes(content) | ||
{ | ||
encoding: 'base64', | ||
content: Base64.encode64(content), | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"file_path":"path","branch_name":"branch","encoding":"base64","content":"Y29udGVudA==","commit_message":"commit message"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require "spec_helper" | ||
|
||
describe Gitlab::Client do | ||
describe ".create_file" do | ||
let!(:request_stub) { stub_post("/projects/3/repository/files", "repository_file") } | ||
let!(:file) { Gitlab.create_file(3, "path", "branch", "content", "commit message") } | ||
|
||
it "should create the correct resource" do | ||
expect(request_stub).to have_been_made | ||
end | ||
|
||
it "should return information about the new file" do | ||
expect(file.file_path).to eq "path" | ||
expect(file.branch_name).to eq "branch" | ||
end | ||
end | ||
|
||
describe ".edit_file" do | ||
let!(:request_stub) { stub_put("/projects/3/repository/files", "repository_file") } | ||
let!(:file) { Gitlab.edit_file(3, "path", "branch", "content", "commit message") } | ||
|
||
it "should create the correct resource" do | ||
expect(request_stub).to have_been_made | ||
end | ||
|
||
it "should return information about the new file" do | ||
expect(file.file_path).to eq "path" | ||
expect(file.branch_name).to eq "branch" | ||
end | ||
end | ||
|
||
describe ".remove_file" do | ||
let!(:request_stub) { stub_delete("/projects/3/repository/files", "repository_file") } | ||
let!(:file) { Gitlab.remove_file(3, "path", "branch", "commit message") } | ||
|
||
it "should create the correct resource" do | ||
expect(request_stub).to have_been_made | ||
end | ||
|
||
it "should return information about the new file" do | ||
expect(file.file_path).to eq "path" | ||
expect(file.branch_name).to eq "branch" | ||
end | ||
end | ||
end |