Useful snippets of coding information

Month: December 2024

Clone Bit bucket repo

#!/bin/bash

# Configuration
BITBUCKET_URL="https://yourcompany.bitbucket.server"
PROJECT_KEY="CHE"  # Replace with your project key
USERNAME="your_username"  # Replace with your Bitbucket username
ACCESS_TOKEN="your_access_token"  # Replace with your personal access token

# Directory to clone repositories
CLONE_DIR="./${PROJECT_KEY}_repositories"
mkdir -p "$CLONE_DIR"

# Fetch repositories using the Bitbucket API
REPOS=$(curl -s -u "$USERNAME:$ACCESS_TOKEN" \
  "$BITBUCKET_URL/rest/api/1.0/projects/$PROJECT_KEY/repos?limit=100" | \
  jq -r '.values[].links.clone[] | select(.name == "http").href')

# Clone each repository
echo "Cloning repositories from project $PROJECT_KEY..."
for REPO in $REPOS; do
  REPO_NAME=$(basename "$REPO" .git)
  echo "Cloning $REPO_NAME..."
  git clone "$REPO" "$CLONE_DIR/$REPO_NAME"
done

echo "All repositories have been cloned into $CLONE_DIR."

curl -s -u "$USERNAME:$ACCESS_TOKEN" \
  "$BITBUCKET_URL/rest/api/1.0/projects/$PROJECT_KEY/repos?limit=100" > api_response.json

for REPO in $REPOS; do
  echo "Cloning from $REPO"
  REPO_NAME=$(basename "$REPO" .git)
  git clone "$REPO" "$CLONE_DIR/$REPO_NAME"
done


git -c http.sslVerify=false clone https://yourcompany.bitbucket.server/scm/project/repo1.git


Git clone and zip

This is how to clone and zip all sub repositories from bit bucket into a single zip fil

#!/bin/bash

# Configuration
WORKSPACE="your-workspace-name"          # Replace with your Bitbucket workspace name
PROJECT="CHE"                            # Replace with the project or folder name in Bitbucket
BITBUCKET_API="https://api.bitbucket.org/2.0"
ACCESS_TOKEN="your-access-token"         # Replace with your Bitbucket personal access token
DEST_FOLDER="CHE"                        # Destination folder for cloned repositories

# Create destination folder
mkdir -p "$DEST_FOLDER"

# Fetch repositories from Bitbucket
echo "Fetching repositories from Bitbucket workspace '$WORKSPACE', project '$PROJECT'..."
REPO_URLS=$(curl -s -H "Authorization: Bearer $ACCESS_TOKEN" \
    "$BITBUCKET_API/repositories/$WORKSPACE?q=project.key=\"$PROJECT\"" | \
    jq -r '.values[] | .links.clone[] | select(.name == "https") | .href')

# Check if any repositories were found
if [ -z "$REPO_URLS" ]; then
    echo "No repositories found for project '$PROJECT' in workspace '$WORKSPACE'."
    exit 1
fi

# Clone each repository
echo "Cloning repositories..."
for REPO_URL in $REPO_URLS; do
    REPO_NAME=$(basename "$REPO_URL" .git)
    echo "Cloning $REPO_NAME..."
    git clone "$REPO_URL" "$DEST_FOLDER/$REPO_NAME"
done

echo "All repositories have been cloned into the '$DEST_FOLDER' folder."