Skip to content

Instantly share code, notes, and snippets.

@veggiemonk veggiemonk/bitbucket.sh
Last active Sep 6, 2019

Embed
What would you like to do?
Download all repos in bitbucket and extract Jenkinsfile
#!/usr/bin/env bash
# Best tips and tricks for bash ==> https://github.com/gruntwork-io/bash-commons
BASE_URL="https://git.netent.com/rest/api/1.0"
AUTHZ=${AUTHZ:?"AUTHZ var is not defined, it should contain the base64 encoded \`username:password\`"}
H_JSON="accept: application/json"
H_AUTHZ="Authorization: Basic $AUTHZ"
function log {
local -r message="$1"
local -r timestamp=$(date +"%Y-%m-%d %H:%M:%S")
local -r script_name="$(basename "$0")"
echo -e "${timestamp} [$script_name] ${message}"
}
check_dependency_installed () {
command -v "${1}" >/dev/null 2>&1 || { \
echo >&2 "${1} is required but is not installed. Aborting."; exit 1; }
}
check_dependency_installed "git"
check_dependency_installed "jq"
check_dependency_installed "curl"
save(){
local path=${1:-"projects"}
local file_name=${2:-${path##*/}}
local limit=${LIMIT:-"2000"}
local query=${3:-"pages&start=0&limit=$limit"}
local url="$BASE_URL/$path?$query"
log "url=$url"
curl -H "$H_AUTHZ" -H "$H_JSON" "$url" > "$file_name.json"
}
format_name(){
local name=${1:?"no name specified. error."}
echo "$name" | sed \
-e "s/julien.bisconti@//g" \
-e "s/https:\/\///g" \
-e "s/.git//g" \
-e "s/git.netent.com\///g" | \
tr '/' '_'
}
get_projects(){
save "projects"
number_project=$(jq '.values | length' < projects.json)
log "number of project: $number_project"
}
extract_repos_urls(){
local project=${1:?"no project defined. error."}
local file_name="repos-$project.json"
repos_urls=$(jq -r '.values[].links.clone[] | select(.name | contains("http")) | .href' < "$file_name")
echo $repos_urls
}
clone_repo(){
repo_url=${1:?"no url defined. error"}
repo_name=${2:?"no repo name defined. error"}
log "cloning $repo_url >> "
git clone --depth=1 $repo_url $repo_name;
}
backup_jenkinsfile(){
repo_name=${1:?"no repo name defined. error"}
log "Jenkinsfile in $repo_name";
cp "$repo_name/Jenkinsfile" "all_jenkinsfile/$repo_name.Jenkinsfile"
}
clone_repos(){
project=${1:?"no project defined. error"}
save "projects/$project/repos" "repos-$project"
number_repo=$(jq '.size' < "repos-$p".json)
log "number of repository: $number_repo"
repos_urls=$(extract_repos_urls "$project")
for r in $repos_urls;
do
repo_name=$(format_name "$r")
clone_repo $r $repo_name;
backup_jenkinsfile $repo_name;
rm -rf $repo_name
done
}
main() {
get_projects
mkdir -p all_jenkinsfile
list_projects=$(jq -r '.values[].key' < projects.json);
for p in $list_projects;
do
clone_repos $p;
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.