File Coverage

blib/lib/GitHub/Actions.pm
Criterion Covered Total %
statement 47 51 92.1
branch 6 12 50.0
condition 2 2 100.0
subroutine 17 18 94.4
pod 11 11 100.0
total 83 94 88.3


line stmt bran cond sub pod time code
1             package GitHub::Actions;
2              
3 4     4   296241 use Exporter 'import'; # needed to use @EXPORT
  4         36  
  4         154  
4 4     4   27 use warnings;
  4         10  
  4         107  
5 4     4   18 use strict;
  4         14  
  4         102  
6 4     4   21 use Carp;
  4         6  
  4         269  
7              
8 4     4   92 use v5.14;
  4         15  
9              
10             # Module implementation here
11             our %github;
12              
13             our @EXPORT = qw( %github set_output set_env debug error warning set_failed command_on_file error_on_file warning_on_file start_group end_group);
14              
15             BEGIN {
16 4     4   107 for my $k ( keys(%ENV) ) {
17 138 100       335 if ( $k =~ /^GITHUB_/ ) {
18 2         8 my ($nogithub) = ( $k =~ /^GITHUB_(\w+)/ );
19 2         7 $github{$nogithub} = $ENV{$k} ;
20             }
21             }
22             }
23              
24 4     4   2105 use version; our $VERSION = qv('0.1.0');
  4         8326  
  4         21  
