Skip to content

Commit 8234385

Browse files
authored
Merge pull request #241 from fbourigault/repository-files
Add RepositoryFiles api endpoint
2 parents 84fbf5d + 032b7cc commit 8234385

File tree

6 files changed

+487
-245
lines changed

6 files changed

+487
-245
lines changed

lib/Gitlab/Api/Repositories.php

+18-12
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,9 @@ public function tree($project_id, array $params = array())
309309
*/
310310
public function blob($project_id, $sha, $filepath)
311311
{
312-
return $this->get($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($filepath).'/raw'), array(
313-
'ref' => $sha,
314-
));
312+
@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);
313+
314+
return $this->client->repositoryFiles()->getRawFile($project_id, $filepath, $sha);
315315
}
316316

317317
/**
@@ -322,9 +322,9 @@ public function blob($project_id, $sha, $filepath)
322322
*/
323323
public function getFile($project_id, $file_path, $ref)
324324
{
325-
return $this->get($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($file_path)), array(
326-
'ref' => $ref
327-
));
325+
@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);
326+
327+
return $this->client->repositoryFiles()->getFile($project_id, $file_path, $ref);
328328
}
329329

330330
/**
@@ -340,15 +340,17 @@ public function getFile($project_id, $file_path, $ref)
340340
*/
341341
public function createFile($project_id, $file_path, $content, $branch, $commit_message, $encoding = null, $author_email = null, $author_name = null)
342342
{
343-
return $this->post($this->getProjectPath($project_id, 'repository/files'), array(
343+
@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);
344+
345+
return $this->client->repositoryFiles()->createFile($project_id, [
344346
'file_path' => $file_path,
345347
'branch' => $branch,
346348
'content' => $content,
347349
'commit_message' => $commit_message,
348350
'encoding' => $encoding,
349351
'author_email' => $author_email,
350352
'author_name' => $author_name,
351-
));
353+
]);
352354
}
353355

354356
/**
@@ -364,15 +366,17 @@ public function createFile($project_id, $file_path, $content, $branch, $commit_m
364366
*/
365367
public function updateFile($project_id, $file_path, $content, $branch, $commit_message, $encoding = null, $author_email = null, $author_name = null)
366368
{
367-
return $this->put($this->getProjectPath($project_id, 'repository/files'), array(
369+
@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);
370+
371+
return $this->client->repositoryFiles()->updateFile($project_id, [
368372
'file_path' => $file_path,
369373
'branch' => $branch,
370374
'content' => $content,
371375
'commit_message' => $commit_message,
372376
'encoding' => $encoding,
373377
'author_email' => $author_email,
374378
'author_name' => $author_name,
375-
));
379+
]);
376380
}
377381

378382
/**
@@ -386,13 +390,15 @@ public function updateFile($project_id, $file_path, $content, $branch, $commit_m
386390
*/
387391
public function deleteFile($project_id, $file_path, $branch, $commit_message, $author_email = null, $author_name = null)
388392
{
389-
return $this->delete($this->getProjectPath($project_id, 'repository/files'), array(
393+
@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);
394+
395+
return $this->client->repositoryFiles()->deleteFile($project_id, [
390396
'file_path' => $file_path,
391397
'branch' => $branch,
392398
'commit_message' => $commit_message,
393399
'author_email' => $author_email,
394400
'author_name' => $author_name,
395-
));
401+
]);
396402
}
397403

