Browse Source

add the ghch script that I use to make changelogs

sungo 1 year ago
parent
commit
959aaa39b7
1 changed files with 98 additions and 0 deletions
  1. 98 0
      misc/ghch

+ 98 - 0
misc/ghch

@@ -0,0 +1,98 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+use v5.20;
+
+use JSON::XS qw(decode_json);
+use HTTP::Tiny;
+
+sub get {
+	my $short = shift;
+	my $url = "https://api.github.com$short";
+	if (my $token = $ENV{TOKEN} // $ENV{GITHUB_OAUTH_TOKEN}) {
+		$url .= "?access_token=$token";
+	}
+	my $res = HTTP::Tiny->new->get($url);
+	if($res->{success}) {
+		return decode_json($res->{content});
+	} else {
+		die $url.' - '.$res->{status}.' '.$res->{reason}."\n".$res->{content};
+	}
+}
+
+chomp(my $end = $ARGV[1] // 'HEAD');
+chomp $end;
+
+chomp(my $start = $ARGV[0] // `git tag --merged $end --sort version:refname | tail -n 1`);
+
+print "# using start: $start\n";
+print "# using end: $end\n";
+
+my @lines = (`git --no-pager log --oneline --merges --no-decorate $start...$end`);
+
+foreach my $line (reverse @lines) {
+	$line =~ /^(.+?) Merge pull request #(\d+) / or next;
+	my $hash = $1;
+	my $pr = $2;
+
+	my $pull_data = get "/repos/joyent/conch-shell/pulls/$pr";
+
+	my $labels = join(", ", map { $_->{name} } $pull_data->{labels}->@*);
+
+	say <<EOF;
+[PR #$pr]($pull_data->{html_url}) - `$pull_data->{title}`
+===
+* Branch: $pull_data->{head}->{ref}
+* Revision: $hash
+* Labels: $labels
+EOF
+
+	my $body = join('', map "> $_\n", split "\n", ($pull_data->{body} // ''));
+	if ($body) {
+		$body =~ s/\r//g;
+		say $body;
+	}
+
+	my $review_data = get "/repos/joyent/conch-shell/pulls/$pr/reviews";
+	my $reviews = join("\n", map "* Reviewed By: [$_->{user}{login} - $_->{state}]($_->{html_url})", $review_data->@*);
+
+	say <<EOF;
+People
+---
+* Author: [$pull_data->{user}{login}]($pull_data->{user}{html_url})
+$reviews
+* Merged By: [$pull_data->{merged_by}{login}]($pull_data->{merged_by}{html_url})
+* Merged: $pull_data->{merged_at}
+EOF
+
+	say "Commits\n---\n";
+
+	my $commit_data = get "/repos/joyent/conch-shell/pulls/$pr/commits";
+	for my $commit ($commit_data->@*) {
+		my $h = substr($commit->{sha},0,6);
+		my @bits = split("\n", $commit->{commit}->{message});
+
+		say "* [$h]($commit->{html_url}) - \`$bits[0]\`";
+	}
+	say "\n- - -\n";
+}
+
+# if this is a release that doesn't contain pull requests but rather cherry-picks, we need to
+# do something different...
+exit if @lines;
+
+@lines = (`git --no-pager log --oneline --no-decorate $start...$end`);
+say "Commits\n---\n";
+foreach my $line (reverse @lines) {
+	my ($hash, $rest) = split(/ /, $line, 2);
+
+	my $commit = get "/repos/joyent/conch-shell/commits/$hash";
+
+	my $h = substr($commit->{sha},0,6);
+	my @bits = split("\n", $commit->{commit}->{message});
+
+	say "* [$h]($commit->{html_url}) - \`$bits[0]\`";
+}
+say "\n- - -\n";
+