Skip to content

Commit

Permalink
Covert to helper library
Browse files Browse the repository at this point in the history
  • Loading branch information
JaZo committed Dec 21, 2023
1 parent f6c28b1 commit ce512b5
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 137 deletions.
13 changes: 1 addition & 12 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
# Changelog

All notable changes to `swisnl/phpstan-bitbucket` will be documented in this file.
All notable changes to `swisnl/bitbucket-reports` will be documented in this file.

Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) principles.

## [Unreleased]

- Nothing

## [0.2.0] - 2022-11-04

This release lists changes compared to [alxt/phpstan-bitbucket:0.1.0](https://github.com/modprobe/phpstan-bitbucket/releases/tag/v0.1.0).

### Changed

- Bumped minimum PHP version to 7.4 and add support for PHP 8.
- Bumped dependencies.
- This error formatter will now also report errors using the default table error formatter.
- Changed package name to `swisnl/phpstan-bitbucket` and namespace to `Swis\PHPStan\ErrorFormatter`.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Contributions are **welcome** and will be fully **credited**.

We accept contributions via Pull Requests on [GitHub](https://github.com/swisnl/phpstan-bitbucket).
We accept contributions via Pull Requests on [GitHub](https://github.com/swisnl/bitbucket-reports).


## Pull Requests
Expand Down
45 changes: 12 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,24 @@
# PHPStan Bitbucket error formatter
# Bitbucket reports

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Buy us a tree][ico-treeware]][link-treeware]
[![Total Downloads][ico-downloads]][link-downloads]
[![Maintained by SWIS][ico-swis]][link-swis]

This PHPStan error formatter will add annotations in Bitbucket, for example in pull requests, similar to the built-in GitHub formatting.
Helper classes for Bitbucket reports.

## Installation

Via Composer

```bash
composer require --dev swisnl/phpstan-bitbucket
composer require swisnl/bitbucket-reports
```

If you also have [phpstan/extension-installer](https://github.com/phpstan/extension-installer) installed, then you're all set!

<details>
<summary>Manual installation</summary>

If you don't want to use `phpstan/extension-installer`, include extension.neon in your project's PHPStan config:

```neon
includes:
- vendor/swisnl/phpstan-bitbucket/extension.neon
```
</details>

## Usage

To use this custom error formatter you need to run PHPStan with `--error-format=bitbucket` option. For example:
```shell
vendor/bin/phpstan analyse src -l8 --error-format=bitbucket
```

## Screenshots

![Annotations](img/annotations.png)
![Report](img/report.png)
This is a collection of helper classes for Bitbucket reports, used by several of our projects. Please refer to these projects for usage examples.

## Changelog

Expand All @@ -55,9 +34,9 @@ If you discover any security related issues, please email [email protected] inste

## Credits

This package is a fork of [alxt/phpstan-bitbucket][link-fork] with some updates.
This package contains files from [alxt/phpstan-bitbucket][link-fork].

- [Alexander Timmermann][link-author]
- [Jasper Zonneveld][link-author]
- [All Contributors][link-contributors]

## License
Expand All @@ -70,16 +49,16 @@ This package is [Treeware](https://treeware.earth). If you use it in production,

[SWIS][link-swis] is a web agency from Leiden, the Netherlands. We love working with open source software.

[ico-version]: https://img.shields.io/packagist/v/swisnl/phpstan-bitbucket.svg?style=flat-square
[ico-version]: https://img.shields.io/packagist/v/swisnl/bitbucket-reports.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-treeware]: https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-lightgreen.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/swisnl/phpstan-bitbucket.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/swisnl/bitbucket-reports.svg?style=flat-square
[ico-swis]: https://img.shields.io/badge/%F0%9F%9A%80-maintained%20by%20SWIS-%230737A9.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/swisnl/phpstan-bitbucket
[link-downloads]: https://packagist.org/packages/swisnl/phpstan-bitbucket
[link-treeware]: https://plant.treeware.earth/swisnl/phpstan-bitbucket
[link-packagist]: https://packagist.org/packages/swisnl/bitbucket-reports
[link-downloads]: https://packagist.org/packages/swisnl/bitbucket-reports
[link-treeware]: https://plant.treeware.earth/swisnl/bitbucket-reports
[link-fork]: https://github.com/modprobe/phpstan-bitbucket
[link-author]: https://github.com/modprobe
[link-author]: https://github.com/JaZo
[link-contributors]: ../../contributors
[link-swis]: https://www.swis.nl
23 changes: 6 additions & 17 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{
"name": "swisnl/phpstan-bitbucket",
"type": "phpstan-extension",
"description": "PHPStan error formatter for Bitbucket Pipeline Reports",
"name": "swisnl/bitbucket-reports",
"type": "library",
"description": "Helper classes for Bitbucket Pipeline Reports",
"keywords": [
"phpstan",
"error-formatter",
"bitbucket",
"pipeline",
"reports",
"testing"
],
"homepage": "https://github.com/swisnl/phpstan-bitbucket",
"homepage": "https://github.com/swisnl/bitbucket-reports",
"license": "MIT",
"authors": [
{
Expand All @@ -25,29 +23,20 @@
}
],
"require": {
"php": "^7.4|^8.0",
"php": "^8.0",
"ext-json": "*",
"guzzlehttp/guzzle": "^7.0",
"phpstan/phpstan": "^1.0",
"ramsey/uuid": "^4.0"
},
"require-dev": {
"laravel/pint": "^1.2"
},
"autoload": {
"psr-4": {
"Swis\\PHPStan\\ErrorFormatter\\": "src"
}
},
"extra": {
"phpstan": {
"includes": [
"extension.neon"
]
"Swis\\Bitbucket\\Reports\\": "src"
}
},
"scripts": {
"analyse": "vendor/bin/phpstan analyse",
"format": "vendor/bin/pint"
},
"config": {
Expand Down
Binary file removed img/annotations.png
Binary file not shown.
Binary file removed img/report.png
Binary file not shown.
12 changes: 0 additions & 12 deletions phpstan.neon

This file was deleted.

17 changes: 10 additions & 7 deletions src/BitbucketApiClient.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace Swis\PHPStan\ErrorFormatter;
declare(strict_types=1);

namespace Swis\Bitbucket\Reports;

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
Expand All @@ -16,29 +18,30 @@ class BitbucketApiClient

private const PROXY_URL = 'http://localhost:29418';

private const REPORT_TITLE = 'PHPStan Report';

private Client $httpClient;

private ParentDirectoryRelativePathHelper $relativePathHelper;

public function __construct(string $baseUrl = self::BASE_URL, string $proxyUrl = self::PROXY_URL)
{
$this->httpClient = new Client([
'base_uri' => $baseUrl,
RequestOptions::PROXY => $proxyUrl,
]);
$this->relativePathHelper = new ParentDirectoryRelativePathHelper(BitbucketConfig::cloneDir());
}

public function createReport(int $numberOfIssues = 0): UuidInterface
public function createReport(string $title, int $numberOfIssues = 0): UuidInterface
{
$payload = $numberOfIssues > 0
? [
'title' => self::REPORT_TITLE,
'title' => $title,
'details' => sprintf('This PR introduces %d new issue(s).', $numberOfIssues),
'report_type' => 'BUG',
'result' => 'FAILED',
]
: [
'title' => self::REPORT_TITLE,
'title' => $title,
'details' => 'This PR introduces no new issues.',
'report_type' => 'BUG',
'result' => 'PASSED',
Expand All @@ -65,7 +68,7 @@ public function addAnnotation(
];

if ($filePath !== null) {
$payload['path'] = $filePath;
$payload['path'] = $this->relativePathHelper->getRelativePath($filePath);
}

if ($line !== null) {
Expand Down
4 changes: 3 additions & 1 deletion src/BitbucketConfig.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace Swis\PHPStan\ErrorFormatter;
declare(strict_types=1);

namespace Swis\Bitbucket\Reports;

class BitbucketConfig
{
Expand Down
54 changes: 0 additions & 54 deletions src/BitbucketErrorFormatter.php

This file was deleted.

55 changes: 55 additions & 0 deletions src/ParentDirectoryRelativePathHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Swis\Bitbucket\Reports;

/**
* Copied from https://github.com/phpstan/phpstan-src/blob/1.11.x/src/File/ParentDirectoryRelativePathHelper.php
*/
class ParentDirectoryRelativePathHelper
{
public function __construct(private string $parentDirectory)
{
}

public function getRelativePath(string $filename): string
{
return implode('/', $this->getFilenameParts($filename));
}

/**
* @return string[]
*/
public function getFilenameParts(string $filename): array
{
$schemePosition = strpos($filename, '://');
if ($schemePosition !== \false) {
$filename = substr($filename, $schemePosition + 3);
}
$parentParts = explode('/', trim(str_replace('\\', '/', $this->parentDirectory), '/'));
$parentPartsCount = count($parentParts);
$filenameParts = explode('/', trim(str_replace('\\', '/', $filename), '/'));
$filenamePartsCount = count($filenameParts);
$i = 0;
for (; $i < $filenamePartsCount; $i++) {
if ($parentPartsCount < $i + 1) {
break;
}
$parentPath = implode('/', array_slice($parentParts, 0, $i + 1));
$filenamePath = implode('/', array_slice($filenameParts, 0, $i + 1));
if ($parentPath !== $filenamePath) {
break;
}
}
if ($i === 0) {
return [$filename];
}
$dotsCount = $parentPartsCount - $i;
if ($dotsCount < 0) {
throw new \RuntimeException('This should not happen.');
}

return array_merge(array_fill(0, $dotsCount, '..'), array_slice($filenameParts, $i));
}
}

0 comments on commit ce512b5

Please sign in to comment.