Skip to content

Instantly share code, notes, and snippets.

@omindu
Created April 19, 2019 15:13
Show Gist options
  • Save omindu/89b4211891beca3caa0963790f83fc64 to your computer and use it in GitHub Desktop.
Save omindu/89b4211891beca3caa0963790f83fc64 to your computer and use it in GitHub Desktop.
USER=#
TOKEN=#
REMOTE=wso2-extensions
# Reads entries in the extensions.txt. An entry should be in the format of "<repo_name>,<owner1>,<owner2>"
for entry in `cat extensions.txt`
do
# Getting the reponame: Split from ',' and get first entry. Then trim.
repo=$(echo $entry | cut -d',' -f -1 | xargs)
# Getting the users: Split from ',' and get all entries from second entry.
users=$(echo $entry | cut -d',' -f 2-)
# Delete the existing fork.
curl -k -u $USER:$TOKEN -X DELETE https://api.github.com/repos/$USER/$repo
# Creat a new fork.
curl -k -u $USER:$TOKEN -X POST https://api.github.com/repos/$REMOTE/$repo/forks
# Check for existing CODEOWNER file and get the SHA if exist. The SHA is needed when updatin gthe file.
sha=$(curl -k -v -u $USER:$TOKEN https://api.github.com/repos/$USER/$repo/contents/CODEOWNERS --silent 2>&1 | grep sha | cut -d'"' -f4)
all_users=""
for i in ${users//,/ }
do
# Create space delimited user list with '@' appended.
all_users=$all_users"@"$i" "
done
# Prepare content for the CODEOWNERS file. Format: '* @user1 @user2' > Trim the output > B64 encode
content=$(echo "* $all_users" | sed -e 's/[[:space:]]*$//' | base64)
# Create CODEOWNERS file or replace the existing file.
curl -k -u $USER:$TOKEN -X PUT -d '{"message": "Adding '"$all_users"' as repo owners" ,"content": "'$content'", "sha" : "'$sha'"}' https://api.github.com/repos/$USER/$repo/contents/CODEOWNERS
# Create a PR for the remote repo wit the changes.
curl -k -u $USER:$TOKEN -X POST -H "Content-Type: application/json" -H "test:$all_users" -d '{"title": "Adding repo owners", "head": "'$USER':master","base": "master", "body":"Adding '"$all_users"' as repo owners"}' https://api.github.com/repos/$REMOTE/$repo/pulls
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment