# Cut one release on every forge in forges.toml: notes from the local# history, a CI gate, the release with its files, then a check that# each forge serves exactly those bytes.# jennifer run release.j <checkout> <tag> <file>...use convert;
use encoding;
use fs;
use hash;
use io;
use lists;
use os;
use path;
use regex;
import"./forges.j"as forges;
import"@deckyard/codehost/"as codehost;
import"@deckyard/git/"as git;
/**
* The release notes: one line per commit since the previous tag.
*
* @param r {git.Repo} the checkout
* @param newTag {string} the new tag
* @return {string} the notes, Markdown
*/funcnotesFor(r asgit.Repo, newTag asstring) {
def prev asstringinitgit.describe($r, $newTag + "~1");
$prev = regex.replace('-[0-9]+-g[0-9a-f]+$', $prev, "");
def text asstringinit"Changes since " + $prev + ":\n\n";
for (def c ingit.log($r, $prev + ".." + $newTag, "", 0)) {
$text = $text + "- " + $c.subject + "\n";
}
return$text;
}
/**
* Why a commit is not green on a forge: "" when every check passed.
*
* @param s {forges.Site} the forge
* @param commit {string} the commit
* @return {string} the reason, or ""
*/funcnotGreen(s asforges.Site, commit asstring) {
def seen aslistofstringinit [];
def all aslistofcodehost.CommitStatus initcodehost.commitStatuses($s.forge, $s.repo, $commit);
for (def st in$all) {
if (notlists.contains($seen, $st.context)) {
$seen[] = $st.context;
if ($st.state != "success") {
return$st.context + " is " + $st.state;
}
}
}
if (len($seen) == 0) {
return"no checks reported";
}
return"";
}
/**
* The SHA-256 of some bytes, in hex.
*
* @param data {bytes} the bytes
* @return {string} the digest
*/funcsha256(data asbytes) {
returnencoding.toText("hex", hash.compute("sha256", $data));
}
/**
* Stop with an error of kind "release".
*
* @param message {string} what went wrong
*/funcstop(message asstring) {
throw Error{kind: "release", message: $message};
}
if (len(os.ARGS) < 4) {
stop("usage: release.j <checkout> <tag> <file>...");
}
def tag asstringinitos.ARGS[2];
def files aslistofstringinitos.ARGS[3..len(os.ARGS)];
def repo asgit.Repo initgit.open(git.config(), os.ARGS[1]);
def sha asstringinitgit.revParse($repo, $tag);
def notes asstringinitnotesFor($repo, $tag);
def sites aslistofforges.Site initforges.load();
def want asmapofstringtostringinit {};
for (def file in$files) {
$want[path.base($file)] = sha256(fs.readBytes($file));
}
# 1. the gate: every forge's CI passed on the tagged commitdef red asintinit0;
for (def s in$sites) {
def why asstringinitnotGreen($s, $sha);
if ($why != "") {
io.printf("%s: %s at %s\n", $s.forge.backend, $why, $sha[0..7]);
$red = $red + 1;
}
}
if ($red > 0) {
io.printf("not released\n");
exit1;
}
# 2. the release and its files, forge by forgefor (def s in$sites) {
def f ascodehost.Forge init$s.forge;
def r ascodehost.Release initcodehost.createRelease(
$f, $s.repo, $tag, $sha, $tag, $notes, false);
for (def file in$files) {
def data asbytesinitfs.readBytes($file);
codehost.uploadAsset($f, $s.repo, $r, path.base($file), $data);
}
io.printf("%s: released %s\n", $f.backend, $r.htmlUrl);
}
# 3. read it back: every file there, byte for bytefor (def s in$sites) {
def f ascodehost.Forge init$s.forge;
def r ascodehost.Release initcodehost.release($f, $s.repo, $tag);
if (len($r.assets) != len($want)) {
stop($f.backend + ": " + convert.toString(len($r.assets)) + " files");
}
for (def a in$r.assets) {
def got asbytesinitcodehost.downloadAsset($f, $a, 0);
if (sha256($got) != $want[$a.name]) {
stop($f.backend + ": " + $a.name + " differs");
}
io.printf("%s: %s ok, %d bytes\n", $f.backend, $a.name, len($got));
}
}