25              
26             sub set_output {
27 2 50   2 1 3402 carp "Need name and value" unless @_;
28 2         7 my ($output_name, $output_value) = @_;
29 2   100     10 $output_value ||='';
30 2         120 say "::set-output name=$output_name\::$output_value";
31             }
32              
33             sub set_env {
34 0     0 1 0 my ($env_var_name, $env_var_value) = @_;
35 0 0       0 open(my $fh, '>>', $github{'ENV'}) or die "Could not open file ". $github{'ENV'} ." $!";
36 0         0 say $fh "$env_var_name=$env_var_value";
37 0         0 close $fh;
38             }
39              
40             sub debug {
41 1     1 1 1532 my $debug_message = shift;
42 1         35 say "::debug::$debug_message";
43             }
44              
45             sub error {
46 2     2 1 1605 my $error_message = shift;
47 2         122 say "::error::$error_message"
48             }
49              
50             sub warning {
51 2     2 1 1592 my $warning = shift;
52 2         51 say "::warning::$warning"
53             }
54              
55             sub error_on_file {
56 1     1 1 1550 command_on_file( "::error", @_ );
57             }
58              
59             sub warning_on_file {
60 1     1 1 1512 command_on_file( "::warning", @_ );
61             }
62              
63             sub command_on_file {
64 3     3 1 1899 my $command = shift;
65 3         7 my $message = shift;
66 3         20 my ($file, $line, $col ) = @_;
67 3 50       15 if ( $file ) {
68 3         5 my @data;
69 3         8 push( @data, "file=$file");
70 3 50       10 push( @data, "line=$line") if $line;
71 3 50       8 push( @data, "col=$col") if $col;
72 3         10 $command .= " ".join(",", @data );
73             }
74 3         111 say $command."::$message"
75             }
76              
77             sub start_group {
78 1     1 1 1570 say "::group::" . shift;
79             }
80              
81             sub end_group {
82 1     1 1 19 say "::endgroup::";
83             }
84              
85             sub set_failed {
86 1     1 1 1388 error( @_ );
87 1         11 exit( 1);
88             }
89              
90             "Action!"; # Magic true value required at end of module
91             __END__
92              
93             =head1 NAME
94              
95             GitHub::Actions - Work in GitHub Actions using Perl
96              
97              
98             =head1 VERSION
99              
100             This document describes GitHub::Actions version 0.0.3
101              
102              
103             =head1 SYNOPSIS
104              
105             use GitHub::Actions;
106             use v5.14;
107              
108             # %github contains all GITHUB_* environment variables
109             for my $g (keys %github ) {
110             say "GITHUB_$g -> ", $github{$g}
111             }
112              
113             # Set step output
114             set_output("FOO", "BAR");
115              
116             # Set environment variable value
117             set_env("FOO", "BAR");
118              
119             Install this module within a GitHub action
120              
121             . name: "Install GitHub::Actions"
122             run: sudo cpan GitHub::Actions
123              
124             (we need C<sudo> since we're using the system Perl)
125              
126             You can use this as a C<step>
127              
128             - name: Test env variables
129             shell: perl {0}
130             run: |
131             use GitHub::Actions;
132             set_env( 'FOO', 'BAR');
133              
134             =head1 DESCRIPTION
135              
136             GitHub Actions include, by default, at least in its linux runners, a
137             system Perl which you can use directly in your GitHub actions. This is
138             a (for the time being) minimalistic module that tries to help a bit
139             with that, by defining a few functions that will be useful when
140             performing GitHub actions. Besides the system Perl, you can use any of
141             L<the modules
142             installed|https://gist.github.com/JJ/edf3a39d68525439978da2a02763d42b>. You
143             can install other modules via cpan or, preferably for speed, via the
144             Ubuntu package (or equivalent)
145              
146             Check out an example of using it in the L<repository|https://github.com/JJ/perl-GitHub-Actions/blob/main/.github/workflows/self-test.yml>
147              
148             =head1 INTERFACE
149              
150             =head2 set_env( $env_var_name, $env_var_value)
151              
152             This is equivalent to L<setting an environment variable|https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable>
153              
154             =head2 set_output( $output_name, $output_value)
155              
156             Equivalent to L<C<set_output>|https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-output-parameter>
157              
158             =head2 debug( $debug_message )
159              
160             Equivalent to L<C<debug>|https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-a-debug-message>
161              
162             =head2 error( $error_message )
163              
164             Equivalent to L<C<error>|https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-error-message>, prints an error message.
165              
166             =head2 warning( $warning_message )
167              
168             Equivalent to L<C<warning>|https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-a-warning-message>, simply prints a warning.
169              
170             =head2 command_on_file( $error_message, $file, $line, $col )
171              
172             Common code for L<error_on_file> and L<warning_on_file>. Can be used for any future commands.
173              
174             =head2 error_on_file( $error_message, $file, $line, $col )
175              
176             Equivalent to L<C<error>|https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-error-message>, prints an error message with file and line info
177              
178             =head2 warning_on_file( $warning_message, $file, $line, $col )
179              
180             Equivalent to L<C<warning>|https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-a-warning-message>, prints an warning with file and line info.
181              
182             =head2 set_failed( $error_message )
183              
184             Exits with an error status of 1 after setting the error message.
185              
186             =head2 start_group( $group_name )
187              
188             Starts a group in the logs, grouping the following messages. Corresponds to L<C<group>|https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#grouping-log-lines>.
189              
190             =head2 end_group
191              
192             Ends current log grouping.
193              
194             =head1 CONFIGURATION AND ENVIRONMENT
195              
196             GitHub::Actions requires no configuration files or environment
197             variables. Those set by GitHub Actions will only be available there,
198             or if you set them explicitly. Remember that they will need to be set
199             during the C<BEGIN> phase to be available when this module loads.
200              
201             BEGIN {
202             $ENV{'GITHUB_FOO'} = 'foo';
203             $ENV{'GITHUB_BAR'} = 'bar';
204             }
205              
206              
207             =head1 DEPENDENCIES
208              
209             Intentionally, no dependencies are included.
210              
211              
212             =head1 INCOMPATIBILITIES
213              
214             None reported.
215              
216              
217             =head1 BUGS AND LIMITATIONS
218              
219             No bugs have been reported.
220              
221             Please report any bugs or feature requests to L<https://github.com/JJ/perl-GitHub-Actions/issues>.
222              
223              
224             =head1 AUTHOR
225              
226             JJ Merelo C<< <jmerelo@CPAN.org> >>. Many thanks to RENEEB and Gabor Szabo for their help with test and metadata.
227              
228              
229             =head1 LICENCE AND COPYRIGHT
230              
231             Copyright (c) 2021, JJ Merelo C<< <jmerelo@CPAN.org> >>. All rights reserved.
232              
233             This module is free software; you can redistribute it and/or
234             modify it under the same terms as Perl itself. See L<perlartistic>.
235              
236              
237             =head1 DISCLAIMER OF WARRANTY
238              
239             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
240             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
241             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
242             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
243             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
244             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
245             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
246             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
247             NECESSARY SERVICING, REPAIR, OR CORRECTION.
248              
249             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
250             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
251             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
252             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
253             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
254             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
255             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
256             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
257             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
258             SUCH DAMAGES.