GrahamCampbell / Laravel-GitHub

A GitHub API bridge for Laravel
https://gjcampbell.co.uk
MIT License
622 stars 120 forks source link

Cannot set Page or PerPage in api call #79

Closed davemg3 closed 6 years ago

davemg3 commented 6 years ago

Hello, Seems to be related to php-github-api library. Some methods cannot be used with page or perpage parameters (using an array) As: repo()->contributors(), user()->repositories, repo()->activity() or repo()->frequency() etc... So we cannot fetch other result pages provided by Github

davemg3 commented 6 years ago

Ok link to previous one https://github.com/GrahamCampbell/Laravel-GitHub/issues/78

The quickest solution i found was to use

$client = resolve('github.connection');//to get the services provided by the provider
and then use on it the paginator of native lib KnpLabs/php-github-api
 $repoApi = $client->api('repo');
            $paginator  = new \Github\ResultPager($client );
            $parameters = array($org_name, ['page' => 1, 'per_page' => 100]);
            $repos_array  = $paginator->fetch($repoApi, $method, $parameters);
GrahamCampbell commented 6 years ago

Thanks for getting in touch. Yeh, you'll need to use the paginator. You don't need to pass the extra page info however, and the fetchAll method is probably the one you're after here. The repo api is not what you want though - that will end up listing everything on all of GitHub. I stead, you'll want the organization API:

The following will suffice (using dependency injection to get the client instance - equivalent to manual resolution of the alias like you did):

<?php

use Github\Client;
use Github\ResultPager;

class Example
{
    function getRepos(Client $client)
    {
        $pager = new ResultPager($client);

        return $pager->fetchAll(
            $client->organization(),
            'repositories',
            ['some_org_name']
        );
    }
}

app()->call('Example@getRepos');

If you're ever in doubt of what stuff is available, take a gander at https://github.com/KnpLabs/php-github-api/tree/master/lib/Github/Api. That's essentially what I pulled up to write this example for you. :)

davemg3 commented 6 years ago

Thank you for advises. I was looking for a clean way to still use dependency injection with some other arguments in my method which is called by a job. Based on your solution i slightly modified it and put it in case someone has same question as me. Hopefully it is not dirty.

<?php
namespace App\Service;
use Github\Client;
use Github\ResultPager;

class Example
{
    function getRepos(Client $client, $argument)
    {
        $pager = new ResultPager($client);
        //argument for example for in place of 'some_org_name'
        return $pager->fetchAll(
            $client->organization(),
            'repositories',
            ['some_org_name']
        );
    }
}

      $example = App::make('App\Service\Example');
        App::call([$example ,'getRepos'],['argument'=>$someArgument]);