Created
December 19, 2019 06:23
-
-
Save nazoking/98b33fc30226357ad6326244c8ac7f9e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 -o pipefail | |
| if [ -z "$GITHUB_TOKEN" ];then | |
| echo "GITHUB_TOKEN を設定してください" | |
| exit -1 | |
| fi | |
| if [ -z "$1" ];then | |
| echo "引数に組織名を入れてください" | |
| exit -1 | |
| fi | |
| login="$1" | |
| function get_repositories | |
| { | |
| set -e -o pipefail | |
| local login="$1" | |
| local endCursor="$1" | |
| local query=' | |
| query ($login: String!, $endCursor: String){ | |
| organization(login:$login){ | |
| repositories(after: $endCursor, first:100, privacy:PRIVATE, orderBy: {field: PUSHED_AT, direction: DESC}){ | |
| pageInfo{ | |
| hasNextPage | |
| endCursor | |
| } | |
| nodes{ | |
| name | |
| url | |
| pushedAt | |
| defaultBranchRef{ | |
| target{ | |
| ... on Commit{ | |
| oid | |
| messageHeadline | |
| author{ | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| }' | |
| local variables | |
| if [ -n "$endCursor" ];then | |
| variables="$(jq --arg login "$login" --arg endCursor "$endCursor" -n '{ "endCursor": $endCursor, "login" : $login }')" | |
| else | |
| variables="$(jq --arg login "$login" -n '{ "endCursor": $endCursor, "login" : $login }')" | |
| fi | |
| local payload=$(jq --arg query "$query" --arg variables "$variables" -n '{ "query": $query, "variables": $variables }') | |
| curl -s -H "Authorization: bearer $GITHUB_TOKEN" -X POST -d "$payload" https://api.github.com/graphql | |
| } | |
| function echo_sleep | |
| { | |
| set -e -o pipefail | |
| printf "${2:-sleep}" | |
| sleep $1 | |
| printf "\033[1K\r" | |
| } | |
| function search_commit | |
| { | |
| set -e -o pipefail | |
| local hash="$1" | |
| while true | |
| do | |
| local ret="$(curl -s "https://api.github.com/search/commits?q=hash:$hash" -H "Accept: application/vnd.github.cloak-preview")" | |
| if [ "$(echo "$ret"| jq .documentation_url -r)" == "https://developer.github.com/v3/#abuse-rate-limits" ];then | |
| echo_sleep 4 "api limit..." >&2 | |
| else | |
| echo "$ret" | |
| break | |
| fi | |
| done | |
| } | |
| function endCursor | |
| { | |
| set -e -o pipefail | |
| local pageInfo="$1" | |
| local current="$2" | |
| if [ "true" == "$(echo "$pageInfo"|jq '.hasNextPage')" ];then | |
| local e="$(echo "$pageInfo"|jq '.endCursor' -r)" | |
| if [ "$e" == "$current" ];then | |
| echo "無限ループ $e" >&2 | |
| exit -1 | |
| fi | |
| echo "$e" | |
| fi | |
| } | |
| function get_all | |
| { | |
| set -e -o pipefail | |
| local login="$1" | |
| local endCursor="" | |
| while true | |
| do | |
| local ret="$(get_repositories "$login" "$endCursor")" | |
| local pageInfo="$(echo "$ret"|jq .data.organization.repositories.pageInfo)" | |
| echo "$ret"|jq '.data.organization.repositories.nodes[]' -crM | |
| endCursor=$(endCursor "$pageInfo" "$endCursor") | |
| if [ -z "$endCursor" ]; then | |
| break | |
| fi | |
| echo "next page $endCursor" >&2 | |
| done | |
| } | |
| function save_all | |
| { | |
| local tmp=$1 | |
| mkdir -p $tmp | |
| while read -r one | |
| do | |
| name=$(echo "$one"|jq .name -r) | |
| echo "$one" > "$1/$name.repo.json" | |
| if [ "$2" == "-a" ];then | |
| echo "$one" | |
| fi | |
| done | |
| } | |
| function search_files | |
| { | |
| for one_file in "$@" | |
| do | |
| one_file="$1" | |
| search_file="${one_file/.repo.json/.search.json}" | |
| if [ -e $search_file -a $one_file -ot $search_file ];then | |
| continue | |
| fi | |
| oid=$(cat $one_file|jq .defaultBranchRef.target.oid -r) | |
| if [ "$oid" == "null" ];then | |
| continue | |
| fi | |
| search_commit "$oid" > $search_file | |
| done | |
| } | |
| function result_files | |
| { | |
| for search_file in "$@" | |
| do | |
| search_json="$(cat $search_file)" | |
| if [ "$(echo "$search_json"|jq .total_count -r)" == "0" ];then | |
| continue | |
| fi | |
| repo_file="${search_file/.search.json/.repo.json}" | |
| repo_json="$(cat $repo_file)" | |
| echo $repo_json | jq '[.url,.defaultBranchRef.target.messageHeadline, .defaultBranchRef.target.author.email]|@tsv' -r | |
| echo "$search_json" | jq '.items[]|["","","",.repository.html_url]|@tsv' -r | |
| done | |
| } | |
| get_all "$login" | save_all tmp | |
| search_files $(ls -1 tmp/*.repo.json) | |
| result_files $(ls -1 tmp/*.search.json) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment