You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DevOps-Bash-tools/github_generate_status_page.sh

102 lines
3.0 KiB
Bash

#!/usr/bin/env bash
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2020-02-07 15:01:31 +0000 (Fri, 07 Feb 2020)
#
# https://github.com/harisekhon/bash-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/harisekhon
#
# Script to generate Status.md at top level to review all GitHub repos across all CI platforms on a single page
set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
srcdir="$(dirname "$0")"
USER="${GITHUB_USER:-${USERNAME:-${USER}}}"
PASSWORD="${GITHUB_PASSWORD:-${GITHUB_TOKEN:-${PASSWORD}}}"
top_N=20
repolist=""
if [ -n "${PASSWORD:-}" ]; then
echo "using authenticated access" >&2
fi
get_repos(){
page=1
while true; do
echo "fetching repos page $page" >&2
if [ -n "${PASSWORD:-}" ]; then
export USER
export PASSWORD
# shellcheck disable=SC2086
if ! output="$("$srcdir/curl_auth.sh" -sS --connect-timeout 3 ${CURL_OPTS:-} "https://api.github.com/users/$USER/repos?page=$page&per_page=100")"; then
echo "ERROR" >&2
exit 1
fi
else
# use authenticated requests if you are hitting the API rate limit - this is automatically done above now if USER/PASSWORD GITHUB_USER/GITHUB_PASSWORD/GITHUB_TOKEN environment variables are detected
# eg. CURL_OPTS="-u harisekhon:$GITHUB_TOKEN" ...
# shellcheck disable=SC2086
if ! output="$(curl -sS --connect-timeout 3 ${CURL_OPTS:-} "https://api.github.com/users/$USER/repos?page=$page&per_page=100")"; then
echo "ERROR" >&2
exit 1
fi
fi
if [ -z "$(jq '.[]' <<< "$output")" ]; then
break
elif jq -r '.message' <<< "$output" >&2 2>/dev/null; then
exit 1
fi
jq -r '.[] | select(.fork | not) | [.name, .stargazers_count] | @tsv' <<< "$output"
((page+=1))
done
}
repolist="$(get_repos | grep -v spark-apps | sort -k2nr | awk '{print $1}' | head -n "$top_N")"
#echo "$repolist" >&2
# make portable between linux and mac
head(){
if [ "$(uname -s)" = Darwin ]; then
ghead
else
command head
fi
}
{
cat <<EOF
# GitHub Status Page
generated by ${0##*/} from [HariSekhon/]DevOps Bash Tools](https://github.com/HariSekhon/DevOps-Bash-tools)
EOF
for repo in $repolist; do
echo "getting repo $repo" >&2
curl -sS "https://raw.githubusercontent.com/$USER/$repo/master/README.md" |
sed -n '1,/^[^\[[:space:]=]/ p' |
head -n -1 |
#perl -ne 'print unless /=============/;' |
grep -v "===========" |
sed '1 s/^[^#]/# &/' |
# \\ escapes the newlines to allow them inside the sed for literal replacement since \n doesn't work
sed "2 s|^|\\
[$USER/$repo repo](https://github.com/$USER/$repo)\\
\\
|"
echo
echo
done
} | tee "$srcdir/Status.md"