File Coverage

blib/lib/Badge/Depot/Plugin/Githubactions.pm
Criterion Covered Total %
statement 35 43 81.4
branch 4 10 40.0
condition n/a
subroutine 11 12 91.6
pod 0 1 0.0
total 50 66 75.7


line stmt bran cond sub pod time code
1 1     1   72336 use 5.10.0;
  1         15  
2 1     1   6 use strict;
  1         2  
  1         20  
3 1     1   8 use 5.10.1;
  1         4  
4 1     1   6 use warnings;
  1         2  
  1         27  
5 1     1   5 use strict;
  1         1  
  1         80  
6              
7              
8             package Badge::Depot::Plugin::Githubactions;
9              
10             # ABSTRACT: Github Actions plugin for Badge::Depot
11             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
12             our $VERSION = '0.0100';
13              
14 1     1   558 use Moose;
  1         483269  
  1         8  
15 1     1   8450 use namespace::autoclean;
  1         8414  
  1         5  
16 1     1   650 use Types::Standard qw/Str HashRef Maybe/;
  1         78252  
  1         12  
17 1     1   1919 use Path::Tiny;
  1         11568  
  1         65  
18 1     1   513 use JSON::MaybeXS 'decode_json';
  1         7361  
  1         564  
19             with 'Badge::Depot';
20              
21             has user => (
22             is => 'ro',
23             isa => Str,
24             lazy => 1,
25             default => sub {
26             my $self = shift;
27             if($self->has_meta) {
28             return $self->_meta->{'username'} if exists $self->_meta->{'username'};
29             }
30             },
31             );
32             has repo => (
33             is => 'ro',
34             isa => Str,
35             lazy => 1,
36             default => sub {
37             my $self = shift;
38             if($self->has_meta) {
39             return $self->_meta->{'repo'} if exists $self->_meta->{'repo'};
40             }
41             },
42             );
43             has branch => (
44             is => 'ro',
45             isa => Maybe[Str],
46             default => sub { undef }
47             );
48             has workflow => (
49             is => 'ro',
50             isa => Str,
51             required => 1,
52             );
53              
54             has _meta => (
55             is => 'ro',
56             isa => HashRef,
57             predicate => 'has_meta',
58             builder => '_build_meta',
59             );
60              
61             sub _build_meta {
62 0     0   0 my $self = shift;
63              
64 0 0       0 return {} if !path('META.json')->exists;
65              
66 0         0 my $json = path('META.json')->slurp_utf8;
67 0         0 my $data = decode_json($json);
68              
69 0 0       0 return {} if !exists $data->{'resources'}{'repository'}{'web'};
70              
71 0         0 my $repository = $data->{'resources'}{'repository'}{'web'};
72 0 0       0 return {} if $repository !~ m{^https://(?:www\.)?github\.com/([^/]+)/(.*)(?:\.git)?$};
73              
74             return {
75 0         0 username => $1,
76             repo => $2,
77             };
78             }
79              
80             sub BUILD {
81 2     2 0 4375 my $self = shift;
82 2 100       119 my $link_url_branch = $self->branch ? '+branch%3A' . $self->branch : '';
83 2         67 $self->link_url(sprintf 'https://github.com/%s/%s/actions?query=workflow%%3A%s%s', $self->user, $self->repo, $self->workflow, $link_url_branch);
84 2         4402 my $image_url = sprintf 'https://img.shields.io/github/workflow/status/%s/%s/%s', $self->user, $self->repo, $self->workflow;
85 2 100       67 $image_url .= '/' . $self->branch if $self->branch;
86 2         55 $self->image_url($image_url);
87 2         387 $self->image_alt('Build status at Github');
88             }
89              
90             1;
91              
92             __END__
93              
94             =pod
95              
96             =encoding UTF-8
97              
98             =head1 NAME
99              
100             Badge::Depot::Plugin::Githubactions - Github Actions plugin for Badge::Depot
101              
102              
103              
104             =begin html
105              
106             <p>
107             <img src="https://img.shields.io/badge/perl-5.10+-blue.svg" alt="Requires Perl 5.10+" />
108             <img src="https://img.shields.io/badge/coverage-75.8%25-orange.svg" alt="coverage 75.8%" />
109             </p>
110              
111             =end html
112              
113             =head1 VERSION
114              
115             Version 0.0100, released 2020-12-27.
116              
117             =head1 SYNOPSIS
118              
119             use Badge::Depot::Plugin::Githubactions;
120              
121             my $badge = Badge::Depot::Plugin::Githubactions->new(user => 'my_name', repo => 'the_repo', branch => 'master', workflow => 'gh-actions-workflow');
122              
123             print $badge->to_html;
124             # prints '<a href="https://github.com/my_name/the_repo/actions?query=workflow%3Agh-actions-workflow+branch%3Amaster"><img src="https://img.shields.io/github/workflow/status/my_name/the_repo/gh-actions-workflow/master" alt="Build status at Github" /></a>'
125              
126             =head1 DESCRIPTION
127              
128             Create a L<Github Actions|https://docs.github.com/en/free-pro-team@latest/actions> badge for a github repository.
129              
130             This class consumes the L<Badge::Depot> role.
131              
132             =for html The badge will look similar to this:
133             <a href="https://github.com/Csson/p5-Badge-Depot-Plugin-Githubactions/actions?query=workflow%3Amakefile-test+branch%3Amaster"><img src="https://img.shields.io/github/workflow/status/Csson/p5-Badge-Depot-Plugin-Githubactions/makefile-test/master" alt="Build status at Github" /></a>
134              
135             =head1 ATTRIBUTES
136              
137             The C<user> and C<repo> attributes are required or optional, depending on your configuration. It looks for the C<resources/repository/web> setting in C<META.json>:
138              
139             =over 4
140              
141             =item *
142              
143             If C<META.json> doesn't exist in the dist root, C<user> and C<repo> are required.
144              
145             =item *
146              
147             If C<resources/repository/web> doesn't exist (or is not a github url), C<user> and C<repo> are required.
148              
149             =back
150              
151             =head2 user
152              
153             Github username.
154              
155             =head2 repo
156              
157             Github repository.
158              
159             =head2 workflow
160              
161             The name of the Github Actions workflow. Required.
162              
163             =head2 branch
164              
165             Github branch. Optional, no default.
166              
167             =head1 SEE ALSO
168              
169             =over 4
170              
171             =item *
172              
173             L<Badge::Depot>
174              
175             =back
176              
177             =head1 SOURCE
178              
179             L<https://github.com/Csson/p5-Badge-Depot-Plugin-Githubactions>
180              
181             =head1 HOMEPAGE
182              
183             L<https://metacpan.org/release/Badge-Depot-Plugin-Githubactions>
184              
185             =head1 AUTHOR
186              
187             Erik Carlsson <info@code301.com>
188              
189             =head1 COPYRIGHT AND LICENSE
190              
191             This software is copyright (c) 2020 by Erik Carlsson.
192              
193             This is free software; you can redistribute it and/or modify it under
194             the same terms as the Perl 5 programming language system itself.
195              
196             =cut