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.
342 lines
13 KiB
Bash
342 lines
13 KiB
Bash
5 years ago
|
#!/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)
|
||
|
#
|
||
3 years ago
|
# https://github.com/HariSekhon/DevOps-Bash-tools
|
||
5 years ago
|
#
|
||
|
# 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
|
||
|
#
|
||
3 years ago
|
# https://www.linkedin.com/in/HariSekhon
|
||
5 years ago
|
#
|
||
|
|
||
|
set -euo pipefail
|
||
|
[ -n "${DEBUG:-}" ] && set -x
|
||
3 years ago
|
srcdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||
5 years ago
|
|
||
1 year ago
|
# shellcheck disable=SC1090,SC1091
|
||
3 years ago
|
. "$srcdir/lib/github.sh"
|
||
4 years ago
|
|
||
3 years ago
|
top_N="${TOP_N:-100}"
|
||
4 years ago
|
|
||
|
# shellcheck disable=SC2034,SC2154
|
||
|
usage_description="
|
||
3 years ago
|
Script to generate a Markdown page containing the headers and CI/CD status badges of the Top N rated by stars GitHub repos
|
||
4 years ago
|
|
||
|
|
||
3 years ago
|
Examples:
|
||
4 years ago
|
|
||
|
|
||
3 years ago
|
Without arguments queries for all non-fork repos for your \$GITHUB_ORGANIZATION or \$GITHUB_USER and iterates up to $top_N of them to generate the page
|
||
4 years ago
|
|
||
3 years ago
|
./github_generate_status_page.sh
|
||
4 years ago
|
|
||
|
|
||
3 years ago
|
With arguments will query those repo's README.md at the top level - if omitting the owner prefix will prepend \$GITHUB_ORGANIZATION/ or \$GITHUB_USER/
|
||
4 years ago
|
|
||
3 years ago
|
GITHUB_USER=HariSekhon ./github_generate_status_page.sh DevOps-Python-tools DevOps-Perl-tools SomeOtherCoolGuy/his-repo
|
||
4 years ago
|
|
||
3 years ago
|
GITHUB_ORGANIZATION=my-org ./github_generate_status_page.sh HariSekhon/DevOps-Python-tools some-org-repo
|
||
|
|
||
|
Supported Environment Variables:
|
||
|
|
||
|
EXCLUDE_REPOS - an ERE regex or repos to exclude
|
||
|
TOP_N - an integer of the top N number of repos to include, ranked by star count
|
||
4 years ago
|
"
|
||
|
|
||
|
# used by usage() in lib/utils.sh
|
||
|
# shellcheck disable=SC2034
|
||
|
usage_args="[<user/repo1> <user/repo2> ...]"
|
||
|
|
||
|
help_usage "$@"
|
||
|
|
||
5 years ago
|
trap 'echo ERROR >&2' exit
|
||
|
|
||
3 years ago
|
is_int "$top_N" || die "Invalid TOP_N '$top_N' specified, must be an integer"
|
||
5 years ago
|
|
||
5 years ago
|
repolist="$*"
|
||
5 years ago
|
|
||
5 years ago
|
# this leads to confusion as it generates some randomly unexpected output by querying a github user who happens to have the same name as your local user eg. hari, so force explicit now
|
||
|
#USER="${GITHUB_USER:-${USERNAME:-${USER}}}"
|
||
3 years ago
|
GITHUB_USER="${GITHUB_USER:-$(get_github_user || :)}"
|
||
3 years ago
|
if is_blank "${GITHUB_USER:-}" || [ "$GITHUB_USER" = null ]; then
|
||
3 years ago
|
die "\$GITHUB_USER not set and could not infer user from token!"
|
||
|
fi
|
||
|
OWNER="${GITHUB_ORGANIZATION:-$GITHUB_USER}"
|
||
|
if is_blank "${OWNER:-}" || [ "$OWNER" = null ]; then
|
||
|
die "\$GITHUB_ORGANIZATION / \$GITHUB_USER not set and could not infer user from token!"
|
||
|
fi
|
||
|
|
||
|
prefix="users"
|
||
|
if [ -n "${GITHUB_ORGANIZATION:-}" ]; then
|
||
|
prefix="orgs"
|
||
5 years ago
|
fi
|
||
5 years ago
|
|
||
5 years ago
|
get_repos(){
|
||
|
page=1
|
||
|
while true; do
|
||
4 years ago
|
echo "fetching GitHub repos - page $page" >&2
|
||
3 years ago
|
if ! output="$("$srcdir/github_api.sh" "/$prefix/$OWNER/repos?page=$page&per_page=100")"; then
|
||
5 years ago
|
echo "ERROR" >&2
|
||
|
exit 1
|
||
5 years ago
|
fi
|
||
5 years ago
|
# 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" ...
|
||
5 years ago
|
if [ -z "$(jq '.[]' <<< "$output")" ]; then
|
||
|
break
|
||
5 years ago
|
elif jq -r '.message' <<< "$output" >&2 2>/dev/null; then
|
||
5 years ago
|
exit 1
|
||
|
fi
|
||
4 years ago
|
jq -r '.[] | select(.fork | not) | select(.private | not) | [.full_name, .stargazers_count, .forks] | @tsv' <<< "$output"
|
||
5 years ago
|
((page+=1))
|
||
|
done
|
||
|
}
|
||
|
|
||
5 years ago
|
original_sources=0
|
||
|
|
||
5 years ago
|
if [ -z "$repolist" ]; then
|
||
3 years ago
|
repolist="$(get_repos |
|
||
|
if [ -n "${EXCLUDE_REPOS:-}" ]; then
|
||
|
grep -Ev "$EXCLUDE_REPOS" || :
|
||
|
else
|
||
|
cat
|
||
|
fi |
|
||
|
sort -k2nr -k3nr |
|
||
|
awk '{print $1}' |
|
||
|
head -n "$top_N")"
|
||
5 years ago
|
original_sources=1
|
||
5 years ago
|
fi
|
||
5 years ago
|
|
||
5 years ago
|
num_repos="$(wc -w <<< "$repolist")"
|
||
|
num_repos="${num_repos// /}"
|
||
|
|
||
5 years ago
|
#echo "$repolist" >&2
|
||
5 years ago
|
|
||
|
# make portable between linux and mac
|
||
|
head(){
|
||
|
if [ "$(uname -s)" = Darwin ]; then
|
||
5 years ago
|
# from brew's coreutils package (installed by 'make')
|
||
5 years ago
|
ghead "$@"
|
||
5 years ago
|
else
|
||
5 years ago
|
command head "$@"
|
||
5 years ago
|
fi
|
||
|
}
|
||
|
|
||
5 years ago
|
tempfile="$(mktemp)"
|
||
2 years ago
|
tempfile_summary="$(mktemp)"
|
||
2 years ago
|
tempfile_header="$(mktemp)"
|
||
2 years ago
|
#trap 'echo ERROR >&2; rm -f "$tempfile" "$tempfile_summary" "$tempfile_header"' exit
|
||
5 years ago
|
|
||
5 years ago
|
{
|
||
|
actual_repos=0
|
||
5 years ago
|
|
||
4 years ago
|
total_stars=0
|
||
|
total_forks=0
|
||
4 years ago
|
echo "getting followers" >&2
|
||
3 years ago
|
followers="$("$srcdir/github_api.sh" "/$prefix/$OWNER" | jq -r .followers)"
|
||
4 years ago
|
|
||
4 years ago
|
echo "---" >&2
|
||
5 years ago
|
for repo in $repolist; do
|
||
5 years ago
|
if ! [[ "$repo" =~ / ]]; then
|
||
3 years ago
|
repo="$OWNER/$repo"
|
||
5 years ago
|
fi
|
||
4 years ago
|
echo "fetching GitHub repo info for '$repo'" >&2
|
||
|
repo_json="$("$srcdir/github_api.sh" "/repos/$repo")"
|
||
|
description="$(jq -r .description <<< "$repo_json")"
|
||
|
stars="$(jq -r .stargazers_count <<< "$repo_json")"
|
||
|
forks="$(jq -r .forks <<< "$repo_json")"
|
||
4 years ago
|
#watchers="$(jq -r .watchers <<< "$repo_json")"
|
||
4 years ago
|
((total_stars += stars))
|
||
|
((total_forks += forks))
|
||
4 years ago
|
#((total_watchers += watchers))
|
||
4 years ago
|
echo "fetching GitHub README.md for '$repo'" >&2
|
||
5 years ago
|
echo "---"
|
||
4 years ago
|
echo "---" >&2
|
||
|
{
|
||
|
#perl -e '$/ = undef; my $content=<STDIN>; $content =~ s/<!--[^>]+-->//gs; print $content' |
|
||
|
curl -sS "https://raw.githubusercontent.com/$repo/master/README.md" |
|
||
|
perl -pe '$/ = undef; s/<!--[^>]+-->//gs' |
|
||
|
sed -n '1,/^[^\[[:space:]<=-]/ p' |
|
||
|
head -n -1 |
|
||
|
#perl -ne 'print unless /=============/;' |
|
||
|
grep -v "===========" |
|
||
|
sed '1 s/^[^#]/# &/'
|
||
|
} |
|
||
|
{
|
||
|
read -r title
|
||
|
printf '%s\n' "$title"
|
||
|
echo
|
||
|
printf '%s\n' "Link: [$repo](https://github.com/$repo)"
|
||
|
echo
|
||
|
printf '%s\n' "$description"
|
||
|
cat
|
||
|
}
|
||
4 years ago
|
# works for Link which is a limited format, but couldn't expect to safely inject a description line pulled from the GitHub API because all sorts of characters could be contained which break this, so instead doing this via the brace piping trick above
|
||
5 years ago
|
# \\ escapes the newlines to allow them inside the sed for literal replacement since \n doesn't work
|
||
4 years ago
|
#sed "2 s|^|\\
|
||
|
#\\
|
||
|
#Link: [$repo](https://github.com/$repo)
|
||
|
#|"
|
||
5 years ago
|
echo
|
||
5 years ago
|
((actual_repos+=1))
|
||
5 years ago
|
done
|
||
5 years ago
|
} > "$tempfile"
|
||
|
|
||
|
if [ "$num_repos" != "$actual_repos" ]; then
|
||
5 years ago
|
echo "ERROR: differing number of target github repos ($num_repos) vs actual repos ($actual_repos)"
|
||
5 years ago
|
exit 1
|
||
|
fi
|
||
|
|
||
2 years ago
|
lines_of_code_counts="$(
|
||
|
grep -Ei 'img.shields.io/badge/lines%20of%20code-[[:digit:]]+(\.[[:digit:]]+)?k' "$tempfile" |
|
||
|
sed 's|.*img.shields.io/badge/lines%20of%20code-||; s/[[:alpha:]].*$//'|
|
||
|
tr '\n' '+' |
|
||
|
sed 's/+$//' || :
|
||
|
)"
|
||
|
# on Mac piping to | bc -l works, but on Linux breaks, but <<< works
|
||
|
lines_of_code="$(bc -l <<< "$lines_of_code_counts" || echo "unknown")"
|
||
|
|
||
4 years ago
|
hosted_build_regex='\[.+('
|
||
|
hosted_build_regex+='travis-ci.+\.svg'
|
||
5 years ago
|
hosted_build_regex+='|github\.com/.+/workflows/.+/badge\.svg'
|
||
|
hosted_build_regex+='|dev\.azure\.com/.+/_apis/build/status'
|
||
|
hosted_build_regex+='|app\.codeship\.com/projects/.+/status'
|
||
|
hosted_build_regex+='|appveyor\.com/api/projects/status'
|
||
|
hosted_build_regex+='|circleci\.com/.+\.svg'
|
||
|
hosted_build_regex+='|cloud\.drone\.io/api/badges/.+/status.svg'
|
||
|
hosted_build_regex+='|g\.codefresh\.io/api/badges/pipeline/'
|
||
|
hosted_build_regex+='|api\.shippable\.com/projects/.+/badge'
|
||
|
hosted_build_regex+='|app\.wercker\.com/status/'
|
||
4 years ago
|
hosted_build_regex+='|img\.shields\.io/.*/buildspec.yml'
|
||
|
hosted_build_regex+='|img\.shields\.io/.*/cloudbuild.yaml'
|
||
5 years ago
|
hosted_build_regex+='|img\.shields\.io/.+/pipeline'
|
||
|
hosted_build_regex+='|img\.shields\.io/.+/build/'
|
||
|
hosted_build_regex+='|img\.shields\.io/buildkite/'
|
||
|
hosted_build_regex+='|img\.shields\.io/cirrus/'
|
||
|
hosted_build_regex+='|img\.shields\.io/docker/build/'
|
||
|
hosted_build_regex+='|img\.shields\.io/docker/cloud/build/'
|
||
|
hosted_build_regex+='|img\.shields\.io/travis/'
|
||
3 years ago
|
hosted_build_regex+='|img.shields.io/badge/Shippable'
|
||
3 years ago
|
hosted_build_regex+='|img.shields.io/badge/TravisCI'
|
||
5 years ago
|
hosted_build_regex+='|img\.shields\.io/shippable/'
|
||
4 years ago
|
hosted_build_regex+='|img\.shields\.io/wercker/ci/'
|
||
4 years ago
|
hosted_build_regex+='|app\.buddy\.works/.*/pipelines/pipeline/.*/badge.svg'
|
||
4 years ago
|
hosted_build_regex+='|img\.shields\.io/badge/Buddy'
|
||
4 years ago
|
hosted_build_regex+='|\.semaphoreci\.com/badges/'
|
||
2 years ago
|
hosted_build_regex+='|api\.netlify\.com/api/v1/badges/'
|
||
4 years ago
|
hosted_build_regex+=')'
|
||
4 years ago
|
# to check for any badges missed, just go
|
||
3 years ago
|
#grep -Ev "$hosted_build_regex" README.md
|
||
5 years ago
|
|
||
|
self_hosted_build_regex='\[\!\[[^]]+\]\(.*\)\]\(.*/blob/master/('
|
||
|
self_hosted_build_regex+='Jenkinsfile'
|
||
|
self_hosted_build_regex+='|.concourse.yml'
|
||
|
self_hosted_build_regex+='|.gocd.yml'
|
||
|
self_hosted_build_regex+=')\)'
|
||
4 years ago
|
self_hosted_build_regex+='|img\.shields\.io/badge/TeamCity'
|
||
5 years ago
|
|
||
2 years ago
|
num_CI_systems=22
|
||
|
|
||
|
is_owner_harisekhon(){
|
||
|
shopt -s nocasematch
|
||
|
[[ "$OWNER" =~ ^HariSekhon$ ]]
|
||
|
}
|
||
|
|
||
|
if is_owner_harisekhon; then
|
||
|
cat >> "$tempfile_summary" <<EOF
|
||
|
[![Generate README](https://github.com/HariSekhon/CI-CD/actions/workflows/readme.yaml/badge.svg)](https://github.com/HariSekhon/CI-CD/actions/workflows/readme.yaml)
|
||
|
[![pages-build-deployment](https://github.com/HariSekhon/CI-CD/actions/workflows/pages/pages-build-deployment/badge.svg)](https://github.com/HariSekhon/CI-CD/actions/workflows/pages/pages-build-deployment)
|
||
|
[![Netlify Status](https://api.netlify.com/api/v1/badges/853ef60c-c01b-4b83-99ba-8fda541f850f/deploy-status)](https://app.netlify.com/sites/harisekhon/deploys)
|
||
|
![Last Generated](https://img.shields.io/badge/Last%20Generated-$(date +%F |
|
||
|
# "$srcdir/urlencode.sh" |
|
||
|
# need to escape dashes to avoid shields.io interpreting them as field separators
|
||
|
sed 's/-/--/g')-yellowgreen?logo=github)
|
||
2 years ago
|
[![GitHub Last Commit](https://img.shields.io/github/last-commit/HariSekhon/CI-CD?logo=github)](https://github.com/HariSekhon/CI-CD/commits/master)
|
||
2 years ago
|
EOF
|
||
|
fi
|
||
|
|
||
5 years ago
|
if [ -n "${DEBUG:-}" ]; then
|
||
5 years ago
|
echo
|
||
|
echo "Hosted Builds:"
|
||
|
echo
|
||
2 years ago
|
grep -Eh "$hosted_build_regex" "$tempfile" "$tempfile_header" || :
|
||
5 years ago
|
echo
|
||
|
echo "Self-Hosted Builds:"
|
||
|
echo
|
||
2 years ago
|
grep -Eh "$self_hosted_build_regex" "$tempfile" "$tempfile_header" || :
|
||
|
fi >&2
|
||
|
num_hosted_builds="$(grep -Ehc "$hosted_build_regex" "$tempfile" "$tempfile_summary" | tr '\n' '+' | sed 's/+$/\n/' | bc -l | sed 's/[[:space:]]//g')"
|
||
|
num_self_hosted_builds="$(grep -Ehc "$self_hosted_build_regex" "$tempfile" "$tempfile_summary" | tr '\n' '+' | sed 's/+$/\n/' | bc -l | sed 's/[[:space:]]//g')"
|
||
5 years ago
|
|
||
5 years ago
|
num_builds=$((num_hosted_builds + num_self_hosted_builds))
|
||
|
|
||
2 years ago
|
cat > "$tempfile_header" <<EOF
|
||
3 years ago
|
# CI/CD Status Page
|
||
5 years ago
|
|
||
4 years ago
|
![Original Repos](https://img.shields.io/badge/Repos-$actual_repos-blue?logo=github)
|
||
|
![Stars](https://img.shields.io/badge/Stars-$total_stars-blue?logo=github)
|
||
|
![Forks](https://img.shields.io/badge/Forks-$total_forks-blue?logo=github)
|
||
4 years ago
|
![Followers](https://img.shields.io/badge/Followers-$followers-blue?logo=github)
|
||
3 years ago
|
EOF
|
||
|
|
||
3 years ago
|
if ! is_blank "$lines_of_code" &&
|
||
|
[ "$lines_of_code" != unknown ]; then
|
||
2 years ago
|
cat >> "$tempfile_header" <<EOF
|
||
3 years ago
|
![Lines of Code](https://img.shields.io/badge/lines%20of%20code-${lines_of_code}k-lightgrey?logo=codecademy)
|
||
|
EOF
|
||
|
fi
|
||
|
|
||
2 years ago
|
cat >> "$tempfile_header" <<EOF
|
||
2 years ago
|
[![GitStar Ranking Profile](https://img.shields.io/badge/GitStar%20Ranking-$OWNER-blue?logo=github)](https://gitstar-ranking.com/$OWNER)
|
||
|
EOF
|
||
|
|
||
3 years ago
|
if is_owner_harisekhon; then
|
||
2 years ago
|
cat >> "$tempfile_header" <<-EOF
|
||
3 years ago
|
[![StarCharts](https://img.shields.io/badge/Star-Charts-blue?logo=github)](https://github.com/HariSekhon/DevOps-Bash-tools/blob/master/STARCHARTS.md)
|
||
|
EOF
|
||
|
fi
|
||
|
|
||
3 years ago
|
if is_owner_harisekhon; then
|
||
2 years ago
|
# XXX: workaround to include Readme Generate, GitHub Pages and Netlify builds here because we cannot precalculate them from $tempfile_header as they are added after the CI Builds count is needed here
|
||
|
# XXX: must update this if editing this header to add/remove any builds
|
||
2 years ago
|
((num_CI_systems += 1)) # for Netlify
|
||
2 years ago
|
cat >> "$tempfile_header" <<EOF
|
||
3 years ago
|
|
||
4 years ago
|
[![Azure DevOps Profile](https://img.shields.io/badge/Azure%20DevOps-HariSekhon-0078D7?logo=azure%20devops)](https://dev.azure.com/harisekhon/GitHub)
|
||
|
[![GitHub Profile](https://img.shields.io/badge/GitHub-HariSekhon-2088FF?logo=github)](https://github.com/HariSekhon)
|
||
|
[![GitLab Profile](https://img.shields.io/badge/GitLab-HariSekhon-FCA121?logo=gitlab)](https://gitlab.com/HariSekhon)
|
||
|
[![BitBucket Profile](https://img.shields.io/badge/BitBucket-HariSekhon-0052CC?logo=bitbucket)](https://bitbucket.org/HariSekhon)
|
||
3 years ago
|
[![GitHub Pages](https://img.shields.io/badge/GitHub-Pages-2088FF?logo=github)](https://harisekhon.github.io/CI-CD/)
|
||
2 years ago
|
[![Netlify](https://img.shields.io/badge/Netlify-site-00C7B7?logo=netlify)](https://harisekhon.netlify.app/)
|
||
3 years ago
|
|
||
2 years ago
|
[![CI/CD Builds](https://img.shields.io/badge/CI%2FCD%20Builds-$num_builds-blue?logo=circleci)](https://harisekhon.github.io/CI-CD/)
|
||
|
$(cat "$tempfile_summary")
|
||
3 years ago
|
EOF
|
||
|
fi
|
||
|
|
||
3 years ago
|
#if is_owner_harisekhon; then
|
||
|
# cat <<EOF
|
||
|
#
|
||
|
#[git.io/hari-ci](https://git.io/hari-ci) generated by \`${0##*/}\` in [HariSekhon/DevOps-Bash-tools](https://github.com/HariSekhon/DevOps-Bash-tools)
|
||
|
#
|
||
|
#This page usually loads better on [BitBucket](https://bitbucket.org/harisekhon/ci-cd/src/master/README.md) due to less aggressive proxy timeouts cutting off badge loading than GitHub / GitLab
|
||
|
#
|
||
|
#EOF
|
||
|
#
|
||
|
#fi
|
||
3 years ago
|
|
||
2 years ago
|
cat "$tempfile_header"
|
||
3 years ago
|
echo
|
||
5 years ago
|
printf "%s " "$num_repos"
|
||
|
if [ "$original_sources" = 1 ]; then
|
||
|
printf "original source "
|
||
|
fi
|
||
2 years ago
|
printf 'git repos with %s CI builds (%s hosted, %s self-hosted) across %s different CI/CD systems:\n\n' "$num_builds" "$num_hosted_builds" "$num_self_hosted_builds" "$num_CI_systems"
|
||
5 years ago
|
cat "$tempfile"
|
||
2 years ago
|
printf '\n%s git repos summarized with %s CI builds (%s hosted, %s self-hosted) across %s different CI/CD systems\n' "$actual_repos" "$num_builds" "$num_hosted_builds" "$num_self_hosted_builds" "$num_CI_systems"
|
||
5 years ago
|
|
||
|
trap '' exit
|