Skip to content

Instantly share code, notes, and snippets.

@lopezm1
Created March 31, 2020 20:53
Show Gist options
  • Save lopezm1/35fcd2c2b966043c155b4650b9b078ee to your computer and use it in GitHub Desktop.
Save lopezm1/35fcd2c2b966043c155b4650b9b078ee to your computer and use it in GitHub Desktop.
Script used to pull all application changes and repo specific changes. Creates csv files that can be uploaded to an audit.
# /bin/bash
GITHUB_TOKEN="xxxxx"
GITHUB_ORG="your-org-name-here"
APP_CHANGES_FILE="application-changes.csv" # filename for application changes
INFRA_REPO_1="infra-repo-name-1" # infra repo #1
INFRA_REPO_2="infra-repo-name-2" # infra repo #2
INFA_FILE_1="${INFRA_REPO_1}-changes.csv"
INFA_FILE_2="${INFRA_REPO_2}-changes.csv"
REPO_API_SEARCH_DATE="20191101T000000-0700" # different date format used for the /repos/:owner/:repo/issues
# $1 = Pass through the csv filename
setup_all_issues_headers() {
echo "Repository,User,PR #,Title,State,Link,Created At, Updated At, Closed At" >> $1
}
# Search ALL organization issues using /search/issues
# $1 = Pass through dates in the format of "2019-11-01..2019-11-30"
search_issues () {
TOTAL_NUM=$(curl -H "Authorization: token ${GITHUB_TOKEN}" https://api.github.com/search/issues\?q\=user:$GITHUB_ORG+state:closed+merged:$1\&page\=1\&per_page\=100 | jq '.total_count')
ITERATE=$(($TOTAL_NUM/100+1))
echo "Total: ${TOTAL_NUM}, Iterate: $ITERATE"
for ((i=1; ; i+=1)); do
contents=$(curl -H "Authorization: token ${GITHUB_TOKEN}" https://api.github.com/search/issues\?q\=user:$GITHUB_ORG+state:closed+merged:$1\&page\=$i\&per_page\=100)
echo "$contents" | jq -r '.items[] | (.html_url | split("/")[4]) + ",\(.user.login),\(.number)," + (.title | split(",")[0]) + ",\(.state),\(.html_url),\(.created_at),\(.updated_at),\(.closed_at)"' >> ${APP_CHANGES_FILE}
if [ $i -eq $ITERATE ]; then
echo "Reached final"
break
fi
done
}
# Search an issues on an individual repo using /repos/:owner/:repo/issues
# $1 = Date searching in the format of "20191101T000000-0700"
# $2 = Repository Name
# $3 = Name of the file we are writing to
search_repo() {
TOTAL_NUM=$(curl -H "Authorization: token ${GITHUB_TOKEN}" https://api.github.com/repos/$GITHUB_ORG/$2/issues\?state\=closed\&since\=$1\&page\=1\&per_page\=100 | jq length)
ITERATE=$(($TOTAL_NUM/100+1)) # the repos API does not have a "total_items" in the api response
echo "Total: ${TOTAL_NUM}, Iterate: $ITERATE"
for ((i=1; ; i+=1)); do
contents=$(curl -H "Authorization: token ${GITHUB_TOKEN}" https://api.github.com/repos/$GITHUB_ORG/$2/issues\?state\=closed\&since\=$1\&page\=$i\&per_page\=100)
echo "$contents" | jq -r '.[] | (.html_url | split("/")[4]) + ",\(.user.login),\(.number)," + (.title | split(",")[0]) + ",\(.state),\(.html_url),\(.created_at),\(.updated_at),\(.closed_at)"' >> $3
if [ $i -eq $ITERATE ]; then
echo "Reached final"
break
fi
done
}
# Search for application changes
setup_all_issues_headers $APP_CHANGES_FILE
search_issues "2019-11-01..2019-11-30"
search_issues "2019-12-01..2019-12-31"
search_issues "2020-01-01..2020-01-31"
search_issues "2020-02-01..2020-02-29"
search_issues "2020-03-01..2020-03-26"
# Search for repo specific changes
setup_all_issues_headers $INFA_FILE_1
search_repo $REPO_API_SEARCH_DATE $INFRA_REPO_1 $INFA_FILE_1
setup_all_issues_headers $INFA_FILE_2
search_repo $REPO_API_SEARCH_DATE $INFRA_REPO_2 $INFA_FILE_2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment