updated bitbucket
This commit is contained in:
parent
2f17207f77
commit
1582f35154
1 changed files with 93 additions and 60 deletions
|
@ -15,6 +15,7 @@ APP_PASS_FILE="$HOME/.bitbucket_app_password"
|
||||||
API_URL="https://api.bitbucket.org/2.0/repositories/$BITBUCKET_WORKSPACE/$REPO_NAME"
|
API_URL="https://api.bitbucket.org/2.0/repositories/$BITBUCKET_WORKSPACE/$REPO_NAME"
|
||||||
SSH_REMOTE="git@bitbucket.org:$BITBUCKET_WORKSPACE/$REPO_NAME.git"
|
SSH_REMOTE="git@bitbucket.org:$BITBUCKET_WORKSPACE/$REPO_NAME.git"
|
||||||
WEB_LINK="https://bitbucket.org/$BITBUCKET_WORKSPACE/$REPO_NAME"
|
WEB_LINK="https://bitbucket.org/$BITBUCKET_WORKSPACE/$REPO_NAME"
|
||||||
|
SCRIPT_VERSION="1.0"
|
||||||
|
|
||||||
# ╭─────────────────────────────────────╮
|
# ╭─────────────────────────────────────╮
|
||||||
# │ LOGGING UTILS │
|
# │ LOGGING UTILS │
|
||||||
|
@ -23,6 +24,25 @@ info() { echo -e "\n\e[1;34m[INFO]\e[0m $*"; }
|
||||||
warn() { echo -e "\n\e[1;33m[WARN]\e[0m $*"; }
|
warn() { echo -e "\n\e[1;33m[WARN]\e[0m $*"; }
|
||||||
error() { echo -e "\n\e[1;31m[ERROR]\e[0m $*" >&2; exit 1; }
|
error() { echo -e "\n\e[1;31m[ERROR]\e[0m $*" >&2; exit 1; }
|
||||||
|
|
||||||
|
# ╭─────────────────────────────────────╮
|
||||||
|
# │ OS + HARDWARE FINGERPRINT │
|
||||||
|
# ╰─────────────────────────────────────╯
|
||||||
|
OS_NAME=$(uname -s)
|
||||||
|
KERNEL_VERSION=$(uname -r)
|
||||||
|
ARCHITECTURE=$(uname -m)
|
||||||
|
OS_PRETTY_NAME=$(grep PRETTY_NAME /etc/os-release 2>/dev/null | cut -d= -f2 | tr -d '"' || echo "Unknown")
|
||||||
|
DOCKER_CHECK=$(grep -qE '/docker|/lxc' /proc/1/cgroup 2>/dev/null && echo "Yes" || echo "No")
|
||||||
|
WSL_CHECK=$(grep -qi microsoft /proc/version 2>/dev/null && echo "Yes" || echo "No")
|
||||||
|
VM_CHECK=$(command -v systemd-detect-virt >/dev/null && systemd-detect-virt || echo "Unknown")
|
||||||
|
UPTIME=$(uptime -p 2>/dev/null || echo "Unknown")
|
||||||
|
HOSTNAME=$(hostname)
|
||||||
|
CURRENT_USER=$(whoami)
|
||||||
|
TIMEZONE=$(date +%Z)
|
||||||
|
LOCAL_IP=$(hostname -I | awk '{print $1}' || echo "Unknown")
|
||||||
|
MAC_ADDRESS=$(ip link show | awk '/ether/ {print $2; exit}' || echo "Unknown")
|
||||||
|
CPU_MODEL=$(grep -m1 "model name" /proc/cpuinfo | cut -d: -f2 | sed 's/^ //g' || echo "Unknown")
|
||||||
|
TOTAL_RAM=$(awk '/MemTotal/ {printf "%.2f", $2/1024/1024}' /proc/meminfo 2>/dev/null || echo "Unknown")
|
||||||
|
|
||||||
# ╭─────────────────────────────────────╮
|
# ╭─────────────────────────────────────╮
|
||||||
# │ CHECK + INSTALL TOOLS │
|
# │ CHECK + INSTALL TOOLS │
|
||||||
# ╰─────────────────────────────────────╯
|
# ╰─────────────────────────────────────╯
|
||||||
|
@ -58,10 +78,7 @@ if ssh -T git@bitbucket.org 2>&1 | grep -q "authenticated"; then
|
||||||
else
|
else
|
||||||
warn "❌ SSH key not authorized with Bitbucket."
|
warn "❌ SSH key not authorized with Bitbucket."
|
||||||
echo "→ Visit: https://bitbucket.org/account/settings/ssh-keys/"
|
echo "→ Visit: https://bitbucket.org/account/settings/ssh-keys/"
|
||||||
echo "→ Paste this key:"
|
|
||||||
echo
|
|
||||||
cat ~/.ssh/id_rsa.pub
|
cat ~/.ssh/id_rsa.pub
|
||||||
echo
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -69,28 +86,21 @@ fi
|
||||||
# │ BITBUCKET APP PASSWORD SETUP │
|
# │ BITBUCKET APP PASSWORD SETUP │
|
||||||
# ╰─────────────────────────────────────╯
|
# ╰─────────────────────────────────────╯
|
||||||
if [ ! -f "$APP_PASS_FILE" ]; then
|
if [ ! -f "$APP_PASS_FILE" ]; then
|
||||||
echo
|
|
||||||
echo "🔐 Create a Bitbucket App Password (repo:admin + write + webhook)"
|
echo "🔐 Create a Bitbucket App Password (repo:admin + write + webhook)"
|
||||||
echo "→ https://bitbucket.org/account/settings/app-passwords/"
|
echo "→ https://bitbucket.org/account/settings/app-passwords/"
|
||||||
read -rsp "Enter Bitbucket App Password (input hidden): " APP_PASS
|
read -rsp "Enter Bitbucket App Password (input hidden): " APP_PASS
|
||||||
echo "$APP_PASS" > "$APP_PASS_FILE"
|
echo "$APP_PASS" > "$APP_PASS_FILE"
|
||||||
chmod 600 "$APP_PASS_FILE"
|
chmod 600 "$APP_PASS_FILE"
|
||||||
echo
|
|
||||||
info "App password saved at $APP_PASS_FILE"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
APP_PASS=$(<"$APP_PASS_FILE")
|
APP_PASS=$(<"$APP_PASS_FILE")
|
||||||
|
|
||||||
# ╭─────────────────────────────────────╮
|
# ╭─────────────────────────────────────╮
|
||||||
# │ GIT INIT & COMMIT │
|
# │ GIT INIT & COMMIT │
|
||||||
# ╰─────────────────────────────────────╯
|
# ╰─────────────────────────────────────╯
|
||||||
if [ ! -d .git ]; then
|
if [ ! -d .git ]; then
|
||||||
info "Initializing Git repository..."
|
|
||||||
git init
|
git init
|
||||||
git add . || warn "Nothing to add"
|
git add .
|
||||||
git commit -m "Initial commit" || warn "Nothing to commit"
|
git commit -m "Initial commit"
|
||||||
else
|
|
||||||
info "✓ Git repo already initialized."
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# ╭─────────────────────────────────────╮
|
# ╭─────────────────────────────────────╮
|
||||||
|
@ -98,41 +108,29 @@ fi
|
||||||
# ╰─────────────────────────────────────╯
|
# ╰─────────────────────────────────────╯
|
||||||
REPO_EXISTS=$(curl -s -u "$BITBUCKET_USER:$APP_PASS" "$API_URL" | jq -r '.name // empty')
|
REPO_EXISTS=$(curl -s -u "$BITBUCKET_USER:$APP_PASS" "$API_URL" | jq -r '.name // empty')
|
||||||
if [ -z "$REPO_EXISTS" ]; then
|
if [ -z "$REPO_EXISTS" ]; then
|
||||||
info "Creating Bitbucket repository '$REPO_NAME'..."
|
curl -s -u "$BITBUCKET_USER:$APP_PASS" -X POST "$API_URL" \
|
||||||
CREATE_RESPONSE=$(curl -s -w "%{http_code}" -o /tmp/create_resp.txt -u "$BITBUCKET_USER:$APP_PASS" -X POST "$API_URL" \
|
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d "{\"scm\": \"git\", \"is_private\": false}")
|
-d '{"scm": "git", "is_private": false}'
|
||||||
if [[ "$CREATE_RESPONSE" != "200" && "$CREATE_RESPONSE" != "201" ]]; then
|
|
||||||
cat /tmp/create_resp.txt
|
|
||||||
error "Failed to create repository (HTTP $CREATE_RESPONSE)"
|
|
||||||
fi
|
|
||||||
info "✓ Repository created."
|
|
||||||
else
|
|
||||||
info "✓ Remote Bitbucket repo already exists."
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# ╭─────────────────────────────────────╮
|
git remote remove "$REMOTE_NAME" 2>/dev/null || true
|
||||||
# │ REMOTE VALIDATION + SETUP │
|
git remote add "$REMOTE_NAME" "$SSH_REMOTE"
|
||||||
# ╰─────────────────────────────────────╯
|
|
||||||
EXPECTED_REMOTE="$SSH_REMOTE"
|
|
||||||
CURRENT_REMOTE=$(git remote get-url "$REMOTE_NAME" 2>/dev/null || echo "")
|
|
||||||
if [[ "$CURRENT_REMOTE" != "$EXPECTED_REMOTE" ]]; then
|
|
||||||
if [ -n "$CURRENT_REMOTE" ]; then
|
|
||||||
warn "Removing incorrect remote: $CURRENT_REMOTE"
|
|
||||||
git remote remove "$REMOTE_NAME"
|
|
||||||
fi
|
|
||||||
info "Setting correct Bitbucket remote: $EXPECTED_REMOTE"
|
|
||||||
git remote add "$REMOTE_NAME" "$EXPECTED_REMOTE"
|
|
||||||
else
|
|
||||||
info "✓ Remote already correctly set to: $EXPECTED_REMOTE"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# ╭─────────────────────────────────────╮
|
# ╭─────────────────────────────────────╮
|
||||||
# │ WRITE BITBUCKET-SPECIFIC MARKDOWN │
|
# │ WRITE METADATA MARKDOWN │
|
||||||
# ╰─────────────────────────────────────╯
|
# ╰─────────────────────────────────────╯
|
||||||
MARKDOWN_FILE=".bitbucket-link.md"
|
MARKDOWN_FILE=".bitbucket-link.md"
|
||||||
TIMESTAMP="$(date '+%Y-%m-%d %H:%M:%S')"
|
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
|
||||||
info "Creating Bitbucket markdown reference: $MARKDOWN_FILE"
|
DEFAULT_BRANCH=$(git symbolic-ref --short HEAD)
|
||||||
|
REPO_PATH=$(pwd)
|
||||||
|
LATEST_SHA=$(git rev-parse HEAD)
|
||||||
|
LAST_COMMIT_MSG=$(git log -1 --pretty=format:"%s")
|
||||||
|
LAST_COMMIT_DATE=$(git log -1 --pretty=format:"%ad")
|
||||||
|
LAST_COMMIT_AUTHOR=$(git log -1 --pretty=format:"%an <%ae>")
|
||||||
|
TOTAL_COMMITS=$(git rev-list --count HEAD)
|
||||||
|
TRACKED_FILES=$(git ls-files | wc -l)
|
||||||
|
UNCOMMITTED=$(if ! git diff --quiet || ! git diff --cached --quiet; then echo "Yes"; else echo "No"; fi)
|
||||||
|
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "None")
|
||||||
|
|
||||||
cat > "$MARKDOWN_FILE" <<EOF
|
cat > "$MARKDOWN_FILE" <<EOF
|
||||||
# 🔗 Bitbucket Repository Link
|
# 🔗 Bitbucket Repository Link
|
||||||
|
@ -140,29 +138,64 @@ cat > "$MARKDOWN_FILE" <<EOF
|
||||||
- **Repo Name**: \`$REPO_NAME\`
|
- **Repo Name**: \`$REPO_NAME\`
|
||||||
- **Bitbucket Workspace**: \`$BITBUCKET_WORKSPACE\`
|
- **Bitbucket Workspace**: \`$BITBUCKET_WORKSPACE\`
|
||||||
- **Remote URL**: [$WEB_LINK]($WEB_LINK)
|
- **Remote URL**: [$WEB_LINK]($WEB_LINK)
|
||||||
- **Commit Timestamp**: \`$TIMESTAMP\`
|
- **Local Repo Path**: \`$REPO_PATH\`
|
||||||
EOF
|
- **Remote Label**: \`$REMOTE_NAME\`
|
||||||
|
- **Default Branch**: \`$DEFAULT_BRANCH\`
|
||||||
|
- **Repo Created**: \`$TIMESTAMP\`
|
||||||
|
|
||||||
LATEST_SHA=$(git rev-parse HEAD)
|
---
|
||||||
echo "- **Last Commit SHA**: \`$LATEST_SHA\`" >> "$MARKDOWN_FILE"
|
|
||||||
echo "- **Commit URL**: [$WEB_LINK/commits/$LATEST_SHA]($WEB_LINK/commits/$LATEST_SHA)" >> "$MARKDOWN_FILE"
|
## 📦 Commit Info
|
||||||
echo -e "\n_Auto-generated by \`gitfield-bitbucket\` push script._" >> "$MARKDOWN_FILE"
|
|
||||||
|
- **Commit Timestamp**: \`$TIMESTAMP\`
|
||||||
|
- **Last Commit SHA**: \`$LATEST_SHA\`
|
||||||
|
- **Commit Message**: \`$LAST_COMMIT_MSG\`
|
||||||
|
- **Commit Author**: \`$LAST_COMMIT_AUTHOR\`
|
||||||
|
- **Commit Date**: \`$LAST_COMMIT_DATE\`
|
||||||
|
- **Commit URL**: [$WEB_LINK/commits/$LATEST_SHA]($WEB_LINK/commits/$LATEST_SHA)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📊 Repo Status
|
||||||
|
|
||||||
|
- **Total Commits**: \`$TOTAL_COMMITS\`
|
||||||
|
- **Tracked Files**: \`$TRACKED_FILES\`
|
||||||
|
- **Uncommitted Changes**: \`$UNCOMMITTED\`
|
||||||
|
- **Latest Tag**: \`$LATEST_TAG\`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🧭 Environment
|
||||||
|
|
||||||
|
- **Host Machine**: \`$HOSTNAME\`
|
||||||
|
- **Current User**: \`$CURRENT_USER\`
|
||||||
|
- **Time Zone**: \`$TIMEZONE\`
|
||||||
|
- **Script Version**: \`v$SCRIPT_VERSION\`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🧬 Hardware & OS Fingerprint
|
||||||
|
|
||||||
|
- **OS Name**: \`$OS_NAME\`
|
||||||
|
- **OS Version**: \`$OS_PRETTY_NAME\`
|
||||||
|
- **Kernel Version**: \`$KERNEL_VERSION\`
|
||||||
|
- **Architecture**: \`$ARCHITECTURE\`
|
||||||
|
- **CPU Model**: \`$CPU_MODEL\`
|
||||||
|
- **Total RAM (GB)**: \`$TOTAL_RAM\`
|
||||||
|
- **MAC Address**: \`$MAC_ADDRESS\`
|
||||||
|
- **Local IP**: \`$LOCAL_IP\`
|
||||||
|
- **Running in Docker**: \`$DOCKER_CHECK\`
|
||||||
|
- **Running in WSL**: \`$WSL_CHECK\`
|
||||||
|
- **Virtual Machine**: \`$VM_CHECK\`
|
||||||
|
- **System Uptime**: \`$UPTIME\`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
_Auto-generated by \`gitfield-bitbucket\` push script._
|
||||||
|
EOF
|
||||||
|
|
||||||
git add "$MARKDOWN_FILE"
|
git add "$MARKDOWN_FILE"
|
||||||
git commit -m "Bitbucket metadata link commit at $TIMESTAMP" || warn "No changes to commit for $MARKDOWN_FILE"
|
git commit -m "Bitbucket metadata link commit at $TIMESTAMP" || warn "No changes to commit for $MARKDOWN_FILE"
|
||||||
|
|
||||||
# ╭─────────────────────────────────────╮
|
git push -u "$REMOTE_NAME" "$DEFAULT_BRANCH"
|
||||||
# │ COMMIT + PUSH LOGIC │
|
|
||||||
# ╰─────────────────────────────────────╯
|
|
||||||
BRANCH=$(git rev-parse --abbrev-ref HEAD)
|
|
||||||
if ! git config --get branch."$BRANCH".remote &>/dev/null; then
|
|
||||||
info "Pushing with upstream..."
|
|
||||||
git push -u "$REMOTE_NAME" "$BRANCH" || error "Push failed"
|
|
||||||
else
|
|
||||||
info "Pushing to $REMOTE_NAME/$BRANCH..."
|
|
||||||
git push "$REMOTE_NAME" "$BRANCH" || error "Push failed"
|
|
||||||
fi
|
|
||||||
|
|
||||||
info "✅ Bitbucket push complete."
|
|
||||||
echo -e "\n🔗 View in browser: $WEB_LINK\n"
|
echo -e "\n🔗 View in browser: $WEB_LINK\n"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue