Skip to content

Add RepositoryFiles api endpoint #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions lib/Gitlab/Api/Repositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ public function tree($project_id, array $params = array())
*/
public function blob($project_id, $sha, $filepath)
{
return $this->get($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($filepath).'/raw'), array(
'ref' => $sha,
));
@trigger_error(sprintf('The %s() method is deprecated since version 9.2 and will be removed in 10.0. Use the %s::getRawFile() method instead.', __METHOD__, RepositoryFiles::class), E_USER_DEPRECATED);

return $this->client->repositoryFiles()->getRawFile($project_id, $filepath, $sha);
}

/**
Expand All @@ -322,9 +322,9 @@ public function blob($project_id, $sha, $filepath)
*/
public function getFile($project_id, $file_path, $ref)
{
return $this->get($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($file_path)), array(
'ref' => $ref
));
@trigger_error(sprintf('The %s() method is deprecated since version 9.2 and will be removed in 10.0. Use the %s::getFile() method instead.', __METHOD__, RepositoryFiles::class), E_USER_DEPRECATED);

return $this->client->repositoryFiles()->getFile($project_id, $file_path, $ref);
}

/**
Expand All @@ -340,15 +340,17 @@ public function getFile($project_id, $file_path, $ref)
*/
public function createFile($project_id, $file_path, $content, $branch, $commit_message, $encoding = null, $author_email = null, $author_name = null)
{
return $this->post($this->getProjectPath($project_id, 'repository/files'), array(
@trigger_error(sprintf('The %s() method is deprecated since version 9.2 and will be removed in 10.0. Use the %s::createFile() method instead.', __METHOD__, RepositoryFiles::class), E_USER_DEPRECATED);

return $this->client->repositoryFiles()->createFile($project_id, [
'file_path' => $file_path,
'branch' => $branch,
'content' => $content,
'commit_message' => $commit_message,
'encoding' => $encoding,
'author_email' => $author_email,
'author_name' => $author_name,
));
]);
}

/**
Expand All @@ -364,15 +366,17 @@ public function createFile($project_id, $file_path, $content, $branch, $commit_m
*/
public function updateFile($project_id, $file_path, $content, $branch, $commit_message, $encoding = null, $author_email = null, $author_name = null)
{
return $this->put($this->getProjectPath($project_id, 'repository/files'), array(
@trigger_error(sprintf('The %s() method is deprecated since version 9.2 and will be removed in 10.0. Use the %s::updateFile() method instead.', __METHOD__, RepositoryFiles::class), E_USER_DEPRECATED);

return $this->client->repositoryFiles()->updateFile($project_id, [
'file_path' => $file_path,
'branch' => $branch,
'content' => $content,
'commit_message' => $commit_message,
'encoding' => $encoding,
'author_email' => $author_email,
'author_name' => $author_name,
));
]);
}

/**
Expand All @@ -386,13 +390,15 @@ public function updateFile($project_id, $file_path, $content, $branch, $commit_m
*/
public function deleteFile($project_id, $file_path, $branch, $commit_message, $author_email = null, $author_name = null)
{
return $this->delete($this->getProjectPath($project_id, 'repository/files'), array(
@trigger_error(sprintf('The %s() method is deprecated since version 9.2 and will be removed in 10.0. Use the %s::deleteFile() method instead.', __METHOD__, RepositoryFiles::class), E_USER_DEPRECATED);

return $this->client->repositoryFiles()->deleteFile($project_id, [
'file_path' => $file_path,
'branch' => $branch,
'commit_message' => $commit_message,
'author_email' => $author_email,
'author_name' => $author_name,
));
]);
}

/**
Expand Down
135 changes: 135 additions & 0 deletions lib/Gitlab/Api/RepositoryFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Gitlab\Api;

use Symfony\Component\OptionsResolver\OptionsResolver;

class RepositoryFiles extends AbstractApi
{
/**
* @param int $project_id
* @param string $file_path
* @param string $ref
* @return mixed
*/
public function getFile($project_id, $file_path, $ref)
{
return $this->get($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($file_path)), array(
'ref' => $ref
));
}

/**
* @param int $project_id
* @param string $file_path
* @param string $ref
* @return mixed
*/
public function getRawFile($project_id, $file_path, $ref)
{
return $this->get($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($file_path).'/raw'), array(
'ref' => $ref,
));
}

/**
* @param int $project_id
* @param array $parameters (
*
* @var string $file_path Url encoded full path to new file. Ex. lib%2Fclass%2Erb.
* @var string $branch Name of the branch.
* @var string $start_branch Name of the branch to start the new commit from.
* @var string $encoding Change encoding to 'base64'. Default is text.
* @var string $author_email Specify the commit author's email address.
* @var string $author_name Specify the commit author's name.
* @var string $content File content.
* @var string $commit_message Commit message.
* )
*
* @return mixed
*/
public function createFile($project_id, array $parameters = [])
{
$resolver = new OptionsResolver();
$resolver->setRequired('file_path');
$resolver->setRequired('branch');
$resolver->setDefined('start_branch');
$resolver->setDefined('encoding')
->setAllowedValues('encoding', ['text', 'base64'])
;
$resolver->setDefined('author_email');
$resolver->setDefined('author_name');
$resolver->setRequired('content');
$resolver->setRequired('commit_message');

$resolved = $resolver->resolve($parameters);

return $this->post($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($resolved['file_path'])), $resolved);
}

/**
* @param int $project_id
* @param array $parameters (
*
* @var string $file_path Url encoded full path to new file. Ex. lib%2Fclass%2Erb.
* @var string $branch Name of the branch.
* @var string $start_branch Name of the branch to start the new commit from.
* @var string $encoding Change encoding to 'base64'. Default is text.
* @var string $author_email Specify the commit author's email address.
* @var string $author_name Specify the commit author's name.
* @var string $content File content.
* @var string $commit_message Commit message.
* @var string $last_commit_id Last known file commit id.
* )
*
* @return mixed
*/
public function updateFile($project_id, array $parameters = [])
{
$resolver = new OptionsResolver();
$resolver->setRequired('file_path');
$resolver->setRequired('branch');
$resolver->setDefined('start_branch');
$resolver->setDefined('encoding')
->setAllowedValues('encoding', ['text', 'base64'])
;
$resolver->setDefined('author_email');
$resolver->setDefined('author_name');
$resolver->setRequired('content');
$resolver->setRequired('commit_message');
$resolver->setDefined('last_commit_id');

$resolved = $resolver->resolve($parameters);

return $this->put($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($resolved['file_path'])), $resolved);
}

/**
* @param int $project_id
* @param array $parameters (
*
* @var string $file_path Url encoded full path to new file. Ex. lib%2Fclass%2Erb.
* @var string $branch Name of the branch.
* @var string $start_branch Name of the branch to start the new commit from.
* @var string $author_email Specify the commit author's email address.
* @var string $author_name Specify the commit author's name.
* @var string $commit_message Commit message.
* )
*
* @return mixed
*/
public function deleteFile($project_id, array $parameters = [])
{
$resolver = new OptionsResolver();
$resolver->setRequired('file_path');
$resolver->setRequired('branch');
$resolver->setDefined('start_branch');
$resolver->setDefined('author_email');
$resolver->setDefined('author_name');
$resolver->setRequired('commit_message');

$resolved = $resolver->resolve($parameters);

return $this->delete($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($resolved['file_path'])), $resolved);
}
}
8 changes: 8 additions & 0 deletions lib/Gitlab/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ public function repositories()
return new Api\Repositories($this);
}

/**
* @return Api\RepositoryFiles
*/
public function repositoryFiles()
{
return new Api\RepositoryFiles($this);
}

/**
* @return Api\Snippets
*/
Expand Down
50 changes: 47 additions & 3 deletions lib/Gitlab/Model/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,22 @@ public function getFile($sha, $filepath)
*/
public function createFile($file_path, $content, $branch_name, $commit_message, $author_email = null, $author_name = null)
{
$data = $this->client->repositories()->createFile($this->id, $file_path, $content, $branch_name, $commit_message, null, $author_email, $author_name);
$parameters = [
'file_path' => $file_path,
'branch' => $branch_name,
'content' => $content,
'commit_message' => $commit_message,
];

if ($author_email !== null) {
$parameters['author_email'] = $author_email;
}

if ($author_name !== null) {
$parameters['author_name'] = $author_name;
}

$data = $this->client->repositoryFiles()->createFile($this->id, $parameters);

return File::fromArray($this->getClient(), $this, $data);
}
Expand All @@ -598,7 +613,22 @@ public function createFile($file_path, $content, $branch_name, $commit_message,
*/
public function updateFile($file_path, $content, $branch_name, $commit_message, $author_email = null, $author_name = null)
{
$data = $this->client->repositories()->updateFile($this->id, $file_path, $content, $branch_name, $commit_message, null, $author_email, $author_name);
$parameters = [
'file_path' => $file_path,
'branch' => $branch_name,
'content' => $content,
'commit_message' => $commit_message,
];

if ($author_email !== null) {
$parameters['author_email'] = $author_email;
}

if ($author_name !== null) {
$parameters['author_name'] = $author_name;
}

$data = $this->client->repositoryFiles()->updateFile($this->id, $parameters);

return File::fromArray($this->getClient(), $this, $data);
}
Expand All @@ -613,7 +643,21 @@ public function updateFile($file_path, $content, $branch_name, $commit_message,
*/
public function deleteFile($file_path, $branch_name, $commit_message, $author_email = null, $author_name = null)
{
$this->client->repositories()->deleteFile($this->id, $file_path, $branch_name, $commit_message, $author_email, $author_name);
$parameters = [
'file_path' => $file_path,
'branch' => $branch_name,
'commit_message' => $commit_message,
];

if ($author_email !== null) {
$parameters['author_email'] = $author_email;
}

if ($author_name !== null) {
$parameters['author_name'] = $author_name;
}

$this->client->repositoryFiles()->deleteFile($this->id, $parameters);

return true;
}
Expand Down
Loading