about summary refs log tree commit diff
path: root/pkgs/servers/mail/mailpit/update.sh
blob: 979cd11cda8a70dfc6bca80995bb16c264d327d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p nix coreutils curl jq nix-prefetch-git prefetch-npm-deps

set -euo pipefail

OWNER=axllent
REPO=mailpit

TARGET_TAG="$(curl -L -s ${GITHUB_TOKEN:+-u ":${GITHUB_TOKEN}"} "https://api.github.com/repos/$OWNER/$REPO/releases/latest" | jq -r '.tag_name')"
TARGET_VERSION="$(echo "$TARGET_TAG" | sed -e 's/^v//')"

if [[ "$UPDATE_NIX_OLD_VERSION" == "$TARGET_VERSION" ]]; then
    # mailpit is up-to-date
    if [[ -n "$UPDATE_NIX_ATTR_PATH" ]]; then
        echo "[{}]";
    fi

    exit 0
fi

extractVendorHash() {
    original="${1?original hash missing}"
    result="$(nix-build -A mailpit.goModules 2>&1 | tail -n3 | grep 'got:' | cut -d: -f2- | xargs echo || true)"
    [ -z "$result" ] && { echo "$original"; } || { echo "$result"; }
}

TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' exit

NIXPKGS_MAILPIT_PATH=$(cd $(dirname ${BASH_SOURCE[0]}); pwd -P)/
VERSIONS_NIX="$NIXPKGS_MAILPIT_PATH/versions.nix"

PREFETCH_JSON=$TMP/prefetch.json
nix-prefetch-git --rev "$TARGET_TAG" --url "https://github.com/$OWNER/$REPO" > "$PREFETCH_JSON"
PREFETCH_HASH="$(jq '.hash' -r < "$PREFETCH_JSON")"
PREFETCH_PATH="$(jq '.path' -r < "$PREFETCH_JSON")"
NPM_DEPS_HASH="$(prefetch-npm-deps $PREFETCH_PATH/package-lock.json)"

FAKE_HASH="sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";

cat > $VERSIONS_NIX <<-EOF
{
  version = "$TARGET_VERSION";
  hash = "$PREFETCH_HASH";
  npmDepsHash = "$NPM_DEPS_HASH";
  vendorHash = "$FAKE_HASH";
}
EOF

GO_HASH="$(nix-instantiate --eval -A mailpit.vendorHash | tr -d '"')"
VENDOR_HASH=$(extractVendorHash "$GO_HASH")

cat > $VERSIONS_NIX <<-EOF
{
  version = "$TARGET_VERSION";
  hash = "$PREFETCH_HASH";
  npmDepsHash = "$NPM_DEPS_HASH";
  vendorHash = "$VENDOR_HASH";
}
EOF

if [[ -z "$UPDATE_NIX_ATTR_PATH" ]]; then
    exit 0
fi

cat <<-EOF
[{
  "attrPath": "$UPDATE_NIX_ATTR_PATH",
  "oldVersion": "$UPDATE_NIX_OLD_VERSION",
  "newVersion": "$TARGET_VERSION",
  "files": ["$VERSIONS_NIX"]
}]
EOF