Skip to content
This repository has been archived by the owner on Sep 27, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2 from fgrosse/feature/labels-api
Browse files Browse the repository at this point in the history
Feature/labels api
  • Loading branch information
fgrosse committed Sep 26, 2015
2 parents 31cf9b1 + a9e3bcd commit ac2db1b
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 33 deletions.
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ disabled:
- unalign_equals
- unalign_double_arrow
- phpdoc_separation
- phpdoc_short_description
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ You can have a quick overview of the implemented gitlab APIs in the yml files in
* [Commits](lib/Client/ServiceDescription/commits_api.yml)
* [Issues](lib/Client/ServiceDescription/issues_api.yml)
* [Merge Requests](lib/Client/ServiceDescription/merge_requests_api.yml)
* [Labels](lib/Client/ServiceDescription/labels_api.yml)

## Dependencies

Expand Down Expand Up @@ -61,7 +62,6 @@ parameters for each API call.
* deploy_key_multiple_projects API
* deploy_keys API
* groups API
* labels API
* milestones API
* notes API
* oauth2 API
Expand Down
64 changes: 32 additions & 32 deletions lib/Client/GitlabClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use GuzzleHttp\Command\Command;
use GuzzleHttp\Command\Guzzle\DescriptionInterface;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Message\ResponseInterface;

/**
Expand All @@ -24,34 +23,43 @@
* The following magic methods are available through the service definition:
*
* Merge Requests API:
* @see https://github.com/gitlabhq/gitlabhq/blob/master/doc/api/merge_requests.md
* @see https://github.com/gitlabhq/gitlabhq/blob/v7.7.0/doc/api/merge_requests.md
* @method array listMergeRequests($parameters)
* @method array getMergeRequest($parameters)
* @method array getMergeRequestChanges($parameters)
* @method array createMergeRequest($parameters)
* @method array updateMergeRequest($parameters)
* @method array createMergeRequestComment($parameters)
* @method array listMergeRequestComments($parameters)
* @method array acceptMergeRequest($parameters)
*
* Commits API
* @see https://github.com/gitlabhq/gitlabhq/blob/master/doc/api/commits.md
* @see https://github.com/gitlabhq/gitlabhq/blob/v7.7.0/doc/api/commits.md
* @method array listCommits($parameters)
* @method array getCommit($parameters)
* @method array getCommitDiff($parameters)
* @method array getCommitComments($parameters)
* @method array createCommitComment($parameters)
*
* Issues API
* @see https://github.com/gitlabhq/gitlabhq/blob/master/doc/api/issues.md
* @see https://github.com/gitlabhq/gitlabhq/blob/v7.7.0/doc/api/issues.md
* @method array listIssues($parameters)
* @method array listProjectIssues($parameters)
* @method array getIssue($parameters)
* @method array createIssue($parameters)
* @method array updateIssue($parameters)
*
* Labels API
* @see https://github.com/gitlabhq/gitlabhq/blob/v7.7.0/doc/api/labels.md
* @method array listLabels($parameters)
* @method array createLabel($parameters)
* @method array updateLabel($parameters)
* @method array deleteLabel($parameters)
*
* TODO: branches API
* TODO: deploy_key_multiple_projects API
* TODO: deploy_keys API
* TODO: groups API
* TODO: labels API
* TODO: milestones API
* TODO: notes API
* TODO: oauth2 API
Expand All @@ -76,6 +84,19 @@ public static function factory(array $config)
return GuzzleClientFactory::createClient($config);
}

/**
* A list of operations that are executed with a body.
* TODO: this is a hack until https://github.com/guzzle/guzzle-services supports request bodies on PUT or DELETE
* @var string[]
*/
private $executeWithBody = [
'updateMergeRequest',
'acceptMergeRequest',
'updateIssue',
'updateLabel',
'deleteLabel',
];

/**
* @param ClientInterface $client
* @param DescriptionInterface $description
Expand All @@ -87,38 +108,17 @@ public function __construct(ClientInterface $client, DescriptionInterface $descr
parent::__construct($client, $description, $config);
}

/**
* @param array $parameters
* @return ResponseInterface
* @throws RequestException When an error is encountered
*/
public function updateMergeRequest(array $parameters)
public function __call($name, array $arguments)
{
return $this->executeRequestWithBody('updateMergeRequest', $parameters);
}
if (in_array($name, $this->executeWithBody)) {
return $this->executeRequestWithBody($name, $arguments[0]);
}

/**
* @param array $parameters
* @return ResponseInterface
* @throws RequestException When an error is encountered
*/
public function acceptMergeRequest(array $parameters)
{
return $this->executeRequestWithBody('acceptMergeRequest', $parameters);
}

/**
* @param array $parameters
* @return ResponseInterface
* @throws RequestException When an error is encountered
*/
public function updateIssue(array $parameters)
{
return $this->executeRequestWithBody('updateIssue', $parameters);
return parent::__call($name, $arguments);
}

