|
1 | 1 | <?php namespace Gitlab\Api;
|
2 | 2 |
|
| 3 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 4 | + |
3 | 5 | class Repositories extends AbstractApi
|
4 | 6 | {
|
5 | 7 | /**
|
@@ -168,6 +170,69 @@ public function commit($project_id, $sha)
|
168 | 170 | return $this->get($this->getProjectPath($project_id, 'repository/commits/'.$this->encodePath($sha)));
|
169 | 171 | }
|
170 | 172 |
|
| 173 | + /** |
| 174 | + * @param int $project_id |
| 175 | + * @param array $parameters ( |
| 176 | + * |
| 177 | + * @var string $branch Name of the branch to commit into. To create a new branch, also provide start_branch. |
| 178 | + * @var string $commit_message Commit message. |
| 179 | + * @var string $start_branch Name of the branch to start the new commit from. |
| 180 | + * @var array $actions ( |
| 181 | + * |
| 182 | + * @var string $action he action to perform, create, delete, move, update. |
| 183 | + * @var string $file_path Full path to the file. |
| 184 | + * @var string $previous_path Original full path to the file being moved. |
| 185 | + * @var string $content File content, required for all except delete. Optional for move. |
| 186 | + * @var string $encoding text or base64. text is default. |
| 187 | + * ) |
| 188 | + * @var string $author_email Specify the commit author's email address. |
| 189 | + * @var string $author_name Specify the commit author's name. |
| 190 | + * ) |
| 191 | + * |
| 192 | + * @return mixed |
| 193 | + */ |
| 194 | + public function createCommit($project_id, array $parameters = []) |
| 195 | + { |
| 196 | + $resolver = new OptionsResolver(); |
| 197 | + $resolver->setDefined('branch') |
| 198 | + ->setRequired('branch') |
| 199 | + ; |
| 200 | + $resolver->setDefined('commit_message') |
| 201 | + ->setRequired('commit_message') |
| 202 | + ; |
| 203 | + $resolver->setDefined('start_branch'); |
| 204 | + $resolver->setDefined('actions') |
| 205 | + ->setRequired('actions') |
| 206 | + ->setAllowedTypes('actions', 'array') |
| 207 | + ->setAllowedValues('actions', function (array $actions) { |
| 208 | + return !empty($actions); |
| 209 | + }) |
| 210 | + ->setNormalizer('actions', function (OptionsResolver $resolver, array $actions) { |
| 211 | + $actionsOptionsResolver = new OptionsResolver(); |
| 212 | + $actionsOptionsResolver->setDefined('action') |
| 213 | + ->setRequired('action') |
| 214 | + ->setAllowedValues('action', ['create', 'delete', 'move', 'update']) |
| 215 | + ; |
| 216 | + $actionsOptionsResolver->setDefined('file_path') |
| 217 | + ->setRequired('file_path') |
| 218 | + ; |
| 219 | + $actionsOptionsResolver->setDefined('previous_path'); |
| 220 | + $actionsOptionsResolver->setDefined('content'); |
| 221 | + $actionsOptionsResolver->setDefined('encoding') |
| 222 | + ->setAllowedValues('encoding', ['test', 'base64']) |
| 223 | + ; |
| 224 | + |
| 225 | + return array_map(function ($action) use ($actionsOptionsResolver) { |
| 226 | + return $actionsOptionsResolver->resolve($action); |
| 227 | + }, $actions); |
| 228 | + }) |
| 229 | + ; |
| 230 | + $resolver->setDefined('author_email'); |
| 231 | + $resolver->setDefined('author_name'); |
| 232 | + |
| 233 | + return $this->post($this->getProjectPath($project_id, 'repository/commits'), $resolver->resolve($parameters)); |
| 234 | + } |
| 235 | + |
171 | 236 | /**
|
172 | 237 | * @param int $project_id
|
173 | 238 | * @param string $sha
|
|
0 commit comments