Skip to content

Instantly share code, notes, and snippets.

@piksel
Created April 5, 2024 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piksel/4c6943510f45d3c9ef0cd149aa7cbbce to your computer and use it in GitHub Desktop.
Save piksel/4c6943510f45d3c9ef0cd149aa7cbbce to your computer and use it in GitHub Desktop.
Bitbucket Export
#!/bin/bash
if [[ -z "$BB_WORKSPACE" ]]; then echo "Missing variable BB_WORKSPACE"; exit 1; fi
if [[ -z "$BB_TOKEN" ]]; then echo "Missing variable BB_TOKEN"; exit 1; fi
if [[ -z "$BB_REPO" ]]; then echo "Missing variable BB_REPO"; exit 1; fi
step=0
while read id; do
if [[ -d "prs/pr-$id" ]]; then
echo "Skipping PR #$id comments... "
continue
fi
echo -n "Fething PR #$id comments... "
mkdir -p prs/pr-$id
curl -s --oauth2-bearer "$BB_TOKEN" "https://api.bitbucket.org/2.0/repositories/$BB_WORKSPACE/$BB_REPO/pullrequests/$id/comments?pagelen=100" > prs/pr-$id/comments.json
comments=$(jq .size prs/pr-$id/comments.json)
comments_total=$comments
if [[ "$comments" -gt 100 ]]; then comments="100"; fi
echo "Got $comments comment(s)!"
if [[ "$comments" -ne "$comments_total" ]]; then
echo " Warning: Omitted $(( $comments_total - $comments )) comment(s)!"
echo
fi
if (( step % 10 == 9 )); then
echo -n "Waiting for 3s... "
sleep 3s
echo "Okay!"
fi
((step++))
done < $(jq .[].id prs/pullrequests.json)
#!/bin/bash
if [[ -z "$BB_WORKSPACE" ]]; then echo "Missing variable BB_WORKSPACE"; exit 1; fi
if [[ -z "$BB_TOKEN" ]]; then echo "Missing variable BB_TOKEN"; exit 1; fi
if [[ -z "$BB_REPO" ]]; then echo "Missing variable BB_REPO"; exit 1; fi
page=1
pagelen=50
prs=0
if [[ -f prs/pr-page1.json ]]; then
echo "Removing old files..."
rm -v prs/pr-page*.json
echo
fi
while true; do
echo -n "Fething PR page $page... "
pr_file="prs/pr-page$page.json"
curl -s --oauth2-bearer "$BB_TOKEN" "https://api.bitbucket.org/2.0/repositories/$BB_WORKSPACE/$BB_REPO/pullrequests?state=MERGED&state=DECLINED&state=SUPERSEDED&state=OPEN&pagelen=$pagelen&page=$page" > $pr_file
error=$(jq -er .error.message $pr_file)
if [[ $? -eq 0 ]]; then
echo "Failed: $error"
exit 1
fi
pr_count=$(jq .size $pr_file)
if jq -e .next $pr_file > /dev/null; then
((prs += pagelen))
((page++))
echo "Fetched $prs of $pr_count PRs!"
else
echo "Feched all $pr_count PRs!"
echo -n "Merging responses... "
if [[ -f prs/pr-page2.json ]]; then
jq -s 'map(.values) | flatten' prs/pr-page*.json > prs/pullrequests.json
else
jq .values prs/pr-page1.json > prs/pullrequests.json
fi
echo "Merged $(jq .[].id prs/pullrequests.json | wc -l) PRs into pullrequests.json"
echo "Done!"
exit 0
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment