Skip to content

Instantly share code, notes, and snippets.

@khoimm92
Last active February 10, 2020 03:52
Show Gist options
  • Save khoimm92/1eb26f196c6fc712dda59d9825039037 to your computer and use it in GitHub Desktop.
Save khoimm92/1eb26f196c6fc712dda59d9825039037 to your computer and use it in GitHub Desktop.
Fetch all Bitbucket repositories link to satis.json using JQ
USER="bitbucket_username";
REPOUSER="bitbucket_password";
SATISFILEPATH="./satis.json";
PAGE=1;
PAGESIZE=100;
REPO=''; // string, repositories data
getrepo() {
response="$(curl -u ${USER} https://api.bitbucket.org/2.0/repositories/${REPOUSER}?pagelen=${PAGESIZE}"&page=${PAGE}" > temp.json)"
repotem="$(cat temp.json | jq -r '.values[].links.clone[] | select(.name=="https") | "{\"type\": \"git\", \"url\": " + "\""+ .href + "\" },"')";
REPO="$REPO $repotem"
nextpage="$(cat temp.json | jq '.next')"
if [ ! -z "$nextpage" ] | [ "$nextpage" != "null" ];
then
echo "$nextpage";
PAGE=$((PAGE+1))
getrepo
fi
}
getrepo
REPO=[${REPO%?}]; # remove last comma, wrap it in a array
tmp=$(mktemp)
jq -r ".repositories = $REPO" ${SATISFILEPATH} > "$tmp" && mv "$tmp" ${SATISFILEPATH} # replace 'repositories' node in satis.json file by repo list, then put to temp, then replace to satis file
rm temp.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment