Created
October 15, 2016 03:46
-
-
Save simplyadrian/9b7b9f62bc183de8ad2629bce9aae588 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
set -o pipefail | |
if [[ -z "$GITHUB_TOKEN" ]]; then | |
echo "Set the GITHUB_TOKEN env variable." | |
exit 1 | |
fi | |
URI=https://api.github.com | |
API_VERSION=v3 | |
API_HEADER="Accept: application/vnd.github.${API_VERSION}+json" | |
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}" | |
DEFAULT_PER_PAGE=100 | |
LAST_PAGE=1 | |
# get the last page from the headers | |
get_last_page(){ | |
header=${1%%" rel=\"last\""*} | |
header=${header#*"rel=\"next\""} | |
header=${header%%">;"*} | |
LAST_PAGE=$(echo ${header#*"&page="} | bc 2>/dev/null) | |
} | |
get_pulls(){ | |
local repo=$1 | |
local page=$2 | |
# send the request | |
local response=$(curl -i -sSL -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/repos/${repo}/pulls?per_page=${DEFAULT_PER_PAGE}&page=${page}&state=open") | |
# seperate the headers and body into 2 variables | |
local head=true | |
local header= | |
local body= | |
while read -r line; do | |
if $head; then | |
if [[ $line = $'\r' ]]; then | |
head=false | |
else | |
header="$header"$'\n'"$line" | |
fi | |
else | |
body="$body"$'\n'"$line" | |
fi | |
done < <(echo "${response}") | |
get_last_page "${header}" | |
local length=$(echo $body | jq length) | |
echo "${repo} has ${length} open PRs" | |
if [[ "$length" -gt 0 ]]; then | |
local pulls=$(echo $body | jq --raw-output '.[] | {number: .number, html_url: .html_url, title: .title, body: .body, created_at: .created_at, author: .base.repo.owner.login} | @base64') | |
for p in $pulls; do | |
pull="$(echo $p | base64 --decode)" | |
local number=$(echo $pull | jq --raw-output '.number') | |
local html_url=$(echo $pull | jq --raw-output '.html_url') | |
local title=$(echo $pull | jq --raw-output '.title') | |
local body=$(echo $pull | jq --raw-output '.body') | |
local created_at=$(echo $pull | jq --raw-output '.created_at') | |
local author=$(echo $pull | jq --raw-output '.author') | |
cat <<-EOF | |
${repo}#${number}: ${title} | |
`date -jf"%Y-%m-%dT%H:%M:%SZ" "${created_at}" +'%A, %B %d, %Y at %r'` | |
${html_url} | |
@${author}: ${body} | |
--- | |
EOF | |
done | |
else | |
return 0 | |
fi | |
} | |
main(){ | |
# send the request | |
local response=$(curl -sSL -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/user/repos?per_page=${DEFAULT_PER_PAGE}") | |
local repos=$(echo $response | jq --raw-output '.[].full_name | tostring') | |
for repo in $repos; do | |
page=1 | |
get_pulls "${repo}" "${page}" | |
if [ ! -z "$LAST_PAGE" ] && [ "$LAST_PAGE" -ge "$page" ]; then | |
for page in $(seq $((page + 1)) 1 ${LAST_PAGE}); do | |
echo "On page ${page} of ${LAST_PAGE}" | |
get_pulls "${repo}" "${page}" | |
done | |
fi | |
done | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment