summaryrefslogtreecommitdiffstats
path: root/lib/Devscripts/Salsa/last_ci_status.pm
blob: c275efb75fcb5f0ff262fbc571d5de2816f6c318 (plain)
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
package Devscripts::Salsa::last_ci_status;

use strict;
use Devscripts::Output;
use Moo::Role;

with "Devscripts::Salsa::Repo";

use constant OK      => 'success';
use constant SKIPPED => 'skipped';
use constant FAILED  => 'failed';

sub last_ci_status {
    my ($self, @repos) = @_;
    unless (@repos or $self->config->all or $self->config->all_archived) {
        ds_warn "Usage $0 ci_status <--all|--all-archived|names>";
        return 1;
    }
    if (@repos and $self->config->all) {
        ds_warn "--all with a reponame makes no sense";
        return 1;
    }
    if (@repos and $self->config->all_archived) {
        ds_warn "--all-archived with a reponame makes no sense";
        return 1;
    }
    # If --all is asked, launch all projects
    @repos = map { $_->[1] } $self->get_repo(0, @repos) unless (@repos);
    my $ret = 0;
    foreach my $repo (@repos) {
        my $id        = $self->project2id($repo) or return 1;
        my $pipelines = $self->api->pipelines($id);
        unless ($pipelines and @$pipelines) {
            ds_warn "No pipelines for $repo";
            $ret++;
            return 1 unless $self->config->no_fail;
        } else {
            my $status = $pipelines->[0]->{status};
            if ($status eq OK) {
                print "Last result for $repo: $status\n";
            } else {
                print STDERR "Last result for $repo: $status\n";
                my $jobs
                  = $self->api->pipeline_jobs($id, $pipelines->[0]->{id});
                my %jres;
                foreach my $job (sort { $a->{id} <=> $b->{id} } @$jobs) {
                    next if $job->{status} eq SKIPPED;
                    push @{ $jres{ $job->{status} } }, $job->{name};
                }
                if ($jres{ OK() }) {
                    print STDERR '    success: '
                      . join(', ', @{ $jres{ OK() } }) . "\n";
                    delete $jres{ OK() };
                }
                foreach my $k (sort keys %jres) {
                    print STDERR '    '
                      . uc($k) . ': '
                      . join(', ', @{ $jres{$k} }) . "\n";
                }
                print STDERR "\n  See: " . $pipelines->[0]->{web_url} . "\n\n";
                if ($status eq FAILED) {
                    $ret++;
                    unless ($self->config->no_fail) {
                        ds_verbose "Use --no-fail to continue";
                        return 1;
                    }
                }
            }
        }
    }
    return $ret;
}

1;