| 1 | package Dist::Zilla::Plugin::GitHub::Meta; | ||
| 2 | # ABSTRACT: Add a GitHub repo's info to META.{yml,json} | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '0.49'; | ||
| 7 | |||
| 8 | use JSON::MaybeXS; | ||
| 9 | use Moose; | ||
| 10 | |||
| 11 | extends 'Dist::Zilla::Plugin::GitHub'; | ||
| 12 | |||
| 13 | with 'Dist::Zilla::Role::MetaProvider'; | ||
| 14 | |||
| 15 | has homepage => ( | ||
| 16 | is => 'ro', | ||
| 17 | isa => 'Bool', | ||
| 18 | default => 1 | ||
| 19 | ); | ||
| 20 | |||
| 21 | has bugs => ( | ||
| 22 | is => 'ro', | ||
| 23 | isa => 'Bool', | ||
| 24 | default => 1 | ||
| 25 | ); | ||
| 26 | |||
| 27 | has wiki => ( | ||
| 28 | is => 'ro', | ||
| 29 | isa => 'Bool', | ||
| 30 | default => 0 | ||
| 31 | ); | ||
| 32 | |||
| 33 | has fork => ( | ||
| 34 | is => 'ro', | ||
| 35 | isa => 'Bool', | ||
| 36 | default => 1 | ||
| 37 | ); | ||
| 38 | |||
| 39 | has require_auth => ( | ||
| 40 | is => 'ro', | ||
| 41 | isa => 'Bool', | ||
| 42 | default => 0 | ||
| 43 | ); | ||
| 44 | |||
| 45 | #pod =head1 SYNOPSIS | ||
| 46 | #pod | ||
| 47 | #pod Configure git with your GitHub login name: | ||
| 48 | #pod | ||
| 49 | #pod $ git config --global github.user LoginName | ||
| 50 | #pod | ||
| 51 | #pod then, in your F<dist.ini>: | ||
| 52 | #pod | ||
| 53 | #pod # default config | ||
| 54 | #pod [GitHub::Meta] | ||
| 55 | #pod | ||
| 56 | #pod # to override the repo name | ||
| 57 | #pod [GitHub::Meta] | ||
| 58 | #pod repo = SomeRepo | ||
| 59 | #pod | ||
| 60 | #pod See L</ATTRIBUTES> for more options. | ||
| 61 | #pod | ||
| 62 | #pod =head1 DESCRIPTION | ||
| 63 | #pod | ||
| 64 | #pod This Dist::Zilla plugin adds some information about the distribution's GitHub | ||
| 65 | #pod repository to the META.{yml,json} files, using the official L<CPAN::Meta> | ||
| 66 | #pod specification. | ||
| 67 | #pod | ||
| 68 | #pod Note that, to work properly, L<GitHub::Meta> needs the network to connect to | ||
| 69 | #pod GitHub itself. If the network is not available, it will try to come up with | ||
| 70 | #pod sensible data, but it may be inaccurate. | ||
| 71 | #pod | ||
| 72 | #pod L<GitHub::Meta> currently sets the following fields: | ||
| 73 | #pod | ||
| 74 | #pod =over 4 | ||
| 75 | #pod | ||
| 76 | #pod =item C<homepage> | ||
| 77 | #pod | ||
| 78 | #pod The official home of this project on the web, taken from the GitHub repository | ||
| 79 | #pod info. If the C<homepage> option is set to false this will be skipped (default is | ||
| 80 | #pod true). | ||
| 81 | #pod | ||
| 82 | #pod When offline, this is not set. | ||
| 83 | #pod | ||
| 84 | #pod =item C<repository> | ||
| 85 | #pod | ||
| 86 | #pod =over 4 | ||
| 87 | #pod | ||
| 88 | #pod =item C<web> | ||
| 89 | #pod | ||
| 90 | #pod URL pointing to the GitHub page of the project. | ||
| 91 | #pod | ||
| 92 | #pod =item C<url> | ||
| 93 | #pod | ||
| 94 | #pod URL pointing to the GitHub repository (C<git://...>). | ||
| 95 | #pod | ||
| 96 | #pod =item C<type> | ||
| 97 | #pod | ||
| 98 | #pod This is set to C<git> by default. | ||
| 99 | #pod | ||
| 100 | #pod =back | ||
| 101 | #pod | ||
| 102 | #pod =item C<bugtracker> | ||
| 103 | #pod | ||
| 104 | #pod =over 4 | ||
| 105 | #pod | ||
| 106 | #pod =item C<web> | ||
| 107 | #pod | ||
| 108 | #pod URL pointing to the GitHub issues page of the project. If the C<bugs> option is | ||
| 109 | #pod set to false (default is true) or the issues are disabled in the GitHub | ||
| 110 | #pod repository, this will be skipped. | ||
| 111 | #pod | ||
| 112 | #pod When offline, this is not set. | ||
| 113 | #pod | ||
| 114 | #pod =back | ||
| 115 | #pod | ||
| 116 | #pod =back | ||
| 117 | #pod | ||
| 118 | #pod =cut | ||
| 119 | |||
| 120 | sub metadata { | ||
| 121 | my $self = shift; | ||
| 122 | my ($opts) = @_; | ||
| 123 | my $offline = 0; | ||
| 124 | |||
| 125 | my $repo_name = $self->_get_repo_name; | ||
| 126 | return {} if (!$repo_name); | ||
| 127 | |||
| 128 | my $http = HTTP::Tiny->new; | ||
| 129 | |||
| 130 | $self->log("Getting GitHub repository info"); | ||
| 131 | |||
| 132 | my $url = $self->api."/repos/$repo_name"; | ||
| 133 | $self->log_debug("Sending GET $url"); | ||
| 134 | my $response = $http->request('GET', $url, $self->require_auth ? {headers => $self->_auth_headers} : ()); | ||
| 135 | |||
| 136 | my $repo = $self->_check_response($response); | ||
| 137 | $offline = 1 if not $repo; | ||
| 138 | |||
| 139 | $self->log("Using offline repository information") if $offline; | ||
| 140 | |||
| 141 | if (!$offline && $repo->{fork} == JSON->true() && $self->fork == 1) { | ||
| 142 | my $parent = $repo->{parent}{full_name}; | ||
| 143 | my $url = $self->api.'/repos/'.$parent; | ||
| 144 | my $response = $http->request('GET', $url); | ||
| 145 | |||
| 146 | $repo = $self->_check_response($response); | ||
| 147 | return if not $repo; | ||
| 148 | } | ||
| 149 | |||
| 150 | my ($html_url, $git_url, $homepage, $bugtracker, $wiki); | ||
| 151 | |||
| 152 | $html_url = $offline ? | ||
| 153 | "https://github.com/$repo_name" : | ||
| 154 | $repo->{html_url}; | ||
| 155 | |||
| 156 | $git_url = $offline ? | ||
| 157 | "git://github.com/$repo_name.git" : | ||
| 158 | $repo->{git_url}; | ||
| 159 | |||
| 160 | $homepage = $offline ? undef : $repo->{homepage}; | ||
| 161 | |||
| 162 | if (!$offline && $repo->{has_issues} == JSON->true()) { | ||
| 163 | $bugtracker = "$html_url/issues"; | ||
| 164 | } | ||
| 165 | |||
| 166 | if (!$offline && $repo->{has_wiki} == JSON->true()) { | ||
| 167 | $wiki = "$html_url/wiki"; | ||
| 168 | } | ||
| 169 | |||
| 170 | my $meta; | ||
| 171 | $meta->{resources} = { | ||
| 172 | repository => { | ||
| 173 | web => $html_url, | ||
| 174 | url => $git_url, | ||
| 175 | type => 'git' | ||
| 176 | } | ||
| 177 | }; | ||
| 178 | |||
| 179 | if ($self->wiki && $self->wiki == 1 && $wiki) { | ||
| 180 | $meta->{resources}{homepage} = $wiki; | ||
| 181 | } elsif ($self->homepage && $self->homepage == 1 && $homepage) { | ||
| 182 | $meta->{resources}{homepage} = $homepage; | ||
| 183 | |||
| 184 | } | ||
| 185 | |||
| 186 | if ($self->bugs && $self->bugs == 1 && $bugtracker) { | ||
| 187 | $meta->{resources}{bugtracker} = | ||
| 188 | { web => $bugtracker }; | ||
| 189 | } | ||
| 190 | |||
| 191 | return $meta; | ||
| 192 | } | ||
| 193 | |||
| 194 | __PACKAGE__->meta->make_immutable; | ||
| 195 | |||
| 196 | 1; # End of Dist::Zilla::Plugin::GitHub::Meta | ||
| 197 | |||
| 198 | __END__ |