summary refs log tree commit diff
path: root/pkgs/applications/networking/cluster/terraform/providers/update-all
blob: e7ded437edb78442c8aa69bde22bc6a338cfe0d0 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env nix-shell
#! nix-shell -i bash -p bash coreutils curl jq nix
# vim: ft=sh sw=2 et
#
# This scripts scans the github terraform-providers repo for new releases,
# generates the corresponding nix code and finally generates an index of
# all the providers.
set -euo pipefail

GET() {
  local url=$1
  echo "fetching $url" >&2
  curl -#fL -u "$GITHUB_AUTH" "$url"
}

get_org_repos() {
  local org=$1
  local page=1
  GET "https://api.github.com/orgs/$org/repos?per_page=100" | jq -r '.[].name'
}

get_repo_tags() {
  local owner=$1
  local repo=$2
  GET "https://api.github.com/repos/$owner/$repo/git/refs/tags?per_page=100" | \
    jq -r '.[].ref' | \
    cut -d '/' -f 3- | \
    sort --version-sort
}

prefetch_github() {
  local owner=$1
  local repo=$2
  local rev=$3
  nix-prefetch-url --unpack "https://github.com/$owner/$repo/archive/$rev.tar.gz"
}

echo_entry() {
  local owner=$1
  local repo=$2
  local version=${3:1}
  local sha256=$4
  cat <<EOF
{
  owner   = "$owner";
  repo    = "$repo";
  version = "$version";
  sha256  = "$sha256";
};
EOF
}

indent() { sed 's/^/    /'; }

## Main ##

cd "$(dirname "$0")"

if [[ -z "${GITHUB_AUTH:-}" ]]; then
  cat <<'HELP'
Missing the GITHUB_AUTH env. This is required to work around the 60 request
per hour rate-limit.

Go to https://github.com/settings/tokens and create a new token with the
"public_repo" scope.

Then `export GITHUB_AUTH=<your user>:<your token>` and run this script again.
HELP
  exit 1
fi

org=terraform-providers

repos=$(get_org_repos "$org" | grep terraform-provider- | grep -v terraform-provider-scaffolding | grep -v terraform-provider-telefonicaopencloud | sort)


# Get all the providers with index

cat <<HEADER > data.nix
# Generated with ./update-all
{
HEADER

for repo in $repos; do
  echo "*** $repo ***"
  name=$(echo "$repo" | cut -d - -f 3-)
  last_tag=$(get_repo_tags "$org" "$repo" | tail -1)
  last_tag_sha256=$(prefetch_github "$org" "$repo" "$last_tag")

  {
    echo "  $name ="
    echo_entry "$org" "$repo" "$last_tag" "$last_tag_sha256" | indent
  } >> data.nix
done

cat <<FOOTER >> data.nix
}
FOOTER

echo Done.