398404
/**

lib/Gitlab/Api/RepositoryFiles.php

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace Gitlab\Api;
4+
5+
use Symfony\Component\OptionsResolver\OptionsResolver;
6+
7+
class RepositoryFiles extends AbstractApi
8+
{
9+
/**
10+
* @param int $project_id
11+
* @param string $file_path
12+
* @param string $ref
13+
* @return mixed
14+
*/
15+
public function getFile($project_id, $file_path, $ref)
16+
{
17+
return $this->get($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($file_path)), array(
18+
'ref' => $ref
19+
));
20+
}
21+
22+
/**
23+
* @param int $project_id
24+
* @param string $file_path
25+
* @param string $ref
26+
* @return mixed
27+
*/
28+
public function getRawFile($project_id, $file_path, $ref)
29+
{
30+
return $this->get($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($file_path).'/raw'), array(
31+
'ref' => $ref,
32+
));
33+
}
34+
35+
/**
36+
* @param int $project_id
37+
* @param array $parameters (
38+
*
39+
* @var string $file_path Url encoded full path to new file. Ex. lib%2Fclass%2Erb.
40+
* @var string $branch Name of the branch.
41+
* @var string $start_branch Name of the branch to start the new commit from.
42+
* @var string $encoding Change encoding to 'base64'. Default is text.
43+
* @var string $author_email Specify the commit author's email address.
44+
* @var string $author_name Specify the commit author's name.
45+
* @var string $content File content.
46+
* @var string $commit_message Commit message.
47+
* )
48+
*
49+
* @return mixed
50+
*/
51+
public function createFile($project_id, array $parameters = [])
52+
{
53+
$resolver = new OptionsResolver();
54+
$resolver->setRequired('file_path');
55+
$resolver->setRequired('branch');
56+
$resolver->setDefined('start_branch');
57+
$resolver->setDefined('encoding')
58+
->setAllowedValues('encoding', ['text', 'base64'])
59+
;
60+
$resolver->setDefined('author_email');
61+
$resolver->setDefined('author_name');
62+
$resolver->setRequired('content');
63+
$resolver->setRequired('commit_message');
64+
65+
$resolved = $resolver->resolve($parameters);
66+
67+
return $this->post($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($resolved['file_path'])), $resolved);
68+
}
69+
70+
/**
71+
* @param int $project_id
72+
* @param array $parameters (
73+
*
74+
* @var string $file_path Url encoded full path to new file. Ex. lib%2Fclass%2Erb.
75+
* @var string $branch Name of the branch.
76+
* @var string $start_branch Name of the branch to start the new commit from.
77+
* @var string $encoding Change encoding to 'base64'. Default is text.
78+
* @var string $author_email Specify the commit author's email address.
79+
* @var string $author_name Specify the commit author's name.
80+
* @var string $content File content.
81+
* @var string $commit_message Commit message.
82+
* @var string $last_commit_id Last known file commit id.
83+
* )
84+
*
85+
* @return mixed
86+
*/
87+
public function updateFile($project_id, array $parameters = [])
88+
{
89+
$resolver = new OptionsResolver();
90+
$resolver->setRequired('file_path');
91+
$resolver->setRequired('branch');
92+
$resolver->setDefined('start_branch');
93+
$resolver->setDefined('encoding')
94+
->setAllowedValues('encoding', ['text', 'base64'])
95+
;
96+
$resolver->setDefined('author_email');
97+
$resolver->setDefined('author_name');
98+
$resolver->setRequired('content');
99+
$resolver->setRequired('commit_message');
100+
$resolver->setDefined('last_commit_id');
101+
102+
$resolved = $resolver->resolve($parameters);
103+
104+
return $this->put($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($resolved['file_path'])), $resolved);
105+
}
106+
107+
/**
108+
* @param int $project_id
109+
* @param array $parameters (
110+
*
111+
* @var string $file_path Url encoded full path to new file. Ex. lib%2Fclass%2Erb.
112+
* @var string $branch Name of the branch.
113+
* @var string $start_branch Name of the branch to start the new commit from.
114+
* @var string $author_email Specify the commit author's email address.
115+
* @var string $author_name Specify the commit author's name.
116+
* @var string $commit_message Commit message.
117+
* )
118+
*
119+
* @return mixed
120+
*/
121+
public function deleteFile($project_id, array $parameters = [])
122+
{
123+
$resolver = new OptionsResolver();
124+
$resolver->setRequired('file_path');
125+
$resolver->setRequired('branch');
126+
$resolver->setDefined('start_branch');
127+
$resolver->setDefined('author_email');
128+
$resolver->setDefined('author_name');
129+
$resolver->setRequired('commit_message');
130+
131+
$resolved = $resolver->resolve($parameters);
132+
133+
return $this->delete($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($resolved['file_path'])), $resolved);
134+
}
135+
}

lib/Gitlab/Client.php

+8
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ public function repositories()
197197
return new Api\Repositories($this);
198198
}
199199

200+
/**
201+
* @return Api\RepositoryFiles
202+
*/
203+
public function repositoryFiles()
204+
{
205+
return new Api\RepositoryFiles($this);
206+
}
207+
200208
/**
201209
* @return Api\Snippets
202210
*/

lib/Gitlab/Model/Project.php

+47-3
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,22 @@ public function getFile($sha, $filepath)
582582
*/
583583
public function createFile($file_path, $content, $branch_name, $commit_message, $author_email = null, $author_name = null)
584584
{
585-
$data = $this->client->repositories()->createFile($this->id, $file_path, $content, $branch_name, $commit_message, null, $author_email, $author_name);
585+
$parameters = [
586+
'file_path' => $file_path,
587+
'branch' => $branch_name,
588+
'content' => $content,
589+
'commit_message' => $commit_message,
590+
];
591+
592+
if ($author_email !== null) {
593+
$parameters['author_email'] = $author_email;
594+
}
595+
596+
if ($author_name !== null) {
597+
$parameters['author_name'] = $author_name;
598+
}
599+
600+
$data = $this->client->repositoryFiles()->createFile($this->id, $parameters);
586601

587602
return File::fromArray($this->getClient(), $this, $data);
588603
}
@@ -598,7 +613,22 @@ public function createFile($file_path, $content, $branch_name, $commit_message,
598613
*/
599614
public function updateFile($file_path, $content, $branch_name, $commit_message, $author_email = null, $author_name = null)
600615
{
601-
$data = $this->client->repositories()->updateFile($this->id, $file_path, $content, $branch_name, $commit_message, null, $author_email, $author_name);
616+
$parameters = [
617+
'file_path' => $file_path,
618+
'branch' => $branch_name,
619+
'content' => $content,
620+
'commit_message' => $commit_message,
621+
];
622+
623+
if ($author_email !== null) {
624+
$parameters['author_email'] = $author_email;
625+
}
626+
627+
if ($author_name !== null) {
628+
$parameters['author_name'] = $author_name;
629+
}
630+
631+
$data = $this->client->repositoryFiles()->updateFile($this->id, $parameters);
602632

603633
return File::fromArray($this->getClient(), $this, $data);
604634
}
@@ -613,7 +643,21 @@ public function updateFile($file_path, $content, $branch_name, $commit_message,
613643
*/
614644
public function deleteFile($file_path, $branch_name, $commit_message, $author_email = null, $author_name = null)
615645
{
616-
$this->client->repositories()->deleteFile($this->id, $file_path, $branch_name, $commit_message, $author_email, $author_name);
646+
$parameters = [
647+
'file_path' => $file_path,
648+
'branch' => $branch_name,
649+
'commit_message' => $commit_message,
650+
];
651+
652+
if ($author_email !== null) {
653+
$parameters['author_email'] = $author_email;
654+
}
655+
656+
if ($author_name !== null) {
657+
$parameters['author_name'] = $author_name;
658+
}
659+
660+
$this->client->repositoryFiles()->deleteFile($this->id, $parameters);
617661

618662
return true;
619663
}

0 commit comments

Comments
 (0)