| 1 | package Dist::Zilla::Plugin::GitHub::RequireGreenBuild; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | extends 'Dist::Zilla::Plugin::GitHub'; | |||||||||||||||||||||||||||
| 5 | 1 | with qw( | |||||||||||||||||||||||||||
| 6 | Dist::Zilla::Role::BeforeRelease | ||||||||||||||||||||||||||||
| 7 | ); | ||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Git::Wrapper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use List::Util qw(first); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | has repo_root => ( | |||||||||||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||||||||||
| 17 | isa => 'Str', | ||||||||||||||||||||||||||||
| 18 | lazy_build => 1, | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub _build_repo_root { | ||||||||||||||||||||||||||
|
Subroutine
_build_repo_root: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 21 | 0 | return shift->zilla->root->stringify; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub before_release { | ||||||||||||||||||||||||||
|
Subroutine
before_release: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # What is the "HEAD" commit that we're attempting to release? | ||||||||||||||||||||||||||||
| 28 | 0 | my $git = Git::Wrapper->new($self->repo_root); | |||||||||||||||||||||||||||
| 29 | 0 | my ($head_sha) = $git->rev_parse('HEAD'); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # What GitHub repo should we be querying? | ||||||||||||||||||||||||||||
| 32 | 0 | my $repo_name = $self->_get_repo_name; | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | unless ($repo_name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | $self->log('Unable to determine GitHub repo; cannot check GitHub Actions for a green build'); | |||||||||||||||||||||||||||
| 35 | 0 | return; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # Dig through the GitHub Actions run history, to find the most recent run for | ||||||||||||||||||||||||||||
| 39 | # our HEAD sha. | ||||||||||||||||||||||||||||
| 40 | # | ||||||||||||||||||||||||||||
| 41 | # Query the results one page at a time, as the response sizes are LARGE and | ||||||||||||||||||||||||||||
| 42 | # we're hoping/expecting that you're doing a release close to the point in | ||||||||||||||||||||||||||||
| 43 | # time when you ran the build. | ||||||||||||||||||||||||||||
| 44 | 0 | my $per_page = 50; | |||||||||||||||||||||||||||
| 45 | 0 | my $page = 1; | |||||||||||||||||||||||||||
| 46 | 0 | my $http = HTTP::Tiny->new(verify_SSL => 1); | |||||||||||||||||||||||||||
| 47 | 0 | $self->log_debug("Checking GitHub Actions for successful run of $head_sha"); | |||||||||||||||||||||||||||
| 48 | 0 | while (1) { | |||||||||||||||||||||||||||
| 49 | 0 | my $url = $self->api . "/repos/$repo_name/actions/runs?per_page=$per_page&page=$page"; | |||||||||||||||||||||||||||
| 50 | 0 | my $res = $http->request('GET', $url, { headers => $self->_auth_headers }); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | my $data = $self->_check_response($res); | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | unless ($data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | $self->log_fatal('Unable to query GitHub Actions'); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # ... were there any Workflow Runs? | ||||||||||||||||||||||||||||
| 58 | 0 | my $workflows = $data->{workflow_runs}; | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | unless ($workflows && @{$workflows}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$workflows and @{$workflows;}
MC/DC0%0/2
$workflows and @{$workflows;}
$workflows
@{$workflows;}
Truth table0%0/3
A and B
A= $workflowsB= @{$workflows;}
| |||||||||||||||||||||||||||||
| 60 | 0 | $self->log_fatal("Unable to find successful GitHub Actions workflow run for $head_sha"); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # ... were any of those Workflow Runs for this HEAD? If not, get next page. | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my $run = first { $_->{head_sha} eq $head_sha } @{$workflows}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | unless ($run) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | $page++; | |||||||||||||||||||||||||||
| 67 | 0 | next; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 0 | $self->log_debug("found workflow runs for $head_sha"); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # ... was the Workflow Run complete? | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | unless ($run->{status} eq 'completed') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | my $build_url = $run->{html_url}; | |||||||||||||||||||||||||||
| 74 | 0 | $self->log_fatal("GitHub Actions workflow incomplete; see $build_url"); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # ... was the Workflow Run successful? | ||||||||||||||||||||||||||||
| 78 | 0 | ▶ | unless ($run->{conclusion} eq 'success') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | my $build_url = $run->{html_url}; | |||||||||||||||||||||||||||
| 80 | 0 | $self->log_fatal("GitHub Actions workflow unsuccessful; see $build_url"); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Looks good! | ||||||||||||||||||||||||||||
| 84 | 0 | $self->log("found successful GitHub Actions workflow run for $head_sha"); | |||||||||||||||||||||||||||
| 85 | 0 | last; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 90 | 1 | 1; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head1 NAME | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Dist::Zilla::Plugin::GitHub::RequireGreenBuild - Require a successful GitHub Actions workflow run | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # in your dist.ini: | ||||||||||||||||||||||||||||
| 99 | [GitHub::RequireGreenBuild] | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | This C<Dist::Zilla> plugin checks your GitHub Actions for a successful run, | ||||||||||||||||||||||||||||
| 104 | before allowing a release. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | e.g. until we can determine that you have a green build for a GitHub Actions run | ||||||||||||||||||||||||||||
| 107 | against C<HEAD>, you're not allowed to release. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | To determine whether or not a successful run exists, we page through all of | ||||||||||||||||||||||||||||
| 110 | your GitHub Actions runs, to find a successful run for the current C<HEAD> SHA. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | This comes with the implicit expectation that you have already pushed your | ||||||||||||||||||||||||||||
| 113 | commits back up to GitHub. If you haven't pushed, we find any runs matching the | ||||||||||||||||||||||||||||
| 114 | current C<HEAD>, and you won't be able to release. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Graham TerMarsch (cpan@howlingfrog.com) | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Copyright (C) 2021-, Graham TerMarsch. All Rights Reserved. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | This is free software; you can redistribute it and/or modify it under the same | ||||||||||||||||||||||||||||
| 125 | license as Perl itself. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =over | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =item L<Dist::Zilla> | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =item L<Dist::Zilla::Plugin::GitHub> | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =back | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =cut | ||||||||||||||||||||||||||||