blib/lib/Dist/Zilla/Plugin/UploadToStratopan.pm

filter: Statementstmt 55.7% 29 / 52 Branchbran 10.0% 1 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 55.5% 5 / 9 Podpod 100% 1 / 1 totaltotal 50.0% 36 / 72 CC 6 SCAR 15.7
CC
6
Cov
48%
release
12.0
_assert_stack
6.0
_login
3.8
CRAP 4.8
↑ Summary
1 package Dist::Zilla::Plugin::UploadToStratopan;
2  
3 2 our $VERSION = '0.015';
4  
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 with 'Dist::Zilla::Role::Releaser';
10  
11 # ABSTRACT: Automate Stratopan releases with Dist::Zilla
12  
13 2 has agent => (
14 is => 'ro',
15 isa => 'Str',
16 default => 'stratopan-uploader/' . $VERSION
17 );
18  
19 2 has repo => (
20 is => 'ro',
21 isa => 'Str',
22 required => 1
23 );
24  
25 2 has stack => (
26 is => 'ro',
27 isa => 'Str',
28 default => 'master',
29 required => 1
30 );
31  
32 2 has recurse => (
33 is => 'ro',
34 isa => 'Bool',
35 default => 0
36 );
37  
38 2 has _strato_base => (
39 is => 'ro',
40 isa => 'Str',
41 default => 'https://stratopan.com'
42 );
43  
44 2 has _ua => (
45 is => 'ro',
46 isa => 'Mojo::UserAgent',
47 lazy_build => 1
48 );
49  
50 2 has _username => (
51 is => 'ro',
52 isa => 'Str',
53 lazy_build => 1
54 );
55  
56 2 has _password => (
57 is => 'ro',
58 isa => 'Str',
59 lazy_build => 1
60 );
61  
62 0 ▶sub _build__username {
Subroutine _build__username: not called CC=1 SCAR=6.9
63 0 my $self = shift;
64  
65 0 return $self->zilla->chrome->prompt_str("Stratopan username: ");
66 }
67  
68 0 ▶sub _build__password {
Subroutine _build__password: not called CC=1 SCAR=6.9
69 0 my $self = shift;
70 0 return $self->zilla->chrome->prompt_str("Stratopan password: ",
71 { noecho => 1 });
72 }
73  
74 1 ▶sub _build__ua {
Subroutine _build__ua: called CC=1 SCAR=0.0
75 1 my $self = shift;
76  
77 1 my $ua = Mojo::UserAgent->new;
78 1 $ua->transactor->name($self->agent);
79 1 return $ua;
80 }
81  
82 1 ▶sub _login {
Subroutine _login: called CC=3 SCAR=13.5
83 1 my $self = shift;
84  
85 1 my $ua = $self->_ua;
86  
87 1 my $tx = $ua->post(
88 $self->_strato_base . '/signin',
89 form => {
90 login => $self->_username,
91 password => $self->_password
92 }
93 );
94  
95 1 ▶ if ($tx->res->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tx->res->error) 1 0 1
96 1 $self->log_fatal("Unable to login to Stratopan: " . $tx->res->error->{message});
97 }
98  
99 # stratopan redirects on a post (302)
100 # and returns some div alert thing when posting - also returning a
101 # 200
102 0 ▶ if (my $error = $tx->res->dom->at('div#page-alert p')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $tx->res->dom->at("div#page-alert p")) 0 0 0
103 0 $self->log_fatal($error->text);
104 }
105  
106 }
107  
108 0 ▶sub _assert_stack {
Subroutine _assert_stack: not called CC=2 SCAR=17.9
109 0 my ($self) = @_;
110  
111 0 my $stack_uri = sprintf '%s/%s/%s/%s',
112 $self->_strato_base, $self->_username, $self->repo, $self->stack;
113  
114 0 my $tx = $self->_ua->get($stack_uri);
115  
116 0 ▶ if ($tx->res->code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tx->res->code == 200) 0 0 0
117 0 return $stack_uri;
118 }
119 $self->log_fatal(
120 0 sprintf(
121 "Stack %s does not exist in repo '%s', create it first",
122 $self->stack, $self->repo
123 )
124 );
125  
126 }
127  
128 0 ▶sub release {
Subroutine release: not called CC=3 SCAR=24.8
Pod documented
129 0 my ($self, $tarball) = @_;
130  
131 0 $tarball = "$tarball"; # stringify object
132 0 ▶ $self->log_fatal("No tarball found with name $tarball") unless $tarball;
Branch: 1 branch
BranchTrueFalseTotal
unless $tarball 0 0 0
133  
134 0 $self->_login;
135 0 my $ua = $self->_ua;
136  
137 0 my $submit_url = join('/', $self->_assert_stack, qw(stack add));
138  
139 0 $self->log(["uploading %s to %s", $tarball, $submit_url]);
140  
141 0 my $tx = $ua->post(
142 $submit_url,
143 form => {
144 recurse => $self->recurse,
145 message => "Uploaded by " . __PACKAGE__,
146 archive => { file => $tarball }
147 }
148 );
149  
150 0 ▶ if ($tx->res->code == 302) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tx->res->code == 302) 0 0 0
151 0 return $self->log("success.");
152 }
153 0 $self->log_fatal($tx->res->dom->at('div#page-alert p')->text);
154 }
155  
156  
157 2 1;
158  
159 __END__
↑ Summary