nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at python-updates 129 lines 3.9 kB view raw
1#!/usr/bin/env nix-shell 2#!nix-shell -i bash -p jq curl 3#shellcheck shell=bash 4set -euo pipefail 5 6# For Linux version checks we rely on Repology API to check 1Password managed Arch User Repository. 7REPOLOGY_PROJECT_URI="https://repology.org/api/v1/project/1password" 8 9# For Darwin version checks we query the same endpoint 1Password 8 for Mac queries. 10# This is the base URI. For stable channel an additional path of "N", for beta channel, "Y" is required. 11APP_UPDATES_URI_BASE="https://app-updates.agilebits.com/check/2/99/aarch64/OPM8/en/0/A1" 12 13CURL=( 14 "curl" "--silent" "--show-error" "--fail" 15 "--proto" "=https" # enforce https 16 "--tlsv1.2" # do not accept anything below tls 1.2 17 "-H" "user-agent: nixpkgs#_1password-gui update.sh" # repology requires a descriptive user-agent 18) 19 20JQ=( 21 "jq" 22 "--raw-output" 23 "--exit-status" # exit non-zero if no output is produced 24) 25 26 27read_local_versions() { 28 local channel="$1" 29 30 while IFS='=' read -r key value; do 31 local_versions["${key}"]="${value}" 32 done < <(jq -r --arg channel "${channel}" ' 33 .[$channel] | to_entries[] | .key as $os | .value.version as $version | 34 "\($channel)/\($os)=\($version)" 35 ' sources.json) 36} 37 38read_remote_versions() { 39 local channel="$1" 40 local darwin_beta_maybe 41 42 if [[ ${channel} == "stable" ]]; then 43 remote_versions["stable/linux"]=$( 44 "${CURL[@]}" "${REPOLOGY_PROJECT_URI}" \ 45 | "${JQ[@]}" '.[] | select(.repo == "aur" and .srcname == "1password" and .status == "newest") | .version' 46 ) 47 48 remote_versions["stable/darwin"]=$( 49 "${CURL[@]}" "${APP_UPDATES_URI_BASE}/N" \ 50 | "${JQ[@]}" 'select(.available == "1") | .version' 51 ) 52 else 53 remote_versions["beta/linux"]=$( 54 # AUR version string uses underscores instead of dashes for betas. 55 # We fix that with a `sub` in jq query. 56 "${CURL[@]}" "${REPOLOGY_PROJECT_URI}" \ 57 | "${JQ[@]}" '.[] | select(.repo == "aur" and .srcname == "1password-beta") | .version | sub("_"; "-")' 58 ) 59 60 # Handle macOS Beta app-update feed quirk. 61 # If there is a newer release in the stable channel, queries for beta 62 # channel will return the stable channel version; masking the current beta. 63 darwin_beta_maybe=$( 64 "${CURL[@]}" "${APP_UPDATES_URI_BASE}/Y" \ 65 | "${JQ[@]}" 'select(.available == "1") | .version' 66 ) 67 # Only consider versions that end with '.BETA' 68 if [[ ${darwin_beta_maybe} =~ \.BETA$ ]]; then 69 remote_versions["beta/darwin"]=${darwin_beta_maybe} 70 fi 71 fi 72} 73 74render_versions_json() { 75 local key value 76 77 for key in "${!local_versions[@]}"; do 78 value="${local_versions[${key}]}" 79 echo "${key}" 80 echo "${value}" 81 done \ 82 | jq -nR 'reduce inputs as $i ({}; . + { $i: input })' 83} 84 85 86cd -- "$(dirname "${BASH_SOURCE[0]}")" 87 88attr_path=${UPDATE_NIX_ATTR_PATH} 89case "${attr_path}" in 90 _1password-gui) channel="stable" ;; 91 _1password-gui-beta) channel="beta" ;; 92 *) 93 echo "Unknown attribute path ${attr_path}" >&2 94 exit 1 95esac 96 97declare -A local_versions remote_versions 98declare -a new_version_available=() 99read_local_versions "${channel}" 100read_remote_versions "${channel}" 101for i in "${!remote_versions[@]}"; do 102 if [[ "${local_versions[$i]}" != "${remote_versions[$i]}" ]]; then 103 old_version="${local_versions[$i]}" 104 new_version="${remote_versions[$i]}" 105 new_version_available+=("$i/$new_version") 106 fi 107done 108 109num_updates=${#new_version_available[@]} 110if (( num_updates == 0 )); then 111 exit # up to date 112elif (( num_updates == 1 )); then 113 os=$(cut -d / -f 2 <<<"${new_version_available[@]}") 114 os_specific_update=" (${os} only)" 115fi 116 117./update-sources.py "${new_version_available[@]}" 118cat <<EOF 119[ 120 { 121 "attrPath": "${attr_path}", 122 "oldVersion": "${old_version}", 123 "newVersion": "${new_version}${os_specific_update-}", 124 "files": [ 125 "$PWD/sources.json" 126 ] 127 } 128] 129EOF