unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 2/7] www_coderepo: support /$REPO/tags.atom endpoint
Date: Sat, 28 Jan 2023 11:02:50 +0000	[thread overview]
Message-ID: <20230128110255.2950084-3-e@80x24.org> (raw)
In-Reply-To: <20230128110255.2950084-1-e@80x24.org>

Providing an Atom feed for tags can be a nice way for users
to subscribe to new releases without excessive noise.
---
 lib/PublicInbox/RepoAtom.pm    | 62 ++++++++++++++++++++++++++++------
 lib/PublicInbox/WwwCoderepo.pm |  3 ++
 t/solver_git.t                 |  8 +++++
 3 files changed, 62 insertions(+), 11 deletions(-)

diff --git a/lib/PublicInbox/RepoAtom.pm b/lib/PublicInbox/RepoAtom.pm
index 66f12157..4a013147 100644
--- a/lib/PublicInbox/RepoAtom.pm
+++ b/lib/PublicInbox/RepoAtom.pm
@@ -10,10 +10,15 @@ use URI::Escape qw(uri_escape);
 use Scalar::Util ();
 use PublicInbox::Hval qw(ascii_html);
 
+# git for-each-ref and log use different format fields :<
 my $ATOM_FMT = '--pretty=tformat:'.join('%n',
 				map { "%$_" } qw(H ct an ae at s b)).'%x00';
 
-sub log2atom_ok { # parse_hdr for qspawn
+my $EACH_REF_FMT = '--format='.join(';', map { "\$r{'$_'}=%($_)" } qw(
+	objectname refname:short creator contents:subject contents:body
+	*subject *body)).'%00';
+
+sub atom_ok { # parse_hdr for qspawn
 	my ($r, $bref, $ctx) = @_;
 	return [ 404, [], [ "Not Found\n"] ] if $r == 0;
 	bless $ctx, __PACKAGE__;
@@ -42,28 +47,62 @@ sub translate {
 	my @out;
 	my $lbuf = delete($self->{lbuf}) // shift;
 	$lbuf .= shift if @_;
+	my $is_tag = $self->{-is_tag};
+	my ($H, $ct, $an, $ae, $at, $s, $bdy);
 	while ($lbuf =~ s/\A([^\0]+)\0\n//s) {
-		my $ent = $1;
-		utf8::decode($ent);
-		$ent = ascii_html($ent);
-		my ($H, $ct, $an, $ae, $at, $s, $bdy) = split(/\n/, $ent, 7);
-		undef $ent;
+		utf8::decode($bdy = $1);
+		if ($is_tag) {
+			my %r;
+			eval "$bdy";
+			for (qw(contents:subject contents:body)) {
+				$r{$_} =~ /\S/ or delete($r{$_})
+			}
+			$H = $r{objectname};
+			$s = $r{'contents:subject'} // $r{'*subject'};
+			$bdy = $r{'contents:body'} // $r{'*body'};
+			$s .= " ($r{'refname:short'})";
+			$_ = ascii_html($_) for ($s, $bdy, $r{creator});
+			($an, $ae, $at) = split(/\s*&[gl]t;\s*/, $r{creator});
+			$at =~ s/ .*\z//; # no TZ
+			$ct = $at = strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($at));
+		} else {
+			$bdy = ascii_html($bdy);
+			($H, $ct, $an, $ae, $at, $s, $bdy) =
+							split(/\n/, $bdy, 7);
+			$at = strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($at));
+			$ct = strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($ct));
+		}
 		$bdy //= '';
-		$_ = strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($_)) for ($ct, $at);
-
-		push @out, <<"", $bdy, '</pre></div></content></entry>'
+		push @out, <<"";
 <entry><title>$s</title><updated>$ct</updated><author><name>$an</name>
 <email>$ae</email></author><published>$at</published><link
 rel="alternate" type="text/html" href="$self->{-base_url}$H/s/"
-/><id>$H</id><content type="xhtml"><div
+/><id>$H</id>
+
+		push @out, <<'', $bdy, '</pre></div></content>' if $bdy ne '';
+<content type="xhtml"><div
 xmlns="http://www.w3.org/1999/xhtml"><pre style="white-space:pre-wrap">
 
+		push @out, '</entry>';
 	}
 	$self->{lbuf} = $lbuf;
 	chomp @out;
 	$self->SUPER::translate(@out);
 }
 
