File Coverage

blib/lib/Ixchel/functions/github_releases.pm
Criterion Covered Total %
statement 20 49 40.8
branch 0 18 0.0
condition n/a
subroutine 7 8 87.5
pod 1 1 100.0
total 28 76 36.8


line stmt bran cond sub pod time code
1             package Ixchel::functions::github_releases;
2              
3 5     5   96496 use 5.006;
  5         18  
4 5     5   45 use strict;
  5         10  
  5         139  
5 5     5   20 use warnings;
  5         8  
  5         298  
6 5     5   421 use File::Slurp;
  5         36998  
  5         398  
7 5     5   27 use Exporter 'import';
  5         9  
  5         227  
8             our @EXPORT = qw(github_releases);
9 5     5   2101 use LWP::UserAgent ();
  5         172945  
  5         156  
10 5     5   1307 use JSON;
  5         42723  
  5         37  
11              
12             =head1 NAME
13              
14             Ixchel::functions::github_releases - Fetches release information for the specified Github repo
15              
16             =head1 VERSION
17              
18             Version 0.0.1
19              
20             =cut
21              
22             our $VERSION = '0.0.1';
23              
24             =head1 SYNOPSIS
25              
26             use Ixchel::functions::github_releases;
27              
28             my $releases;
29             eval{ $releases=github_releases(owner=>'mikefarah', repo=>'yq'); };
30             if ($@) {
31             print 'Error: '.$@."\n";
32             }
33              
34             =head1 Functions
35              
36             =head2 github_releases
37              
38             The following args are required.
39              
40             - owner :: The owner of the repo in question.
41              
42             - repo :: Repo to fetch the releases for.
43              
44             The following are optional.
45              
46             - raw :: Return the raw JSON and don't decode it.
47             Default :: 0
48              
49             If the $ENV variables below are set, they will be used for proxy info,
50             but the ones above will take president over that and set the env vars.
51              
52             $ENV{FTP_PROXY}
53             $ENV{HTTP_PROXY}
54             $ENV{HTTPS_PROXY}
55              
56             Upon errors, this will die.
57              
58             =cut
59              
60             sub github_releases {
61 0     0 1   my (%opts) = @_;
62              
63 0 0         if ( !defined( $opts{owner} ) ) {
64 0           die('owner not specified');
65             }
66              
67 0 0         if ( !defined( $opts{repo} ) ) {
68 0           die('repo not specified');
69             }
70              
71 0           my $url = 'https://api.github.com/repos/' . $opts{owner} . '/' . $opts{repo} . '/releases';
72 0           my $content;
73 0           eval{
74 0           my $ua = LWP::UserAgent->new(timeout => 10);
75 0 0         if (defined($ENV{HTTP_PROXY})) {
76 0           $ua->proxy(['http'], $ENV{HTTP_PROXY});
77             }
78 0 0         if (defined($ENV{HTTPS_PROXY})) {
79 0           $ua->proxy(['https'], $ENV{HTTPS_PROXY});
80             }
81 0 0         if (defined($ENV{FTP_PROXY})) {
82 0           $ua->proxy(['ftp'], $ENV{FTP_PROXY});
83             }
84              
85 0           my $response = $ua->get($url);
86              
87 0 0         if ($response->is_success) {
88 0           $content=$response->decoded_content;
89             }else {
90 0           die($response->status_line);
91             }
92             };
93 0 0         if ($@) {
94 0           die( 'Fetching "' . $url . '" failed'... $@ );
95             }
96              
97 0 0         if ( $opts{raw} ) {
98 0           return $content;
99             }
100              
101 0           my $json;
102 0           eval { $json = decode_json($content); };
  0            
103 0 0         if ($@) {
104 0           die( 'Decoding JSON from "' . $url . '" failed... ' . $@ );
105             }
106              
107 0           return $json;
108             } ## end sub github_releases
109              
110             1;