How to get commits list using Rest API for main repository not for all submodules

Hello,

May be do you know how to get commits list using Rest API for main repository not for all submodules?

There is also some option in commits endpoint, may be someone know, what does it mean?

all - boolean - Retrieve every commit from the repository

Thanks,

Hi,

can you share the existing code you’ve been implementing (bash, Python, etc)? Maybe there’s a chance to use an existing API library like https://python-gitlab.readthedocs.io/en/latest/gl_objects/commits.html

Cheers,
Michael

I use the existing API (Perl, GitLab::API::v4) but unfortunately I can’t filter out commits of a main repository.

Hi,

okidoki. I have never used the Perl API bindings, so please include a reproducible example next time for anyone trying to help :slight_smile:

Sandbox environment

  • Use a git repo with some commits
  • Add a submodule and commit it with git submodule add https://github.com/bluefeet/GitLab-API-v4 && git commit -av -m "Add submodule"
  • Install the Perl API bindings
  • Create some Perl code to query things

Test case

query.pl

#!/usr/bin/env perl

use strict;
use warnings;

use GitLab::API::v4;
use Data::Dumper;
use feature qw(switch say);

my $project_id= 15730252; # this project
my $gitlab_api_url = 'https://gitlab.com/api/v4';
my $gitlab_api_token = $ENV{'GITLAB_COM_PRIVATE_TOKEN'};

my $api = GitLab::API::v4->new(
    url           => $gitlab_api_url,
    private_token => $gitlab_api_token,
);
my %params;

my $branches = $api->branches( $project_id );
#say Dumper($branches);

my $commits = $api->commits(
    $project_id,
    \%params,
);

my @coll_commits;

foreach(@{$commits}) {
  my $c = $_;
  push @coll_commits, $c->{'title'};
}
say join("\n", @coll_commits);

generates

Perl: Add query.pl
Perl: Add API Playground
Perl: Add library as submodule
Python: Adjust protected branches levels
Python: Add protected branch creation tests
Import Python API client playground
Initial commit

which is the exact same as https://gitlab.com/dnsmichi/api-playground/commits/master

Conclusion

I’m not able to reproduce your problem, can you share your code and setup?

Cheers,
Michael

1 Like

Thanks!

I use the same logic like yours. This is a part of history module:

use GitLab::API::v4;
use Try::Tiny;

my $projectId = "project id";
my %args = ();

my $history = [];
my $gitLabHistory = $self->api->paginator('commits', $projectId, \%args);

if ($gitLabHistory->isa('GitLab::API::v4::Paginator')) {
    PAGE: while(1) {
        my $commits = [];
        try {
            $commits = $gitLabHistory->next_page();
        } catch {
            $commits = [];

            if ($_) {
                print "Can't get history: $_\n";
            }
        };

        last PAGE unless (ref $commits eq 'ARRAY' && @{$commits});

        push @{$history}, @{$commits};
    }
}

And I was pretty sure that commits method returns all the commits including submodules.

Hi,

ok, so you are using the paginator class which maybe behaves differently to just fetching the commit history.

I’ve modified my code to use pagination as well, and yet I cannot see any commit logs from the submodule itself.

======== Commits ========
Add Perl to README
Perl: Add query.pl
Perl: Add API Playground
Perl: Add library as submodule
Python: Adjust protected branches levels
Python: Add protected branch creation tests
Import Python API client playground
Initial commit
======== Paginated Commits ========
Add Perl to README
Perl: Add query.pl
Perl: Add API Playground
Perl: Add library as submodule
Python: Adjust protected branches levels
Python: Add protected branch creation tests
Import Python API client playground
Initial commit

Can you test that in your code as well, and provide some evidence from your git commit history - a screenshot from Gitlab’s view (or git log) and the output of your API script?

Besides, which Perl version and GitLab::API::v4 module version are involved here?

Cheers,
Michael