| 1 | package Dist::Zilla::Plugin::GitHub::Create; | ||
| 2 | # ABSTRACT: Create a new GitHub repo on dzil new | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '0.49'; | ||
| 7 | |||
| 8 | use JSON::MaybeXS; | ||
| 9 | use Moose; | ||
| 10 | use Try::Tiny; | ||
| 11 | use Git::Wrapper; | ||
| 12 | use File::Basename; | ||
| 13 | |||
| 14 | extends 'Dist::Zilla::Plugin::GitHub'; | ||
| 15 | |||
| 16 | with 'Dist::Zilla::Role::AfterMint'; | ||
| 17 | with 'Dist::Zilla::Role::TextTemplate'; | ||
| 18 | |||
| 19 | has org => ( | ||
| 20 | is => 'ro', | ||
| 21 | isa => 'Maybe[Str]' | ||
| 22 | ); | ||
| 23 | |||
| 24 | has public => ( | ||
| 25 | is => 'ro', | ||
| 26 | isa => 'Bool', | ||
| 27 | default => 1 | ||
| 28 | ); | ||
| 29 | |||
| 30 | has prompt => ( | ||
| 31 | is => 'ro', | ||
| 32 | isa => 'Bool', | ||
| 33 | default => 0 | ||
| 34 | ); | ||
| 35 | |||
| 36 | has has_issues => ( | ||
| 37 | is => 'ro', | ||
| 38 | isa => 'Bool', | ||
| 39 | default => 1 | ||
| 40 | ); | ||
| 41 | |||
| 42 | has has_wiki => ( | ||
| 43 | is => 'ro', | ||
| 44 | isa => 'Bool', | ||
| 45 | default => 1 | ||
| 46 | ); | ||
| 47 | |||
| 48 | has has_downloads => ( | ||
| 49 | is => 'ro', | ||
| 50 | isa => 'Bool', | ||
| 51 | default => 1 | ||
| 52 | ); | ||
| 53 | |||
| 54 | #pod =head1 SYNOPSIS | ||
| 55 | #pod | ||
| 56 | #pod Configure git with your GitHub credentials: | ||
| 57 | #pod | ||
| 58 | #pod $ git config --global github.user LoginName | ||
| 59 | #pod $ git config --global github.password GitHubPassword | ||
| 60 | #pod | ||
| 61 | #pod Alternatively you can install L<Config::Identity> and write your credentials | ||
| 62 | #pod in the (optionally GPG-encrypted) C<~/.github> file as follows: | ||
| 63 | #pod | ||
| 64 | #pod login LoginName | ||
| 65 | #pod password GitHubpassword | ||
| 66 | #pod | ||
| 67 | #pod (if only the login name is set, the password will be asked interactively) | ||
| 68 | #pod | ||
| 69 | #pod then, in your F<profile.ini>: | ||
| 70 | #pod | ||
| 71 | #pod # default config | ||
| 72 | #pod [GitHub::Create] | ||
| 73 | #pod | ||
| 74 | #pod # to override publicness | ||
| 75 | #pod [GitHub::Create] | ||
| 76 | #pod public = 0 | ||
| 77 | #pod | ||
| 78 | #pod # use a template for the repository name | ||
| 79 | #pod [GitHub::Create] | ||
| 80 | #pod repo = {{ lc $dist->name }} | ||
| 81 | #pod | ||
| 82 | #pod See L</ATTRIBUTES> for more options. | ||
| 83 | #pod | ||
| 84 | #pod =head1 DESCRIPTION | ||
| 85 | #pod | ||
| 86 | #pod This Dist::Zilla plugin creates a new git repository on GitHub.com when | ||
| 87 | #pod a new distribution is created with C<dzil new>. | ||
| 88 | #pod | ||
| 89 | #pod It will also add a new git remote pointing to the newly created GitHub | ||
| 90 | #pod repository's private URL. See L</"ADDING REMOTE"> for more info. | ||
| 91 | #pod | ||
| 92 | #pod =cut | ||
| 93 | |||
| 94 | sub after_mint { | ||
| 95 | my $self = shift; | ||
| 96 | my ($opts) = @_; | ||
| 97 | |||
| 98 | return if $self->prompt and not $self->_confirm; | ||
| 99 | |||
| 100 | my $root = $opts->{mint_root}; | ||
| 101 | |||
| 102 | my $repo_name; | ||
| 103 | |||
| 104 | if ($opts->{repo}) { | ||
| 105 | $repo_name = $opts->{repo}; | ||
| 106 | } elsif ($self->repo) { | ||
| 107 | $repo_name = $self->fill_in_string( | ||
| 108 | $self->repo, { dist => \($self->zilla) }, | ||
| 109 | ); | ||
| 110 | } else { | ||
| 111 | $repo_name = $self->zilla->name; | ||
| 112 | } | ||
| 113 | |||
| 114 | my $http = HTTP::Tiny->new; | ||
| 115 | |||
| 116 | $self->log([ "Creating new GitHub repository '%s'", $repo_name ]); | ||
| 117 | |||
| 118 | my ($params, $headers, $content); | ||
| 119 | |||
| 120 | $params->{name} = $repo_name; | ||
| 121 | $params->{public} = $self->public; | ||
| 122 | $params->{description} = $opts->{descr} if $opts->{descr}; | ||
| 123 | |||
| 124 | $params->{has_issues} = $self->has_issues; | ||
| 125 | $self->log([ 'Issues are %s', $params->{has_issues} ? | ||
| 126 | 'enabled' : 'disabled' ]); | ||
| 127 | |||
| 128 | $params->{has_wiki} = $self->has_wiki; | ||
| 129 | $self->log([ 'Wiki is %s', $params->{has_wiki} ? | ||
| 130 | 'enabled' : 'disabled' ]); | ||
| 131 | |||
| 132 | $params->{has_downloads} = $self->has_downloads; | ||
| 133 | $self->log([ 'Downloads are %s', $params->{has_downloads} ? | ||
| 134 | 'enabled' : 'disabled' ]); | ||
| 135 | |||
| 136 | my $url = $self->api; | ||
| 137 | $url .= $self->org ? '/orgs/' . $self->org . '/' : '/user/'; | ||
| 138 | $url .= 'repos'; | ||
| 139 | |||
| 140 | $content = encode_json($params); | ||
| 141 | |||
| 142 | $self->log_debug("Sending POST $url"); | ||
| 143 | my $response = $http->request('POST', $url, { | ||
| 144 | content => $content, | ||
| 145 | headers => $self->_auth_headers, | ||
| 146 | }); | ||
| 147 | |||
| 148 | my $repo = $self->_check_response($response); | ||
| 149 | |||
| 150 | return if not $repo; | ||
| 151 | |||
| 152 | if ($repo eq 'redo') { | ||
| 153 | $self->log("Retrying with two-factor authentication"); | ||
| 154 | $self->prompt_2fa(1); | ||
| 155 | $repo = $self->after_mint($opts); | ||
| 156 | return if not $repo; | ||
| 157 | } | ||
| 158 | |||
| 159 | my $git_dir = "$root/.git"; | ||
| 160 | my $rem_ref = $git_dir."/refs/remotes/".$self->remote; | ||
| 161 | |||
| 162 | if ((-d $git_dir) && (not -d $rem_ref)) { | ||
| 163 | my $git = Git::Wrapper->new($root); | ||
| 164 | |||
| 165 | $self->log([ "Setting GitHub remote '%s'", $self->remote ]); | ||
| 166 | $git->remote("add", $self->remote, $repo->{ssh_url}); | ||
| 167 | |||
| 168 | my ($branch) = try { $git->rev_parse( | ||
| 169 | { abbrev_ref => 1, symbolic_full_name => 1 }, 'HEAD' | ||
| 170 | ) }; | ||
| 171 | |||
| 172 | if ($branch) { | ||
| 173 | try { | ||
| 174 | $git->config("branch.$branch.merge"); | ||
| 175 | $git->config("branch.$branch.remote"); | ||
| 176 | } catch { | ||
| 177 | $self->log([ "Setting up remote tracking for branch '%s'", $branch ]); | ||
| 178 | |||
| 179 | $git->config("branch.$branch.merge", "refs/heads/$branch"); | ||
| 180 | $git->config("branch.$branch.remote", $self->remote); | ||
| 181 | }; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | sub _confirm { | ||
| 187 | my ($self) = @_; | ||
| 188 | |||
| 189 | my $dist = $self->zilla->name; | ||
| 190 | my $prompt = "Shall I create a GitHub repository for $dist?"; | ||
| 191 | |||
| 192 | return $self->zilla->chrome->prompt_yn($prompt, {default => 1} ); | ||
| 193 | } | ||
| 194 | |||
| 195 | __PACKAGE__->meta->make_immutable; | ||
| 196 | 1; # End of Dist::Zilla::Plugin::GitHub::Create | ||
| 197 | |||
| 198 | __END__ |