xbar and bitbucket Pull Requests

If you are like me, “out of sight, out of mind” gets a whole new dimension. Therefore I want to have an overview about all the things I need to take care of. One thing which I often forget is code review of the pull requests.

While looking through this I stumbled over xbar which is a nice app one can program to add more stuff to the menu bar. For example the count of the PullRequests I need to review. So I wrote a script to do exactly that. With the help of a bitbucket client written by Graham Campbell, thanks!

Details

The REST-API of bitbucket is not the best if you ask me, because I need to loop over the repos to loop over the pull requests to get everything I need.

Then I collect all pull requests, I’m a reviewer of and check wether they are already approved.

<?php

declare(strict_types=1);

namespace Winkelwagen\Bitbucket;

use Bitbucket\Client;
use Bitbucket\ResultPager;

readonly class PullRequestCollector
{
    public function __construct(private Client $client)
    {
    }

    public function collect(string $name): void
    {
        $atlassianId = 'my-atlassian-id';

        $repoPaginator = new ResultPager($this->client);
        $repositories = $repoPaginator->fetchAll($this->client->repositories()->workspaces('winkelwagen'), 'list');
        $lines = [];
        foreach ($repositories as $repo) {
            $pullRequestsPaginator = new ResultPager($this->client);
            $pullRequests = $pullRequestsPaginator->fetchAll(
                $this->client->repositories()->workspaces('winkelwagen')->pullRequests($repo['slug']),
                'list',
                [['fields' => '+values.reviewers']],
            );
            foreach ($pullRequests as $pullRequest) {
                if (isset($pullRequest['reviewers'])) {
                    foreach ($pullRequest['reviewers'] as $reviewer) {
                        if ($reviewer['account_id'] === $atlassianId && !$reviewer['approved']) {
                            $infos = [
                                "[{$repo['name']}]" . substr($pullRequest['title'], 0, 50),
                                "href={$pullRequest['links']['html']['href']}",
                                'dropdown=true',
                            ];
                            $lines[] = implode(' | ', $infos);
                        }
                    }
                }
            }
        }
        $count = count($lines);
        echo ($count ? ':fire::fire:' : '') . 'PR: ' . $count . ($count ? ' :fire::fire:' : '') . "\n";
        echo '---' . "\n";
        echo 'All PRs to review | '
            . 'href=https://bitbucket.org/winkelwagen/workspace/pull-requests/?user_filter=REVIEWING' . "\n";
        echo implode("\n", $lines);
    }
}

Hope this helps someone.

Leave a Reply