Last active
August 2, 2022 08:37
-
-
Save imkiva/a2dd573914bdf563bc987652d4bccb5a to your computer and use it in GitHub Desktop.
PLCT Auto Report
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.aya.plct; | |
import com.google.gson.GsonBuilder; | |
import com.google.gson.JsonDeserializer; | |
import com.google.gson.annotations.SerializedName; | |
import kala.collection.immutable.ImmutableSeq; | |
import kala.tuple.Tuple; | |
import kala.tuple.Tuple2; | |
import org.aya.pretty.doc.Doc; | |
import org.jetbrains.annotations.NotNull; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.URI; | |
import java.net.http.HttpClient; | |
import java.net.http.HttpRequest; | |
import java.net.http.HttpResponse; | |
import java.time.LocalDate; | |
import java.time.LocalDateTime; | |
public class PLCTReport { | |
public static final @NotNull ImmutableSeq<Tuple2<String, String>> REPO = ImmutableSeq.of( | |
Tuple.of("Aya Prover", "aya-prover/aya-dev"), | |
Tuple.of("Aya Intellij Plugin", "aya-prover/intellij-aya") | |
); | |
public static final @NotNull HttpClient CLIENT = HttpClient.newBuilder() | |
.version(HttpClient.Version.HTTP_2).build(); | |
public static void main(String[] args) throws Exception { | |
var markdown = REPO | |
.mapChecked(t -> generate(t._1, t._2)) | |
.view() | |
.prepended(Doc.plain("## The Aya Theorem Prover")) | |
.appended(Doc.empty()) | |
.appended(Doc.stickySep( | |
Doc.english("Auto generated from PRs by"), | |
Doc.cat( | |
Doc.plain("[PLCTReport.java]"), | |
Doc.parened(Doc.plain("https://gist.github.com/imkiva/a2dd573914bdf563bc987652d4bccb5a")) | |
) | |
)) | |
.foldLeft(Doc.empty(), Doc::vcat); | |
System.out.println(markdown.debugRender()); | |
} | |
public static @NotNull Doc generate(@NotNull String name, @NotNull String repo) throws Exception { | |
var req = HttpRequest.newBuilder().GET() | |
.uri(URI.create("https://api.github.com/repos/" + repo + "/pulls?state=closed&sort=updated&direction=desc&per_page=100")) | |
.build(); | |
var since = sinceDate().atStartOfDay(); | |
return parse(CLIENT.send(req, HttpResponse.BodyHandlers.ofInputStream()).body()) | |
.view() | |
.filter(i -> i.updatedAt.isAfter(since)) | |
.map(i -> Doc.stickySep( | |
Doc.symbol("+"), | |
Doc.english(i.title), | |
Doc.cat( | |
Doc.plain("[PR-%d]".formatted(i.number)), | |
Doc.parened(Doc.plain(i.url)) | |
) | |
)) | |
.map(d -> Doc.hang(2, d)) | |
.prepended(Doc.cat( | |
Doc.plain("[Watch %s]".formatted(name)), | |
Doc.parened(Doc.plain("https://github.com/%s".formatted(repo))) | |
)) | |
.foldLeft(Doc.empty(), Doc::vcat); | |
} | |
public static @NotNull LocalDate sinceDate() { | |
var now = LocalDate.now(); | |
var year = now.getYear(); | |
var month = now.getMonthValue(); | |
return now.getDayOfMonth() > 25 | |
// Generating the report at the end of the month -- collect current month | |
? LocalDate.of(year, month, 1) | |
// Generating the report at the start of next month -- collect last month | |
: month == 1 ? LocalDate.of(year - 1, 12, 1) | |
: LocalDate.of(year, month - 1, 1); | |
} | |
private static @NotNull ImmutableSeq<PR> parse(@NotNull InputStream input) { | |
return ImmutableSeq.from(new GsonBuilder() | |
.registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json, type, ctx) -> | |
LocalDateTime.parse(json.getAsJsonPrimitive().getAsString().replace("Z", ""))) | |
.create() | |
.fromJson(new InputStreamReader(input), PR[].class)); | |
} | |
private static final class PR { | |
int number; | |
String title; | |
@SerializedName("html_url") String url; | |
@SerializedName("updated_at") LocalDateTime updatedAt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment