Skip to content

Instantly share code, notes, and snippets.

@nanpuyue
Created June 30, 2018 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nanpuyue/634a4d991de43d5da7ef707d3fdf05d2 to your computer and use it in GitHub Desktop.
Save nanpuyue/634a4d991de43d5da7ef707d3fdf05d2 to your computer and use it in GitHub Desktop.
#!/bin/bash
# file: migrate_github_plugin.sh
# date: 2018-06-30
# license: GPLv3 https://www.gnu.org/licenses/gpl-3.0.txt
# author: nanpuyue <nanpuyue@gmail.com> https://blog.nanpuyue.com
search_account_meta(){
grep -r "$*" ??/|awk -F':' '{print $1}'|uniq
}
search_account_meta2(){
grep -r "$*" ??/|awk -F':' '{print $1}'|uniq|\
while read line;do
echo -e "---\n$line:"
cat $line
echo
done
}
migrate_github_plugin(){
local external_id_info access_token account_id userinfo username github_uid email
external_id_info="$(git config -lf $1)"
access_token=$(echo "$external_id_info"|grep -Po "(?<=github_oauth\:)[0-9a-f]*(?=\.)")
if [ -n "$access_token" ]; then
account_id=$(echo "$external_id_info"|grep -Po "(?<=accountid\=)[0-9]*")
echo -e "---\naccess_token: \t$access_token" >> ../migrate.log
echo -e "account_id:\t$account_id" >> ../migrate.log
userinfo=$(curl -s https://api.github.com/user?access_token=${access_token})
if ( echo "$userinfo"|grep -q "Bad credentials" ); then
echo "$account_id $access_token" >> ../bad_credentials.txt
echo "bad credentials" >> ../migrate.log
echo "account $account_id: bad credentials"
rm -rvf "$1"
echo
return 1
fi
username=$(echo "$userinfo"|jq .login|tr -d '"')
echo -e "username:\t$username" >> ../migrate.log
github_uid=$(echo "$userinfo"|jq .id)
echo -e "github_uid:\t$github_uid" >> ../migrate.log
email=$(echo "$userinfo"|jq .email|tr -d '"')
echo -e "email:\t\t$email" >> ../migrate.log
if [[ "$github_uid" != null ]]; then
local external_id_new="github-oauth:$github_uid"
local file=$(echo -n "$external_id_new"|sha1sum|tr -d ' -'|sed -r 's#^(\w{2})#\1/#')
mkdir -p $(dirname $file) || true
git config -f "$file" "externalId.${external_id_new}.accountId" "$account_id" &&\
echo -e "file:\t\t$file" >> ../migrate.log
[[ "$email" != null ]] &&\
git config -f "$file" "externalId.${external_id_new}.email" "$email"
cat "$file"
rm -rvf "$1"
echo
fi
fi
}
@nanpuyue
Copy link
Author

nanpuyue commented Sep 2, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment