Gitlab.php 1.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
<?php

namespace App\Repository;

use GuzzleHttp\Client;

class MergeRequest implements Proposal
{
    private $merge_request;

    public function __construct($merge_request)
    {
        $this->merge_request = $merge_request;
    }

    public function iid() : int
    {
        return $this->merge_request->iid;
    }

    public function id() : int
    {
        return $this->merge_request->id;
    }

    public function url() : string
    {
        return $this->merge_request->web_url;
    }

    public function title() : string
    {
        return $this->merge_request->title;
    }

    public function author() : string
    {
        return $this->merge_request->author->username;
    }

    public function created_at() : int
    {
        return strtotime($this->merge_request->created_at);
    }
}

class Gitlab implements Repository
{
    private $client;
    private $base_url;
    private const stateToString = [ State::Opened => 'opened',
                                    State::Merged => 'merged'];

    public function __construct(Client $client, string $repository_url)
    {
        $this->client = $client;
        $this->base_url = $repository_url;
    }

    public function mergeRequests($state)
    {
        $url = $this->base_url . '/merge_requests?scope=all&per_page=50&state=' . Self::stateToString[$state];
        $response = $this->client->request('GET', $url);
        return collect(json_decode($response->getBody()))->map(function ($merge_request) {
            return new MergeRequest($merge_request);
        });
    }

    public function getNewFiles($merge_request)
    {
        $url = $this->base_url . '/merge_requests/' . $merge_request->iid() . '/changes';
        $response = $this->client->request('GET', $url);
        return collect(json_decode($response->getBody())->changes)->filter(function ($change) {
            return $change->new_file;
        })->map(function ($change) {
            return $change->new_path;
        });
    }
}