|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +=head DESCRIPTION |
| 4 | +
|
| 5 | +Automatically `git tag` by travisCI. |
| 6 | +
|
| 7 | +When the pull request is merged and the branch name is like "bump-version-N.N.N", |
| 8 | +the merge commit is automatically tagged as `vN.N.N`. |
| 9 | +
|
| 10 | +=head SYNOPSIS |
| 11 | +
|
| 12 | + % tool/autotag |
| 13 | +
|
| 14 | +=cut |
| 15 | + |
| 16 | +use 5.014; |
| 17 | +use warnings; |
| 18 | + |
| 19 | +use HTTP::Tiny; |
| 20 | +use JSON::PP; |
| 21 | + |
| 22 | +my $OWNER = 'mackerelio'; |
| 23 | +my $REPO = 'go-check-plugins'; |
| 24 | + |
| 25 | +sub get_branch_from_msg { |
| 26 | + my $msg = shift; |
| 27 | + |
| 28 | + my ($pr_num, $branch) = $msg =~ /^[a-f0-9]{7} Merge pull request #([0-9]+) from (.*)$/; |
| 29 | + return unless $pr_num; |
| 30 | + |
| 31 | + my $res = HTTP::Tiny->new->get( |
| 32 | + sprintf 'https://api.github.com/repos/%s/%s/pulls/%d?state=closed', $OWNER, $REPO, $pr_num |
| 33 | + ); |
| 34 | + unless ($res->{success}) { |
| 35 | + warn 'API request failed'; return; |
| 36 | + } |
| 37 | + my $data = decode_json $res->{content}; |
| 38 | + my $label = $data->{head}{label}; |
| 39 | + $label =~ s!:!/!; # $user:branch/name -> $user/branch/name |
| 40 | + |
| 41 | + if ($label ne $branch) { |
| 42 | + return; |
| 43 | + } |
| 44 | + ($data->{head}{ref}, $data->{title}, $data->{body}); |
| 45 | +} |
| 46 | + |
| 47 | +sub tag_name_from_branch { |
| 48 | + my $branch = shift; |
| 49 | + |
| 50 | + my ($tag) = $branch =~ m!^bump-version-([0-9]+(?:\.[0-9]+){2})$!; |
| 51 | + $tag && "v$tag"; |
| 52 | +} |
| 53 | + |
| 54 | +### main |
| 55 | +if (!$ENV{HARNESS_ACTIVE}) { |
| 56 | + main(); |
| 57 | +} else { |
| 58 | + # When called via `prove`, tests will run. |
| 59 | + run_tests(); |
| 60 | +} |
| 61 | + |
| 62 | +sub main { |
| 63 | + # e.g. 492764e Merge pull request #3 from Songmu/fix/remote |
| 64 | + chomp(my $msg = `git log HEAD~.. --merges --oneline`); |
| 65 | + unless ($msg) { |
| 66 | + say 'not a merged commit'; return; |
| 67 | + } |
| 68 | + |
| 69 | + my ($branch, $title, $body) = get_branch_from_msg($msg); |
| 70 | + unless ($branch) { |
| 71 | + say 'not a pull request'; return; |
| 72 | + } |
| 73 | + my $tag = tag_name_from_branch($branch); |
| 74 | + unless ($tag) { |
| 75 | + say 'not a autotag target'; return; |
| 76 | + } |
| 77 | + |
| 78 | + my %tags = map { chomp; ($_ => 1) } `git tag`; |
| 79 | + if ($tags{$tag}) { |
| 80 | + say "tag: $tag already tagged"; return; |
| 81 | + } |
| 82 | + system(qw/git tag -a/, $tag, '-m', "$title\n\n$body"); |
| 83 | + system(qw/git push --tags/); |
| 84 | +} |
| 85 | + |
| 86 | +sub run_tests { |
| 87 | + require Test::More; |
| 88 | + Test::More->import; |
| 89 | + |
| 90 | + my ($branch) = get_branch_from_msg('58a535b Merge pull request #44 from naokibtn/feature-munin'); |
| 91 | + is($branch, 'feature-munin'); |
| 92 | + ($branch) = get_branch_from_msg('86b6a58 Merge pull request #36 from mackerelio/bump-version-0.5.0'); |
| 93 | + is($branch, 'bump-version-0.5.0'); |
| 94 | + |
| 95 | + is(tag_name_from_branch('bump-version-0.5.0'), 'v0.5.0'); |
| 96 | + ok(!tag_name_from_branch('feature-hoge')); |
| 97 | + |
| 98 | + done_testing(); |
| 99 | +} |
0 commit comments