/**
* This is a hack to allow guzzle PUT requests to have a body
* This is a hack to allow guzzle PUT or DELETE requests to have a body.
* TODO submit github issue for that.
* @param $commandName
* @param array $parameters
Expand Down
72 changes: 72 additions & 0 deletions lib/Client/ServiceDescription/labels_api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
##################################################################################
# Gitlab Labels API #
# See https://github.com/gitlabhq/gitlabhq/blob/v7.7.0/doc/api/labels.md #
##################################################################################

operations:
listLabels:
summary: Get all labels for a given project.
httpMethod: GET
uri: projects/{project_id}/labels
parameters:
project_id:
required: true
description: The ID of a project
type: string
location: uri

createLabel:
summary: Creates a new label for given repository with given name and color.
httpMethod: POST
uri: projects/{project_id}/labels
parameters:
project_id:
required: true
description: The ID of a project
type: string
location: uri
name:
required: true
description: The name of the label
type: string
location: postField
color:
required: true
description: Color of the label given in 6-digit hex notation with leading '#' sign (e.g. #FFAABB)
type: string
location: postField

updateLabel:
extends: createLabel
summary: >
Updates an existing label with new name or now color.
Either the new name or new color is required to update the label.
httpMethod: PUT
uri: projects/{project_id}/labels
parameters:
new_name:
required: false
description: The new name of the label
type: string
location: postField
color:
required: false
description: New color of the label given in 6-digit hex notation with leading '#' sign (e.g. #FFAABB)
type: string
location: postField

deleteLabel:
summary: Deletes a label given by its name.
httpMethod: DELETE
uri: projects/{project_id}/labels
parameters:
project_id:
required: true
description: The ID of a project
type: string
location: uri
name:
required: true
description: The name of the label
type: string
location: postField
1 change: 1 addition & 0 deletions lib/Client/ServiceDescription/service_description.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ imports:
- commits_api.yml
- issues_api.yml
- merge_requests_api.yml
- labels_api.yml
81 changes: 81 additions & 0 deletions tests/Client/LabelsAPITest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/*
* This file is part of fgrosse/gitlab-api.
*
* Copyright © Friedrich Große <friedrich.grosse@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gitlab\Test\Client;

class LabelsAPITest extends GitlabClientTest
{
public function testListLabels()
{
$this->setMockResponse(__DIR__.'/fixtures/labels/list_labels.http');
$projectId = 'fgrosse/example-project';
$this->client->listLabels([
'project_id' => $projectId,
]);

$request = $this->requestHistory->getLastRequest();
$this->assertEquals('GET', $request->getMethod());
$this->assertEquals('/gitlab/api/v3/projects/'.urlencode($projectId).'/labels', $request->getPath());
$this->assertEquals('application/json', $request->getHeader('Accept'));
}

public function testCreateNewLabel()
{
$this->setMockResponse(__DIR__.'/fixtures/labels/create_update_or_delete_label.http');
$projectId = 'fgrosse/example-project';
$this->client->createLabel([
'project_id' => $projectId,
'name' => 'pink',
'color' => '#FFAABB',
]);

$request = $this->requestHistory->getLastRequest();
$this->assertEquals('POST', $request->getMethod());
$this->assertEquals('/gitlab/api/v3/projects/'.urlencode($projectId).'/labels', $request->getPath());
$this->assertEquals('application/json', $request->getHeader('Accept'));
$this->assertRequestHasPostParameter('name', 'pink', $request);
$this->assertRequestHasPostParameter('color', '#FFAABB', $request);
}

public function testUpdateLabel()
{
$this->setMockResponse(__DIR__.'/fixtures/labels/create_update_or_delete_label.http');
$projectId = 'fgrosse/example-project';
$this->client->updateLabel([
'project_id' => $projectId,
'name' => 'pink',
'new_name' => 'awesome pink',
'color' => '#FFAABB',
]);

$request = $this->requestHistory->getLastRequest();
$this->assertEquals('PUT', $request->getMethod());
$this->assertEquals('/gitlab/api/v3/projects/'.urlencode($projectId).'/labels', $request->getPath());
$this->assertEquals('application/json', $request->getHeader('Accept'));
$this->assertRequestHasPostParameter('name', 'pink', $request);
$this->assertRequestHasPostParameter('new_name', 'awesome pink', $request);
$this->assertRequestHasPostParameter('color', '#FFAABB', $request);
}

public function testDeleteLabel()
{
$this->setMockResponse(__DIR__.'/fixtures/labels/create_update_or_delete_label.http');
$projectId = 'fgrosse/example-project';
$this->client->deleteLabel([
'project_id' => $projectId,
'name' => 'pink',
]);

$request = $this->requestHistory->getLastRequest();
$this->assertEquals('DELETE', $request->getMethod());
$this->assertEquals('/gitlab/api/v3/projects/'.urlencode($projectId).'/labels', $request->getPath());
$this->assertRequestHasPostParameter('name', 'pink', $request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HTTP/1.1 200 OK
Content-Type: application/json

{
"name": "Awesome",
"color": "#DD10AA"
}
21 changes: 21 additions & 0 deletions tests/Client/fixtures/labels/list_labels.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
HTTP/1.1 200 OK
Content-Type: application/json

[
{
"name": "Awesome",
"color": "#DD10AA"
},
{
"name": "Documentation",
"color": "#1E80DD"
},
{
"name": "Feature",
"color": "#11FF22"
},
{
"name": "Bug",
"color": "#EE1122"
}
]

0 comments on commit ac2db1b

Please sign in to comment.