cocciagialla/bitbucket-api-bundle
Install the Bundle
Add to composer.json:
composer require cocciagialla/bitbucket-api-bundle
Enable in config/bundles.php:
Cocciagialla\BitbucketApiBundle\BitbucketApiBundle::class => ['all' => true],
Configure OAuth2 Credentials
Add to config/packages/bitbucket_api.yaml:
bitbucket_api:
client_id: '%env(BITBUCKET_CLIENT_ID)%'
client_secret: '%env(BITBUCKET_CLIENT_SECRET)%'
Store secrets in .env:
BITBUCKET_CLIENT_ID=your_client_id
BITBUCKET_CLIENT_SECRET=your_secret
First Use Case: Fetch Repository Data Inject the API client and call a method (e.g., list repositories):
use Bitbucket\API\Api;
public function listRepos(Api $bitbucketApi) {
$repos = $bitbucketApi->repository()->get();
return $this->json($repos);
}
Authentication & Token Management
CacheInterface) for reuse:
$token = $bitbucketApi->getAccessToken(); // Auto-refreshes if expired
Repository Operations
$repos = $bitbucketApi->repository()->get(['pagelen' => 10]);
$repo = $bitbucketApi->repository()->get('owner/repo-slug');
Pull Request Workflows
$prs = $bitbucketApi->pullrequest()->get('owner/repo-slug');
$newPr = $bitbucketApi->pullrequest()->create(
'owner/repo-slug',
['title' => 'Fix bug', 'source' => 'branch-name']
);
Webhooks & Events
webhook service to verify and process incoming events:
$payload = $request->getContent();
$signature = $request->headers->get('X-Hub-Signature-256');
$verified = $bitbucketApi->webhook()->verify($payload, $signature);
bitbucket.api.event) after API calls for logging/auditing.Api $bitbucketApi) over manual container access.Api to handle rate limits (Bitbucket’s default is 100 requests/10 minutes).Token Expiry Handling
client_id/secret are correct. Test with:
try {
$token = $bitbucketApi->getAccessToken();
} catch (\Bitbucket\API\Exception\RuntimeException $e) {
// Log or rethrow for OAuth issues
}
Pagination Quirks
/repositories) require manual iteration:
$page = 1;
do {
$repos = $bitbucketApi->repository()->get(['page' => $page, 'pagelen' => 50]);
$page++;
} while (!empty($repos));
Webhook Verification
if (!$bitbucketApi->webhook()->verify($payload, $signature)) {
throw new \RuntimeException('Invalid webhook signature');
}
Deprecated Methods
bitbucket-api library may change. Check its docs for breaking changes.# config/packages/bitbucket_api.yaml
bitbucket_api:
debug: true # Logs raw API responses
HttpClient mock or replace the Api service in tests:
$container->set('Bitbucket\API\Api', $this->createMock(Api::class));
Custom API Clients
Api service by creating a decorator:
class CustomBitbucketApiDecorator implements ApiInterface {
private $decorated;
public function __construct(Api $decorated) {
$this->decorated = $decorated;
}
public function repository() {
// Add custom logic (e.g., logging)
return $this->decorated->repository();
}
}
services.yaml:
services:
App\Service\CustomBitbucketApiDecorator:
decorates: 'Bitbucket\API\Api'
arguments: ['@App\Service\CustomBitbucketApiDecorator.inner']
Event Listeners
// src/EventListener/BitbucketListener.php
public static function getSubscribedEvents() {
return [
'bitbucket.api.event' => 'onBitbucketEvent',
];
}
How can I help you explore Laravel packages today?