+# $REPO/tags.atom endpoint
+sub srv_tags_atom {
+	my ($ctx) = @_;
+	my $max = 50; # TODO configurable
+	my @cmd = ('git', "--git-dir=$ctx->{git}->{git_dir}",
+			qw(for-each-ref --sort=-creatordate), "--count=$max",
+			'--perl', $EACH_REF_FMT, 'refs/tags');
+	$ctx->{-feed_title} = "$ctx->{git}->{nick} tags";
+	my $qsp = PublicInbox::Qspawn->new(\@cmd);
+	$ctx->{-is_tag} = 1;
+	$qsp->psgi_return($ctx->{env}, undef, \&atom_ok, $ctx);
+}
+
 sub srv_atom {
 	my ($ctx, $path) = @_;
 	return if index($path, '//') >= 0 || index($path, '/') == 0;
@@ -73,6 +112,7 @@ sub srv_atom {
 			$ATOM_FMT, "-$max");
 	my $tip = $ctx->{qp}->{h}; # same as cgit
 	$ctx->{-feed_title} = $ctx->{git}->{nick};
+	$ctx->{-feed_title} .= " $path" if $path ne '';
 	if (defined($tip)) {
 		push @cmd, $tip;
 		$ctx->{-feed_title} .= ", $tip";
@@ -81,7 +121,7 @@ sub srv_atom {
 	push @cmd, '--';
 	push @cmd, $path if $path ne '';
 	my $qsp = PublicInbox::Qspawn->new(\@cmd);
-	$qsp->psgi_return($ctx->{env}, undef, \&log2atom_ok, $ctx);
+	$qsp->psgi_return($ctx->{env}, undef, \&atom_ok, $ctx);
 }
 
 1;
diff --git a/lib/PublicInbox/WwwCoderepo.pm b/lib/PublicInbox/WwwCoderepo.pm
index 8dcd9772..e3d45c56 100644
--- a/lib/PublicInbox/WwwCoderepo.pm
+++ b/lib/PublicInbox/WwwCoderepo.pm
@@ -240,6 +240,9 @@ sub srv { # endpoint called by PublicInbox::WWW
 	} elsif ($path_info =~ m!\A/(.+?)/atom/(.*)\z! and
 			($ctx->{git} = $cr->{$1})) {
 		PublicInbox::RepoAtom::srv_atom($ctx, $2) // r(404);
+	} elsif ($path_info =~ m!\A/(.+?)/tags\.atom\z! and
+			($ctx->{git} = $cr->{$1})) {
+		PublicInbox::RepoAtom::srv_tags_atom($ctx);
 	} elsif ($path_info =~ m!\A/(.+?)\z! and ($git = $cr->{$1})) {
 		my $qs = $ctx->{env}->{QUERY_STRING};
 		my $url = $git->base_url($ctx->{env});
diff --git a/t/solver_git.t b/t/solver_git.t
index 7743913b..0090bc06 100644
--- a/t/solver_git.t
+++ b/t/solver_git.t
@@ -403,6 +403,14 @@ EOF
 		is($res->code, 404, 'got 404 for non-existent ref README');
 		$res = $cb->(GET('/public-inbox/tree/Documentation?h=no-dir'));
 		is($res->code, 404, 'got 404 for non-existent ref directory');
+
+		$res = $cb->(GET('/public-inbox/tags.atom'));
+		is($res->code, 200, 'Atom feed');
+		SKIP: {
+			require_mods('XML::TreePP', 1);
+			my $t = XML::TreePP->new->parse($res->content);
+			ok(scalar @{$t->{feed}->{entry}}, 'got tag entries');
+		}
 	};
 	test_psgi(sub { $www->call(@_) }, $client);
 	my $env = { PI_CONFIG => $cfgpath, TMPDIR => $tmpdir };

  parent reply	other threads:[~2023-01-28 11:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-28 11:02 [PATCH 0/7] www_coderepo updates Eric Wong
2023-01-28 11:02 ` [PATCH 1/7] www_coderepo: tree: quiet and 404 on non-existent refs Eric Wong
2023-01-28 11:02 ` Eric Wong [this message]
2023-01-28 11:02 ` [PATCH 3/7] www_coderepo: fix snapshot link generation Eric Wong
2023-01-28 11:02 ` [PATCH 4/7] www_coderepo: reduce utf8::decode calls Eric Wong
2023-01-28 11:02 ` [PATCH 5/7] repo_atom: translate: account for multiple args Eric Wong
2023-01-28 11:02 ` [PATCH 6/7] www_coderepo: support $REPO/refs/{heads,tags}/ endpoints Eric Wong
2023-01-28 11:02 ` [PATCH 7/7] www_coderepo: summary: fix mis-linkification of `...' Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230128110255.2950084-3-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).